Snippet - WiFi RS21 module wrapper

WiFi RS21 module wrapper

The GHI drivers are straightforward enough to use. I put this together as a helper, to facilitate some (what I would think) are typical uses.

Key points:

  • Events are setup for SSID, Address, Status, RSSI changes
  • Automatically join the strongest known network
  • If strongest network join fails, it will try to join other known networks
  • Caching of Scan() results, as you can’t scan after joining a network
  • Optional getting time from NIST (borrowed code from Nicholas’ WizNet code: )

In my app, I store the hashtable values in an SD, but this could be done very easily in code (see example). In the video, the debug output is only from event handlers.


  • Add Static IP assignment, currently only DHCP
  • Add the ability to join unsecured networks automatically
  • Add the ability to join unsecured networks automatically, with an approval “I agree” page
  • Find work around to the module automatically rejoining a network after a Disconnect() is issued

Uses my Hashtable extension to search keys in an case insensitive manner - I included it here as well, it’s available separately here:


Hi, I’m evaluating your WiFi wrapper and it works great!! Can you give me an example how to use the events?


Glad it works for you. Feel to modify to suit your needs.

What are you looking to do with the events?

Below are the events and some basic description.

// This is fired off when the NNTP based time is retrieved
        public event TimeUpdateDelegate TimeUpdated;       

// Used for detailed logging
        public event LogRequestDelegate LogRequested;

// I was hoping this would have been fired even if connected, but it seems to be a limitation of the driver.
        public event StrengthChangeDelegate StrengthChanged;

// this is fired when Address property changed (ie, when DHCP address is retrieved)
        public event AddressChangeDelegate AddressChanged;

// when the SSID property was changed 
        public event WifiSSIDDelegate SSIDChanged;

// when the wifi class status is changed 
        public event WifiStatusDelegate StatusChanged;

I’m using your wrapper for quite a while now! I’ve implemented several events (@ mhectorgato: thx for the info) and it works almost for a 100%. Almost, because i’ve got a small issue and I don’t know if it’s the wrapper or the wifi rs21 module. When I boot up my spider for the first time it connect to the wifi network and it does a time update. So far so good. But when I restart the Spider and it tries to connect again it refuses and it give me an error that it can’t join the network. If I power of the Spider for about 30 to 45 secs and power it up again it connects without any issues.

Do you have any idea?


Glad it’s working for you! I’ve not experienced that specific issue.

I’ll play around with it for a while and see if I can replicate the issue. Do you by any chance have another Spider or RS21 module handy?

No, currently not. I’m also investigating this on my Cisco AP but there is nothing to find.

RFID-Dude -

I just rebuilt my solution with my 2 Codeshare software components:


  1. Spider
  2. DP
  3. T35
  4. WiFi RS32 in socket 6.

I ran the project in VS and it worked fine. Then I disconnected the USB and stopped the VS running project. Next I reconnected the USB (not using the USB), and it started up and connected. Then I hit the restart button and it connected OK, and repeated with it connected OK again.

So far I can’t recreate the issue. If you can provide additional debug info, I would be glad to do more looking into it.

Below is my test code:

using System.Collections;
using GT = Gadgeteer;

namespace WiFiTesting
    public partial class Program
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
            GT.Timer t = new GT.Timer(500);
            t.Tick += (tmr) =>
        WiFiModule _wifi;

        void rs21()
            // wifi is the object generated by the Gadgeteer designer for the WiFi RS21
            _wifi = new WiFiModule(wifi);
            Hashtable creds = new Hashtable();
            creds.Add("network1", "password1");
            creds.Add("Network1", "Password2");

            // Setup event handlers you are interested in receiving here. In the video, all debug output is only from event handlers.
            ScreenDebugger.T35 = display;

            _wifi.StatusChanged += new WiFiModule.WifiStatusDelegate(_wifi_StatusChanged);
            _wifi.StrengthChanged += new WiFiModule.StrengthChangeDelegate(_wifi_StrengthChanged);
            _wifi.TimeUpdated += new WiFiModule.TimeUpdateDelegate(_wifi_TimeUpdated);
            _wifi.SSIDChanged += new WiFiModule.WifiSSIDDelegate(_wifi_SSIDChanged);
            _wifi.LogRequested += new WiFiModule.LogRequestDelegate(_wifi_LogRequested);
            _wifi.AddressChanged += new WiFiModule.AddressChangeDelegate(_wifi_AddressChanged);

            _wifi.NetworkCredentials = creds;

        void _wifi_AddressChanged(object sender, WiFiModule.AddressEventArg e)
            ScreenDebugger.AddToLog("Address = " + e.Address);

        void _wifi_LogRequested(object sender, WiFiModule.LogRequestArgs e)
            ScreenDebugger.AddToLog("LogEvent = " + e.LogEvent);

        void _wifi_SSIDChanged(object sender, WiFiModule.SSIDEventArg e)
            ScreenDebugger.AddToLog("SSID = " + e.SSID);

        void _wifi_TimeUpdated(object sender, WiFiModule.TimeUpdatedArgs e)
            ScreenDebugger.AddToLog("Time updated = " + e.NewTime);

        void _wifi_StrengthChanged(object sender, WiFiModule.StrengthEventArg e)
            ScreenDebugger.AddToLog("Strength = " + e.Strength);

        void _wifi_StatusChanged(object sender, WiFiModule.StatusEventArg e)
            ScreenDebugger.AddToLog("Status = " + _wifi.StatusString);

Hi mhectorgato,

Thanks for your research! :slight_smile: When I get back from work at the and of the day I’ll copy past your code and run it on my Spider. I get back to you this evening.

Triggered to find the error, I went home during lunch and copy paste your code and ran it on my spider. The first time it fired up as expected:

Using mainboard GHIElectronics-FEZSpider version 1.0
RS21 WiFi module version Number is 4.4.5
WiFi Module’s MAC Address is 03516727202234
Status = Scanning
Status = Not connected
SSID = devnet
Status = Joining
The thread ‘’ (0x3) has exited with code 0 (0x0).
Status = Connecting
The thread ‘’ (0x6) has exited with code 0 (0x0).
Address =
Strength = -56
Status = Getting time
Time updated = 08/29/2012 12:30:33
Status = Connected
The thread ‘’ (0x8) has exited with code 0 (0x0).

after that I stopped my project en rebooted the spider and it failed to join my wifi, the follow output was created:

Using mainboard GHIElectronics-FEZSpider version 1.0
RS21 WiFi module version Number is 4.4.5
WiFi Module’s MAC Address is 03516727202234
Status = Scanning
Status = Not connected
SSID = devnet
Status = Joining
#### Exception System.Exception - 0x00000000 (6) ####
#### Message:
#### GHIElectronics.NETMF.Net.RS21Driver::Join [IP: 0032] ####
#### GHIElectronics.NETMF.Net.WiFi::Join [IP: 0082] ####
#### Gadgeteer.Modules.GHIElectronics.WiFi_RS21::Join [IP: 0008] ####
#### PAVESoft.Modules.WiFiModule::doStartWifi [IP: 0092] ####
#### Exception GHIElectronics.NETMF.Net.WiFi+WiFiException - 0x00000000 (6) ####
#### Message:
#### GHIElectronics.NETMF.Net.WiFi::Join [IP: 00ce] ####
#### Gadgeteer.Modules.GHIElectronics.WiFi_RS21::Join [IP: 0008] ####
#### PAVESoft.Modules.WiFiModule::doStartWifi [IP: 0092] ####
The thread ‘’ (0x3) has exited with code 0 (0x0).
A first chance exception of type ‘GHIElectronics.NETMF.Net.WiFi.WiFiException’ occurred in GHIElectronics.NETMF.Net.dll
LogEvent = wifi start exception: Exception was thrown: GHIElectronics.NETMF.Net.WiFi+WiFiException
Status = Failed to join
The thread ‘’ (0x6) has exited with code 0 (0x0).

If ordered a new Access Point to see its a problem of my cisco. It should be delivered this afternoon.

I’ve got a cheapo Linksys AP – figured that would have an issue before a Cisco.

Interesting … Looks like the exception is being thrown somewhere in GHI driver land - not sure if it’s in the native or managed code. I’m just calling the GHI.Gadgeteer Join method, passing in the WiFi_RS21.WiFiNetworkInfo,

You may to want to put a breakpoint on this line and verify the values are valid:

_wifi.Join(((WiFi_RS21.WiFiNetworkInfo)info), (string)pass);

edit: this is working while in debug mode and it’s joining the same network in both of your outputs, so likely this isn’t the cause of the issue.

Also, you may want to download the GHI RS21 driver source code and run it along with the code and see where exactly the exception is occuring.Here’s the wiki article on this:

Are you deploying Release code when running it outside of Visual Studio? I’ve not tried that myself (on any of my projects)

Ok, I did some testing with a different access point (a zyxell) and the system.exception doesn’t occur. I’ll pinpoint my research towards my cisco access point for now. Tomorrow I start testing the wifi class with the WiFi network at work . It is also Cisco based but with different access points.

I post my results here in this thread.


Maybe this is helpful for you (Page 2 and 3):

I’ll have a look at that! thanks