NodeMCU HTTP Client Sketch is extremely unstable :(
Posted: Wed Jan 13, 2016 11:15 am
Hi,
I read analog values with my NodeMCU V1.0 and post to a Webserver on a regular base.
After approx. 5000 seconds I can see on my webserver, that the NodeMCU has rebootet.
Sometimes it happens after 100 sec, sometimes after 10000 - without any external influence.
And I have NO idea why :(
Can someone help me ?
I read analog values with my NodeMCU V1.0 and post to a Webserver on a regular base.
After approx. 5000 seconds I can see on my webserver, that the NodeMCU has rebootet.
Sometimes it happens after 100 sec, sometimes after 10000 - without any external influence.
And I have NO idea why :(
Can someone help me ?
Code: Select all
#include <ESP8266WiFi.h>
const char* ssid = "mywlan";
const char* password = "mypassword";
const char* host = "10.0.0.100";
const int httpPort = 80;
unsigned long prev_millis = 0;
int sensorValue = 0;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to WLAN ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.print("My IP is: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Establish WiFi if disconnected
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("WLAN Connection lost - try to reconnect ...");
WiFi.begin(ssid, password);
}
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
sensorValue = analogRead(A0);
Serial.println("Value: "+String(sensorValue));
delay(50);
if (!client.connect(host, httpPort)) {
Serial.println("Connection failed");
Serial.println();
return;
}
else
{
Serial.println("verbunden ...");
}
String data = "";
data = "param=Power_Tot&user=test&value=100&p1=nodemcu&p2=50&p3="+String(millis()/1000);
Serial.println("POST1: "+String(data));
client.println("POST /api/values HTTP/1.1");
client.println("Host: " + String(host));
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.println(data);
Serial.println("Daten übertragen");
Serial.println();
}