Problem with Chipworkx and RS21 wifi module

I have connected the RS21 module to the SV6 UEXT header on the Chipworkx development board. I’m trying to get it work using the code found in the GHI documentation in GHIElectronics.NETMF.Net.Wifi class. The problem is that the green LED on the module turns on, but I get this error message and WiFi.IsEnabled throws an exception, but I can’t understand why. Can you help me? Thanks

Enable WiFi RS2 module
#### Exception GHIElectronics.NETMF.Net.WiFi+WiFiException - 0x00000000 (1) ####
#### Message:
#### GHIElectronics.NETMF.Net.WiFi::Enable [IP: 0082] ####
#### Program::Main [IP: 001c] ####
A first chance exception of type ‘GHIElectronics.NETMF.Net.WiFi.WiFiException’ occurred in GHIElectronics.NETMF.Net.dll
#### Exception System.Exception - 0x00000000 (1) ####
#### Message:
#### Program::Main [IP: 00a4] ####
A first chance exception of type ‘System.Exception’ occurred in ChipworkX Application.exe
An unhandled exception of type ‘System.Exception’ occurred in ChipworkX Application.exe

Please post the code you are using

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

If you could edit your existing post and highlight the code section, then click the CODE (1010) button that would be great !
/// Add these libraries to your project's References
/// System
/// Microsoft.SPOT.Hardware
/// Microsoft.SPOT.Net
/// GHIElectronics.NETMF.System
/// GHIElectronics.NETMF.Net
using System;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Hardware;


    public class Program
    {

        static public ManualResetEvent wait = new ManualResetEvent(false);
        static public string myWiFiNetworkSSID = "linksys1";
        static public string PassPhrase = "hellopass";
        public static void Main()
        {
            //ChipworkX Developement System V1.5 UEXT header with WiFi RS21 Module: P/N:GHI-WIFIEXP2-298
            SPI.SPI_module _spi = SPI.SPI_module.SPI2; /*SPI bus*/
            Cpu.Pin _cs = ChipworkX.Pin.PC9; /*ChipSelect*/
            Cpu.Pin _ExtInt = ChipworkX.Pin.PA19; /*External Interrupt*/
            Cpu.Pin _reset = ChipworkX.Pin.PC8; /*Reset*/
            /*-------------------------------------------------------------------*/
            //EMX Developement System V1.3 UEXT header with WiFi RS21 Module: P/N:GHI-WIFIEXP2-298
            //SPI.SPI_module _spi = SPI.SPI_module.SPI1; /*SPI bus*/
            //Cpu.Pin _cs = EMX.Pin.IO2; /*ChipSelect*/
            //Cpu.Pin _ExtInt = EMX.Pin.IO26; /*External Interrupt*/
            //Cpu.Pin _reset = EMX.Pin.IO3; /*Reset*/
            /*-------------------------------------------------------------------*/
            //FEZ Cobra OEM board V1.2 or V1.3 UEXT header with WiFi RS21 Module: P/N:GHI-WIFIEXP2-298
            //SPI.SPI_module _spi = SPI.SPI_module.SPI2; /*SPI bus*/
            //Cpu.Pin _cs = EMX.Pin.IO2; /*ChipSelect*/
            //Cpu.Pin _ExtInt = EMX.Pin.IO26; /*External Interrupt*/
            //Cpu.Pin _reset = EMX.Pin.IO3; /*Reset*/
            /*-------------------------------------------------------------------*/

            Debug.Print("Enable WiFi RS2 module");

            try
            {
                //ChipworkX Developement System UEXT header with WiFi RS21 Module: P/N:GHI-WIFIEXP2-298
                WiFi.Enable(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible,
                            _spi,/*SPI bus*/
                            _cs,  /*ChipSelect*/
                            _ExtInt, /*External Interrupt*/
                            _reset); /*Reset*/
            }
            catch (WiFi.WiFiException e)
            {
                if (e.errorCode == WiFi.WiFiException.ErrorCode.HardwareCommunicationFailure ||
                    e.errorCode == WiFi.WiFiException.ErrorCode.HardwareFirmwareVersionMismatch)
                {
                    //ChipworkX Developement System UEXT header with WiFi RS21 Module: P/N:GHI-WIFIEXP2-298
                    WiFi.UpdateFirmware(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible,
                                        _spi,/*SPI bus*/
                                        _cs,  /*ChipSelect*/
                                        _ExtInt, /*External Interrupt*/
                                        _reset); /*Reset*/
                    WiFi.Enable(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible,
                                 _spi,/*SPI bus*/
                                 _cs,  /*ChipSelect*/
                                 _ExtInt, /*External Interrupt*/
                                 _reset); /*Reset*/
                }
                else if (e.errorCode == WiFi.WiFiException.ErrorCode.HardwareCommunicationFailure)
                {
                    Debug.Print("Error Message: " + e.ErrorMsg);
                    Debug.Print("Check WiFi module hardware connections and SPI/signals configurations.");
                    throw;
                }
            }

            if (WiFi.IsEnabled)
                Debug.Print("Enabled Successfully. At this point, the on-board LED on RS9110_N_11_21_1_Compatible module is ON.");
            else
                throw new Exception();

            // This event handler checks netowrk avaialblty (Ethernet, WiFi and PPP).
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

            Debug.Print("Searching for WiFi APs");
            WiFiNetworkInfo[] ScanResp = WiFi.Scan();
            if (ScanResp != null)
            {
                Debug.Print("Total Available Networks are " + ScanResp.Length.ToString());
                foreach (WiFiNetworkInfo x in ScanResp)
                {
                    Debug.Print(WiFiNetworkInfoToString(x));
                    Debug.Print("-----------------------------------------------------");
                }
            }

            // Check if myWiFiNetworkSSID one of the found networks.
            int i = 0;
            for (i = 0; i < ScanResp.Length; i++)
            {
                if (string.Compare(ScanResp[i].SSID, myWiFiNetworkSSID) == 0)
                {
                    break;
                }
            }
            if (i >= ScanResp.Length)
            {
                Debug.Print("The netwrok you are looking for is not there");
                throw new Exception();
            }
            else
            {
                for(int count = 0; count <3; count++)
                {

                    Debug.Print("Connecting to " + ScanResp[i].SSID);
                    wait.Reset();
                    try
                    {
                        WiFi.Join(ScanResp[i], PassPhrase);
                    }
                    catch (WiFi.WiFiException e)
                    {
                        switch (e.errorCode)
                        {
                            case WiFi.WiFiException.ErrorCode.AuthenticationFailed:
                                Debug.Print("AuthenticationFailed");
                                break;
                            default:
                                Debug.Print(e.errorCode.ToString());
                                break;
                        }
                        Debug.Print("Error Message: " + e.ErrorMsg);
                        throw;
                    }
                    Debug.Print("Done connecting");

                    wait.WaitOne();

                    Debug.Print("We got NetworkAvailable event .WiFi link is ready!");

                    Debug.Print("Enable DHCP");
                    try
                    {
                        NetworkInterface[] netif = NetworkInterface.GetAllNetworkInterfaces();

                        // Static IP
                        /*****************************/
                        //netif[0].EnableStaticIP("192.168.137.2", "255.255.255.0", "192.168.137.1");
                        //netif[0].EnableStaticDns(new string[] { "10.1.10.1" });

                        // Dynamic IP
                        /*****************************/
                        if (!netif[0].IsDhcpEnabled)
                            netif[0].EnableDhcp();// This function is blocking
                        else
                        {
                            netif[0].RenewDhcpLease();// This function is blocking
                        }
                        Debug.Print("Network settings:");
                        Debug.Print("IP Address: " + netif[0].IPAddress);
                        Debug.Print("Subnet Mask: " + netif[0].SubnetMask);
                        Debug.Print("Default Getway: " + netif[0].GatewayAddress);
                        Debug.Print("DNS Server: " + netif[0].DnsAddresses[0]);
                    }
                    catch (SocketException e)
                    {
                        Debug.Print("DHCP Faild");
                        if (e.ErrorCode == 11003)
                            Debug.Print("Re-Enable the module.");

                        throw;
                    }
                    Debug.Print("Test DNS");
                    try
                    {
                        IPHostEntry myIP = Dns.GetHostEntry("www.ghielectronics.com");

                        if (myIP != null)
                        {
                            Debug.Print(myIP.HostName + ": " + myIP.AddressList[0].ToString());
                        }
                    }
                    catch (SocketException e)
                    {
                        Debug.Print("Faild to Get the host entry of the FQN from DNS server!");
                        if (e.ErrorCode == 11003)
                            Debug.Print("Re-Enable the module.");
                        throw;
                    }

                    Debug.Print("Disconnect WiFi link.");
                    wait.Reset();
                    WiFi.Disconnect();
                    wait.WaitOne();
                    Thread.Sleep(1000);
                }
                Debug.Print("Disable WiFi interface");
                WiFi.Disable();
                // At this point networkintrface is back to Ethernet.
                Debug.Print("The End of Test");
                Thread.Sleep(Timeout.Infinite);
            }

        }
        /// <summary>
        /// WiFiNetworkInfo to String
        /// </summary>
        /// <param name="info">WiFiNetworkInfo</param>
        /// <returns>string</returns>
        static string WiFiNetworkInfoToString(WiFiNetworkInfo info)
        {
            string str;
            str = "SSID: " + info.SSID +"\n";
            str += "Channel Number: " + info.ChannelNumber + "\n";
            str += "RSSI: -" + info.RSSI + "dB" + "\n";
            str += "Security Mode: ";
            switch (info.SecMode)
            {
                case SecurityMode.Open:
                    str += "Open";
                    break;
                case SecurityMode.WEP:
                    str += "WEP";
                    break;
                case SecurityMode.WPA:
                    str += "WPA";
                    break;
                case SecurityMode.WPA2:
                    str += "WPA2";
                    break;
            }
            str += "\n";
            str += "Network Type: ";
            switch (info.networkType)
            {
                case NetworkType.AccessPoint:
                    str += "Access Point";
                    break;
                case NetworkType.AdHoc:
                    str += "AdHoc";
                    break;
            }
            str += "\n";
            str += "BS MAC: "   + Auxiliary.ByteToHex(info.PhysicalAddress[0]) + "-"
                                + Auxiliary.ByteToHex(info.PhysicalAddress[1]) + "-"
                                + Auxiliary.ByteToHex(info.PhysicalAddress[2]) + "-"
                                + Auxiliary.ByteToHex(info.PhysicalAddress[3]) + "-"
                                + Auxiliary.ByteToHex(info.PhysicalAddress[4]) + "-"
                                + Auxiliary.ByteToHex(info.PhysicalAddress[5]) + "\n";
            return str;
        }



        static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            Debug.Print("Network Availability Event Triggered");
            if (e.IsAvailable)
            {
                if (WiFi.IsEnabled)// Make sure that the event is fired by WiFi interface, not other networking interface.
                if (WiFi.IsLinkConnected)
                {
                    wait.Set();
                    Debug.Print("WiFi connection was established!");
                }
            }
            else
            {
                if (WiFi.IsEnabled)// Make sure that the event is fired by WiFi interface, not other networking interface.
                if (!WiFi.IsLinkConnected) 
                {
                    wait.Set();
                    Debug.Print("WiFi connection was dropped or disconnected!");
                }
            }
        }
    }
    /// <summary>
    /// Auxiliary class.
    /// </summary>
    public static class Auxiliary
    {
        /// <summary>
        /// Convert Byte to HEX string.
        /// </summary>
        /// <param name="number">number</param>
        /// <returns>HEX in a string</returns>
        public static string ByteToHex(byte number)
        {
            string hex = "0123456789ABCDEF";
            return new string(new char[] { hex[(number & 0xF0) >> 4], hex[number & 0x0F] });
        }
    }

The problem arises at the first try-catch block. As I said, the led turns on, but WiFi.IsEsnabled throws an exception. What could it be?

I understand that it raise an exception at WiFiu.Enable() then it should got into this code. Right? What happens after that?

catch (WiFi.WiFiException e)
            {
                if (e.errorCode == WiFi.WiFiException.ErrorCode.HardwareCommunicationFailure ||
                    e.errorCode == WiFi.WiFiException.ErrorCode.HardwareFirmwareVersionMismatch)
                {
                    //ChipworkX Developement System UEXT header with WiFi RS21 Module: P/N:GHI-WIFIEXP2-298
                    WiFi.UpdateFirmware(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible,
                                        _spi,/*SPI bus*/
                                        _cs,  /*ChipSelect*/
                                        _ExtInt, /*External Interrupt*/
                                        _reset); /*Reset*/
                    WiFi.Enable(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible,
                                 _spi,/*SPI bus*/
                                 _cs,  /*ChipSelect*/
                                 _ExtInt, /*External Interrupt*/
                                 _reset); /*Reset*/
                }
                else if (e.errorCode == WiFi.WiFiException.ErrorCode.HardwareCommunicationFailure)
                {
                    Debug.Print("Error Message: " + e.ErrorMsg);
                    Debug.Print("Check WiFi module hardware connections and SPI/signals configurations.");
                    throw;
                }
            }

Using the debugger I get the first exception in the try part

        try
        {
            //ChipworkX Developement System UEXT header with WiFi RS21 Module: P/N:GHI-WIFIEXP2-298
            WiFi.Enable(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible,
                        _spi,/*SPI bus*/
                        _cs,  /*ChipSelect*/
                        _ExtInt, /*External Interrupt*/
                        _reset); /*Reset*/
        }

and I get this message:

#### Exception GHIElectronics.NETMF.Net.WiFi+WiFiException - 0x00000000 (1) ####
#### Message: 
#### GHIElectronics.NETMF.Net.WiFi::Enable [IP: 0082] ####
#### Program::Main [IP: 001c] ####

A first chance exception of type ‘GHIElectronics.NETMF.Net.WiFi.WiFiException’ occurred in GHIElectronics.NETMF.Net.dll

Then the program jumps to the catch part but the “if” and “else if” conditions are not verified, so it jumps to:

if (WiFi.IsEnabled)
            Debug.Print("Enabled Successfully. At this point, the on-board LED on RS9110_N_11_21_1_Compatible module is ON.");
        else
            throw new Exception();

and it throws this unhandled exception, but the green led on the RS21 module is on.

The UEXT cable is ok, I verified it with a multimeter.

Since the if and else if did not match the error code of the exception, then the error
could be one of the other 9 error codes.

In the catch code print out the error code. it might be a valuable clue.

After trying what you suggested I got this informations:

e.ToString: GHIElectronics.NETMF.Net.WiFi+WiFiException
e.ErrorMsg:
e.errorCode: 8
e.GetType: GHIElectronics.NETMF.Net.WiFi+WiFiException

Looking at the definitions of errorCode its seems to be this kind of error:

            // Summary:
            //     It means that the system could communicate with the WiFi module but it did
            //     not get the expected initialization response. This happens when the WiFi
            //     module firmware is corrupted. The firmware can be updated using WiFi FirmwareUpdate()
            //     method.
            HardwareCommunicationTimeout = 8,

How does the firmware update procedure work? Thanks

Not sure what you want? Are you asking for a technical explanation on how the update works, or how to force the update to occur?

To see if updating will work, add the HardwareCommunicationTimeout to the two error codes that cause an update in the exception handler. Worth a try.

You were right! I wanted to know how to force the update. I added this condition and it did the firmware upgrade and now it finds all the wifi networks in the area. I suppose that if you want to use a different unsupported module, you need to write all the code to perform the update, right?

Thanks for the help!

catch (WiFi.WiFiException e)
            {
                if (e.errorCode == WiFi.WiFiException.ErrorCode.HardwareCommunicationFailure ||
                    e.errorCode == WiFi.WiFiException.ErrorCode.HardwareFirmwareVersionMismatch ||
                    e.errorCode == WiFi.WiFiException.ErrorCode.HardwareCommunicationTimeout)   // The new condition
                {
                    //ChipworkX Developement System UEXT header with WiFi RS21 Module: P/N:GHI-WIFIEXP2-298
                    WiFi.UpdateFirmware(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible,
                                        _spi,/*SPI bus*/
                                        _cs,  /*ChipSelect*/
                                        _ExtInt, /*External Interrupt*/
                                        _reset); /*Reset*/
                    WiFi.Enable(WiFi.HardwareModule.RS9110_N_11_21_1_Compatible,
                                 _spi,/*SPI bus*/
                                 _cs,  /*ChipSelect*/
                                 _ExtInt, /*External Interrupt*/
                                 _reset); /*Reset*/
                }

Assuming that the unsupported module has an update function, you would have to write the code and a lot more code. :slight_smile: