using System; using System.Threading; using System.Net; using System.Text; using System.Net.Sockets; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; //need for Utility class using Microsoft.SPOT.Net; using GHI.Premium.Net; using HSM; using WIFI; namespace WIFI { public class WiFi { private static SynchronizedEventQueue _anEventQ; private static WiFiRS9110 _wifi; private static bool _useDebug = false; public WiFi(SynchronizedEventQueue eq) { _anEventQ = eq; _wifi = new WiFiRS9110(SPI.SPI_module.SPI2, (Cpu.Pin)10, (Cpu.Pin)18, (Cpu.Pin)20);// FEZ Spider Socket 6 } public static void Start_WIFI_Network() { if (!_wifi.IsOpen) _wifi.Open(); _wifi.NetworkInterface.EnableStaticIP("134.89.8.58", "255.255.254.0", "134.89.8.1"); _wifi.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(wifi_NetworkAddressChanged); _wifi.WirelessConnectivityChanged += new WiFiRS9110.WirelessConnectivityChangedEventHandler(wifi_WirelessConnectivityChanged); NetworkInterfaceExtension.AssignNetworkingStackTo(_wifi); JoinNetwork(); } // Your router SSID and PassKey neede to be set in here public static void JoinNetwork() { try { // Your router SSID needed here WiFiNetworkInfo[] ScanResp = _wifi.Scan("MBARI WiFi"); //< Needs to be changed to SSID you are looking for if (ScanResp != null && ScanResp.Length > 0) { //Your passkey needed here _wifi.Join(ScanResp[0], ""); //< needs to be changed to your Key _anEventQ.Add((Signal)ProfilerEvents.WIFI_JoinedNetwork, 0, DateTime.Now.ToString()); if (_useDebug) { Debug.Print("***Joined to MBARI WiFi..."); } } else { if (_useDebug) { Debug.Print("***MBARI WiFi was not found"); } } } catch (Exception e) { if (_useDebug) { Debug.Print("!!! Exception : " + e.Message); } } } public static void wifi_NetworkAddressChanged(object sender, EventArgs e) { NTP.Time("us.pool.ntp.org", (-(60 * 7))); _anEventQ.Add((Signal)ProfilerEvents.WIFI_TimeSetBy_NTP, 0, DateTime.Now.ToString()); RunServer(); } public static void wifi_WirelessConnectivityChanged(object sender, WiFiRS9110.WirelessConnectivityEventArgs e) { string s = _wifi.NetworkInterface.IPAddress; int rssi = e.NetworkInformation.RSSI; _anEventQ.Add((Signal)ProfilerEvents.WIFI_ConnectivityChanged, rssi, s); if (e.IsConnected) { // Add the time zone offset to the retrieved UTC Time // (in this example, it assumes that the time zone is GMT-6). // You will need to adjust TimeService.SetTimeZoneOffset value for your time zone //UTC -6 hours for Central Standard Time. Use -(60 * 5) for Daylight savings time. //You will need to change NTPTime to your Time Zone } } private static void RunServer() { // Initiates a new shell on port 23 WIFI.ShellCore Shell = new WIFI.ShellCore(new IntegratedSocket("", 23)); // When disconnected, we want to start listening again Shell.OnDisconnected += new WIFI.ShellCore.ConnectionStateChange(Shell_OnDisconnected); // Lets bind to the authorization provider (optional, but useful ;-)) WIFI.Auth LoginModule = new WIFI.Auth(Shell); LoginModule.OnAuthorize += new WIFI.Auth.IsAuthorized(Auth_OnAuthorize); // Lets bind some more apps WIFI.ShellCommands.Bind(Shell); // Some basic shell commands like MOTD, CLS, etc. // We start the shell Shell.Start(); } // Triggered when the connection has been lost static void Shell_OnDisconnected(WIFI.ShellCore Shell, string RemoteAddress, DateTime Time) { Thread.Sleep(1000); // Starts to listen again Shell.Start(); } // Asks if a user is authorized static bool Auth_OnAuthorize(WIFI.ShellCore Shell, string Username, string Password, string RemoteAddress) { if (Username == "gene" && Password == "LeGrandFromage") return true; if (Username == "wayne" && Password == "CodeMonkey") return true; Shell.TelnetServer.Print("Answer correctly or you will be assimilated! :-)"); return false; } } }