Raptor & RS21: A happy couple

Hi All,

read some questionable posts on having not the proper events fired with an RS21 in 4.2 so I tried a little bit and came to the following result …

I have tried this over and over again and did not get my Raptor to hook up with my AP. Important is to be patient …

The output:
Using mainboard GHI Electronics FEZRaptor version 1.0
RS9110 firmware version Number is 4.4.5
RS9110 driver version Number is 4.4.5
06/01/2011 00:55:52.leaving main program
06/01/2011 00:55:53…Timer event started.
06/01/2011 00:55:53…hooking up the event…
06/01/2011 00:55:53…the event is hooked.
06/01/2011 00:55:53…setting to use DHCP…
06/01/2011 00:55:53…DHCP set executed.
06/01/2011 00:55:53…scanning the network…
06/01/2011 00:55:54…joining the network…
06/01/2011 00:56:00…Wireless connectivity event started.
06/01/2011 00:56:00…RSSI: 54
06/01/2011 00:56:00…MAC : F8-1A-67-7E-21-C3
06/01/2011 00:56:00…network joined.
06/01/2011 00:56:00…Timer event stopped.
The thread ‘’ (0x3) has exited with code 0 (0x0).
06/01/2011 00:56:01…IP : 192.168.39.29
06/01/2011 00:56:01…Wireless connectivity event ended.

Using the following code:


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Net;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

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

using System.Net;
using System.Net.Sockets;
using System.Text;

namespace GadgeteerApp1
{
    public partial class Program
    {
        GHI.Premium.Net.WiFiNetworkInfo[] wi = null;

        // 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_Tick2);
            t.Start();

            Debug.Print(DateTime.Now + ".leaving main program");
        }

        void t_Tick2(GT.Timer timer)
        {
            // The start text
            Debug.Print(DateTime.Now + "..Timer event started.");

            // Prevent the timer from firing again
            timer.Stop();

            // Do simple event hook up
            Debug.Print(DateTime.Now + "..hooking up the event...");
            wifi_RS21.Interface.WirelessConnectivityChanged += Interface_WirelessConnectivityChanged;
            Debug.Print(DateTime.Now + "..the event is hooked.");

            // use the router's DHCP server to set my network info
            Debug.Print(DateTime.Now + "..setting to use DHCP....");
            wifi_RS21.UseDHCP();
            Debug.Print(DateTime.Now + "..DHCP set executed.");

            // Acquire our network
            Debug.Print(DateTime.Now + "..scanning the network....");
            wi = wifi_RS21.Interface.Scan("your-ssid");
            if (wi != null)
            {
                Debug.Print(DateTime.Now + "...joining the network....");
                wifi_RS21.Interface.Join(wi[0], "your-phrase");
                Debug.Print(DateTime.Now + "...network joined.");
            }

            // The end text
            Debug.Print(DateTime.Now + "..Timer event stopped.");
        }

        void Interface_WirelessConnectivityChanged(object sender, GHI.Premium.Net.WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            Debug.Print(DateTime.Now + "..Wireless connectivity event started.");

            if (e.IsConnected)
            {
                Debug.Print(DateTime.Now + "...RSSI: " + e.NetworkInformation.RSSI.ToString());
                Debug.Print(DateTime.Now + "...MAC : " + GetMACAddress(e.NetworkInformation.PhysicalAddress));

                // Wait here until an IP address is assigned for five seconds
                // I didn't want to wait for eternity, that's why 5 secs.
                // But the delay can be changed to wait while true = true
                // Maybe than you want to flash the on board LED in a  
                // certain way so you are at least kind of informed why it is not working
                bool Is_LED_On = true;
                int secs = 50;
                while (secs > 0)
                {
                    if (wifi_RS21.NetworkSettings.IPAddress != "0.0.0.0") break;
                    secs -= 1;
                    // Blink on board LED
                    Mainboard.SetDebugLED(Is_LED_On);
                    Is_LED_On = !Is_LED_On;
                    Thread.Sleep(100);
                }
                // Do not forget to turn off the LED
                Mainboard.SetDebugLED(false);
                Debug.Print(DateTime.Now + "...IP  : " + wifi_RS21.NetworkSettings.IPAddress);
            }
            // We can set a one time wait to signal the main loop or a timed init to signal we have an IP
            // Maybe we should ping something easy to signal proper ip as well
            // And set the RTC, once we have internet connectivity
            Debug.Print(DateTime.Now + "..Wireless connectivity event ended.");
        }

        // borrowed from GHI's documentation
        string GetMACAddress(byte[] PhysicalAddress)
        {
            return ByteToHex(PhysicalAddress[0]) + "-"
                                + ByteToHex(PhysicalAddress[1]) + "-"
                                + ByteToHex(PhysicalAddress[2]) + "-"
                                + ByteToHex(PhysicalAddress[3]) + "-"
                                + ByteToHex(PhysicalAddress[4]) + "-"
                                + ByteToHex(PhysicalAddress[5]);
        }

        string ByteToHex(byte number)
        {
            string hex = "0123456789ABCDEF";
            return new string(new char[] { hex[(number & 0xF0) >> 4], hex[number & 0x0F] });
        }
    }
}

Enjoy and have your go, it works as a dream for me. The blinking on board LED is just 4, 5 times flashing than an IP address is assigned and that as you can read with a pretty stable 9 secs.