static void Main(string[] args) { Console.WriteLine("Hello DHT!"); DHTSensor dht = new DHTSensor(26, DhtType.Dht22); while (true) { // You have 2 ways to read the data, both are equivalent // First way to read the data bool readret = dht.ReadData(); if (readret) { Console.WriteLine($"Temperature: {dht.Temperature.Celsius.ToString("0.00")} °C, Humidity: {dht.Humidity.ToString("0.00")} %"); } else { Console.WriteLine("Error reading the sensor"); } Thread.Sleep(1000); // Second way to read the data Temperature Temp; double Hum; if (dht.TryGetTemperatureAndHumidity(out Temp, out Hum)) { Console.WriteLine($"Temperature: {Temp.Celsius.ToString("0.00")} °C, Humidity: {Hum.ToString("0.00")} %"); } else { Console.WriteLine("Error reading the sensor"); } Thread.Sleep(1000); } }
public override double Measure(SensorType type) { _logger.Info(String.Format("TemperatureHumiditySensor.Measure ", type)); switch (type) { case SensorType.Temperature: _logger.Info(String.Format("TemperatureHumiditySensor.Measure Temperature")); return(_sensor.ReadData().TempCelcius); case SensorType.Humidity: _logger.Info(String.Format("TemperatureHumiditySensor.Measure Humidity")); return(_sensor.ReadData().Humidity); default: throw new NotImplementedException(); } }
/// <summary> /// Measures the value of the sensor /// </summary> /// <returns></returns> public override double Measure() { _logger.Info("HumiditySensor.Measure"); var measured = false; double data = 0; while (!measured) { try { data = _sensor.ReadData().Humidity; measured = true; } catch { Thread.Sleep(2000); } } return(data); }