3
votes

I am trying to set up node mcu as Access Point with static IP. I am able to connect to the AP using tcp sockets and receive response from it.

However, if the client is not connected to it for a certain amount of time, node MCU doesn't respond at all.

Say if 1 hr passed as idle time then node mcu allows clients to connect to the AP but it doesn't respond to the queries.

Here is my code.

#include <ESP8266WiFi.h>

WiFiServer server(8080);

IPAddress IP(192,168,4,15);
IPAddress mask = (255, 255, 255, 0);
//byte ledPin = 16;
byte ledPin = 2;

void setup() {
 Serial.begin(115200);
 WiFi.mode(WIFI_AP);
 WiFi.softAP("PRAKASH", "1234567890");
 WiFi.softAPConfig(IP, IP, mask);

 server.begin();

 pinMode(ledPin, OUTPUT);
 Serial.println();
 Serial.println("Server started.");
 Serial.print("IP: "); Serial.println(WiFi.softAPIP());
 Serial.print("MAC:"); Serial.println(WiFi.softAPmacAddress());
}

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

  if (client) {
    Serial.println("Client connected.");

    while (client.connected()) {
      if (client.available()) {
        String request = client.readStringUntil('\r');
        Serial.println("____NEW_COMMAND_____");
        if (request == "LED_ON") {
          digitalWrite(ledPin, LOW);
          Serial.println("LED is now on.");
        } else if (request == "LED_OFF") {
          digitalWrite(ledPin, HIGH);
          Serial.println("LED is now off.");
        } else {
          Serial.println("Invalid command:");
          Serial.println(request);
        }
         Serial.println("From the station: " + request);
         client.flush();
         Serial.print("Byte sent to the station: ");
         Serial.println(client.println(request + "ca" + "\r"));
         Serial.println("*____COMMAND_COMPLETED____*");
      }
    }
    Serial.println("Client disconnected.");
    client.stop();
  }
}

I am using Arduino ide to flash the code.

Arduino version: 1.8.10 Board: Node Mcu 1.0 12E Upload speed: 115200 CPU freq: 80Mhz (Board Manager) Esp8266 : 2.6.2

Just to summarize after idle period node mcu let me connect using tcp sockets but it won't turn off/on the led and it doesn't send any response back. If I reset node mcu then it works again.

I am not able to figure out why this is happening. Any help is appreciated.

1
allows only connect to AP or the TCP connecton to port 8080 is successful too? Does it print "Client connected"?Juraj
Yeah it does. It also responds for some time. Later it does allow to connect using tcp socket but doesn't respond.zero
I asked about the state when it doesn't respond. Does it print "Client connected" then?Juraj
I didn't test it. I will do that today.zero

1 Answers

1
votes

I figured out the problem here.

When I didn't terminate the tcp socket connection properly then node mcu was in stale connected state. It would show client is still connected.

I tested the AP connection using phone wifi. If I directly turn off the phone wifi without calling explicit disconnect then I was facing this issue.

To solve this I have added a check if number of connected stations are zero then disconnect the client.

This solved my problem.

Thanks juraj for the debugging hint.