private async void SensorUpdateTimerCallback(object state)
        {
            try
            {
                UnitsNet.Temperature temperature = bme280Sensor.Temperature;
                double            humidity       = bme280Sensor.Humidity;
                UnitsNet.Pressure airPressure    = bme280Sensor.Pressure;

                Debug.WriteLine($"{DateTime.UtcNow.ToLongTimeString()} Timer triggered " +
                                $"Temperature: {temperature.DegreesCelsius:0.0}°C {temperature.DegreesFahrenheit:0.0}°F " +
                                $"Humidity: {humidity:0.0}% " +
                                $"AirPressure: {airPressure.Kilopascals:0.000}KPa ");

                SensorPayload sensorPayload = new SensorPayload()
                {
                    Temperature = temperature.DegreesCelsius,
                    Humidity    = humidity,
                    AirPressure = airPressure.Kilopascals,
                };

                string payloadText = JsonConvert.SerializeObject(sensorPayload);

                using (var message = new Message(Encoding.ASCII.GetBytes(payloadText)))
                {
                    Debug.WriteLine("AzureIoTHubClient SendEventAsync starting");
                    await azureIoTHubClient.SendEventAsync(message);

                    Debug.WriteLine("AzureIoTHubClient SendEventAsync finished");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Retrieving or sending sensor values failed:{ex.Message}");
            }
        }
        private async void SensorUpdateTimerCallback(object state)
        {
            try
            {
                UnitsNet.Temperature temperature = bme280Sensor.Temperature;
                double            humidity       = bme280Sensor.Humidity;
                UnitsNet.Pressure airPressure    = bme280Sensor.Pressure;

                Debug.WriteLine($"{DateTime.UtcNow.ToLongTimeString()} Timer triggered " +
                                $"Temperature: {temperature.DegreesCelsius:0.0}°C {temperature.DegreesFahrenheit:0.0}°F " +
                                $"Humidity: {humidity:0.0}% " +
                                $"AirPressure: {airPressure.Kilopascals:0.000}KPa ");

                // Manually construct JSON payload, not recomended
                string payloadText = @"{""Temperature"":" + temperature.DegreesCelsius.ToString("f1") +
                                     @",""Humidity"":" + humidity.ToString("F0") +
                                     @",""AirPressure"":" + airPressure.Kilopascals.ToString("F3") +
                                     @"}";

                using (var message = new Message(Encoding.ASCII.GetBytes(payloadText)))
                {
                    Debug.WriteLine("AzureIoTHubClient SendEventAsync starting");
                    await azureIoTHubClient.SendEventAsync(message);

                    Debug.WriteLine("AzureIoTHubClient SendEventAsync finished");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Retrieving or sending sensor values failed:{ex.Message}");
            }
        }
Esempio n. 3
0
        private bool CheckDelta(UnitsNet.Temperature temp, UnitsNet.RelativeHumidity hum)
        {
            if (Status.Temp == 0 || Status.Humidity == 0)
            {
                return(true);
            }

            var tempDelta = Math.Abs(temp.DegreesFahrenheit - Status.Temp);
            var humDelta  = Math.Abs(hum.Percent - Status.Humidity);

            if (tempDelta < 10 && humDelta < 10)
            {
                return(true);
            }

            return(false);
        }
Esempio n. 4
0
        private async void SensorUpdateTimerCallback(object state)
        {
            try
            {
                // Checking that SaS token isn't about to expire
                if ((DateTime.UtcNow - sasTokenIssuedAtUtc) >= sasTokenValidityPeriod)
                {
                    Debug.WriteLine($"{DateTime.UtcNow.ToString("hh:mm:ss")} SAS token needs renewing");

                    try
                    {
                        TpmDevice myDevice = new TpmDevice(0);                         // Use logical device 0 on the TPM

                        azureIoTHubUri      = myDevice.GetHostName();
                        deviceId            = myDevice.GetDeviceId();
                        sasToken            = myDevice.GetSASToken((uint)sasTokenValidityPeriod.TotalSeconds);
                        sasTokenIssuedAtUtc = DateTime.UtcNow;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"TpmDevice.GetSASToken refresh failed:{ex.Message}");
                        return;
                    }

                    try
                    {
                        azureIoTHubClient = DeviceClient.Create(azureIoTHubUri, AuthenticationMethodFactory.CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Mqtt);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine($"DeviceClient.Create with TPM info failed:{ex.Message}");
                        return;
                    }

                    Debug.WriteLine($"{DateTime.UtcNow.ToString("hh:mm:ss")} SAS token renewed ");
                }

                UnitsNet.Temperature temperature = bme280Sensor.Temperature;
                double            humidity       = bme280Sensor.Humidity;
                UnitsNet.Pressure airPressure    = bme280Sensor.Pressure;

                Debug.WriteLine($"{DateTime.UtcNow.ToLongTimeString()} Timer triggered " +
                                $"Temperature: {temperature.DegreesCelsius:0.0}°C {temperature.DegreesFahrenheit:0.0}°F " +
                                $"Humidity: {humidity:0.0}% " +
                                $"AirPressure: {airPressure.Kilopascals:0.000}KPa ");

                SensorPayloadDto sensorPayload = new SensorPayloadDto()
                {
                    Temperature = temperature.DegreesCelsius,
                    Humidity    = humidity,
                    AirPressure = airPressure.Kilopascals,
                };

                string payloadText = JsonConvert.SerializeObject(sensorPayload);

                using (var message = new Message(Encoding.ASCII.GetBytes(payloadText)))
                {
                    Debug.WriteLine($" {DateTime.UtcNow.ToString("hh:mm:ss.fff")} AzureIoTHubClient SendEventAsync starting");
                    await azureIoTHubClient.SendEventAsync(message);

                    Debug.WriteLine($" {DateTime.UtcNow.ToString("hh:mm:ss.fff")} AzureIoTHubClient SendEventAsync finished");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Retrieving or sending sensor values failed:{ex.Message}");
            }
        }