How to Initialize the .NET Gadgeteer WiFi Networking Module

Initializing the GHI Electronics WiFi RS21 module involves some different issues than initializing the Ethernet_J11D Module.  It took several runs to get my WiFi module working.  I don’t know all the permutations of WiFi networking, so I used a trial-and-error approach.

WiFi_RS21 Module and Fez Spider Mainboard

WiFi_RS21 Module and Fez Spider Mainboard

First, I plugged in the WiFi_RS21 module and added start-up code, equivalent to what I have been using with my ethernet module, to the ProgramStarted method .  When I started the deployment, the debugger output reported that the WiFi module was being reflashed; evidently the new SDK or driver includes an update.  It worked smoothly and re-flashed the module in about 30 seconds.  But, my initialization code was inadequate or in the wrong sequence, or both, so the application threw an exception.

The start-up process was hanging before it reached the end of the ProgramStarted method, that is, the debugger output didn’t show “Program Started” before the application threw an exception.  I thought the code was wrong, so I tinkered with it a bit, but I kept getting errors.  Because my code was wrong, and, because it was in the ProgramStarted method, it kept the application from running.  A few more attempts in this vein led to a situation where the mainboard lost communication with Visual Studio, and deployment to the mainboard was corrupted.  Resetting the device in the Project Properties/ .NET Micro Framework/Transport/USB/ to EMX_Gadgeteer was useful, but I had to run another application that I knew worked to get past this dilemma.

The requirements, in my environment, include the following settings of the WiFi_RS21 and the WiFiNetworkInfo object.

    wifi.UseDHCP();
    Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo();
    info.SSID = "MySecurityID";
    info.SecMode = WiFi_RS21.SecurityMode.WEP;
    info.networkType = WiFi_RS21.NetworkType.AccessPoint;
    wifi.Join(info, "PassPhrase");

To initialize the WiFiNetworkInfo object, you’ll need the SSID that your router provides for WiFi connections, and you have to identify the Security Mode (SecMode).  The SecMode options are listed in an enumeration that is part of the .NET Gadgeteer Core SDK as follows: Open: Not secured; WEP: Wired Equivalency Privacy; WPA: Wi-Fi Protected Access; WPA2: Wi-Fi Protected Access II.  There is another enumeration for NetworkType: AccessPoint or AdHoc (I don’t know the difference, but there are only two options).  These few properties of the WiFiNetworkInfo object were enough to get my module to run by calling the WiFi_RS21.Join method.  Now that the module is working, I’ve found that I can run all the initialization code in ProgramStarted.

In order to know when and if the WiFi connection is working, you’ll also want to initialize delegates for the NetworkUp and NetworkDown event handlers.

 wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkUp);
 wifi.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkDown);

For testing, all you’ll need is the NetworkUp delegate that prints information to the debugger as shown in the following code.  The NetworkUp delegate also illustrates a quick way to get the IP address for extending this application to make use of this new WiFi connection.

    Private void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
    {
        Debug.Print("Network Up!");
        Debug.Print("IP Address: " + wifi.NetworkSettings.IPAddress);
    }
    private void ethernet_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
    {
        Debug.Print("Network Down!");
    }
Advertisements

,

  1. #1 by Rajdey on February 25, 2012 - 11:33 AM

    Hi Michael,

    I am trting to connect the WiFi module to a EMX board.Below is the code i have written:

    try
    {
    wifi = new GTM.GHIElectronics.WiFi_RS21(9);
    wifi.UseDHCP();
    Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo();
    info.SSID = “RDBD”;
    info.SecMode = WiFi_RS21.SecurityMode.WEP;
    info.networkType = WiFi_RS21.NetworkType.AccessPoint;
    wifi.Join(info, “password”);
    }
    catch (Exception ex) { }

    Exception :

    Using mainboard GHIElectronics-FEZSpider version 1.0
    #### Exception System.Exception – 0xffffffff (1) ####
    #### Message:
    #### GHIElectronics.NETMF.Net.RS21Driver::SetBootloadOption [IP: 0000] ####
    #### GHIElectronics.NETMF.Net.WiFi::Enable [IP: 0051] ####
    #### Gadgeteer.Modules.GHIElectronics.WiFi_RS21::.ctor [IP: 0065] ####
    #### GLed.Program::ProgramStarted [IP: 0008] ####
    #### GLed.Program::Main [IP: 0015] ####
    #### Exception GHIElectronics.NETMF.Net.WiFi+WiFiException – 0x00000000 (1) ####
    #### Message:
    #### GHIElectronics.NETMF.Net.WiFi::Enable [IP: 0058] ####
    #### Gadgeteer.Modules.GHIElectronics.WiFi_RS21::.ctor [IP: 0065] ####
    #### GLed.Program::ProgramStarted [IP: 0008] ####
    #### GLed.Program::Main [IP: 0015] ####
    A first chance exception of type ‘GHIElectronics.NETMF.Net.WiFi.WiFiException’ occurred in GHIElectronics.NETMF.Net.dll
    #### Exception System.ApplicationException – 0x00000000 (1) ####
    #### Message: Unable to enable WiFi module.
    #### Gadgeteer.Modules.GHIElectronics.WiFi_RS21::.ctor [IP: 00c4] ####
    #### GLed.Program::ProgramStarted [IP: 0008] ####
    #### GLed.Program::Main [IP: 0015] ####
    A first chance exception of type ‘System.ApplicationException’ occurred in GTM.GHIElectronics.WiFi_RS21.dll

    Would really appreciate if you could help me.I am also confused about the socket i am connecting. I have got letters in my mainboard , and the Visual studio diagram shows numbers for the socket. Currently i have connected the WiFi module in the socket called “J” but in VS the connection shown is number 9.Is this a reason for the exception ?

    Thanks in advance
    rajdey.

    • #2 by Michael Dodaro on February 25, 2012 - 11:50 AM

      Either socket 6 or socket 9 should be fine to connect the Wifi module. Where is the code running? Is it in PrgramStarted()? I had to put the code in a button pressed event handler to get it to work the first time. Try adding a button and code like this:

      void button_ButtonPressed(Button sender, Button.ButtonState state)
      {
      wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkUp);
      wifi.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkDown);
      wifi.UseDHCP();

      Gadgeteer.Modules.GHIElectronics.WiFi_RS21.WiFiNetworkInfo info = new WiFi_RS21.WiFiNetworkInfo();
      info.SSID = “RDDB”;
      info.SecMode = WiFi_RS21.SecurityMode.WEP;
      info.networkType = WiFi_RS21.NetworkType.AccessPoint;
      wifi.Join(info, “password”);
      }
      Let the mainboard initialize and wait a couple of minutes. Then push the button and wait for it to run. This worked for me. Then after it was working I could put the code in ProgramStarted and it ran.

  2. #3 by Rajdey on February 25, 2012 - 8:50 PM

    Thanks Micheal for the quick reply.

    I have connected a button and tried the code you suggested, but the button released event did not fire. Rather i am getting the exception “Using mainboard GHIElectronics-FEZSpider version 1.0
    #### Exception Gadgeteer.Socket+InvalidSocketException – 0x00000000 (1) ####
    #### Message: Socket 1 does not support one of the types ‘XY’ required by Button module.”

    I think I am definately doing something wrong.The challenge I am facing is that I am not sure I have connected the module to the right socket. All the sockets in the mainboard i am using does not have numbers, but just letters.The mainboard says “MainBoard 0.4”

    I am still trying to figure out the issue.Will keep you posted.

    Thanks
    Rajdey

    • #4 by Michael Dodaro on February 25, 2012 - 9:27 PM

      Which mainboard are you using?

      • #5 by Rajdey on February 26, 2012 - 12:15 AM

        I am using the Mainboard 0.4 , this is written in the board , i have sent you the image of the board to your mail. Please help me to recognise the sockets. I am quite sure now that I have not connected the right socket. I am able to debug code , and it is correctly deploying to the board from visual studio.However , i feel the mainboard is not able to detect the modules.

        Again Thanks so much for helping.
        Rajdey

      • #6 by Michael Dodaro on February 26, 2012 - 7:49 AM

        Rajdey,
        That is not a .NET Gadgeteer mainboard. See those available here: http://www.netmf.com/gadgeteer/showcase.aspx?showcaseid=1 I use the Fez Spider from GHI Electronics: http://www.ghielectronics.com/catalog/product/269

  3. #7 by Rajdey on February 26, 2012 - 10:29 AM

    Thanks Michael, appreciate your help. My bad luck, I will get the Fez Spider and start over again.

  4. #8 by harleydk on May 14, 2012 - 3:03 PM

    This was very helpful to me. Thanks a lot.

  5. #9 by MJ on December 1, 2015 - 2:17 AM

    Hello Micheal, I am trying to control 4 servo motors using a Remote controller(with joysticks). I am also planing to start with Fez Spider board.

    1. Is this good enough to translate the joystick movements to Servo motor control?
    2. Also planning to create an iPad app to connect to the board for future requirements. Which one would suit better Wifi or Bluetooth, the range being 200 m? Which one would be more stable in terms of connectivity etc?

    I appreciate your reply. Kindly excuse if my questions are naive!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: