Bug? WiFi RS21 -- after a Disconnect(), if you Scan(), it automatically re-joins

I was trying to give back to community for help I’ve received, so I tried to tackle the Wiki entry for the WiFi module. What I came across was that after joining a network, if you disconnect and then do a scan, the device automatically will join the network again. I experienced this with my project, actual but figured it was something in that code, but now it happened in a very simple program as well.

I looked at the driver code in Gadgeteer.CodePlex.com for Scan, and it just returns the results.

My debug output shows this:


using Microsoft.SPOT;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using Gadgeteer.Modules.GHIElectronics;

namespace WiFiSample
{
    public partial class Program
    {
        bool firstAttempt = true;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            GT.Timer t = new GT.Timer(1000);
            t.Tick += new GT.Timer.TickEventHandler(t_Tick);
            t.Start();
        }

        void t_Tick(GT.Timer timer)
        {
            timer.Stop();

            // setup events
            wifi.NetworkDown += new GT.Modules.Module.NetworkModule.NetworkEventHandler(wifi_NetworkDown);
            wifi.NetworkUp += new GT.Modules.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp);

            // to use hold the network to connect to
            WiFi_RS21.WiFiNetworkInfo info = null;

            // use the router's DHCP server to set my network info
            wifi.UseDHCP();
            
            info = wifi.Search("Network");                
            if (info != null)
            {
                wifi.Join(info, "Password");
                Debug.Print("Network joined");                
            }
        }

        void wifi_NetworkDown(GT.Modules.Module.NetworkModule sender, GT.Modules.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network down event; state = " + (state == GT.Modules.Module.NetworkModule.NetworkState.Down? "down" : "up") );                            
        }

        void wifi_NetworkUp(GT.Modules.Module.NetworkModule sender, GT.Modules.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network Up event; state = " + (state == GT.Modules.Module.NetworkModule.NetworkState.Down ? "down" : "up"));

            if (firstAttempt)
            {
                Gadgeteer.Networking.HttpRequest wc = WebClient.GetFromWeb("http://www.tinyclr.com");
                wc.ResponseReceived += new HttpRequest.ResponseHandler(wc_ResponseReceived);
                firstAttempt = false;
            }
        }

        void wc_ResponseReceived(HttpRequest sender, HttpResponse response)
        {            
            Debug.Print("Response Received");
            wifi.Disconnect();
            wifi.Scan();
        }
    }
}

void wc_ResponseReceived(HttpRequest sender, HttpResponse response)
        {            
            Debug.Print("Response Received");
            wifi.Disconnect();
            wifi.Scan();
        }

You need to wait till you get the disconnected event then scan again.

Thanks - I’ll give that a try.

Still happening.

It now does it in this: wifi_NetworkDown, Scan(), Join(), wifi_NetworkUp, Disconnect(), wifi_NetworkDown, Scan() and wifi_NetworkUp

After the down + scan, it takes about 30 seconds and then the Up is fired again.


void wifi_NetworkDown(GT.Modules.Module.NetworkModule sender, GT.Modules.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network down event; state = " + (state == GT.Modules.Module.NetworkModule.NetworkState.Down? "down" : "up") );
            if (!firstAttempt)
            {
                wifi.Scan();
            }
        }
 
        void wifi_NetworkUp(GT.Modules.Module.NetworkModule sender, GT.Modules.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network Up event; state = " + (state == GT.Modules.Module.NetworkModule.NetworkState.Down ? "down" : "up"));
 
            if (firstAttempt)
            {
                Gadgeteer.Networking.HttpRequest wc = WebClient.GetFromWeb("http://www.tinyclr.com");
                wc.ResponseReceived += new HttpRequest.ResponseHandler(wc_ResponseReceived);
                firstAttempt = false;
            }
        }
 
        void wc_ResponseReceived(HttpRequest sender, HttpResponse response)
        {            
            Debug.Print("Response Received");
            wifi.Disconnect();            
        }

We will look into it.

You are correct. It is automatically reconnecting after 30 seconds from Scanning for networks again. We need to double check with Redpine Signals on this issue.
The fix might take a while.

Thanks.