An "End line" character is added in my string

Hello,
I am making a project with an arduino nano, and ESP-01 and a web server. The arduino has many sensors, it communicate through Serial to the ESP which is communicating to the web server through Wifi.

I have a problem on the ESP side, just before communicating to the webserver, when building the URL.
The URL contain a constant part, and I add to it the string received through the serial. And that's where the problem appears.

I am currently simulating the arduino nano serial sending with the serial monitor. Here is what I get:

..................
WiFi connected
IP address: 
192.168.1.4
/meteo/create_line.php?sensor=1&temperature=43&humidity=55
/meteo/create_line.php?sensor=1
&temperature=43&humidity=56
/meteo/create_line.php?sensor=1
&temperature=43&humidity=57

The same with my comments after ***. I think it will be easier to show the expected and wrong behavior.

.................. *** Normal, the esp is getting the wifi
WiFi connected
IP address: 
192.168.1.4 *** Normal, it is connected to the wifi
/meteo/create_line.php?sensor=1&temperature=43&humidity=55 *** correct behavior, I sent &temperature=43&humidity=55@ in the serial monitor, the ESP got it and create the url accordingly
/meteo/create_line.php?sensor=1
&temperature=43&humidity=56 *** FAIL starting from the second one, after sending through the serial monitor &temperature=43&humidity=56@, instead of creating the url correctly, it cut it in half and goes to the next line. That avoid the webserver to work correctly as it actually receive only the variable sensor...
/meteo/create_line.php?sensor=1
&temperature=43&humidity=57 *** Same anytime I am sending again...

An important thing to notice is that I actually do have the same behavior when not simulation the Nano, but having the nano really connected. Although I can't read the Serial I can see the webserver behavior which indicate it has received sensor variable but that only.

It's time to show you my code. I hope you'll get an idea on what is wrong, or what is the trick to avoid this Carriage Return or Line Feed (I don't know which one it is).
I have suspected in the code, when I am reseting my variable received to empty, but it is the same than the initial setting, so I am lost.

void loop() { 
  String received = "";
  String newChar="";
  unsigned long timeout;
  while (true) {
 
 if (Serial.available()) { // enter a another loop if receive some serial
    //connect to the website
    WiFiClient client;// Use WiFiClient class to create TCP connections
    const int httpPort = 80;
       if (!client.connect(host, httpPort)) {
          Serial.println("connection failed"); // no use is operation as no check on serial arduino side.
           return;
       }
/*       timeout = millis(); // This is to check client but seems to mix up the results --> Not the right place probably
       while (client.available() == 0) { // Test and wait for connection to server
        if (millis() - timeout > 5000)
          { 
            Serial.println(">>> Client Timeout !"); // no use is operation as no check on serial arduino side.
           return;
            client.stop(); return; 
          }
      } // */

    // Do nothing but wait for Serial transmission, when receiving, building the URL.
     while (Serial.available()) {
        newChar = Serial.read();
        if (newChar=="@") { // this is the end delimiter, If received it, message completed so build the URL and send)
            String url = "/meteo/create_line.php"; // url at the host
            url += "?sensor="; // ?sensor = name of the GET variable
            url += sensor_id; // here the value, in this case the
            url += received; // it is the data received from the arduino. It must be with the format below
            // &type1=value1&type2=value2 ... as &temperature=23.56&humidity=46
             
             // Sending to the server
             client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
    Serial.println(url); // just for debug
             received="";
             newChar="";  
          } 
        else { // serial available but not the end, so just add to the string
         received += newChar;
          }
     }
     

    
      // To make things cleanly, but as the arduino won't check probably useless. Maybe checking that and have an LED showing Wrong...
 /*    
      // Read all the lines of the reply from server and print them to Serial  (NOT NEEDED ON OPERATION
      while (client.available())
        { String line = client.readStringUntil('\r'); 
          Serial.print(line);
        }
       */ 
  }
  
  
}
}

Thanks for your time and your help

What have you got the Serial monitor Line Ending set to ?

Doesn’t compile

What have you got the Serial monitor Line Ending set to ?

No Idea, I am using the default setup of arduino IDE. Where should I look? Not in the setting of the IDE (I couldn't find any option there).

I am surprised, because I am sending the first line through the serial monitor too.

It's because I didn't put the full code, but the main loop only.
Here is the full code with local network information neutralized

/* This sketch sends data via HTTP GET requests to local server from the ESP
 it get the data through serial connection normally with an arduino.

 There is an end character to add to the arduino code when sending the string to tell the ESP it has all received.
 the end character is @

 if we need to change it because we are sending email address for example, then change it in the test  // if (newChar=="@")
 IT IS THE CODE TO BE SET ON THE ESP
*/ 
#include <ESP8266WiFi.h> 
#include <WiFiClient.h>


// WIFI
const char* ssid = "00000"; //replace with your own wifi ssid 
const char* password = "000000"; //replace with your own wifi ssid password 

//SENSORS and SERVER
const char* host = "192.168.X.X";
int sensor_id=1; // id of the sensor the ESP is connected to. It is the id in the database of the server


// Static IP address configuration
IPAddress staticIP(192, 168, X, X); //ESP static IP address
IPAddress gateway(192, 168, 1, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(116,228,111,118); //DNS (could be 8,8,8,8 for google dns or any other DNS
const char* deviceName = "BASE_METEO";

void setup() { 
  Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.println();
/* WARNING, all Serial.print will be sent to the arduino through the serial link, not to the usb as soon as not connected to the computer anymore.  
 *  MORE IMPORTANT, the serial communication will be the link for the data, so should be used for that only. */
 
  Serial.println(); Serial.print("Connecting to ");
  Serial.println(ssid); // */
  
/* Explicitly set the ESP8266 to be a WiFi-client, and with correct mask */


WiFi.hostname(deviceName); // DHCP Hostname
WiFi.config(staticIP, subnet, gateway, dns);
WiFi.begin(ssid, password);

WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only)
WiFi.persistent(true);


while (WiFi.status() != WL_CONNECTED)   {
  delay(500);
  Serial.print("."); // */
  }
Serial.println("");
Serial.println("WiFi connected"); 
Serial.println("IP address: "); 
Serial.println(WiFi.localIP());  // */
} 



void loop() { 
  String received = "";
  String newChar="";
  unsigned long timeout;
  while (true) {
 
 if (Serial.available()) { // enter a another loop if receive some serial
    //connect to the website
    WiFiClient client;// Use WiFiClient class to create TCP connections
    const int httpPort = 80;
       if (!client.connect(host, httpPort)) {
          Serial.println("connection failed"); // no use is operation as no check on serial arduino side.
           return;
       }
/*       timeout = millis(); // This is to check client but seems to mix up the results --> Not the right place probably
       while (client.available() == 0) { // Test and wait for connection to server
        if (millis() - timeout > 5000)
          { 
            Serial.println(">>> Client Timeout !"); // no use is operation as no check on serial arduino side.
           return;
            client.stop(); return; 
          }
      } // */

    // Do nothing but wait for Serial transmission, when receiving, building the URL.
     while (Serial.available()) {
        newChar = Serial.read();
        if (newChar=="@") { // this is the end delimiter, If received it, message completed so build the URL and send)
            String url = "/meteo/create_line.php"; // url at the host
            url += "?sensor="; // ?sensor = name of the GET variable
            url += sensor_id; // here the value, in this case the
            url += received; // it is the data received from the arduino. It must be with the format below
            // &type1=value1&type2=value2 ... as &temperature=23.56&humidity=46
             
             // Sending to the server
             client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
    Serial.println(url); // just for debug
            received = "";
            newChar="";
          } 
        else { // serial available but not the end, so just add to the string
         received += newChar;
          }
     }
     

    
      // To make things cleanly, but as the arduino won't check probably useless. Maybe checking that and have an LED showing Wrong...
 /*    
      // Read all the lines of the reply from server and print them to Serial  (NOT NEEDED ON OPERATION
      while (client.available())
        { String line = client.readStringUntil('\r'); 
          Serial.print(line);
        }
       */ 
  }
  
  
}
}

Drop down menu on the monitor window

Indeed, Should have seen, but not set in English and got mixed up.
It is New Line, hence I guess LineFeed with a proper translation.

Yes, it appends \n to the sent text with this setting.

As I have the same problem with the full setting (not only my simulation with the serial monitor) I guess there is somewhere else with the \n added.

But I think there is an easy solution. I'll add a "begining message character" and just drop everything before...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.