Example #1
0
        public static void Main()
        {
            try
            {
                // Get the first WiFI Adapter
                WiFiAdapter wifi = WiFiAdapter.FindAllAdapters()[0];

                // Set up the AvailableNetworksChanged event to pick up when scan has completed
                wifi.AvailableNetworksChanged += Wifi_AvailableNetworksChanged;

                // Loop forever scanning every 30 seconds
                while (true)
                {
                    Debug.WriteLine("starting WiFi scan");
                    wifi.ScanAsync();

                    Thread.Sleep(30000);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("message:" + ex.Message);
                Debug.WriteLine("stack:" + ex.StackTrace);
            }

            Thread.Sleep(Timeout.Infinite);
        }
        public static void SetupAndConnectNetwork()
        {
            wifi = WiFiAdapter.FindAllAdapters()[0];
            WiFiConnectionResult wiFiConnectionResult = wifi.Connect(c_SSID, WiFiReconnectionKind.Automatic, c_AP_PASSWORD);

            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();

            if (nis.Length > 0)
            {
                NetworkInterface ni = nis[0];
                ni.EnableAutomaticDns();
                ni.EnableDhcp();

                CheckIP();

                if (!NetworkHelpers.IpAddressAvailable.WaitOne(5000, false))
                {
                    throw new NotSupportedException("ERROR: IP address is not assigned to the network interface.");
                }
            }
            else
            {
                throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
            }
        }
        private static void InitWifiAdpater()
        {
            if (_wifiAdapter != null)
            {
                return;
            }

            _wifiAdapter = WiFiAdapter.FindAllAdapters()[0];
            _wifiAdapter.AvailableNetworksChanged += (_, __) => _autoResetEvent.Set();
        }
Example #4
0
        public static void Main()
        {
            Debug.WriteLine("Hello World :-)!");

            try
            {
                int connectRetry = 0;

                // Get the first WiFI Adapter
                WiFiAdapter wifi = WiFiAdapter.FindAllAdapters()[0];
                Debug.WriteLine("Getting wifi adaptor");

                wifi.AvailableNetworksChanged += WifiAvailableNetworksChanged;

rescan:
                wifi.ScanAsync();
                Debug.WriteLine("Scanning...");

                var timeout = DateTime.UtcNow.AddSeconds(10);
                while (!_isConnected)
                {
                    Thread.Sleep(100);
                    if (DateTime.UtcNow > timeout)
                    {
                        goto rescan;
                    }
                }

                NetworkHelpers.SetupAndConnectNetwork(false);

                Debug.WriteLine("Waiting for network up and IP address...");

                while (!NetworkHelpers.CheckIP())
                {
                    Thread.Sleep(500);
                    connectRetry++;
                    if (connectRetry == 5)
                    {
                        connectRetry = 0;
                        goto rescan;
                    }
                }

                using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(ApiController) }))
                {
                    server.Start();
                    Thread.Sleep(Timeout.Infinite);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"{ex}");
            }
        }
Example #5
0
        private static void InitWiFiScan()
        {
            if (wifi == null)
            {
                // Get the first WiFI Adapter
                wifi = WiFiAdapter.FindAllAdapters()[0];

                // Set up the AvailableNetworksChanged event to pick up when scan has completed
                wifi.AvailableNetworksChanged += Wifi_AvailableNetworksChanged;
            }
        }
Example #6
0
        public static void Main()
        {
            Debug.WriteLine("Hello from nanoFramework wifi!");

            try
            {
                // Get the first WiFI Adapter
                WiFiAdapter wifi = WiFiAdapter.FindAllAdapters()[0];

                // Set up the AvailableNetworksChanged event to pick up when scan has completed
                wifi.AvailableNetworksChanged += Wifi_AvailableNetworksChanged;

                Debug.WriteLine("starting WiFi scan");
                wifi.ScanAsync();


                var remoteIp    = IPAddress.Any;
                var receivePort = 12345;

                Debug.WriteLine("receive message");

                using (var udpReceive = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
                {
                    udpReceive.Bind(new IPEndPoint(remoteIp, receivePort));

                    var resBytes = new byte[1024];

                    while (true)
                    {
                        Debug.WriteLine("wait...");

                        var resSize = udpReceive.Receive(resBytes);

                        var receiveMsg = Encoding.UTF8.GetString(resBytes, 0, resSize);

                        Debug.WriteLine($"receiveData   : {receiveMsg}");

                        if (receiveMsg == "exit")
                        {
                            break;
                        }
                    }
                    udpReceive.Close();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("message:" + ex.Message);
                Debug.WriteLine("stack:" + ex.StackTrace);
            }

            Thread.Sleep(Timeout.Infinite);
        }
Example #7
0
        /// <summary>
        /// This is a helper function to pick up first available network interface and use it for communication.
        /// </summary>
        private static void SetupAndConnectNetwork()
        {
            // Get the first WiFI Adapter
            var wifiAdapter = WiFiAdapter.FindAllAdapters()[0];

            // Begin network scan.
            wifiAdapter.ScanAsync();

            // While networks are being scan, continue on configuration. If networks were set previously,
            // board may already be auto-connected, so reconnection is not even needed.
            var wiFiConfiguration = Wireless80211Configuration.GetAllWireless80211Configurations()[0];
            var ipAddress         = NetworkInterface.GetAllNetworkInterfaces()[0].IPv4Address;
            var needToConnect     = string.IsNullOrEmpty(ipAddress) || (ipAddress == "0.0.0.0");

            while (needToConnect)
            {
                foreach (var network in wifiAdapter.NetworkReport.AvailableNetworks)
                {
                    // Show all networks found
                    Debug.WriteLine($"Net SSID :{network.Ssid},  BSSID : {network.Bsid},  rssi : {network.NetworkRssiInDecibelMilliwatts},  signal : {network.SignalBars}");

                    // If its our Network then try to connect
                    if (network.Ssid == wiFiConfiguration.Ssid)
                    {
                        var result = wifiAdapter.Connect(network, WiFiReconnectionKind.Automatic, wiFiConfiguration.Password);

                        if (result.ConnectionStatus == WiFiConnectionStatus.Success)
                        {
                            Debug.WriteLine($"Connected to Wifi network {network.Ssid}.");
                            needToConnect = false;
                        }
                        else
                        {
                            Debug.WriteLine($"Error {result.ConnectionStatus} connecting to Wifi network {network.Ssid}.");
                        }
                    }
                }

                Thread.Sleep(10000);
            }

            ipAddress = NetworkInterface.GetAllNetworkInterfaces()[0].IPv4Address;
            Debug.WriteLine($"Connected to Wifi network with IP address {ipAddress}");
        }
        public void MonitorWifiNetworksContinuously()
        {
            var isOk = false;
            var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            // Get the first WiFI Adapter
            var wifiAdapter = WiFiAdapter.FindAllAdapters()[0];

            // Set up the AvailableNetworksChanged event to pick up when scan has completed
            wifiAdapter.AvailableNetworksChanged += HandleWifiNetworksChanged;

            // Loop forever scanning every 30 seconds
            while (true)
            {
                Debug.WriteLine("Scanning for WiFi networks...");
                wifiAdapter.ScanAsync();

                Thread.Sleep(30000);
            }
        }
Example #9
0
        /// <summary>
        /// Instance method for the application start.
        /// </summary>
        private void Run()
        {
            this.InitializePortPins();

            WiFiAdapter[] adapters = WiFiAdapter.FindAllAdapters();
            if (adapters.Length == 1)
            {
                WiFiAdapter adapter = adapters[0];
                adapter.AvailableNetworksChanged += this.WiFiAdapter_AvailableNetworksChanged;
                adapter.ScanAsync();
            }

            SSD1306_Display display = new SSD1306_Display(1, 0x79);

            display.Initialize();
            display.ClearScreen();
            while (true)
            {
                display.DrawText(0, 0, "Test");
                display.Write();
                Thread.Sleep(1000);
            }
        }
Example #10
0
        public static void Main()
        {
            Debug.WriteLine("Hello from a webserver!");

            try
            {
                int connectRetry = 0;

#if HAS_WIFI
                // Get the first WiFI Adapter
                WiFiAdapter wifi = WiFiAdapter.FindAllAdapters()[0];
                Debug.WriteLine("Getting wifi adaptor");

                wifi.AvailableNetworksChanged += WifiAvailableNetworksChanged;

rescan:
                wifi.ScanAsync();
                Debug.WriteLine("Scanning...");

                var timeout = DateTime.UtcNow.AddSeconds(10);
                while (!_isConnected)
                {
                    Thread.Sleep(100);
                    if (DateTime.UtcNow > timeout)
                    {
                        goto rescan;
                    }
                }
#endif

                NetworkHelpers.SetupAndConnectNetwork(true);

                Debug.WriteLine("Waiting for network up and IP address...");

                NetworkHelpers.IpAddressAvailable.WaitOne();
                NetworkHelpers.DateTimeAvailable.WaitOne();

#if HAS_WIFI
                while (!NetworkHelpers.CheckIP())
                {
                    Thread.Sleep(500);
                    connectRetry++;
                    if (connectRetry == 5)
                    {
                        connectRetry = 0;
                        goto rescan;
                    }
                }
#endif

#if HAS_STORAGE
                _storage = KnownFolders.RemovableDevices.GetFolders()[0];
#endif

                _controller = new GpioController();

#if USE_TLS
                X509Certificate _myWebServerCertificate509 = new X509Certificate2(_myWebServerCrt, _myWebServerPrivateKey, "1234");

                // Instantiate a new web server on port 443.
                using (WebServer server = new WebServer(443, HttpProtocol.Https, new Type[] { typeof(ControllerPerson), typeof(ControllerTest), typeof(ControllerAuth) }))
#else
                // Instantiate a new web server on port 80.
                using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(ControllerPerson), typeof(ControllerTest), typeof(ControllerAuth) }))
#endif
                {
                    // To test authentication with various scenarios
                    server.ApiKey     = _securityKey;
                    server.Credential = new NetworkCredential("user", "password");
                    // Add a handler for commands that are received by the server.
                    server.CommandReceived += ServerCommandReceived;

#if USE_TLS
                    server.HttpsCert = _myWebServerCertificate509;

                    server.SslProtocols = System.Net.Security.SslProtocols.Tls11 | System.Net.Security.SslProtocols.Tls12;
#endif

                    // Start the server.
                    server.Start();

                    Thread.Sleep(Timeout.Infinite);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"{ex}");
            }
        }