public async Task <Humiture> GetHumitureAsync(int maxRetries = DEFAULT_MAX_RETRIES) { var reading = await dht11.GetReadingAsync(DEFAULT_MAX_RETRIES); var returnValue = new Humiture(reading); Debug.WriteLine("Temperature: " + returnValue.Temperature); Debug.WriteLine("Humidity: " + returnValue.Humidity); return(returnValue); }
public async Task <SensorReadingWrapper <DhtReading> > ReadAsync() { var readingData = new SensorReadingWrapper <DhtReading>(d => d.IsValid); var controller = GpioController.GetDefault(); using (GpioPin pin = controller.OpenPin(4, GpioSharingMode.Exclusive)) { using (Dht11 dht11 = new Dht11(pin, GpioPinDriveMode.Input)) { readingData.Data = await dht11.GetReadingAsync().AsTask(); } } return(readingData); }
private async void startDHT11(int port) { int realport = 5; GpioPin pin = null; pin = GpioController.GetDefault().OpenPin(realport, GpioSharingMode.Exclusive); Dht11 _dht = new Dht11(pin, GpioPinDriveMode.Input); pins.Add(pin); DhtReading reading = new DhtReading(); reading = await _dht.GetReadingAsync().AsTask(); callURLAPI(1, reading.Temperature); callURLAPI(2, reading.Humidity); }
private async void Timer_Tick(object sender) { try { DhtReading reading = new DhtReading(); reading = await _dht11.GetReadingAsync(3).AsTask(); if (reading.IsValid) { Console.WriteLine(reading.Temperature); } } catch (Exception e) { Console.WriteLine(e); throw; } }
// takes an instant reading or returns the average accumulated value, and returns // the interpreted value adjusted by calibration settings. public async Task <Models.Measurement> GetTemperature() { Measurement result = _lastMeasurement; try { if (_pin == null || _dht11 == null) { int pinNumber = TEMPERATUREDATAPIN; // default if (CalibrationSettings.ContainsKey(TempGpioPinNumberSetting)) { pinNumber = Int32.Parse(CalibrationSettings[TempGpioPinNumberSetting].ToString()); } var c = GpioController.GetDefault(); if (c != null) { _pin = c.OpenPin(pinNumber, GpioSharingMode.Exclusive); if (_pin != null) { _dht11 = new Dht11(_pin, GpioPinDriveMode.Input); } } } if (_dht11 == null) { return(null); } DhtReading reading = await _dht11.GetReadingAsync().AsTask(); for (int retries = 0; retries < 10; retries++) { if (reading.TimedOut) { Debug.Write("."); } else if (!reading.IsValid) { Debug.Write("x"); } else { break; } } if (reading.IsValid) { //Debug.WriteLine($"Temp reading = {reading.Temperature}"); Measurement.UnitsOfMeasure units = Measurement.UnitsOfMeasure.Fahrenheit; if (CalibrationSettings.ContainsKey(PreferredTemperatureUnits)) { units = (Measurement.UnitsOfMeasure)Enum.Parse(typeof(Measurement.UnitsOfMeasure), CalibrationSettings[PreferredTemperatureUnits].ToString()); } Measurement adjust = new Measurement(0.0f, Measurement.UnitsOfMeasure.Fahrenheit); if (CalibrationSettings.ContainsKey(AdjustTemperatureSetting)) { adjust = CalibrationSettings[AdjustTemperatureSetting] as Measurement; } float temperature = (float)reading.Temperature; temperature = Convert(Measurement.UnitsOfMeasure.Celsius, units, temperature); adjust.Amount = Convert(adjust.Units, units, adjust.Amount); adjust.Units = units; temperature += adjust.Amount; // now that we know they are in the same (preferred) units result = new Measurement(temperature, units); _lastMeasurement = result; OnTemperatureChanged(new MeasurementChangedEventArgs(result)); } else { KegLogger.KegLogException(new TemperatureException("Custom: Unable to Read temperature."), "GetTemperature", SeverityLevel.Warning); Debug.WriteLine($"Unable to read temperature."); } } catch (Exception ex) { Debug.WriteLine("Error, " + ex.Message); KegLogger.KegLogException(ex, "GetTemperature", SeverityLevel.Critical); } return(result); }