public static void Main()
        {
            //Setup I2C pins for ESP32 board
            Configuration.SetPinFunction(21, DeviceFunction.I2C1_DATA);
            Configuration.SetPinFunction(22, DeviceFunction.I2C1_CLOCK);

            // if we are using TLS it requires valid date & time
            NetworkHelpers.SetupAndConnectNetwork(wifiSSID, wifiApPASSWORD, true);

            NetworkHelpers.CheckIP();
            Debug.WriteLine("Waiting for network up and IP address...");
            NetworkHelpers.IpAddressAvailable.WaitOne();


            bme280Sensor    = new BME280(1, BME280.I2CAddresses.Address0);
            someDeviceValue = random.Next(50);

            if (!SetupThingsBoard())
            {
                Debug.WriteLine("Error connecting to the server");
                return;
            }

            // launch telemetry thread
            Thread telemetryThread = new Thread(new ThreadStart(TelemetryLoop));

            telemetryThread.Start();

            Debug.WriteLine("Connected to the server.");

            while (true)
            {
                Thread.Sleep(1000);
            }
        }
Example #2
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(false);

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

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

                // Instantiate a new web server on port 80.
                using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { typeof(ControllerGpio) }))
                {
                    // Start the server.
                    server.Start();

                    Thread.Sleep(Timeout.Infinite);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"{ex}");
            }
        }
Example #3
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}");
            }
        }