My esp8266 server stops responding after i press a few buttons

Hello, i am making a simple rc car that will be controlled over a web server but i am running into an issue where the server responds very slowly or stops responding at all after i press a few buttons. I tried sever solutions but the issue persists. Can someone help me with this issue please. Thanks in advance, Simon.


#include <ESP8266WiFi.h>
 char* ssid = "";
char* password = "";


WiFiServer server(80);

void setup() {
  
  
  

  Serial.begin(115200);
  Serial.println("Hello");
  Serial.print("Wifi connecting to ");
  Serial.println( ssid );

  WiFi.begin(ssid,password);

  Serial.println();
  Serial.print("Connecting");

  while( WiFi.status() != WL_CONNECTED ){
      delay(500);
      Serial.print(F("."));        
  }

  server.begin();
 
  Serial.print(WiFi.localIP());

  
}

void loop() {

  
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  while(!client.available()){
    delay(1);
  }
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

   
 
 
 

  client.println(F("HTTP/1.1 200 OK"));
  client.println(F("Content-Type: text/html\r\n"));
  client.println(F("")); //  do not forget this one
  client.println(F("<!DOCTYPE HTML>"));
  client.println(F("<html>"));
  client.println(F("<meta name= \"HandheldFriendly\" content=\"True\">"));
  client.println(F("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">"));
  client.println(F("<body>"));
  client.println(F("<br><br>"));
   client.println(F("<div style=\"text-align: center; width: 50vh; height: 50vh;\">"));
  client.println(F("<a href=\"/Dir=F\"\"><button style=\"width:25%; height:25%;\" >Forward</button></a>"));
    client.println(F("<br>"));
  client.println(F("<a href=\"/Dir=L\"\"><button style=\"width:25%; height:25%;\">Left</button></a>"));
  client.println(F("<a href=\"/Dir=S\"\"><button style=\"width:25%; height:25%;\">Stop</button></a>")); 
  client.println(F("<a href=\"/Dir=R\"\"><button style=\"width:25%; height:25%;\">Right</button></a>")); 
    client.println(F("<br>"));
  client.println(F("<a href=\"/Dir=B\"\"><button style=\"width:25%; height:25%\">Back</button></a><br />"));
  client.println(F("</html>"));
  delay(1);
  Serial.println(F("Client disonnected"));
  
}

You should call client.close() after you're done with it.

client.flush() probably doesn't do what you expect it to do (it waits for all written bytes to be sent to the opposite side).

Clicking on the buttons just reloads the page. Are you sure you got the problem with exactly that code?

You never close the body

client.println(F("</body></html>"));

Most browsers get stuck after a while if you do not send correct html code
i think it may have been here before

</button></a><br />"));

cause that doesn't look right either.
i have switched from using " to ' as for the html it doesn't matter (hardly anyway, there is 1 exception but it does not apply here) because it improves overview form \ " to just '

client.println(F("<div style='text-align: center; width: 50vh; height: 50vh;'>"));

Thank you that was the problem. My site now works as intended.

Best Regards, Simon

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.