-->
Page 1 of 2

WIFI Connection.

PostPosted: Wed Nov 24, 2021 10:16 am
by john77
My ESP8266 is connected to a router. I get a status - WL_CONNECTED. Now I switch off the router, status - WL_NO_SSID_AVAIL. I switch on the router, status - WL_CONNECTED.
So may I relay on ESP8266 mechanism for connection recovering or should I poll the WIFI connection status in my program any way?

Re: WIFI Connection.

PostPosted: Wed Nov 24, 2021 5:02 pm
by Inq720
I've tested that built-in functionality and it's bullet proof as far as I can see. It was some time ago, but I believe it'll try to reconnect about every three seconds... forever... or at least more than 24 hours which is all I had the patience to wait. There is a low-level method wifi_station_set_reconnect_policy(true) that enables/disables it. You might want to confirm that none of the other libraries libraries between your program and the low level Espressif stuff kicks it.

Re: WIFI Connection.

PostPosted: Thu Nov 25, 2021 2:12 am
by john77
Thank you.

Re: WIFI Connection.

PostPosted: Thu Nov 25, 2021 5:48 am
by Inq720
I forgot to add, I've found that the ESP8266 in some situations will disconnect and reconnect quite regularly. In others, it is solid as a rock. Haven't figured why, but it doesn't seem to be based on strength of the WiFi connection.

I also didn't really answer your question. Whether you continue to poll for WiFi status depends on if you need to know that you're connected. You don't need to, to re-initiate the connecting process, but you obviously can't sends stuff if you're disconnected. So it depends on how important the information you have is to be successfully sent.

If you prefer, instead of polling to see if your WiFi status is "currently" connected, there is a low level method to supply a callback for WiFi events.

wifi_set_event_handler_cb(statusHandler);

And here is a sample statusHandler

Code: Select allICACHE_FLASH_ATTR void statusHandler(System_Event_t *evt)
{
    switch (evt->event)   
    {
        case EVENT_STAMODE_CONNECTED:
            dbg("connect to ssid %s, channel %d\n",   
                 evt->event_info.connected.ssid,   
                 evt->event_info.connected.channel);
            break;

        case EVENT_STAMODE_DISCONNECTED:
            dbg("disconnect from ssid %s, reason %d\n",   
                 evt->event_info.disconnected.ssid,   
                 evt->event_info.disconnected.reason);
// * When router powers off, we get reason REASON_ASSOC_EXPIRE (4) once, then
//      REASON_NO_AP_FOUND (201) repeatedly trying to reconnect.  Might be
//      better to respond to 4.  Keep evaluating.
// * Got 201 once on first connection, then proceeded to connect properly.
            if ((evt->event_info.disconnected.reason == REASON_ASSOC_EXPIRE) &&
                (_Hdr.start == aStrongest))
            {
                wifi_station_disconnect();
                InqInterval::SetInterval(scanner, 1, NULL, AUTO_LBL);
            }
            break;

        case EVENT_STAMODE_AUTHMODE_CHANGE:
            dbg("mode: %d -> %d\n",   
                 evt->event_info.auth_change.old_mode,   
                 evt->event_info.auth_change.new_mode);
            break;

        case EVENT_STAMODE_GOT_IP:
            dbg("ip:" IPSTR " mask: " IPSTR " gw: " IPSTR "\n",
                IP2STR(&evt->event_info.got_ip.ip),
                IP2STR(&evt->event_info.got_ip.mask),
                IP2STR(&evt->event_info.got_ip.gw));
            break;
           
        case EVENT_STAMODE_DHCP_TIMEOUT:
            dbg("EVENT_STAMODE_DHCP_TIMEOUT\n");
            break;

        case EVENT_SOFTAPMODE_STACONNECTED:
            dbg("station: " MACSTR " join, AID = %d\n",   
                MAC2STR(evt->event_info.sta_connected.mac),   
                evt->event_info.sta_connected.aid);
            break;

        case EVENT_SOFTAPMODE_STADISCONNECTED:
            dbg("station: " MACSTR " leave, AID = %d\n",   
                MAC2STR(evt->event_info.sta_disconnected.mac),   
                evt->event_info.sta_disconnected.aid);
            break;
           
        case EVENT_SOFTAPMODE_PROBEREQRECVED:
            dbg("station: " MACSTR " leave, rssi = %d\n",   
                MAC2STR(evt->event_info.ap_probereqrecved.mac),   
                evt->event_info.ap_probereqrecved.rssi);
            break;
           
        case EVENT_OPMODE_CHANGED:
            dbg("OpMode old=%u, new=%u\n",
                evt->event_info.opmode_changed.old_opmode,
                evt->event_info.opmode_changed.new_opmode);
            break;
           
        case EVENT_OPMODE_CHANGED + 1:// EVENT_SOFTAPMODE_DISTRIBUTE_STA_IP:
            {
                u8* p = (u8*)&evt->event_info;
                dbg("station: " MACSTR " ip: " IPSTR " ex: %u, %u\n",
                    MAC2STR(evt->event_info.sta_disconnected.mac),  // 0-5
                    IP2STR(&evt->event_info.got_ip.gw),     // 8-11
                    *(p+6), *(p+7));  // 6,7
            }
            break;

        default:
           break;
     }
}


Good luck