You are on page 1of 6

From GHI

using using using using using Microsoft.SPOT; GHI.Premium.Net; Gadgeteer.Networking; GT = Gadgeteer; Gadgeteer.Modules.GHIElectronics;

namespace TestWiFi { public partial class Program { // 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_Tick); t.Start(); } void t_Tick(GT.Timer timer) { timer.Stop(); // setup events wifi.NetworkDown += new GT.Modules.Module.NetworkModule.NetworkEventHandler(wifi_NetworkDown); wifi.NetworkUp += new GT.Modules.Module.NetworkModule.NetworkEventHandler(wifi_NetworkUp); // to use hold the network to connect to WiFiNetworkInfo info = null; NetworkInterfaceExtension.AssignNetworkingStackTo(wifi.Interface); // use the router's DHCP server to set my network info wifi.UseDHCP(); // look for avaiable networks WiFiNetworkInfo[] scanResults = wifi.Interface.Scan(); // go through each network and print out settings in the debug window foreach (WiFiNetworkInfo result in scanResults) { Debug.Print("****" + result.SSID + "****"); Debug.Print("ChannelNumber = " + result.ChannelNumber); Debug.Print("networkType = " + result.networkType); Debug.Print("PhysicalAddress = " + GetMACAddress(result.PhysicalAddress)); Debug.Print("RSSI = " + result.RSSI); Debug.Print("SecMode = " + result.SecMode); } // locate a specific network if (info != null) { info.SSID = "BTHub3-6TQW"; string pass = "e675ae97d4";

NetworkModule. GT.NetworkState state) { if (state == GT. else Debug. state = Down"). // After connecting.NetworkModule.Module. HttpResponse response) { string text = response. info. } // handle the network changed events void wifi_NetworkDown(GT.Print("Network Up event. pass).NetworkState. } string ByteToHex(byte number) { string hex = "0123456789ABCDEF".info.Module. // now that the information has been returned.NetworkModule sender.Print("Network up2 event.Module.ResponseReceived += new HttpRequest.Modules.Down) Debug. } } // after the data is returned from the web ("http://www.ghielectronics. } void wifi_NetworkUp(GT.Up) Debug.Interface.HttpRequest wc = WebClient.Modules.Interface.Join(info.networkType = NetworkType.NetworkModule.Print("Network Up2 event.NetworkState.SecMode = SecurityMode.com").Print("Not connected!"). wc.Modules. This can also be used to access web services Gadgeteer.Text.Modules.Print("Network Up event.Disconnect(). state = Up"). Debug.Module.GetFromWeb("http://www. this event is fired void wc_ResponseReceived(HttpRequest sender. disconnect from the network wifi.Networking.WPA2. } // 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]).IsOpen) { wifi.Modules.Module.AccessPoint. else Debug.ghielectronics.NetworkState state) { if (state == GT. GT.NetworkModule sender. go out and get a web page.NetworkModule.Print("Network joined"). state = Down"). if (wifi.com" above).Interface.Module.ResponseHandler(wc_ResponseReceived). } Debug. "-" "-" "-" "-" .Modules. state = Up").

SPOT.Print("Program Started").Print("Connecting to " + myAP).Join(scanResult[0]. } private void wifiConnect() { if (!wifi_RS21.Modules.Open().Length > 0) { Debug. Debug. } .Interface. wifi_RS21. WiFiNetworkInfo[] scanResult = wifi_RS21. Debug.Networking.Premium. Gadgeteer.Interface. if (!wifi_RS21.Print("Connected").Interface. Debug.EnableDhcp(). GHI.NetworkInterface.GHIElectronics.Net.IsOpen) wifi_RS21. GT = Gadgeteer.Hardware. private string Pkey = "e675ae97d4". void ProgramStarted() { wifiConnect().Interface.Print("Scan for wireless networks"). hex[number & 0x0F] }). namespace TestWiFi { public partial class Program { private string ssid = "BTHub3-6TQW". // This method is run when the mainboard is powered up or reset.Interface.Net.Interface.AssignNetworkingStackTo(wifi_RS21.SPOT.NetworkInterface. System. if (scanResult != null && scanResult.Sockets. System. Pkey). try { string myAP = ssid.Net. NetworkInterfaceExtension.Interface). Microsoft. } } } Some code I made up using using using using using using using using using Microsoft.return new string(new char[] { hex[(number & 0xF0) >> 4].IsDhcpEnabled) wifi_RS21. System.Scan(myAP). Gadgeteer.

Interface.Networking.Message).Text. Gadgeteer.Disconnect().com").SPOT. System.GHIElectronics. void ProgramStarted() { Debug. } } } From HCI lab using using using using using using using using using using Microsoft.Networking.Hardware.Net. wc. System. Gadgeteer.Net.ghielectronics.Modules.Print("Program Started").HttpRequest wc = WebClient.ResponseHandler(wc_ResponseReceived).ResponseReceived += new HttpRequest.GetFromWeb("http://www. Microsoft. } } catch (Exception e) { Debug. wifi = wifi_RS21.SPOT. System. GHI.SPOT.Print(z. disconnect from the network wifi_RS21. namespace TestWiFi { public partial class Program { private string ssid = "BTHub3-6TQW". GT = Gadgeteer. } } void wc_ResponseReceived(HttpRequest sender. // This method is run when the mainboard is powered up or reset.Message).NetworkInformation.Sockets. } try { Gadgeteer.Print(myAP + " Wireless network was not found"). .Net.Premium. } catch (Exception z) { Debug. private string Pkey = "e675ae97d4". Microsoft.Net. HttpResponse response) { string text = response.Interface. WiFiRS9110 wifi. // now that the information has been returned.else { Debug.Print(e.

Scan(). if (info. int i = 0.Print("WiFi address changed to: " + wifi.WirelessConnectivityChangedEventHandler(Interface_WirelessConnectivityChang ed). " + info.to/").IsOpen) wifi.NetworkInterface.NetworkInterface.NetworkAddressChangedEventHandler(Interface_NetworkAddressCh anged).Print("HTTP-Response: " + response.AssignNetworkingStackTo(wifi). } void Interface_NetworkAddressChanged(object sender.Open().StatusCode == "200") { Debug. String passphrase) { if (!wifi.ToString(). } void TestWeb() { WebClient.Equals(ssid)) { wifi.initWiFi(ssid. break.Join(ScanResp[i].IsOpen) TestWeb().NetworkAddressChanged += new NetworkInterfaceExtension.IPAddress). } void Program_ResponseReceived(HttpRequest sender.ResponseReceived += new HttpRequest. wifi. wifi. wifi.WirelessConnectivityChanged += new WiFiRS9110.yp. } else { Debug.SSID).GetFromWeb("http://cr. // Scan for networks and connect if found WiFiNetworkInfo[] ScanResp = wifi.SSID.StatusCode). NetworkInterfaceExtension. passphrase). Pkey).EnableDhcp(). } if (wifi.Print("Found WLAN: " + i + ". EventArgs e) { Debug.ResponseHandler(Program_ResponseReceived). } } void initWiFi(String ssid. HttpResponse response) { if (response. } i++.Print("HTTP-Response: 200"). } . foreach (WiFiNetworkInfo info in ScanResp) { Debug.

} } } .ToString()).WirelessConnectivityEventArgs e) { Debug.NetworkInformation.Print("WiFi connectivity changed.SSID.void Interface_WirelessConnectivityChanged(object sender. WiFiRS9110. new SSID: " + e.