private async void _timer_Tick(object sender, object e) { DhtReading reading = new DhtReading(); int val = this.TotalAttempts; this.TotalAttempts++; reading = await _dht.GetReadingAsync().AsTask(); _retryCount.Add(reading.RetryCount); this.OnPropertyChanged(nameof(AverageRetriesDisplay)); this.OnPropertyChanged(nameof(TotalAttempts)); this.OnPropertyChanged(nameof(PercentSuccess)); if (reading.IsValid) { this.TotalSuccess++; this.Temperature = Convert.ToSingle(reading.Temperature); this.Humidity = Convert.ToSingle(reading.Humidity); this.LastUpdated = DateTimeOffset.Now; this.OnPropertyChanged(nameof(SuccessRate)); } else { } this.OnPropertyChanged(nameof(LastUpdatedDisplay)); }
private async void ClickMe_Click(object sender, RoutedEventArgs e) { //HelloMessage.Text = "Hello, Windows 10 IoT Core!"; GpioController controller = GpioController.GetDefault(); if (controller != null) { _pin1 = GpioController.GetDefault().OpenPin(piN, GpioSharingMode.Exclusive); _pin1.SetDriveMode(GpioPinDriveMode.Input); _dht1 = new Dht11(_pin1, GpioPinDriveMode.Input); } DhtReading reading = new DhtReading(); reading = await _dht1.GetReadingAsync().AsTask(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); while (stopwatch.Elapsed < TimeSpan.FromSeconds(60)) { if (reading.IsValid) { var Temperature = reading.Temperature; var Humidity = reading.Humidity; var messageDialog = new MessageDialog(string.Format("Temperature : {0} - Humidity : {1}", Temperature, Humidity)); Task.Delay(5000).Wait(); await messageDialog.ShowAsync(); } stopwatch.Stop(); } }
protected async Task <(double Temperature, double Humidity)> ReadTemperature(IDht device) { (double Temperature, double Humidity)returnValue = (0, 0); DhtReading reading = new DhtReading(); reading = await device.GetReadingAsync().AsTask(); if (reading.IsValid) { // *** // *** Get the values from the reading. // *** returnValue.Temperature = reading.Temperature; returnValue.Humidity = reading.Humidity; } else { // *** // *** Show -1 so that it is evident there // *** is an error reading the device. // *** returnValue.Temperature = -1; returnValue.Humidity = -1; } return(returnValue); }
private async void _timer_Tick(object sender, object e) { var vm = this.DataContext as TemperatureViewModel; var dhtData = await dht.GetReadingAsync().AsTask(); if (dhtData.IsValid) { retries.Add(dhtData.RetryCount); vm.Humidity = dhtData.Humidity; vm.Temperature = dhtData.Temperature; vm.AverageRetries = retries.Average(); vm.TotalSuccess++; Debug.WriteLine($"{DateTime.Now} - Temperature: {dhtData.Temperature}, Humidity: {dhtData.Humidity}"); ShouldITurnOnAirCondition(dhtData); await SendDataToAzureAsync(dhtData); await Blink(); } vm.TotalAttempts++; vm.PercentSuccess = 100d * ((double)vm.TotalSuccess / (double)vm.TotalAttempts); vm.LastUpdated = DateTime.Now; }
private async void readSensor() { double temp = 0; double humidity = 0; DhtReading reading = await dht.GetReadingAsync().AsTask(); if (reading.IsValid) { temp = reading.Temperature; humidity = reading.Humidity; Temperature = string.Format("{0:0.0}", temp); Humidity = string.Format("{0:0}", humidity); SensorRead?.Invoke(temp, humidity); } }
private async void _dispatchTimer_Tick(object sender, object e) { try { DhtReading reading = new DhtReading(); int val = this.TotalAttempts; this.TotalAttempts++; reading = await _dhtInterface.GetReadingAsync().AsTask(); _retryCount.Add(reading.RetryCount); this.OnPropertyChanged(nameof(AverageRetriesDisplay)); this.OnPropertyChanged(nameof(TotalAttempts)); this.OnPropertyChanged(nameof(PercentSuccess)); if (reading.IsValid) // if we are able to capture value, display those { this.TotalSuccess++; this.Temperature = Convert.ToSingle(reading.Temperature); this.Humidity = Convert.ToSingle(reading.Humidity); this.LastUpdated = DateTimeOffset.Now; this.OnPropertyChanged(nameof(SuccessRate)); var telemetryDataPoint = new { deviceId = "iot1", temperature = Temperature.ToString(), humidity = Humidity.ToString(), date = DateTime.Now.ToString("dd-MM-yyyy"), hours = DateTime.Now.Hour.ToString(), minutes = DateTime.Now.Minute.ToString(), seconds = DateTime.Now.Second.ToString() }; var messageString = JsonConvert.SerializeObject(telemetryDataPoint); var message = new Message(Encoding.ASCII.GetBytes(messageString)); await deviceClient.SendEventAsync(message); Debug.WriteLine(message); } else // log if the reading is not in valid state { Debug.WriteLine(string.Format("IsValid: {0}, RetryCount: {1}, TimedOut: {2}, Humidity: {3}, Temperature: {4}", reading.IsValid, reading.RetryCount, reading.TimedOut, reading.Humidity, reading.Temperature)); } this.OnPropertyChanged(nameof(LastUpdatedDisplay)); // show when the data was last updated this.OnPropertyChanged(nameof(DateTimeDisplay)); } catch (Exception ex) // log any exception that occurs { Debug.WriteLine(ex.Message); } }
private async void Timer_Tick(object sender, object e) { try { // *** // *** Stop the timer so we do not have more than reading // *** at a time. // *** _timer.Stop(); // *** // *** Increment the counter. // *** _totalRequests++; // *** // *** Read the sensor. // *** var reading = await _sensor.GetReadingAsync(); // *** // *** Check the result. // *** if (reading.Result == DhtReadingResult.Valid) { Trace.WriteLine($"Temperature {reading.Temperature:0.0} °C - Humidity {reading.Humidity:0.0} %"); Temperature = reading.Temperature; Humidity = reading.Humidity; _successfulRequests++; } else { Trace.WriteLine($"Sensor read failed: {reading.Result}"); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { // *** // *** Start the timer again. // *** _timer.Start(); } // *** // *** Update the success rate and running time. // *** RaisePropertyChanged(nameof(SuccessRate)); RaisePropertyChanged(nameof(RunningTime)); }
private async void _timer_Tick(object sender, object e) { reading = new DhtReading(); reading = await _dht.GetReadingAsync().AsTask(); if (reading.IsValid) { txtTemp.Text = (reading.Temperature).ToString() + " °C" + " " + (ConvertTemp.ConvertCelsiusToFahrenheit(reading.Temperature).ToString() + " °F"); } else { } }
private async void Timer_Tick(object sender, object e) { try { // *** // *** Stop the timer so we do not have more than reading // *** at a time. // *** _timer.Stop(); // *** // *** Increment the counter. // *** _totalRequests++; // *** // *** Read the sensor. // *** IDhtReading reading = await _sensor.GetReadingAsync(); // *** // *** Check the result. // *** if (reading.Result == DhtReadingResult.Valid) { //float t = reading.Temperature * 1.8F + 32F; //fahrenheit float t = reading.Temperature;//celsius this.Temperature = t; this.Humidity = reading.Humidity; _successfulRequests++; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } finally { // *** // *** Start the timer again. // *** _timer.Start(); } // *** // *** Update the success rate and running time. // *** this.RaisePropertyChanged(nameof(this.SuccessRate)); this.RaisePropertyChanged(nameof(this.RunningTime)); }
private async void _timer_Tick(object sender, object e) { DhtReading reading = new DhtReading(); int val = this.TotalAttempts; this.TotalAttempts++; reading = await _dht11.GetReadingAsync().AsTask(); _retryCount.Add(reading.RetryCount); this.OnPropertyChanged(nameof(AverageRetriesDisplay)); this.OnPropertyChanged(nameof(TotalAttempts)); this.OnPropertyChanged(nameof(PercentSuccess)); if (reading.IsValid) { this.TotalSuccess++; this.Temperature = Convert.ToSingle(reading.Temperature); this.Humidity = Convert.ToSingle(reading.Humidity); this.LastUpdated = DateTimeOffset.Now; // Set IoT data. _sensorData = new SensorData(); _sensorData.DeviceId = _deviceName; _sensorData.Temperature = Convert.ToSingle(reading.Temperature); _sensorData.ExternalTemperature = 20; _sensorData.Humidity = Convert.ToSingle(reading.Humidity); // Blink led. this.BlinkLed(); // Send IoT data. if (_sendToCloud) { try { await AzureIoTHub.SendDeviceToCloudMessageAsync(JsonConvert.SerializeObject(_sensorData)); _gridOffline.Visibility = Visibility.Collapsed; } catch (Exception ex) { Debug.WriteLine("Problem sending to IoT Hub: " + ex.Message.ToString()); _gridOffline.Visibility = Visibility.Visible; } } // Update UI. this.OnPropertyChanged(nameof(SuccessRate)); } this.OnPropertyChanged(nameof(LastUpdatedDisplay)); }
private async void readSensor() { DhtReading reading = await dht.GetReadingAsync().AsTask(); if (reading.IsValid) { // Send reading to IoT Hub string message = "{\"temperature\":" + reading.Temperature.ToString() + ", \"humidity\":" + reading.Humidity.ToString() + "}"; Message eventMessage = new Message(Encoding.UTF8.GetBytes(message)); DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(IOTHUBCONNECTIONSTRING); await deviceClient.SendEventAsync(eventMessage); } }
public async System.Threading.Tasks.Task getTempetureAsync() { IDhtReading reading = await _sensor.GetReadingAsync(); // *** // *** Check the result. // *** if (reading.Result == DhtReadingResult.Valid) { System.Diagnostics.Debug.WriteLine($"Temperature = {reading.Temperature:0.0} C, Humidity = {reading.Humidity:0.0}%"); } else { System.Diagnostics.Debug.WriteLine($"Error = {reading.Result}"); } }
private async void _timer_Tick(object sender, object e) { DhtReading reading = new DhtReading(); int val = this.TotalAttempts; this.TotalAttempts++; reading = await _dht1.GetReadingAsync(30).AsTask(); _retryCount.Add(reading.RetryCount); this.OnPropertyChanged(nameof(AverageRetriesDisplay)); this.OnPropertyChanged(nameof(TotalAttempts)); this.OnPropertyChanged(nameof(PercentSuccess)); if (reading.IsValid) { this.TotalSuccess++; this.Temperature = Convert.ToSingle(reading.Temperature); this.Humidity = Convert.ToSingle(reading.Humidity); this.LastUpdated = DateTimeOffset.Now; this.OnPropertyChanged(nameof(SuccessRate)); DataItem myItem = new DataItem(lastID++, sensorID, DateTime.Now, (float)reading.Temperature, (float)reading.Humidity); itemList.Add(myItem); try { sendToIotHub(myItem); } catch (Exception ex) //DeviceNotFoundException ex) { Debug.WriteLine(ex.Message); } } this.OnPropertyChanged(nameof(LastUpdatedDisplay)); if (readSensor2) { reading = new DhtReading(); reading = await _dht2.GetReadingAsync(30).AsTask(); if (reading.IsValid) { Humi2.Text = reading.Humidity.ToString("F2"); Temp2.Text = reading.Temperature.ToString("F2"); } } }
private async void timer_Tick(object sender, object e) { DhtReading reading = new DhtReading(); reading = await sensor.GetReadingAsync().AsTask(); if (reading.IsValid) { this.temperatura = Convert.ToSingle(reading.Temperature); this.umidade = Convert.ToSingle(reading.Humidity); //Aqui é possível chamar uma API, passando os valores de temperatura e umidade, fazendo que sua API insira em seu banco de dados esses valores var url = "SUA_URL_AQUI"; var token = "SEU_TOKEN_AQUI"; var client = new HttpClient(); var response = client.GetAsync(new Uri($"http://{url}/api/UpdateInfo/{token}?temperatura={this.temperatura}&umidade={this.umidade}")); } }
private async void _timer_Tick(object sender, object e) { DhtReading reading = new DhtReading(); int val = this.TotalAttempts; this.TotalAttempts++; reading = await _dht.GetReadingAsync().AsTask(); _retryCount.Add(reading.RetryCount); this.OnPropertyChanged(nameof(AverageRetriesDisplay)); this.OnPropertyChanged(nameof(TotalAttempts)); this.OnPropertyChanged(nameof(PercentSuccess)); if (reading.IsValid) { this.TotalSuccess++; this.Temperature = Convert.ToSingle(reading.Temperature); this.Humidity = Convert.ToSingle(reading.Humidity); this.LastUpdated = DateTimeOffset.Now; this.OnPropertyChanged(nameof(SuccessRate)); var temp = this.TemperatureDisplay; var humidity = this.HumidityDisplay; if ((humidity == "30.0% RH") || (humidity == "31.0% RH") || (humidity == "32.0% RH") || (humidity == "33.0% RH") || (humidity == "34.0% RH") || (humidity == "35.0% RH") || (humidity == "36.0% RH") || (humidity == "37.0% RH") || (humidity == "38.0% RH") || (humidity == "39.0% RH") || (humidity == "40.0% RH") || (humidity == "41.0% RH") || (humidity == "42.0% RH") || (humidity == "43.0% RH") || (humidity == "44.0% RH") || (humidity == "45.0% RH") || (humidity == "46.0% RH") || (humidity == "47.0% RH") || (humidity == "48.0% RH") || (humidity == "49.0% RH") || (humidity == "50.0% RH") || (humidity == "51.0% RH") || (humidity == "52.0% RH") || (humidity == "53.0% RH") || (humidity == "54.0% RH") || (humidity == "55.0% RH")) { if (value == GpioPinValue.Low) { value = GpioPinValue.High; pin.Write(value); } } else { value = GpioPinValue.Low; pin.Write(value); } SendToCloud(temp, humidity); } this.OnPropertyChanged(nameof(LastUpdatedDisplay)); }
private async void _timer_Tick(object sender, object e) { DhtReading reading = new DhtReading(); int val = this.TotalAttempts; this.TotalAttempts++; reading = await _dht.GetReadingAsync().AsTask(); _retryCount.Add(reading.RetryCount); this.OnPropertyChanged(nameof(AverageRetriesDisplay)); this.OnPropertyChanged(nameof(TotalAttempts)); this.OnPropertyChanged(nameof(PercentSuccess)); if (reading.IsValid) { this.TotalSuccess++; this.Temperature = Convert.ToSingle(reading.Temperature); this.Humidity = Convert.ToSingle(reading.Humidity); //Send to cloud var telemetryDataPoint = new { DeviceId = 1, temperature = reading.Temperature, unitOfTemp = "C", humidity = reading.Humidity, unitOfHumidity = "%", TimeFlag = Convert.ToString(DateTime.Now) }; var messageString = JsonConvert.SerializeObject(telemetryDataPoint); var message = new Message(System.Text.Encoding.ASCII.GetBytes(messageString)); await deviceClient1.SendEventAsync(message); Debug.WriteLine(messageString); this.LastUpdated = DateTimeOffset.Now; this.OnPropertyChanged(nameof(SuccessRate)); } this.OnPropertyChanged(nameof(LastUpdatedDisplay)); }
private async void OnDHTTick(ThreadPoolTimer timer) { DhtReading reading; reading = await _dht.GetReadingAsync(); if (!reading.IsValid) { return; } //Raise the event to the client that we have new data DhtValuesChanged?.Invoke(this, new DHTTempArgs { Temperature = Math.Round(Convert.ToSingle(Farehieght(reading.Temperature)), 1, MidpointRounding.AwayFromZero), Humid = Math.Round(Convert.ToSingle(reading.Humidity), 1, MidpointRounding.AwayFromZero), DewPoint = Math.Round(Convert.ToSingle(dewPointFast(reading.Temperature, reading.Humidity)), 1, MidpointRounding.AwayFromZero), HeatIndex = Math.Round(Convert.ToSingle(heatIndex(Farehieght(reading.Temperature), reading.Humidity)), 1, MidpointRounding.AwayFromZero) }); }
private async void _dispatchTimer_Tick(object sender, object e) { try { DhtReading reading = new DhtReading(); int val = this.TotalAttempts; this.TotalAttempts++; reading = await _dhtInterface.GetReadingAsync().AsTask(); _retryCount.Add(reading.RetryCount); this.OnPropertyChanged(nameof(AverageRetriesDisplay)); this.OnPropertyChanged(nameof(TotalAttempts)); this.OnPropertyChanged(nameof(PercentSuccess)); if (reading.IsValid) // if we are able to capture value, display those { this.TotalSuccess++; this.Temperature = Convert.ToSingle(reading.Temperature); this.Humidity = Convert.ToSingle(reading.Humidity); this.LastUpdated = DateTimeOffset.Now; this.OnPropertyChanged(nameof(SuccessRate)); } else // log if the reading is not in valid state { Debug.WriteLine(string.Format("IsValid: {0}, RetryCount: {1}, TimedOut: {2}, Humidity: {3}, Temperature: {4}", reading.IsValid, reading.RetryCount, reading.TimedOut, reading.Humidity, reading.Temperature)); } this.OnPropertyChanged(nameof(LastUpdatedDisplay)); // show when the data was last updated this.OnPropertyChanged(nameof(DateTimeDisplay)); } catch (Exception ex) // log any exception that occurs { Debug.WriteLine(ex.Message); } }
private async Task ReadTempHumidity() { if (dhtlock == null) { InitDHT22(); } Reading = new DhtReading(); int val = this.TotalAttempts; this.TotalAttempts++; Reading = await _dht.GetReadingAsync().AsTask(); _retryCount.Add(Reading.RetryCount); if (Reading.IsValid) { this.TotalSuccess++; this.Temperature = Convert.ToSingle(Reading.Temperature); this.Humidity = Convert.ToSingle(Reading.Humidity); this.LastUpdated = DateTimeOffset.Now; } }
private async void _timer_Tick(object sender, object e) { float temperature; DhtReading reading = new DhtReading(); totalAttempts++; reading = await _dht.GetReadingAsync().AsTask(); _retryCount.Add(reading.RetryCount); if (reading.IsValid) { totalSuccess++; temperature = Convert.ToSingle(reading.Temperature); var telemetryDataPoint = new { messageId = totalSuccess, deviceId = deviceName, temperature = temperature, humidity = Convert.ToSingle(reading.Humidity), lastUpdated = DateTimeOffset.Now, successRate = this.SuccessRate, percentSuccess = this.PercentSuccess }; var messageString = JsonConvert.SerializeObject(telemetryDataPoint); var message = new Message(Encoding.ASCII.GetBytes(messageString)); message.Properties.Add("temperatureAlert", (temperature > 24) ? "true" : "false"); await deviceClient.SendEventAsync(message); } }
private async Task ReadData() { string status = ""; double avgTemp = 0; double avgHum = 0; double avgPres = 0; int tempCount = 0; int humCount = 0; int presCount = 0; Log.Info("ReadData"); // Read and format Sensor data try { if (bmp180 != null) { var sensorData = await bmp180.GetSensorDataAsync(Bmp180AccuracyMode.UltraHighResolution); detailData.Bmp180Temperature = sensorData.Temperature; detailData.Bmp180Pressure = sensorData.Pressure; avgTemp += sensorData.Temperature; ++tempCount; avgPres += sensorData.Pressure; ++presCount; Log.Debug($"BMP180: {detailData.Bmp180Temperature},N/A,{detailData.Bmp180Pressure}"); } } catch (Exception ex) { status = "Bmp180 Error: " + ex.Message; Log.Error("Bmp180 read Error", ex); detailData.Bmp180Temperature = double.NaN; detailData.Bmp180Pressure = double.NaN; } try { if (bme280 != null) { if (bme280ForceMode) { await bme280.TakeForcedMeasurement(); } // Read Temperature detailData.Bme280Temperature = await bme280.ReadTemperature(); avgTemp += detailData.Bme280Temperature; ++tempCount; // Read Humidity detailData.Bme280Humidity = await bme280.ReadHumidity(); avgHum += detailData.Bme280Humidity; ++humCount; // Read Barometric Pressure detailData.Bme280Pressure = await bme280.ReadPressure() / 100.0; avgPres += detailData.Bme280Pressure; ++presCount; Log.Debug($"BME280: {detailData.Bme280Temperature},{detailData.Bme280Humidity},{detailData.Bme280Pressure}"); } } catch (Exception ex) { status = "Bme280 Error: " + ex.Message; Log.Error("Bme280 read Error", ex); detailData.Bme280Temperature = double.NaN; detailData.Bme280Humidity = double.NaN; detailData.Bme280Pressure = double.NaN; } try { if (dhtSensor != null) { DhtReading reading = new DhtReading(); reading = await dhtSensor.GetReadingAsync(); if (reading.IsValid) { // *** // *** Get the values from the reading. // *** detailData.DhtTemperature = reading.Temperature; detailData.DhtHumidity = reading.Humidity; avgTemp += reading.Temperature; ++tempCount; avgHum += reading.Humidity; ++humCount; Log.Debug($"DHT22: {detailData.DhtTemperature},{detailData.DhtHumidity},N/A,"); } else { Log.Debug($"DHT22: invalid reading"); } } } catch (Exception ex) { status = "DHT22 Error: " + ex.Message; Log.Error("DHT22 read Error", ex); detailData.DhtTemperature = double.NaN; detailData.DhtHumidity = double.NaN; } //calculate average avgTemp /= tempCount; avgHum /= humCount; avgPres /= presCount; basicData.CurrentTemperature = avgTemp; basicData.CurrentHumidity = avgHum; basicData.CurrentPressure = avgPres; if (status.Length > 0) { OnStatusChanged(status); } }