/// <summary> /// Creates a new socket based on the WiFly socket TCP stack /// </summary> /// <param name="Hostname">The hostname to connect to</param> /// <param name="Port">The port to connect to</param> /// <param name="NetworkInterface">Reference to the WiFly module</param> public WiFlySocket(string Hostname, ushort Port, WiFlyGSX NetworkInterface) { this._Hostname = Hostname; this._Port = Port; this._NetworkInterface = NetworkInterface; this.LineEnding = ""; }
private void WiFlyDriver() { // Note: WiFly is pre-configured for SSID, passphrase, etc, using 3.3V FTDI basic (pins 1,2,3,10) try { _wifly = new WiFlyGSX(); string ver = _wifly.ModuleVersion; string mac = _wifly.MacAddress; Debug.Print("WiFly version: " + ver + ", mac: " + mac); while (true) { string ip = _wifly.LocalIP; Debug.Print("IP: " + ip); if (ip != "0.0.0.0") { string conf = NapkinGet("/config/cerbee2"); Debug.Print("CONF: "); Debug.Print(conf); } Debug.Print("mem: " + Debug.GC(false)); Thread.Sleep(8000); } } catch (Exception ex) { Debug.Print("Exception in WiFlyDriver: " + ex.Message); } }
public WiFlyStream(WiFlyGSX wifly) { this._wifly = wifly; SetBuffer(""); WaitForCloseOrData(); }
private WiFlyGSX GetModule() { using (var initWatchdog = new Watchdog(new TimeSpan(0, 5, 0), () => { Debug.Print("Spent 5 minutes trying to initialize, giving up...."); PowerState.RebootDevice(false, 1000); })) { initWatchdog.Start(); while (null == _module) { _module = SetupModule(); } return(_module); } }
private static void InitWifi() { // Declares the WiFly module, configures the IP address and joins a wireless network WiFlyGSX WifiModule = new WiFlyGSX(SerialPorts.COM1); WifiModule.EnableDHCP(); WifiModule.JoinNetwork("BOKBOKBOK", 0, WiFlyGSX.AuthMode.MixedWPA1_WPA2, "bezout1983"); // Showing some interesting output Debug.Print("Local IP: " + WifiModule.LocalIP); Debug.Print("MAC address: " + WifiModule.MacAddress); // Creates a socket var Socket = new WiFlySocket("www.netmftoolbox.com", 80, WifiModule); // Connects to the socket Socket.Connect(); // Does a plain HTTP request Socket.Send("GET /helloworld/ HTTP/1.1\r\n"); Socket.Send("Host: " + Socket.Hostname + "\r\n"); Socket.Send("Connection: Close\r\n"); Socket.Send("\r\n"); // Prints all received data to the debug window, until the connection is terminated and there's no data left anymore while (Socket.IsConnected || Socket.BytesAvailable > 0) { string Text = Socket.Receive(); if (Text != "") { Debug.Print(Text); } } // Closes down the socket Socket.Close(); }
private WiFlyGSX SetupModule() { WiFlyGSX workingModule = null; var thread = new Thread(() => { var module = new WiFlyGSX(DebugMode: true); try { module.EnableDHCP(); var isConnected = false; for (var i = 0; i < 3 && !(isConnected = module.JoinNetwork(_ssid, 0, _securityMode, _password)); i++) { Thread.Sleep(1000); } Debug.Print("isConnected: " + isConnected); if (!isConnected) { module.Reboot(); module.Dispose(); return; } for (var i = 0; i < 10 && module.LocalIP == "0.0.0.0"; i++) { Thread.Sleep(1000); } Debug.Print("Local IP: " + module.LocalIP); Debug.Print("MAC address: " + module.MacAddress); if (module.LocalIP == "0.0.0.0") { module.Reboot(); module.Dispose(); return; } } catch (ThreadAbortException) { module.Dispose(); Debug.Print("Our watchdog got fired - sleep a bit and try rebooting the module before we return"); Thread.Sleep(2000); using (module = new WiFlyGSX(DebugMode: true)) { module.Reboot(); } throw; } workingModule = module; }); thread.Start(); var fired = false; using (var setupWatchdog = new Watchdog(new TimeSpan(0, 0, 30), () => { if (!fired) { Debug.Print("Triggering setup watchdog"); thread.Abort(); fired = true; } })) { setupWatchdog.Start(); thread.Join(); } return(workingModule); }
public void FetchURL(Uri url) { bool success = false; while (!success) { var module = GetModule(); var thread = new Thread(() => { var httpHost = url.Host; var host = httpHost; var port = (ushort)url.Port; if (host == "pourcast.labs.rightpoint.com") { host = "192.168.100.114"; } var request = "GET " + url.AbsolutePath + " HTTP/1.1\r\nHost: " + httpHost + "\r\nConnection: Close\r\n\r\n"; SimpleSocket socket = new WiFlySocket(host, port, module); try { // Connects to the socket socket.Connect(); // Does a plain HTTP request socket.Send(request); // Prints all received data to the debug window, until the connection is terminated while (socket.IsConnected) { var line = socket.Receive().Trim(); if (line != "" && line != null) { //Debug.Print(line); } } success = true; } finally { socket.Close(); } }); thread.Start(); using (var fetchWatchdog = new Watchdog(new TimeSpan(0, 0, 5), () => { Debug.Print("Triggering fetch watchdog"); thread.Abort(); Thread.Sleep(500); module.Reboot(); module.Dispose(); _module = null; })) { thread.Join(); } } }