Potential IPAddress Bug in Wifi

I seem to have a working wifi with the latest firmware version and such. NOTE I’m running a v1.2 wifi module plugged into socket 9 (I haven’t tested if socket 9 makes a difference yet, I’m just including this for completeness)


        void ProgramStarted()
        {

some stuff none of it network related.

            wifiConnect();

some more stuff none of it network related.

        }

        void wifi_RS21_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network down");
        }

        void wifi_RS21_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network up");
        }

        void Interface_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("Network Address Change");
        }

        void Interface_WirelessConnectivityChanged(object sender, WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            Debug.Print(e.NetworkInformation.RSSI.ToString());

            if (e.IsConnected)
            {
                NTPTime("time.windows.com", -420);
                client = new ICantTellYouWhatHappensHere(); //but it hooks up to the internet OK

                Debug.Print(DateTime.Now.ToString());
            }
        }

        private void wifiConnect()
        {
            if (!wifi_RS21.Interface.IsOpen)
                wifi_RS21.Interface.Open();

            if (!wifi_RS21.Interface.NetworkInterface.IsDhcpEnabled)
                wifi_RS21.Interface.NetworkInterface.EnableDhcp();

            NetworkInterfaceExtension.AssignNetworkingStackTo(wifi_RS21.Interface);

            wifi_RS21.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_RS21_NetworkUp);
            wifi_RS21.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_RS21_NetworkDown);
            wifi_RS21.Interface.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Interface_NetworkAddressChanged);
            wifi_RS21.Interface.WirelessConnectivityChanged += new WiFiRS9110.WirelessConnectivityChangedEventHandler(Interface_WirelessConnectivityChanged);

            try
            {

                string myAP = "NSA Malware Lab 1";
                Debug.Print("Scan for wireless networks");
                WiFiNetworkInfo[] scanResult = wifi_RS21.Interface.Scan(myAP);

                if (scanResult != null && scanResult.Length > 0)
                {
                    Debug.Print("Connecting to " + myAP);
                    wifi_RS21.Interface.Join(scanResult[0], "YOURPASSWORD");
                    Debug.Print("Connected");
                }
                else
                {
                    Debug.Print(myAP + " Wireless network was not found");
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
            }
        }


        public static bool NTPTime(string TimeServer, int GmtOffset = 0)
        {
            Socket s = null;
            try
            {
                EndPoint rep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);
                s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                byte[] ntpData = new byte[48];
                Array.Clear(ntpData, 0, 48);
                ntpData[0] = 0x1B; // Set protocol version
                s.SendTo(ntpData, rep); // Send Request   
                if (s.Poll(30 * 1000 * 1000, SelectMode.SelectRead)) // Waiting an answer for 30s, if nothing: timeout
                {
                    s.ReceiveFrom(ntpData, ref rep); // Receive Time
                    byte offsetTransmitTime = 40;
                    ulong intpart = 0;
                    ulong fractpart = 0;
                    for (int i = 0; i <= 3; i++) intpart = (intpart << 8) | ntpData[offsetTransmitTime + i];
                    for (int i = 4; i <= 7; i++) fractpart = (fractpart << 8) | ntpData[offsetTransmitTime + i];
                    ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
                    s.Close();
                    DateTime dateTime = new DateTime(1900, 1, 1) +
                                        TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
                    Utility.SetLocalTime(dateTime.AddMinutes(GmtOffset));
                    return true;
                }
                s.Close();
            }
            catch
            {
                try
                {
                    s.Close();
                }
                catch
                {
                }
            }
            return false;
        }

Gus I know where your going with this and I’m rather excited about it as its going to be incredible when its done, but for now it takes a bit of stick handling to get things to work, but I got mine to work.

@ Duke: Did you update the WiFi firmware manually when you said your running version v1.2? IF so, how did you do it? Also, were you able to pull an ipaddress once you connected using DHCP?

It did check my wifi firmware but it didn’t need to be updated: RS9110 driver version Number is 4.4.5 (it was updated in the past, but that might have been during the 4.1 days)

What version of the wifi module are you using?

DHCP is how I connect to this wifi so its working here.

This what I see in the output window when I start this app


deploy and loading stuff here
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Users\Blake\Documents\Visual Studio 2010\Projects\Gadgeteer Presentation\MyAppliance\MyAppliance\bin\Debug\le\MyAppliance.exe', Symbols loaded.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZSpider version 1.0
Program Started
RS9110 firmware version Number is 4.4.5
RS9110 driver version Number is 4.4.5
Scan for wireless networks
Connecting to NSA Malware Lab 1
56
Connected
Network down
The thread '<No Name>' (0x3) has exited with code 0 (0x0).
11/09/2012 06:47:01
Network Address Change
etc etc etc

This is what I see:

So i have the same firmware version as you. The reason I asked about your IP address is I can’t retrieve the IPaddress using DHCP although everything works (NTPworks fine). The IP address always defaults to all 0’s.

I also have the same wifi firmware and driver versions.

@ Duke Nukem: What did you mean by version v1.2? I’m asking since firmware version is v4.4.5

My module hardware version is 1.2 (its written on the module)

Mine also has 4.2

Duke Nukem

Just a thanks for posting your WiFi code with the Time Server.

Been fighting the WiFi on version 4.2 and not being very successful. (Until I tried your code)…
My GHI Module PCB is marked Version 1.2

Anyway… BIG THANKS!

@ gus What’s the ETA for the wifi issues to be resolved? Is there any update?

Dear murej
Do you solve your problem?

Dear Duke Nukem;
When I test your code, I got some errors

The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZSpider version 1.0
Program Started
RS9110 firmware version Number is 4.4.5
RS9110 driver version Number is 4.4.5
Scan for wireless networks
Connecting to ZyXEL9FA
#### Exception GHI.Premium.Net.NetworkInterfaceExtensionException - 0x00000000 (1) ####
#### Message:
#### GHI.Premium.Net.WiFiRS9110::Join [IP: 0027] ####
#### GadgeteerApp1.Program::wifiConnect [IP: 00b1] ####
#### GadgeteerApp1.Program::ProgramStarted [IP: 000c] ####
A first chance exception of type ‘GHI.Premium.Net.NetworkInterfaceExtensionException’ occurred in GHI.Premium.Net.dll
Exception was thrown: GHI.Premium.Net.NetworkInterfaceExtensionException
The thread ‘’ (0x3) has exited with code 0 (0x0).

@ Tzu Hsuan: No, I didn’t. I’m still waiting for a reply from Gus, but it seems like nothing is moving forward. I really hope this is resolved soon or at least someone comes up with a bypass. I’m lost for now :confused:

I also have the problem with EMX/Cobra, BuiltinEthernet, FW 4.2.7.0 and DHCP.
Sometimes I get an IP address, sometimes you wait ages and nothing happens.
As soon as I use a fixed IP address, I have no problem anymore.

Dukes code fixed my problem. The point of difference was:
NetworkInterfaceExtension.AssignNetworkingStackTo(wifi_RS21.Interface);

Glad my code (that I no doubt got most of it from folks here) helped. Given some of the changes which have occurred lately I really should redo it to include some of the built in Time features etc.

Hi, Duke Nukem,

Again a big props cause is the only version of code with the actual WiFi NETMF.
For me, I was doing the same thing then you on my side, and I reach the same point of your code. But I got a problem, I dont know if you had it too, but all my connection is open I had an IP address etc., but when I do WebClient.GetFromWeb("…") … it always trigger me the response receive event, but with no response text and a status 302 instead of 200… so I test your code to see if was me the problem or a “I dont know what problem”… and with your code I had the same result … I really dont know where to search now … cause in your output debug that you put in exemple you had a response.Text …

So if you can help me in any manner you see, I will appreciate it!
Cheers!

302=redirection. HTTP 302 - Wikipedia
Try a different URL.

Two other things. Welcome to the forum. Please, in future, don’t double post. People will see your posts and reply if they can help if you just do it once.