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