Esp8266 not responding to client

hey everyone,

I made a basic code using an esp8266 NodeMCU board, in which the esp8266 responds to ON and OFF requests by a client

the following is the code:

#include<ESP8266WiFi.h>

const char *ssid = "-----";
const char *pass = "------";

WiFiServer server (80);

void setup() {

  pinMode(D2,OUTPUT);
  pinMode(2,OUTPUT);
  digitalWrite(2,LOW);
  Serial.begin(115200);
  delay(10);


  Serial.println("connencting to ");
  Serial.println(ssid);

  WiFi.begin(ssid,pass);
  while(WiFi.status() != WL_CONNECTED){

    delay(500);
    Serial.print(".");

  }

  Serial.println("");
  Serial.print("connencted to the internet !");
  digitalWrite(2,HIGH);

  server.begin();

  Serial.println("use this to connect");

  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {

  WiFiClient client = server.available();

  if(!client){
    return;
  }

  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  String request = client.readStringUntil('/r');
  Serial.println(request);
  client.flush();

  if(request.indexOf("/ON")!=-1){

    Serial.println("led on");
    digitalWrite(D2,HIGH);
    digitalWrite(2,HIGH);
    
  }

  if(request.indexOf("/OFF")!=-1){

    Serial.println("led off");
    digitalWrite(D2,LOW);
    digitalWrite(2,LOW);
    
  }


  delay(1);

  

}

after uploading the above code to the nodeMCU i am able to send requests and get a response from the esp8266. However, after a few requests the esp8266 stops responding to any further requests

please help me with this.

Also, I am kinda new to this so go easy on me

  pinMode(D2,OUTPUT);
  pinMode(2,OUTPUT);

It seems that you were not sure how to address the LED that you have connected to pin D2

Remove all references to pin 2 in your code and just use D2 if that is where the LED is connected to

The code works for me

that isn't it.

like I said before the esp8266 responds and turns the led on and off, but after a few requests it stops responding.

I've done what said anyways and it still doesn't work

The problem was probably not the misuse of pin 2 but it is better to do it correctly

I have left mine running on a Wemos D1 Mini since my previous post and it too has stooped responding, but I don't know why

may be you loose the internet connection after a while ? renew DHCP lease ? reconnect ?

No internet connection involved, just the local network

right

don't you need to send back a code 200 and then a client.stop(); so that the client instance is freed?

Not my area of expertise, I am afraid

so I would do that

also client.flush(); won't do much since nothing was printed back to the client anyway.

reading up to the end of the client buffer probably would be good too

hi may i know what u mean by a "code 200"?

try this

#include<ESP8266WiFi.h>

const byte ledPin = LED_BUILTIN;
const char *ssid = "-----";
const char *pass = "------";

WiFiServer server (80);

void setup() {

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(115200);
  Serial.println("connencting to ");   Serial.println(ssid);

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.write('.');
  }
  Serial.println();

  Serial.print("connencted to the wireless network !");

  digitalWrite(ledPin, HIGH);

  server.begin();

  Serial.println("Use this to connect");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

void loop() {
  WiFiClient client = server.available();

  if (!client) return;

  Serial.println("handling new client");

  while (!client.available()) yield();
  String request = client.readStringUntil('\r');
  while (client.available()) client.read();

  if (request.indexOf("/ON") != -1) {
    Serial.println("led on");
    digitalWrite(ledPin, HIGH);
  } else if (request.indexOf("/OFF") != -1) {
    Serial.println("led off");
    digitalWrite(ledPin, LOW);
  } else Serial.println(request);

  client.println("HTTP/1.1 200 OK\r\n");
  delay(1);

  // close the connection:
  client.stop();
  Serial.println("client disconnected");
}

side note: one of the issue is the /r instead of \r in
client.readStringUntil('/r');

ok so i have done some digging and i think this has something to do with heap memory or something like that (I don't know what it is exactly).

i am not entirely sure if that has something to do with this but i thought it might help u guys figure this out.

try the code I provided

Something odd is still going on

Here is the log following a new upload then sending an ON, OFF and ON

connencted to the wireless network !Use this to connect
http://192.168.1.95/
handling new client
led on
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client
led off
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client

Note the lack off response after the final "handling new client" message, and the LED stays off

After rebooting the board I only got as far as sending ON then OFF before the system stopped responding but after rebooting yet again I was able to send ON, OFF several times before the lack of response occurred

For what it is worth, the lack of response always happens after the "handling new client" message is printed

As I said previously, this is not my area of expertise so I cannot suggest what might be wrong

can you try again after removing this line (I added also a bit of code for the browser to terminate the connection)

#include<ESP8266WiFi.h>

const byte ledPin = LED_BUILTIN;
const char *ssid = "-----";
const char *pass = "------";

WiFiServer server (80);

void setup() {

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(115200);
  Serial.println("connecting to ");   Serial.println(ssid);

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.write('.');
  }
  Serial.println();

  Serial.println("connected to the wireless network !");

  digitalWrite(ledPin, HIGH);

  server.begin();

  Serial.println("Use this to connect");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

void loop() {
  WiFiClient client = server.available();

  if (!client) return;

  Serial.println("handling new client");

  String request = client.readStringUntil('\r');
  while (client.available()) client.read();

  if (request.indexOf("/ON") != -1) {
    Serial.println("led on");
    digitalWrite(ledPin, HIGH);
  } else if (request.indexOf("/OFF") != -1) {
    Serial.println("led off");
    digitalWrite(ledPin, LOW);
  } else Serial.println(request);

  client.println("HTTP/1.1 200 OK");
  client.println("Connection: close");
  client.println();
  delay(1);

  // close the connection:
  client.stop();
  Serial.println("client disconnected");
}

Much better

There is sometimes a pause after the "handling new client" message is printed before anything else happens, but it works

Interestingly, when the pause happens the output has a blank line in it, unlike when the response is immediate. See a some examples in this output

connencted to the wireless network !Use this to connect
http://192.168.1.95/
handling new client
led on
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client
led off
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client
led on
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client

client disconnected
handling new client
led off
client disconnected
handling new client
led on
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client
led off
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client
led on
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client
led off
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client
led on
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client

client disconnected
handling new client
led off
client disconnected
handling new client
led on
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client
led off
client disconnected
handling new client
GET /favicon.ico HTTP/1.1
client disconnected
handling new client

client disconnected
handling new client
led on
client disconnected

type or paste code here

if it's like 1 second, that's likely the timeout of the readStringUntil() command (and before we were in an infinite loop as no data was coming in)

seems to work now then !

Good thought. That would imply that the required String termination is not being received

I will change the timeout and report results

Neither reducing the String timeout to 100 milliseconds or increasing it to 100000 milliseconds appears to make any difference to the length of the pause after the "handling new client" message, it is always about 5 seconds

weird... and probably (given nothing is printed and that before you were stuck) that nothing is being sent... but the timeout should kick in.

can you print the length of the String?