Пример #1
0
        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();
        }
Пример #2
0
        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;
        }