WebSocketServer Keeps disconnecting

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WebSocketsServer.h>

const char *ssid =  "SSID";   //Hotspot SSID  
const char *pass =  "Password"; //Hotspot password

IPAddress local_ip(192,168,0,1);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);

WebSocketsServer webSocket = WebSocketsServer(81); //websocket init with port 81

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  
  Serial.println("web socket method called");
  
    String cmd = "", id = "", password = "", uid = "";
    bool states;
    
    switch(type) {
      
        case WStype_DISCONNECTED:
            Serial.println("Websocket is disconnected");
            webSocket.close();
            startLocalHostConnection();
            break;
            
        case WStype_CONNECTED:{
            Serial.println("Websocket is connected");
            Serial.println(webSocket.remoteIP(num).toString());
            webSocket.sendTXT(num, "connected");}
            break;
            
        case WStype_TEXT:
            cmd = "";
            for(int i = 0; i < length; i++) {
                cmd = cmd + (char) payload[i]; 
            }

            if(cmd == "close") {
              WiFi.softAPdisconnect(true);
              ESP.reset();
            } else {
              id = getValue(cmd, '\n', 0);
              password = getValue(cmd, '\n', 1);
              uid = getValue(cmd, '\n', 2);
  
              Serial.println(id);
              Serial.println(password);
              Serial.println(uid);
  
              if(eeprom.updateIdPassword(id, password, uid)) {
                webSocket.sendTXT(num, "success");
  //              ESP.reset();
              } 
              else {
                webSocket.sendTXT(num, "failed");
  //              ESP.reset();
              }
            }
            break;
            
        case WStype_FRAGMENT_TEXT_START:
            break;
            
        case WStype_FRAGMENT_BIN_START:
            break;
            
        case WStype_BIN:
            hexdump(payload, length);
            break;
            
        default:
            break;
    }
}

void startLocalHostConnection() {
  webSocket.begin(); //websocket Begin
  webSocket.onEvent(webSocketEvent); //set Event for websocket
}

void setup() {
  WiFi.softAP(ssid, pass); //turn on Hotspot
  WiFi.softAPConfig(local_ip, gateway, subnet); //set Static IP gateway on NodeMCU
        
  startLocalHostConnection();
}

This is my minimal code, I want some I/O events between my Flutter app and ESP8266 board, some days ago It's working fine but now its always disconnecting, even after connecting to ESP hotspot I try to open "http://192.168.0.1:81" manually but it showing site can't be reached. Please help me in this situation.

what do you have in the loop() ?

My original code is so big so I just shared the part of WebSocket and foegot to add loop. In My loop I have added webSocket.loop(); and just to know that In my loop it takes 3-4 seconds of delay to repeat the loop

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