-->
Page 1 of 1

Convert from UNO to WeMos

PostPosted: Thu Dec 03, 2020 11:58 am
by straygrey
In an attempt to display output from DJRobots weather station on the internet I have the following code.
Code: Select all#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
 const char* ssid = "MySSID"; 
 const char* password ="MyPassword";

 unsigned long Secs = 0;
 bool newData = 0; 
 bool err =1; 
 char data[53];
 byte i = 0 ; 
 byte x = 0 ; 
     
 WiFiServer server(80); 

SoftwareSerial swSer(D1, D2, false); //, 256);     
 void setup()             
     {                     
     Serial.begin(115200);  // This is the baud rate for ESP's Serial at USB which can be viewed in Arduino IDE Serial monitor 
     swSer.begin(9600);     // This is the baud rate for esps Software serial at pins D1 and D2 so it can communicate with arduino   
     delay(10); 
     Serial.println(); 
     Serial.print("Connecting to "); 
     Serial.println(ssid); 
 
     WiFi.mode(WIFI_STA);                               
     WiFi.begin(ssid, password); 
     while (WiFi.status() != WL_CONNECTED)   
         {   
         delay(500); 
         Serial.print(".");   
         } 
     Serial.println("");     
     Serial.println("WiFi connected"); 
     server.begin(); 
     Serial.println("Waiting for the IP Add..."); 
     delay(5000); 
     Serial.println(WiFi.localIP()); 
     } 
   
 void loop()   
     {     
     if (swSer.available() > 0 )   
         {   
         char Z = swSer.read(); 
         if(Z=='c')   // match first Character to check "validity"   
             {        // and this letter 'c' is not stored in the array below   
             newData=1; 
             Secs=millis()/1000; 
             for(i= 0; i< 52; )   
                 { 
                 data[i] = swSer.read(); 
                 i++; 
                 data[i] = '\0'; 
                 } 
             for(i=0; i<52;) 
                 {                // serial print to arduino ide monitor via usb,all the chars stored in data array 
                 Serial.print("char number: "); // and print char number the INDEX number starting from zero   
                    // the actual first char is the Letter "Z' which is not stored 
                 Serial.println(i); 
                 Serial.println(data[i]);               
                 i++;         
                 } 
             } 
         else 
            { 
            err=1; 
            } 
         } 
   
  // setStatus(); 
   
   sendHtml(); 
   
   if(millis()/1000 -Secs > 30 ) 
       { 
       Serial.println("Resetting Arrays"); 
       Serial.println(Secs); 
//   ResetArrays(); 
       Secs=millis()/1000; 
       }   
    }   
   
int transCharToInt(char *_buffer,int _start,int _stop)                               //char to int)
    {
    int _index;
    int result = 0;
    int num = _stop - _start + 1;
    int _temp[num];
    for (_index = _start;_index <= _stop;_index ++)
        {
        _temp[_index - _start] = _buffer[_index] - '0';
        result = 10*result + _temp[_index - _start];
        }
    return result;
    }

int WindDirection()                                                                  //Wind Direction
    {
    return transCharToInt(data,1,3);
    }

float WindSpeedAverage()                                                             //air Speed (1 minute)
    {
    float temp = 0.44704 * transCharToInt(data,5,7);
    return temp;
    }

float WindSpeedMax()                                                                 //Max air speed (5 minutes)
    {
    float temp = 0.44704 * transCharToInt(data,9,11);
    return temp;
    }

float Temperature()                                                                  //Temperature ("C")
    {
    float temp = (transCharToInt(data,13,15) - 32.00) * 5.00 / 9.00;
    return temp;
    }

float RainfallOneHour()                                                              //Rainfall (1 hour)
    {
    float temp = transCharToInt(data,17,19) * 25.40 * 0.01;
    return temp;
    }

float RainfallOneDay()                                                               //Rainfall (24 hours)
    {
    float temp = transCharToInt(data,21,23) * 25.40 * 0.01;
    return temp;
    }

int Humidity()                                                                       //Humidity
    {
    return transCharToInt(data,25,26);
    }

float BarPressure()                                                                  //Barometric Pressure
    {
    float temp = transCharToInt(data,28,32);
    return temp / 10.00;
    }
   
void sendHtml() 
    { 
    WiFiClient client = server.available(); 
   
    if (client)   
        { 
        Serial.println("New client"); 
        boolean blank_line = true; 
     
        while (client.connected())   
            { 
            if (client.available())   
                { 
                char c = client.read(); 
       
                if (c == '\n' && blank_line)   
                    { 
                    client.println("HTTP/1.1 200 OK"); 
                    client.println("Content-Type: text/html"); 
                    client.println("Connection: close"); 
                    client.println(); 
                    client.println("<!DOCTYPE HTML>"); 
                    client.println("<html><center>"); 
                    client.println("<head><meta http-equiv='refresh' content='5'/></head><body><h1>Esp8266 Serial to Wifi Weather Monitor Ver: 1.0</h1>");
                    client.println("Wind Direction: ");
                    client.println(WindDirection());
                    client.println("<br>");
                    client.println("Average Wind Speed (One Minute): ");
                    client.println(WindSpeedAverage());
                    client.println("m/s  <br>");
                    client.println("Max Wind Speed (Five Minutes): ");
                    client.println(WindSpeedMax());
                    client.println("m/s <br>");
                    client.println("Rain Fall (One Hour): ");
                    client.println(RainfallOneHour());
                    client.println("mm <br>");
                    client.println("Rain Fall (24 Hour): ");
                    client.println(RainfallOneDay());
                    client.println("mm <br>");
                    client.println("Temperature: ");
                    client.println(Temperature());
                    client.println("C <br> ");
                    client.println("Humidity: ");
                    client.println(Humidity());
                    client.println("%  <br>");
                    client.println("Barometric Pressure: ");
                    client.println(BarPressure());
                    client.println("hPa <br>");
                    client.println("");
                    client.println("");
                    client.println("</center></body></html>");     
                    break; 
                    } 
                if (c == '\n')   
                    { 
                    blank_line = true; 
                    } 
                else if (c != '\r')   
                    { 
                    blank_line = false; 
                    } 
                }
            }   
        delay(1); 
        client.stop();  // closing the client connection 
        Serial.println("Client disconnected."); 
        } 
    } 


It all seems to work perfectly well except it does not receive any data from the serial port.
Please tell me how I got this wrong.