Restarting Wifi Link Connection?

Has anyone done any work/research on how to reconnect Wifi at the link level after a disconnect?

Do you mean an autoreconnect? Apart from having the program do it?

i did some quick tests on a connected FEZ Feather running 2.0 and I can concur that it does not auto reconnect after a disconnect.

My test procedure:

  1. Enabled WiFi hotspot on my phone
  2. Deployed sample application
  3. Waited for FEZ feather to connect
  4. Disabled hotspot
  5. Waited 10 second then re-enabled hotspot

This resulted with the following:

  1. NetworkLinkConnectedChanged event fire when connected
  2. NetworkAddressChanged event fires 2x, first with address 0.0.0.0 and then with the address from the hotspot
  3. Wifi status light came on
  4. When the hotspot was disabled, the NetworkLinkConnectedChanged event did not get triggered indicating a disconnect and the WiFi status light stays lit.
  5. Had to physically push the reset button to reconnect.
2 Likes

During another test I just did, I noticed that if the enable() method is called and the AP is not present then an exception is thrown.

An unhandled exception of type ‘System.ArgumentException’ occurred in GHIElectronics.TinyCLR.Devices.Network.dll

This is almost the exactly the same as I am experiencing. The only difference, is when the device, also Fez Feather, is in a spotty area, and was initially able to connect to AP, it is not able to reconnect if it loses connection. In my situation, the AP was still there.

We will check why Wifi disconnect event is not fired.

To Re-Enable you have call Disable first.

You can try, once get exception because wifi disconnect, try to Disable and ReEnable again.

Try to call Disable first.

Thanks Dat… I will try later today.

Thanks @Dat_Tran , will keep testing and provide feedback as necessary.

OK, it seems to be working…

After the Disable, I had to add …

 GC.Collect();
 GC.WaitForPendingFinalizers();

… to make sure that everything was cleaned up. Without GC there seemed to be several race conditions resulting in exceptions.

Now I need to do some long term testing.

3 Likes

Hey Mike,

At what point in your WiFi workflow do you called this routine?

I have a thread checking the link connection status, and when it sees the connection disappear, it disables and enables the network connection.

Nice. I’ll try putting some code together for others who maybe having the same issues.

Share it, please?

Here’s what I came up with

var gpioController = GpioController.GetDefault();
    var enablePin = gpioController.OpenPin(GHIElectronics.TinyCLR.Pins.SC20100.GpioPin.PA8);
            enablePin.SetDriveMode(GpioPinDriveMode.Output);
            enablePin.Write(GpioPinValue.High);
            var cs = gpioController.OpenPin(SC20100.GpioPin.PD15);

            WiFiNetworkInterfaceSettings wifiSettings = new WiFiNetworkInterfaceSettings()
            {
                //Ssid = "MOTOROLA-3258C",
                //Password = "0661c66a03810b23b5b1",
                Ssid = "SSID",
                Password = "PASSWORD",
                IsDhcpEnabled = true,
                IsDynamicDnsEnabled = true,
                TlsEntropy = new byte[] { 0, 1, 2, 3 }
            };

            var settings = new SpiConnectionSettings()
            {
                ChipSelectLine = cs,
                ClockFrequency = 4000000,
                Mode = SpiMode.Mode0,
                ChipSelectType = SpiChipSelectType.Gpio,
                ChipSelectHoldTime = TimeSpan.FromTicks(10),
                ChipSelectSetupTime = TimeSpan.FromTicks(10)
            };

            SpiNetworkCommunicationInterfaceSettings netInterfaceSettings = new SpiNetworkCommunicationInterfaceSettings()
            {
                SpiApiName = SC20100.SpiBus.Spi3,
                GpioApiName = SC20100.GpioPin.Id,
                SpiSettings = settings,
                InterruptPin = gpioController.OpenPin(SC20100.GpioPin.PB12),
                InterruptEdge = GpioPinEdge.FallingEdge,
                InterruptDriveMode = GpioPinDriveMode.InputPullUp,
                ResetPin = gpioController.OpenPin(SC20100.GpioPin.PB13),
                ResetActiveState = GpioPinValue.Low
            };
            var networkController = NetworkController.FromName("GHIElectronics.TinyCLR.NativeApis.ATWINC15xx.NetworkController");
            networkController.SetInterfaceSettings(wifiSettings);
            networkController.SetCommunicationInterfaceSettings(netInterfaceSettings);
            networkController.SetAsDefaultController();
            byte[] address = new byte[4];

            networkController.NetworkAddressChanged += (NetworkController sender, NetworkAddressChangedEventArgs e) =>
            {
                var ipProperties = sender.GetIPProperties();
                address = ipProperties.Address.GetAddressBytes();
                if (address[0] > 0)
                {

                }
            };

            networkController.NetworkLinkConnectedChanged += (s, e) =>
            {

            };

            //Starting a new thread for monitoring the WiFi
            new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        //Try enabling WiFi interface
                        networkController.Enable();
                        //Sleep for 5sec to give ample time fr wifi to connect
                        Thread.Sleep(5000);
                        //Poll WiFi connection link every 5sec
                        while (networkController.GetLinkConnected())
                        {
                            Thread.Sleep(5000);
                        }
                    }
                    catch (Exception)
                    {
                        Debug.WriteLine("Unable to connect to AP");
                        Thread.Sleep(2000);
                    }
                    networkController.Disable();
                }


            }).Start();
3 Likes