Main Site Documentation

Internet access with GHI WiFi Module not working


#1

Hello guys!

I’m trying to access a URL using HttpWebRequest class but, somehow the device seems not connecting to internet.

This is the code to setup WiFi(tried DHCP and even Static IP):


private void SetupWifi()
        {
            wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp);
            var network = wifi.Search("automax");
            //var network = wifi.Search("GUTO_WP7_WORK");
            //wifi.UseDHCP();
            //wifi.UseStaticIP("192.168.0.51", "255.255.255.0", "192.168.0.1", new string[] { "192.168.0.1" });
            
            wifi.Join(network, "123@ @ rfine");
            //wifi.Join(network, "120880guto");
        }

This is the code I’m trying to get the HttpResponse:


public void BeginRestRequest(WebMethod method, string action, object requestBody) 
        {
            var request = WebRequest.Create(_baseUrl + action) as HttpWebRequest;
            HttpWebResponse response;
            request.Method = method.ToString();
            

            try
            {
                switch (method)
                {
                    case WebMethod.GET:
                        {
                            response = request.GetResponse() as HttpWebResponse;
                            var reader = new StreamReader(response.GetResponseStream());
                            var jsonString = reader.ReadToEnd();
                            GETRequestDone(new RestEventArgs() { Result = JsonParser.JsonDecode(reader.ReadToEnd()) });
                        }
                        break;
                    case WebMethod.POST:
                        {
                            var jsonSerializer = new Serializer();
                            if (requestBody == null)
                                throw new ArgumentNullException("POST request can't have an empty body...");
                            var buffer = Encoding.UTF8.GetBytes(jsonSerializer.Serialize(requestBody));
                            request.ContentType = "application/json";
                            request.ContentLength = buffer.Length;
                            Stream postData = request.GetRequestStream();
                            postData.Write(buffer, 0, buffer.Length);
                            postData.Close();
                            response = request.GetResponse() as HttpWebResponse;
                            var reader = new StreamReader(response.GetResponseStream());
                            var jsonString = reader.ReadToEnd();                            
                            POSTRequestDone(new RestEventArgs() { Result = jsonSerializer.Deserialize(reader.ReadToEnd()) });
                        }
                        break;
                    case WebMethod.PUT:
                        {
                            var jsonSerializer = new Serializer();
                            var buffer = Encoding.UTF8.GetBytes(jsonSerializer.Serialize(requestBody));
                            request.ContentType = "application/json";
                            request.ContentLength = buffer.Length;
                            Stream postData = request.GetRequestStream();
                            postData.Write(buffer, 0, buffer.Length);
                            postData.Close();
                            response = request.GetResponse() as HttpWebResponse;
                            var reader = new StreamReader(response.GetResponseStream());
                            var jsonString = reader.ReadToEnd();
                            PUTRequestDone(new RestEventArgs() { Result = jsonSerializer.Deserialize(reader.ReadToEnd()) });
                        }
                        break;
                    case WebMethod.DELETE:
                        {
                            var jsonSerializer = new Serializer();
                            var buffer = Encoding.UTF8.GetBytes(jsonSerializer.Serialize(requestBody));
                            request.ContentType = "application/json";
                            request.ContentLength = buffer.Length;
                            Stream postData = request.GetRequestStream();
                            postData.Write(buffer, 0, buffer.Length);
                            postData.Close();
                            response = request.GetResponse() as HttpWebResponse;
                            var reader = new StreamReader(response.GetResponseStream());
                            var jsonString = reader.ReadToEnd();
                            DELETERequestDone(new RestEventArgs() { Result = jsonSerializer.Deserialize(reader.ReadToEnd()) });
                        }
                        break;
                    default:
                        break;
                }
                
            }
            catch (Exception ex)
            {

                throw;
            }
            
        }

And this the exception that is throw when I try to read the response(access internet resource):


Program Started
    #### 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.HttpWebRequest::EstablishConnection [IP: 00e1] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### RFine.NETMF.Engine.Networking.RestClient::BeginRestRequest [IP: 0034] ####
    #### GPSTest.Program::ProgramStarted [IP: 0027] ####
    #### GPSTest.Program::Main [IP: 0015] ####
    #### 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
    #### Exception System.Net.WebException - 0x00000000 (1) ####
    #### Message: host not available
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 00f1] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### RFine.NETMF.Engine.Networking.RestClient::BeginRestRequest [IP: 0034] ####
    #### GPSTest.Program::ProgramStarted [IP: 0027] ####
    #### GPSTest.Program::Main [IP: 0015] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
    #### Exception System.Net.WebException - 0x00000000 (1) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
    #### RFine.NETMF.Engine.Networking.RestClient::BeginRestRequest [IP: 0034] ####
    #### GPSTest.Program::ProgramStarted [IP: 0027] ####
    #### GPSTest.Program::Main [IP: 0015] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll


Even if I try a simple Dns.GetHostEntry(“www.ghieletronics.com”) will throw this exception.

What I’m doing wrong? The URL I’m trying to access is available and working on my Silverlight Application, Windows Phone 7 Application, Windows 8 Metro App. So, this is not a server related problem.

Thanks!

Best regards…


#2

Check your DNS IP


#3

I tried Gus. It is in DHCP mode, so you should get it right. Even if I fix my DNS or even put 8.8.8.8 for google’s DNS, I grab same error.

THanks!


#4

The follow sample program is included with the Gadgeteer source. Try it and see if it works for you.

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

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

namespace WiFi_RS21Tester
{
    public partial class Program
    {
        void ProgramStarted()
        {
            
            wifi.UseDHCP();
            //wifi.UseStaticIP("192.168.1.225", "255.255.255.0", "192.168.1.1", new string[] { "10.1.10.1" });
            string myAP = "linksys1";
            wifi.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkDown);
            wifi.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp);
            
            Debug.Print("Scan for wireless networks");
            WiFi_RS21.WiFiNetworkInfo[] scanResult = wifi.Scan();
            if (scanResult != null)
            {
                foreach (WiFi_RS21.WiFiNetworkInfo x in scanResult)
                {
                    Debug.Print(x.ToString());
                    Debug.Print("------------------------------------");
                }
            }
            else
            {
                Debug.Print("No wireless netowrks were found.");
            }

            Debug.Print("Searching for: " + myAP);
            WiFi_RS21.WiFiNetworkInfo myBSS = wifi.Search(myAP);

            if (myBSS != null)
            {

                Debug.Print("Connecting to " + myAP);
                //wifi.Join(myBSS);// Open netowrk
                wifi.Join(myBSS,"hellopass"); // Network with WPA or WPA2 security.
                //wifi.Join(myBSS,"1A1A1A1A1A"); // Network with WEP 64bit (10 hex digits).
                //wifi.Join(myBSS, "1A1A1A1A1A2B2B2B2B2B123456"); // Network with WEP 128bit (26 hex digits).
                Debug.Print("Connected");
            }
            else
            {
                Debug.Print(myAP +" Wireless network was not found");

            }

        }

        void wifi_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is Up");
            Debug.Print("IP Address: " + wifi.NetworkSettings.IPAddress.ToString());
            Debug.Print("Subnet Mask: " + wifi.NetworkSettings.SubnetMask.ToString());
            Debug.Print("Gateway: " + wifi.NetworkSettings.GatewayAddress.ToString());
            Debug.Print("DNS Server: " + wifi.NetworkSettings.DnsAddresses[0].ToString());
            
        }

        void wifi_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is Down");
        }
    }
}