private static void Dht(DhtBase dht) { while (!Console.KeyAvailable) { var temp = dht.Temperature; var hum = dht.Humidity; // You can only display temperature and humidity if the read is successful otherwise, this will raise an exception as // both temperature and humidity are NAN if (dht.IsLastReadSuccessful) { Console.WriteLine($"Temperature: {temp.DegreesCelsius}\u00B0C, Relative humidity: {hum.Percent}%"); // WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity. Console.WriteLine( $"Heat index: {WeatherHelper.CalculateHeatIndex(temp, hum).DegreesCelsius:0.#}\u00B0C"); Console.WriteLine( $"Dew point: {WeatherHelper.CalculateDewPoint(temp, hum).DegreesCelsius:0.#}\u00B0C"); } else { Console.WriteLine("Error reading DHT sensor"); } // You must wait some time before trying to read the next value Thread.Sleep(2000); } }
public override bool Configure(string jsonDeviceConfiguration) { var config = DeserializeDeviceConfig <DhtConfiguration>(jsonDeviceConfiguration); _dht = GetDhtInstance(config); return(_dht != null); }
private Measure Read(DhtBase sensor) { var measure = new Measure(); var temperature = (float)sensor.Temperature.Celsius; var humidity = sensor.Humidity; if (sensor.IsLastReadSuccessful && humidity >= 0.0f && humidity <= 100.0f && temperature > -100.0f && temperature < 150.0f) { measure.DateTime = DateTime.Now; measure.Temperature = temperature; measure.Humidity = double.IsNaN(humidity) ? null : (float?)humidity; } return(measure); }
//https://github.com/adafruit/Adafruit_Python_DHT/blob/a609d7dcfb2b8208b88498c54a5c099e55159636/Adafruit_DHT/common.py //def read_retry(sensor, pin, retries=15, delay_seconds=2, platform=None) static Measure ReadRetry(DhtBase sensor, int retries = 15) { var measure = new Measure(); for (var i = 0; i < retries; i++) { var temperature = (float)sensor.Temperature.Celsius; var humidity = sensor.Humidity; if (sensor.IsLastReadSuccessful && humidity >= 0.0f && humidity <= 100.0f && temperature > -100.0f && temperature < 150.0f) { measure.DateTime = DateTime.Now; measure.Temperature = temperature; measure.Humidity = double.IsNaN(humidity) ? null : (float?)humidity; break; } Thread.Sleep(DELAY); } return(measure); }
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup <Startup>(); webBuilder.UseUrls("http://*:80"); }) .UseSystemd() .ConfigureServices((hostContext, services) => { DhtBase sensor = null; if (!args.Contains("--fake")) { sensor = new Dht22(2); } services.AddSingleton(x => new TemperatureSensor(sensor)); services.AddSingleton <TemperatureService>(); services.AddHostedService <TemperatureServiceWorker>(); services.AddHostedService <TemperatureWorker>(); });
public TemperatureSensor(DhtBase sensor) { _sensor = sensor; }
public void Dispose() { _dht?.Dispose(); _dht = null; }