Posting Data while using ESP8266WebServer
Posted: Thu Sep 10, 2015 12:13 pm
I am using an ESP01, programmed with the Arduino IDE, along with a US-100 ultrasonic distance module to measure and log the liquid level in a demonstration process tank. Using various example I found online, I have combined and built on them to have the ESP01 trigger the US-100 and to then log this up to data.sparkfun.com periodically using WiFiClient class and a GET request. The code also has some simple server responses to allow me to get spot level data. This has the WWW modem SSID and password coded and fixed in sketch.
To improve things and to make it portable, I have now built a set of web pages on a more advanced version using ESP8266WebServer and its "server.on" function. This will allow the ESP01 to be linked first as a local access point, then, using multiple web pages I can then set it to connect to any WWW linked WiFi connection and set the data interval, logging web page URL etc . This all works just fine and I can see the code running OK using the "serial.print testing comments" over USB link, the web pages over the usual local AP link at 192.168.4.1 and through the WWW WiFi connection on the reported IP address.
However, if I try to use the same code as the simple version to open a WiFiClient client and to log data it causes the ESP01 to reset after about 3 seconds (or ~8 if I issue wt_disable() first).
The code below shows port as 80 but I have tried using different ports for both the server and the WiFi in case this was a problem but the results are the same).
I assume its not compatible to use the WiFiClient TCP connection at the same time as using ESP8266WebServer even though the IDE happily compiles and loads it? I have searched the web but can see no way to get the server that responds to logged clients to simply post a GET request to data.sparkfun.com. This is my first venture into network based projects so I am probably missing a very basic solution, but any help or advice welcome.
Thanks.
Routine that causes the reset.
void TransmitData(){
Serial.print("connecting to ");
Serial.println(strHost);
// Use WiFiClient class to create TCP connections
Serial.println("creating client ");
const int httpPort = 80;
wdt_disable(); // tested to see if ESP01 just needed more time.
char temp[sizeof(strHost)+1];
strHost.toCharArray(temp, sizeof(strHost));
Serial.println (temp);
Serial.println (httpPort);
if (!client2.connect(testhost, httpPort)) { // The ESP01 hangs at this point and does a WDT reset.!
Serial.println("connection failed");
return;
Serial.print("client connected ");
}
wdt_enable(3000);
// We now create a URI for the request
String url = "/input/";
etc.............
......}
To improve things and to make it portable, I have now built a set of web pages on a more advanced version using ESP8266WebServer and its "server.on" function. This will allow the ESP01 to be linked first as a local access point, then, using multiple web pages I can then set it to connect to any WWW linked WiFi connection and set the data interval, logging web page URL etc . This all works just fine and I can see the code running OK using the "serial.print testing comments" over USB link, the web pages over the usual local AP link at 192.168.4.1 and through the WWW WiFi connection on the reported IP address.
However, if I try to use the same code as the simple version to open a WiFiClient client and to log data it causes the ESP01 to reset after about 3 seconds (or ~8 if I issue wt_disable() first).
The code below shows port as 80 but I have tried using different ports for both the server and the WiFi in case this was a problem but the results are the same).
I assume its not compatible to use the WiFiClient TCP connection at the same time as using ESP8266WebServer even though the IDE happily compiles and loads it? I have searched the web but can see no way to get the server that responds to logged clients to simply post a GET request to data.sparkfun.com. This is my first venture into network based projects so I am probably missing a very basic solution, but any help or advice welcome.
Thanks.
Routine that causes the reset.
void TransmitData(){
Serial.print("connecting to ");
Serial.println(strHost);
// Use WiFiClient class to create TCP connections
Serial.println("creating client ");
const int httpPort = 80;
wdt_disable(); // tested to see if ESP01 just needed more time.
char temp[sizeof(strHost)+1];
strHost.toCharArray(temp, sizeof(strHost));
Serial.println (temp);
Serial.println (httpPort);
if (!client2.connect(testhost, httpPort)) { // The ESP01 hangs at this point and does a WDT reset.!
Serial.println("connection failed");
return;
Serial.print("client connected ");
}
wdt_enable(3000);
// We now create a URI for the request
String url = "/input/";
etc.............
......}