Dear all.
I send weather data from an ESP8266 via its RX/TX pins to SoftwareSerial pins 5/6 of an Arduino Uno. The data arrive, and I parse them into 8 strings and display them on the monitor: Everything is fine.
From the Arduino Uno, I send the complete data string via SoftwareSerial pins 7/8 to the RX/TX of an ESP01-S, which is programmed as a server to display the weather data on a “website”. Here, the data string is parsed again into the different temperature etc. values.
Problem: If the first temperature is, e.g., 20.7, the website only display 0.7; all other data are displayed correctly. So, only the first digit/byte of the sent data string is lost.
I am puzzled by this and cannot figure out, why the first digit is lost.
Any idea?
(I include below the code for the Arduino Uno and for the ESP01-S.
#include <SoftwareSerial.h>
float tmain;
float tmainmin;
float tmainmax;
String tmainS;
String tmainminS;
String tmainmaxS;
float hmain;
float pmain;
String hmainS;
String pmainS;
float t2av;
float t2min;
float t2max;
String t2avS;
String t2minS;
String t2maxS;
String receivedStringserial;
String receivedStringserialm;
// Define SoftwareSerial pins: RX = pin 5: connect to TX of ESP8266, TX = pin 6: connect to RX of ESP8266
// for data transmission via serial from ESP8266 to Arduino Uno (temperatures, pressure)
SoftwareSerial ESP8266Serial(5, 6);
// Define SoftwareSerial pins: RX = pin 7: connect to TX of ESP01-S, TX = pin 8: connect to RX of ESP01-S
// for data transmission via serial from Arduino Uno to ESP-01S to display on website
SoftwareSerial ESP01Serial(7, 8);
void setup() {
// Open hardware serial communication for the PC serial monitor
Serial.begin(19200);
while (!Serial) {
; // Wait for serial port to connect
}
// Open communication with the ESP8266
ESP8266Serial.begin(19200); // to later receive a string from ESP8266
Serial.println("Arduino Uno Ready. Waiting for data...");
ESP01Serial.begin(19200); // to later send the string to ESP01-S
}
void loop()
{
ESP8266Serial.listen();
// Check if data is available from the ESP8266
while (ESP8266Serial.available() > 0)
{
char d = ESP8266Serial.read(); // reading the string bit by bit
if (d != '\n') {receivedStringserial = receivedStringserial + d;}
if (d == '\n') {break;}
}
if (receivedStringserial == "")
{}
else
{
Serial.println(receivedStringserial); // to print on monitor
delay(5);
ESP01Serial.println(receivedStringserial); // send data to ESP01-S
receivedStringserialm = receivedStringserial; // "saved" version of receivedStringserial since receivedString later has to be set to ""
delay(10);
// Find the positions of the commas
int index1 = receivedStringserialm.indexOf(',');
int index2 = receivedStringserialm.indexOf(',', index1 + 1);
int index3 = receivedStringserialm.indexOf(',', index2 + 1);
int index4 = receivedStringserialm.indexOf(',', index3 + 1);
int index5 = receivedStringserialm.indexOf(',', index4 + 1);
int index6 = receivedStringserialm.indexOf(',', index5 + 1);
int index7 = receivedStringserialm.indexOf(',', index6 + 1);
// Extract each part using substring
String tmainS = receivedStringserialm.substring(0, index1);
String tmainminS = receivedStringserialm.substring(index1 + 1, index2);
String tmainmaxS = receivedStringserialm.substring(index2 + 1, index3);
String hmainS = receivedStringserialm.substring(index3 + 1, index4);
String pmainS = receivedStringserialm.substring(index4 + 1, index5);
String t2avS = receivedStringserialm.substring(index5 + 1, index6);
String t2minS = receivedStringserialm.substring(index6 + 1, index7);
String t2maxS = receivedStringserialm.substring(index7 + 1);
receivedStringserial = "";
Serial.println(tmainS); // to print on monitor, it is correctly printed
Serial.println(tmainminS);
Serial.println(tmainmaxS);
Serial.println(hmainS);
Serial.println(pmainS);
Serial.println(t2avS);
Serial.println(t2minS);
Serial.println(t2maxS);
}
}
ESP01-S code:
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "***";
const char* password = "????";
float tmain; // just examples, the values will later be retrieved
float tmainmin;
float tmainmax;
String tmainS;
String tmainminS;
String tmainmaxS;
float hmain;
float pmain;
String hmainS;
String pmainS;
float t2av;
float t2min;
float t2max;
String t2avS;
String t2minS;
String t2maxS;
String receivedStringserial;
String receivedStringserialm;
IPAddress local_IP(192,168,68,107);
IPAddress gateway(192,168,68,1);
IPAddress subnet(255,255,255,0);
char htmlfullContent[1250];
char e;
//SoftwareSerial ESP8266Serial(10, 11); // RX and TX pin for data transfer from ESP8266
ESP8266WebServer server(80);
// preliminary html content embedded as a raw string literal with placeholders
const char htmlpreContent[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>House in Ruimsig</title>
</head>
<body>
<h2>House in Ruimsig</h2>
<h3>Mainstation</h3>
<table>
<tr>
<td>current:</td>
<td style="text-align: right;">%s <span>°</span>C</td>
</tr>
<tr>
<td>minimum:</td>
<td style="text-align: right;">%s <span>°</span>C</td>
</tr>
<tr>
<td>maximum:</td>
<td style="text-align: right;">%s <span>°</span>C</td>
</tr>
<tr><td>------------</td><td>--------</td></tr>
<tr>
<td>humidity:</td>
<td style="text-align: right;">%s <span>%</span></td>
</tr>
<tr>
<td>pressure:</td>
<td style="text-align: right;">%s hPa</td>
</tr>
</table>
<h3>Outside</h3>
<table>
<tr>
<td>current:</td>
<td style="text-align: right;">%s <span>°</span>C</td>
</tr>
<tr>
<td>minimum:</td>
<td style="text-align: right;">%s <span>°</span>C</td>
</tr>
<tr>
<td>maximum:</td>
<td style="text-align: right;">%s <span>°</span>C</td>
</tr>
</table>
<p><button onClick="window.location.href=window.location.href">Refresh</button></p>
</body>
</html>
)rawliteral";
void setup()
{
//ESP8266Serial.begin(38400);
Serial.begin(19200);
delay(10);
WiFi.config(local_IP, gateway, subnet); // configure a fixed IP address for the server
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
// generate the full html content by sequentially replacing all placeholders (%s) with the respective strings
snprintf(htmlfullContent, sizeof(htmlfullContent), htmlpreContent, tmainS, tmainminS, tmainmaxS, hmainS, pmainS, t2avS, t2minS, t2maxS);
Serial.println(htmlfullContent);
receivedStringserial = "";
}
void loop()
{
//server.handleClient();
while (Serial.available() > 0)
{
//Core Command: Automatically parses the string stream into a native float
char e = Serial.read();
//Serial.print(e);
if (e != '\n') {receivedStringserial = receivedStringserial + e;}
if (e == '\n') {break;}
Serial.println(receivedStringserial);
}
if (receivedStringserial == "")
{}
else
{
Serial.println(receivedStringserial);
receivedStringserialm = receivedStringserial; // "saved" version of receivedStringserial since receivedString later has to be set to ""
delay(10);
// Find the positions of the commas
int index1 = receivedStringserialm.indexOf(',');
int index2 = receivedStringserialm.indexOf(',', index1 + 1);
int index3 = receivedStringserialm.indexOf(',', index2 + 1);
int index4 = receivedStringserialm.indexOf(',', index3 + 1);
int index5 = receivedStringserialm.indexOf(',', index4 + 1);
int index6 = receivedStringserialm.indexOf(',', index5 + 1);
int index7 = receivedStringserialm.indexOf(',', index6 + 1);
// Extract each part using substring
String tmainS = receivedStringserialm.substring(0, index1);
String tmainminS = receivedStringserialm.substring(index1 + 1, index2);
String tmainmaxS = receivedStringserialm.substring(index2 + 1, index3);
String hmainS = receivedStringserialm.substring(index3 + 1, index4);
String pmainS = receivedStringserialm.substring(index4 + 1, index5);
String t2avS = receivedStringserialm.substring(index5 + 1, index6);
String t2minS = receivedStringserialm.substring(index6 + 1, index7);
String t2maxS = receivedStringserialm.substring(index7 + 1);
receivedStringserial = "";
// generate the full html content by sequentially replacing all placeholders (%s) with the respective strings
snprintf(htmlfullContent, sizeof(htmlfullContent), htmlpreContent, tmainS, tmainminS, tmainmaxS, hmainS, pmainS, t2avS, t2minS, t2maxS);
Serial.println(htmlfullContent);
}
server.handleClient();
}
// Function to handle root path
void handleRoot()
{
server.send_P(200, "text/html", htmlfullContent);
}

