Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By TESTODYN
#79555 Hi All
on IoT device, ESP8266 Arduino, I've have some issue with random disconnections
User can set SSID and PASSWORD with an APP, so the code hase been a little modified starting from standard samples

cutting here below some snippets of my code

Code: Select all

void setup()
{
   Serial.begin(115200);
   WiFi.setAutoConnect(false)
   WiFi.setAutoReconnect(true);
   WiFi.hostname(("MYDEVICE_" + serialNumber).c_str());   
   if (AP)
   {
      turnAP(true);
   }
   else if (WIFI)
   {
      WiFi.mode(WIFI_STA);
      connectToWiFi(&ssid[0], &pass[0]);
   }
   else
   {
      WiFi.mode(WIFI_OFF);
   }

   loopCounter = 0;

} // setup

void loop()
{
   if (WiFi.status() == WL_CONNECTED)
   {
      if (lastConnectionState != WL_CONNECTED)
      {
         initWebSocket();
         connectWebSocket();
      }
      else
      {
         webSocket.loop();
      }
   }
   //Connection error during NORMAL BEHAVIOUR
   else if ((WiFi.status() == WL_NO_SSID_AVAIL ||
          WiFi.status() == WL_CONNECT_FAILED ||
          WiFi.status() == WL_CONNECTION_LOST) && !accessPointIsOn)
   {
      handleNetworkError(false);
   }
   //Connection error during AP
   else if ((WiFi.status() == WL_NO_SSID_AVAIL ||
          WiFi.status() == WL_CONNECT_FAILED ||
          WiFi.status() == WL_CONNECTION_LOST) && accessPointIsOn)
   {
      lastConnectionError = WiFiStatusToString();
      WiFi.disconnect();
      delay(50);
   }

   lastConnectionState = WiFi.status();
   loopCounter++;
}




Everithing works fine but sometimes, randomly, maybe after one hour or one week, I find the ESP not connected

I know... I used WiFi.setAutoConnect(false)
The reason is that I noticed that wiithout it, changing SSID after setup seems to be difficult
Without that, ESP starts immediately with automatic connection and this does not allow me to change SSID/PWD

setAutoReconnect works fine. In Fact, i I restart the router, ESP reconnect automatically

The proble is, don't know why, but sometimes I find ESP not connected

WHat I missing?
Is anybody can help me please :) ?

Thanks
User avatar
By btidey
#79561 See description of setAutoConnect and setAutoReconnect

https://github.com/esp8266/Arduino/blob ... -class.rst

In particular,

"If parameter autoReconnect is set to true, then module will try to reestablish lost connection to the AP. If set to false then module will stay disconnected."

If you want to change SSID / passwords then check out WifiManager for a user friendly solution to this.

I also usually put an occasional check o Wifi status in my application loop to trigger a reconnect if required.
User avatar
By TESTODYN
#79568 ok thanks
in my case, autoreccenct is true... autoconnect is false

Qheat are you used to do for example? do you set autoconnect to false?

btidey wrote:I also usually put an occasional check o Wifi status in my application loop to trigger a reconnect if required.


would you be so kind to share a sample of what you mean?