Cobra II Wifi DHCP issues

I take my previously correction back. the problem must be the Cisco and Asus routers! :slight_smile:

1 Like

Hi guys,
I have noticed similar issue with my setup as mike on a spider.

I have an ADSL as the dhcp modem, and I have dlink dir and dlink extreme gaming edition Iā€™ll get the model numbers later, which are set up as switches or pass through meaning the RJ45 that brings the internet connection is plugged into one of the 4 ports and not the internet port, the same applies to the cable taking the connection from that router to the nextā€¦which is done to widen the WiFi coverageā€¦ all using the same broadcast idā€¦

Static works dhcp throws a networking errorā€¦

@ James - Have you been able to reproduce the issue?

OK so my Cobra II arrived, and Iā€™ve relented and installed the 4.2 SDK that Iā€™d avoided for so long :slight_smile:

Fired up the DHCP example.

I changed the wifi.Scan() to scan only for my SSID.

I am using a Billion 7404VNPX. Technically, I am using WPA2 PSK with AES, if that makes a difference. My PSK ā€œlooksā€ like a WEP string though, itā€™s 0-9, a-f, and itā€™s 16 characters long. My router IS case sensitive for the key.

Stepping through code, I get the scan results back with my SSID showing Channel 1, Network type 1, RSSI 76, Sec Mode 2 (which is WPA2).

Wifi.Join() runs. When the executing GetDefaultLocalAddress, I get:

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
RS9110 firmware version Number is 4.4.5
RS9110 driver version Number is 4.4.5
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
    #### System.Net.Dns::GetHostEntry [IP: 0008] ####
    #### System.Net.IPAddress::GetDefaultLocalAddress [IP: 0049] ####
    #### Cobra_II_DHCP_Test.Program::Main [IP: 00ac] ####
    #### SocketException ErrorCode = -1
    #### SocketException ErrorCode = -1
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = -1
    #### SocketException ErrorCode = -1

@ Brett - you get same exception if you do not step in code, just run

I spent a few more minutes playing with the Cobra II. I made sure the code and references matched Stevenā€™s earlier code. Still no luck.

I noticed somethingā€¦ I was using the ā€œSweetest New APPā€ and found that there were three network configurations on the board, with two of them having the default MAC address. Is it normal to have three configurations?.

Well this is getting interestingā€¦

I decided to try something different.

I reboot my iMac into BootCamp/Windows 7. I checked the version of the SDK that was loaded
and found it was out of date. I uninstalled all the MF stuff and then installed the lastest 4.2 SDK.

I then reloaded the Firmware on the Cobra II.

I then took the code that Steven had posted earlier and loaded it to the Cobra II unchanged. It worked with DHCP!

I then rebooted back to OS X and started a Windows 7 VM.

I then tried to run the test program which I have been using, which is almost identical to Stevenā€™s, and
it still had the DHCP problem.

I have to think about this a while, and get some real work done. There must be a clue hereā€¦

Maybe IP Address conflict ???

More likely a MAC address conflict. Unlikely to have IP address confict with DHCP.

Gus, yep still happens when I donā€™t use breakpoints and step into code.

hereā€™s some more robust code I had used (because there were a few times a scan returned nothing, depending on board orientation/location:

            WiFiNetworkInfo[] scanResults = wifi.Scan("myAPName");  //my AP name is 8 chars long

            if (scanResults == null)
            {
                Debug.Print("Error: no Wifi AP found");
            }
            else
            {
                int targetAP = 0;
                string passPhrase = "9xxxxxxxxx"; //my characters are 0-9, a-f lower case and 16 long

                try
                {
                    wifi.Join(scanResults[targetAP], passPhrase);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                }

                IPAddress ip = null;
                while (true)
                {
                    ctr++;
                    ip = IPAddress.GetDefaultLocalAddress();
                    if (ip != IPAddress.Any)
                        break;
                    System.Threading.Thread.Sleep(10);

                }
                Debug.Print("Loop iterated " + ctr.ToString() +" times.");
                Debug.Print("IP address reports: "+ ip.ToString());
            }

It generated:

RS9110 firmware version Number is 4.4.5
RS9110 driver version Number is 4.4.5
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
    #### System.Net.Dns::GetHostEntry [IP: 0008] ####
    #### System.Net.IPAddress::GetDefaultLocalAddress [IP: 0049] ####
    #### Cobra_II_DHCP_Test.Program::Main [IP: 00b5] ####
    #### SocketException ErrorCode = -1
    #### SocketException ErrorCode = -1

(and that exception goes on and on and on)

I had my debug.print line in both the WirelessConnectivityChanged and NetworkAddressChanged handlers too, and got:

Loop iterated 6 times.
WiFi is: connected

So it does get to that point, but address never changed.

I can see the MAC address in the ARP table on the router but it never gets a DHCP address.

I only have the router in my network, no bridges, no cable boxes, no wireless repeaters. Just a hub and a switch, both passive, hooked to the router.

No, I was out of the office for the past 2 days due to a family emergency. We will be resuming tests shortly.

Progress of sortsā€¦

For some reason, I have gotten to my Cobra II to work with DHCP. I think it might have to do with reloading the firmware.

**** Just remembered that I had recently reboot my AP. Donā€™t know how that could have made a difference since it is not the DHCP server.

I then changed the program that James had posted earlier to start a connection to a remote server.
I am now having a problem with the instaniation of a socket. The program just hangs on the new.

Following is the code that allocates a DHCP address and then hangs on creation of a socket.


using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;
using System.Net.Sockets;
 
using GHI.Premium.Net;
using GHI.Premium.Hardware;
using System.Net;
 
namespace CobraIIWiFiTest
{
    public class Program
    {
        public static WiFiRS9110 wifi;
 
        public static void Main()
        {
            wifi = new WiFiRS9110(SPI.SPI_module.SPI2, G120.Pin.P1_10, G120.Pin.P2_11, G120.Pin.P1_9);
 
            if (!wifi.IsOpen)
                wifi.Open();
 
            if (!wifi.NetworkInterface.IsDhcpEnabled)
                wifi.NetworkInterface.EnableDhcp();
 
            wifi.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(wifi_NetworkAddressChanged);
            wifi.WirelessConnectivityChanged += new WiFiRS9110.WirelessConnectivityChangedEventHandler(wifi_WirelessConnectivityChanged);
 
            //GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(wifi);
 
            WiFiNetworkInfo[] scanResults = wifi.Scan();
 
            int targetAP = 0;
            string passPhrase = "xxxxxx";
            for (targetAP = 0; targetAP < scanResults.Length; targetAP++)
            {
                if (scanResults[targetAP].SSID == "ChloeNet3")
                    break;
            }
 
            wifi.Join(scanResults[targetAP], passPhrase);
 
            IPAddress ip = null;
 
            while (true)
            {
                ip = IPAddress.GetDefaultLocalAddress();
                if (ip != IPAddress.Any)
                    break;
            }
 
            Debug.Print(ip.ToString());

            // hange at the next statement
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.202"), 23);
            socket.Connect(ep);
 
            System.Threading.Thread.Sleep(-1);
        }
 
        static void wifi_WirelessConnectivityChanged(object sender, WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            Debug.Print("WiFi is: " + (e.IsConnected ? "connected" : "disconnected"));
        }
 
        static void wifi_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("Is DhCp enabled: " + wifi.NetworkInterface.IsDhcpEnabled);
            Debug.Print("Is DynamicDnsEnabled enabled: " + wifi.NetworkInterface.IsDynamicDnsEnabled);
            Debug.Print("NetworkInterfaceType " + wifi.NetworkInterface.NetworkInterfaceType);
            Debug.Print("Network settings:");
            Debug.Print("IP Address: " + wifi.NetworkInterface.IPAddress);
            Debug.Print("Subnet Mask: " + wifi.NetworkInterface.SubnetMask);
            Debug.Print("Default Gateway: " + wifi.NetworkInterface.GatewayAddress);
            Debug.Print("Number of DNS servers:" + wifi.NetworkInterface.DnsAddresses.Length);
            Debug.Print("------------------------------------------------------");
        }
    }
}

In continuing my efforts to get the WiFi going on a Cobra II, I went back to Windows 7/Bootcamp on my iMac to see if the problem I was having with instaniating a Socket would occur on a native machine. I added a line to create the Socket object, and the identical problem seems to be appearing.

The program makes the call and then the device appears to freeze. I am unable to break the debugger until I hit the reset button on the Cobra.

Has anyone, who has been able to get a DHCP address, also been able to create a socket?

There is a known problem that maybe related to what you are seeing. On G120, wifi connection is dropped right after it was connected, which hangs any network work.

I bet pinging the device wonā€™t work.

You loseā€¦ :slight_smile:

I ran two tests.

The first test just allocated a DHCP address. It worked, and I was able to ping the device.

In the second test, I obtained a DHCP address and then did a sleep for 20 seconds.
I then tried to new a Socket object. The program then hung. During the 20 seconds,
before creating the socket I was able to ping the device. After the new was done,
and the system hung, I was still able to ping the device. Not what we wanted to hearā€¦

This smells just like the DHCP problem Iā€™m having with the Cerberus. I can reconnect to the network twice but on the third time all goes to hell. Supposedly, thereā€™s a fix coming in the next SDK. Not sure if the problem is related to yours but it sounds like youā€™re having some similar issues.

http://www.tinyclr.com/forum/topic?id=10140&page=4

Web Serverā€¦i have created a webserver but it will not operate for more then a few hours or moreā€¦ I have used the code as belowā€¦anything special i need to do to keep it running

using System;
using Microsoft.SPOT;
using GHI.Premium.Hardware;
using GHINET = GHI.Premium.Net;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace zigbox.fezcobra2
{
    public class wifiredpine
    {
        public static GHINET.WiFiRS9110 wifi;
        int RenewDHCPAttempts = 0;
        int FindEWifiNetworkAttempts = 0;

        public delegate void log(string sInfo);
        public event log loginfo;

        public string senddata(string method, string uri, NetworkCredential credential, string requestText)
        {
            return Program.owebserver2.DoHttpMethod(method, uri, null, requestText);
        }

        static byte[] myMAC = new byte[] { 0x00, 0x30, 0xBD, 0xF7, 0xDF, 0x7C };
        public void load()
        {
            wifi = new GHINET.WiFiRS9110(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, GHI.Hardware.G120.Pin.P1_10, GHI.Hardware.G120.Pin.P2_11, GHI.Hardware.G120.Pin.P1_9, 4000);

            if (!wifi.IsOpen)
                wifi.Open();
            Thread.Sleep(1000);
            if (!wifi.NetworkInterface.IsDhcpEnabled)
                wifi.NetworkInterface.EnableDhcp();

            Thread.Sleep(100);
            wifi.NetworkInterface.EnableStaticIP("192.168.0.6", "255.255.255.0", "192.168.0.1");
            wifi.NetworkInterface.EnableStaticDns(new string[] { "192.168.0.1" });

            //wifi.NetworkAddressChanged += new GHINET.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(wifi_NetworkAddressChanged);
            //wifi.WirelessConnectivityChanged += new GHINET.WiFiRS9110.WirelessConnectivityChangedEventHandler(wifi_WirelessConnectivityChanged);

            Thread.Sleep(100);
            GHINET.NetworkInterfaceExtension.AssignNetworkingStackTo(wifi);
            Thread.Sleep(1000);
            Rejoin();
        }



        public void Shutdown()
        {
            Program.IsNetworkUp = false;

            if (wifi.IsLinkConnected) wifi.Disconnect();


            if (wifi.IsOpen) wifi.Close();

            wifi.Dispose();
        }


        public void Rejoin()
        {
            if (wifi.IsLinkConnected) wifi.Disconnect();
            try
            {
                GHINET.WiFiNetworkInfo[] scanResult = wifi.Scan();
                if (scanResult != null)
                {
                    for (int i = 0; i < scanResult.Length; i++)
                    {
                        Program.loginfo("Found SSID:[" + i + "]" + scanResult[i].SSID);
                        if (scanResult[i].SSID == "ap")
                        {
                            try
                            {
                                wifi.Join(scanResult[i], "041fgdg996");


                                //IPAddress ip = null;

                                //while (true)
                                //{
                                //    ip = IPAddress.GetDefaultLocalAddress();
                                //    if (ip != IPAddress.Any)
                                //        break;
                                //}
                                //Program.loginfo(ip.ToString());
                               // Thread.Sleep(1000);
                            }
                            catch (Exception)
                            {
                                Program.loginfo("Failed to join wifi ssid[" + scanResult[i].SSID + "]");
                            }
                            break;
                        }
                    }
                }
                else
                {
                    
                    Program.loginfo("Find wifi network Attempt count [" + FindEWifiNetworkAttempts + "]");
                    if (FindEWifiNetworkAttempts < 5)
                    {
                        FindEWifiNetworkAttempts += 1;
                        //Program.loginfo("Could not find any wifi networks,try again in 5seconds");
                        Thread.Sleep(5000 + (FindEWifiNetworkAttempts *2000));
                        Rejoin();
                    }
                    else
                    {
                        
                        Program.loginfo("Could not find any wifi network attempt[" + FindEWifiNetworkAttempts + "] Giving up! Make sure wifi is access point is ON");
                    }
                }
            }
            catch (Exception ex)
            {
                Program.loginfo("Rejoin::Exception::" + ex.Message);
            }
        }

        private void wifi_WirelessConnectivityChanged(object sender, GHINET.WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            DisplayNetwork(e.NetworkInformation, "wifi_WirelessConnectivityChanged");

            if (wifi.NetworkInterface.IPAddress == "0.0.0.0")
            {
                Program.loginfo("wifi_WirelessConnectivityChanged::Appears to be an invalid IP address RenewDhcpLease attempt[" + RenewDHCPAttempts + "]");
                RenewDHCPAttempts += 1;

                if (RenewDHCPAttempts == 5)
                {
                    Program.loginfo("RenewDHCPAttempts == 5..Rejoin");
                    Rejoin();
                }
                else
                {
                    Program.loginfo("RenewDHCPAttempts [" + RenewDHCPAttempts + "]");
                    if (RenewDHCPAttempts < 5)
                    {
                        Program.loginfo("RenewDHCPAttempts < 5...wifi.NetworkInterface.RenewDhcpLease()..");
                        try
                        {
                            wifi.NetworkInterface.ReleaseDhcpLease();
                            Thread.Sleep(1000);
                            wifi.NetworkInterface.RenewDhcpLease();
                            Program.loginfo("..wifi.NetworkInterface.RenewDhcpLease()");
                        }
                        catch (Exception ex)
                        {
                            Program.loginfo("..Timeout trying to renew  DHCP..Rejoin::exception::"  + ex.Message.ToString());
                            Rejoin();
                        }
                    }
                    else
                    {
                        Program.loginfo("wifi_NetworkAddressChanged::Cannot get valid IP address attempt[" + RenewDHCPAttempts + "] Giving up!");
                    }
                }
            }
            else
            {
                //Program.IsNetworkUp = true;
                Program.loginfo("wifi.NetworkInterface.IPAddress.ToString() == 0.0.0.0");
            }
        }

        void wifi_NetworkAddressChanged(object sender, EventArgs e)
        {
            Thread.Sleep(1000);
            Program.loginfo("----------------------wifi_NetworkAddressChanged--------------------------------");
            Program.loginfo("Wireless is: " + (wifi.IsLinkConnected ? "Connected" : "Disconnected"));
            Program.loginfo("IsActivated: " + wifi.IsActivated);
            Program.loginfo("IsLinkConnected: " + wifi.IsLinkConnected);
            Program.loginfo("IsOpen: " + wifi.IsOpen);
            Program.loginfo("Is DhCp enabled: " + wifi.NetworkInterface.IsDhcpEnabled);
            Program.loginfo("Is DynamicDnsEnabled enabled: " + wifi.NetworkInterface.IsDynamicDnsEnabled);
            Program.loginfo("NetworkInterfaceType " + wifi.NetworkInterface.NetworkInterfaceType);
            Program.loginfo("Network settings:");
            Program.loginfo("IP Address: " + wifi.NetworkInterface.IPAddress);
            Program.loginfo("Subnet Mask: " + wifi.NetworkInterface.SubnetMask);
            Program.loginfo("Default Gateway: " + wifi.NetworkInterface.GatewayAddress);
            Program.loginfo("Number of DNS servers:" + wifi.NetworkInterface.DnsAddresses.Length);
            for (int i = 0; i < wifi.NetworkInterface.DnsAddresses.Length; i++)
                         Program.loginfo("DNS Server " + i.ToString() + ":" + wifi.NetworkInterface.DnsAddresses[i]);
            Program.loginfo("------------------------------------------------------");
            if (wifi.NetworkInterface.IPAddress.ToString() == "0.0.0.0")
            {
                Program.loginfo("Appears to be an invalid IP address RenewDhcpLease attempt[" + RenewDHCPAttempts + "] ");

                RenewDHCPAttempts += 1;

                if (RenewDHCPAttempts == 5)
                {
                    Program.loginfo("RenewDHCPAttempts == 5..Rejoin");
                    Rejoin();
                }
                else
                {
                    Program.loginfo("RenewDHCPAttempts != 5");
                    if (RenewDHCPAttempts < 5)
                    {
                        Program.loginfo("RenewDHCPAttempts < 5...wifi.NetworkInterface.RenewDhcpLease()..");
                        try
                        {
                            wifi.NetworkInterface.ReleaseDhcpLease();
                            Thread.Sleep(1000);
                            wifi.NetworkInterface.RenewDhcpLease();
                            Thread.Sleep(1000);
                            Program.loginfo("..wifi.NetworkInterface.RenewDhcpLease()");
                        }
                        catch (Exception ex)
                        {
                            Program.loginfo("..Timeout trying to renew  DHCP..Rejoin");
                            Rejoin();
                        }
                    }
                    else
                    {
                        Program.loginfo("wifi_NetworkAddressChanged::Cannot get valid IP address attempt[" + RenewDHCPAttempts + "] Giving up!");
                        Program.IsNetworkUp = false; 
                    }
                }
            }
            else
            {
                Program.loginfo("Appears to be valid IP address RenewDhcpLease attempt[" + RenewDHCPAttempts + "] ");
                Program.IsNetworkUp = true; 
            }
        }

        #region Helpers
        static void DisplayNetworkType(GHINET.WiFiNetworkInfo info)
        {
            switch (info.networkType)
            {
                case GHINET.NetworkType.AccessPoint:
                    {
                        Program.loginfo("Wireless Network Type: Access Point");
                        break;
                    }
                case GHINET.NetworkType.AdHoc:
                    {
                        Program.loginfo("Wireless Network Type: Ad Hoc");
                        break;
                    }
            }
        }

        static void DisplayNetwork(GHINET.WiFiNetworkInfo info, string Message)
        {
            ////Program.loginfo("------------------------" + Message + "------------------------------");
            ////Program.loginfo("Wireless is: " + (wifi.IsLinkConnected ? "Connected" : "Disconnected"));
            ////Program.loginfo("SSID: " + info.SSID);
            ////Program.loginfo("WiFi Channel number: " + info.ChannelNumber);
            ////DisplayNetworkType(info);
            ////DisplayMACAddress(info);
            //////Program.loginfo(String.Format("RSSI: -{0}dB", info.RSSI));
            ////DisplaySecurityMode(info);
            ////Program.loginfo("IsActivated: " + wifi.IsActivated);
            ////Program.loginfo("IsLinkConnected: " + wifi.IsLinkConnected);
            ////Program.loginfo("IsOpen: " + wifi.IsOpen);
            ////Program.loginfo("Is DhCp enabled: " + wifi.NetworkInterface.IsDhcpEnabled);
            ////Program.loginfo("Is DynamicDnsEnabled enabled: " + wifi.NetworkInterface.IsDynamicDnsEnabled);
            ////Program.loginfo("NetworkInterfaceType " + wifi.NetworkInterface.NetworkInterfaceType);
            ////Program.loginfo("Network settings:");
            ////Program.loginfo("IP Address: " + wifi.NetworkInterface.IPAddress);
            ////Program.loginfo("Subnet Mask: " + wifi.NetworkInterface.SubnetMask);
            ////Program.loginfo("Default Gateway: " + wifi.NetworkInterface.GatewayAddress);
            ////Program.loginfo("Number of DNS servers:" + wifi.NetworkInterface.DnsAddresses.Length);
            ////for (int i = 0; i < wifi.NetworkInterface.DnsAddresses.Length; i++)
            ////    Program.loginfo("DNS Server " + i.ToString() + ":" + wifi.NetworkInterface.DnsAddresses[i]);
            ////Program.loginfo("------------------------------------------------------");
        }

        static void DisplayMACAddress(GHINET.WiFiNetworkInfo info)
        {
            string MAC = new string(null);
            int splitCount = 0;

            for (int i = 0; i < info.PhysicalAddress.Length; i++)
            {
                MAC += (info.PhysicalAddress[i].ToString("X"));

                if (splitCount < 5)
                {
                    MAC += ":";
                    splitCount++;
                }
            }

            Program.loginfo("MAC Address: " + MAC);
        }

        static void DisplaySecurityMode(GHINET.WiFiNetworkInfo info)
        {
            switch (info.SecMode)
            {
                case GHINET.SecurityMode.Open:
                    {
                        Program.loginfo("Wireless Security Mode: Open");
                        break;
                    }
                case GHINET.SecurityMode.WEP:
                    {
                        Program.loginfo("Wireless Security Mode: WEP");
                        break;
                    }
                case GHINET.SecurityMode.WPA:
                    {
                        Program.loginfo("Wireless Security Mode: WPA");
                        break;
                    }
                case GHINET.SecurityMode.WPA2:
                    {
                        Program.loginfo("Wireless Security Mode: WPA2");
                        break;
                    }
            }
        #endregion
        }
    }
}

@ zigbox, I donā€™t think your issue is likely to be related to G120 DHCP problems, time to remove that code and start a new thread I think :wink:

Why do you think its not a DHCP problemā€¦i am using the code outlined on GHI websiteā€¦have tried a couple of different access points and still not working. Can you refer me to a simple setup of webserver that works? I have tried many code variation and the only way i ever get it to work is when i use a static IP addressā€¦

Iā€™m sure this is not an isolated issueā€¦but i could be wrong :slight_smile:

Why? Well from your original problem statement: the code fails to work any longer than a few hours - that means it works for a while and then fails. The DHCP issue outlined here explicitly was causing a device to never get an IP address - therefore your web server would never start.

But now youā€™re talking more like this might have a DHCP aspect - but we donā€™t have any evidence or a clear problem statement. If your problem is with DHCP then you donā€™t need the extra layer of complexity in your web server. Cut down a simple program that shows you donā€™t get an IP address. Itā€™s a fact of life that people are unlikely to spend time reviewing a web server code example but when you can show your network stack not getting a DHCP address correctly, in something simple to understand and highlight the problem, then not only will the code be more likely to be looked at closer but youā€™ll show that youā€™ve eliminated the unnecessary and therefore are pointing directly back to the real cause of your issue.

And if it REALLY is a web server problem, then it deserves itā€™s own thread, not piling on a thread just about DHCP problems on WiFi on a Cobra2.