ESP8266 Webserver very unstable

Thread Starter

zazas321

Joined Nov 29, 2015
674
Hey. I have been working on my project for a while. I am using multiple Xbee devices to remotely read/write data to and from webserver. I am using NodeMCU-12 development board and I am having one problem with the webserver that ESP is creating.
C:
void setup() {

  Serial.begin(9600);
u8g2.begin();
  dht.begin();
  ina219.begin();
  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();


}


C:
void WEB_DISPLAY(){


  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
Remotetemperature();
roomtemperature();
powersensor();


    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
       
            client.println();
         
            // turns the GPIOs on and off
            if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("GPIO 5 on");
              output5State = "on";
              setRemoteState(0x5);
             u8g2.setFont(u8g2_font_t0_11_tf);
             u8g2.setCursor(30, 30);
             u8g2.print("ON");
             
           
           
           
           
            } else if (header.indexOf("GET /5/off") >= 0) {
              Serial.println("GPIO 5 off");
              output5State = "off";
              setRemoteState(0x4);
            u8g2.setFont(u8g2_font_t0_11_tf);
             u8g2.setCursor(30, 30);
             u8g2.print("OFF");
           
           
            }
         
           
       
         
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
         
         
            // Web Page Heading
            client.println("<body><h1>ESP8266 Web Server</h1>");
         
            // Display current state, and ON/OFF buttons for GPIO 5
            client.println("<p>GPIO 5 - State " + output5State + "</p>");
            // If the output5State is off, it displays the ON button     
            if (output5State=="off") {
              client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
            }
            //DISPLAY TEMPERATURE
            float h = dht.readHumidity();
float t = dht.readTemperature();
               client.println("<body><h2>Temperature and humidity</h2>");
               client.println("<p>Temperature:");
               client.print(t);
               client.println("C</p>");
               client.print("<p>Humidity:");
              client.print(h);
              client.print("%</p>");
           
         
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
     client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
    ESP.reset();
  }

}

I am basically using this guys code :
https://randomnerdtutorials.com/esp8266-web-server/
just slightly modified. When he is contorlling appliance, it seems to be working fine.


In my case, when I type in IP address to my browser. 90% of the times it continues to load the page for like 1 minute and then nothing appears, just white screen. However, sometimes the webserver would open but respond very slow and most of the time when I press a button to toggle the pin webbrowser will go non responding and continue to load..

Are there any solutions how to make ESP8266 webserver faster?
 
Last edited:
The server is really getting down and the main thing is that many users like me are getting affected by the server down and the work is not getting completed. I have also gone through because the server was not connecting.
 
Last edited by a moderator:

Thread Starter

zazas321

Joined Nov 29, 2015
674
Assuming you are using the Arduino IDE, where is your void loop()?
Where is WEB_DISPLAY() even called?
I call it in loop. Its working fine when I port-forward and use a host whereas when you use IP its much slower. That is very strange because even when you use a host it stil redirects me to same IP
 

Yaakov

Joined Jan 27, 2019
1,612
I call it in loop. Its working fine when I port-forward and use a host whereas when you use IP its much slower. That is very strange because even when you use a host it stil redirects me to same IP
Stick a sniffer on the client and see if it is trying to resolve the IP to a name when you access it directly.
 
Top