#Any suggestions for detecting a dropped network?

1 messages · Page 1 of 1 (latest)

reef crescent
#

Hi all, I have an ESP32 I am running as an MQTT client. Things work great over wifi for a bit, and then the system crashes. The trace says that its crashing during an MQTT keep alive, which leads me to believe that the Wi-Fi is dropping out.

Is there any way to detect when the network has dropped off? I don't want the device to quietly lock up once I start putting them out there.

An event that says this is happening 'Reconnecting' would be awesome, but I dont see anything like that.

Im using the WifiNetworkhelper

Thanks!

echo sky
#
using nanoFramework.Networking;
using System.Device.Wifi;
using System.Net.NetworkInformation;

public class Program
{

    NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;

    private void NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityEventArgs e)
    {
        Debug.WriteLine("\n############ Network Changed ######################");

        if (e.IsAvailable)
        {
            // Do something when Network is ON
        }
        else
        {
            // Do Clean Up when Network is OFF
        }
    }
}```
reef crescent
#

23:04:01:069 SendMQTT Called
23:04:10:568 ++++ Exception System.Net.Sockets.SocketException - CLR_E_FAIL (3) ++++
23:04:10:568 ++++ Message:
23:04:10:568 ++++ System.Net.Sockets.NativeSocket::recv [IP: 0000] ++++
23:04:10:568 ++++ System.Net.Sockets.Socket::Receive [IP: 0018] ++++
23:04:10:568 ++++ nanoFramework.M2Mqtt.MqttNetworkChannel::Receive [IP: 0049] ++++
23:04:10:568 ++++ nanoFramework.M2Mqtt.MqttClient::ReceiveThread [IP: 0015] ++++
23:09:00:067 ++++ Exception nanoFramework.M2Mqtt.Exceptions.MqttCommunicationException - 0x00000000 (4) ++++
23:09:00:067 ++++ Message:
23:09:00:067 ++++ nanoFramework.M2Mqtt.MqttClient::SendReceive [IP: 0044] ++++
23:09:00:067 ++++ nanoFramework.M2Mqtt.MqttClient::SendReceive [IP: 000d] ++++
23:09:00:067 ++++ nanoFramework.M2Mqtt.MqttClient::Ping [IP: 000d] ++++
23:09:00:067 ++++ nanoFramework.M2Mqtt.MqttClient::KeepAliveThread [IP: 0035] ++++

#

This is one of them.

echo sky
#

You need to tear down the MQTT and restart MQTT when the underlaying network is pulled (Wifi connection is lost or connected but no network - happens alot with mobile internet connection).

reef crescent
#

@echo sky Thanks - so for mqtt is that just a disconnect and connect again or do I need to kill all the objects? I put your code in, and of course now its not crashing at all. Going to leave it running over night to see if it catches it before it crashes out on MQTT. Thank you!

echo sky
#

The MQTT uses tcp socket internally and when that goes due to network going missing then the upper layer will be in limbo. so it is best to drop the existing MQTT and re-create a new one.

reef crescent
#

Thanks again, It took a few hours this time, but it did detect the failure right before. I can code around this now - thanks!

echo sky
#

To quickly simulate WiFi failure just switch your router off and simulate no internet, just unplug the router to CPE utp cable

faint vault
frozen ocean
#

@echo sky , any idea why WifiNetworkHelper.Status is 0 (i.e. 'None') after the ESP32 boots & AutoConnects?
Shouldn't there be a 'Connected' value in the NetworkHelperStatus enum?

echo sky
frozen ocean
#

Interesting. Could it be renamed DhcpHelper.Status to avoid confusion??
Is there any way to detect when an ESP32 configured with AutoConnect is no longer communicating?

echo sky
#

My sample code above should detect when Wifi connection dropped out.

frozen ocean
#

I tried it but NetworkChange_NetworkAvailabilityChanged() never gets called. I think the ESP32 unit may be conking out for other reasons.
It may be getting too hot or perhaps the ADC input reading the battery level is getting too much voltage as you suggested in the other thread from 'targets-esp32'.
I'll try disconnecting the ADC input tomorrow.
Thanks much for the help & ideas!!

woeful fog
#

The way I do this is by trapping exception in the network. As an example, if the MQTT gets disconnected, I can try to reconnect but if there is an exception, I can assume there is a network failure. So, In those cases, I sleep for few seconds, wake up and reconnect again. That can be improved but it worked for me perfectly.