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)); }
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}"); } }
/// <summary> /// Gets a reading from the sensor. /// </summary> /// <returns>Returns an IDhtReading instance containing /// the data from the sensor.</returns> public async Task <IDhtReading> GetReadingAsync() { IDhtReading reading = null; for (var attempt = 0; attempt <= RetryCount; attempt++) { pin.Write(GpioPinValue.High); pin.SetDriveMode(GpioPinDriveMode.Output); if (last_success_timestamp == 0) { await Task.Delay(InitializationDelay); } else if (GetTickCount64() - last_success_timestamp < MinSampleInterval) { var delay = Math.Max(MinSampleInterval - (int)(GetTickCount64() - last_success_timestamp), ReinitializationDelay); await Task.Delay(delay); } else { await Task.Delay(ReinitializationDelay); } reading = GetReading(); if (reading.Result == DhtReadingResult.Valid) { last_success_timestamp = GetTickCount64(); break; } last_success_timestamp = 0; #if DEBUG Debug.WriteLine($"Sensor read failed: {reading.Result}, attempt {attempt}"); #endif } return(reading); }