I tried to test NodeMCU ESP8266 with this code from here

                #include <ESP8266WiFi.h>
                #include <WiFiClient.h> 
                #include <ESP8266WebServer.h>
                #include <ESP8266HTTPClient.h>

                /* Set these to your desired credentials. */
                const char *ssid = "dummy";  //ENTER YOUR WIFI SETTINGS
                const char *password = "12345678";

                //Web/Server address to read/write from 
                const char *host = "";   //https://circuits4you.com website or IP address of server

                //                    Power on setup

                void setup() {
                  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
                  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot

                  WiFi.begin(ssid, password);     //Connect to your WiFi router

                  // Wait for connection
                  while (WiFi.status() != WL_CONNECTED) {

                  //If connection successful show IP address in serial monitor
                  Serial.print("Connected to ");
                  Serial.print("IP address: ");
                  Serial.println(WiFi.localIP());  //IP address assigned to your ESP

                //                    Main Program Loop
                void loop() {
                  HTTPClient http;    //Declare object of class HTTPClient

                  String ADCData, station, postData;
                  int adcvalue=analogRead(A0);  //Read Analog value of LDR
                  ADCData = String(adcvalue);   //String to interger conversion
                  station = "A";

                  //Post Data
                  postData = "status=" + ADCData + "&station=" + station ;

                  http.begin("");              //Specify request destination
                  http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header

                  int httpCode = http.POST(postData);   //Send the request
                  String payload = http.getString();    //Get the response payload

                  Serial.println(httpCode);   //Print HTTP return code
                  Serial.println(payload);    //Print request response payload

                  http.end();  //Close connection

                  delay(5000);  //Post Data at every 5 seconds

It was running well and posted data to Database after a few hours. Then It was disconnected from WiFi somehow . Of course I reset power supply again It's connected.

How should I do to fix with that code to reconnect WiFi right after that accident Or any other Solution for that ? Thank you


2 Answers 2


What you need to do is leave the board connected to your computer and watch the serial monitor to capture the output during the crash. If the board has a true crash it will output a bunch of information which may tell you why you it is crashing. It could be a strings/heap problem or maybe a memory issue since you are declaring a new http object every 5 seconds. Maybe try putting the HTTPClient http; line up where you declare the host variable so that it is a global variable and not a local variable.

Since your loop doesn't test to make sure the wifi connection is still good then if you get a disconnection then the program will just fail and never try to re-connect. So at the start of your main loop put the same wifi testing logic that is also in the setup function. If the connection is down then reset and start from the very beginning to re-establish the wifi connection.

if (WiFi.status() != WL_CONNECTED) {
  • Thank you . I declared HTTPClient http ; outside of loop method. However It's still get the same errors . Assume that It could be a strings/heap problem or maybe a memory issue. How should I do please ?
    – ELIP
    Commented Mar 13, 2020 at 7:53
  • That worked for me ! Thank you
    – ELIP
    Commented Mar 16, 2020 at 1:22

I also had the same issues. I've looked for some posting like here. You can try these codes

void loop()
 WiFiClient client;

 Serial.printf("\n[Connecting to %s ... ", host);
if (client.connect(host, 80))

Serial.println("[Sending a request]");
client.print(String("GET /") + " HTTP/1.1\r\n" +
             "Host: " + host + "\r\n" +
             "Connection: close\r\n" +

while (client.connected() || client.available())
  if (client.available())
    String line = client.readStringUntil('\n');
  Serial.println("connection failed!]");

The seconds thing as @tavis said you can try from here. It sounds ESP.restart(); is recommended rather than ESP.reset(); It seems our issue is very hard.

  • Thanks for sharing
    – ELIP
    Commented Mar 16, 2020 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.