# HG changeset patch # User Michiel Broek # Date 1711104515 -3600 # Node ID 586d376ab629e5a9659e399f39dff3690342381c # Parent 5cb83127d2855eeceee52ce6859b0f3c2f0974ab Updates for DHT11 reading diff -r 5cb83127d285 -r 586d376ab629 thermferm/devices.c --- a/thermferm/devices.c Sat Mar 16 11:54:46 2024 +0100 +++ b/thermferm/devices.c Fri Mar 22 11:48:35 2024 +0100 @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (C) 2014..2019 + * Copyright (C) 2014..2024 * * Michiel Broek * @@ -43,6 +43,13 @@ #define MAXTIMINGS 100 +/* + * Since kernel version 4 there is a module, and a dtoverlay so that the + * temperature and humidity can simply read from the /sys filesystem. + * But this isn't as reliable as this userspace function, so we leave + * that here. + */ + int dht11_pin = -1; int dht11_temperature = -1; int dht11_humidity = -1; @@ -929,10 +936,10 @@ */ now = time(NULL); if ((int)(now - dht11_last) > 30) { - dht11_pin = device->gpiopin; - dht11Read(); - dht11_last = now; if (device->subdevice == 0) { + /* Read once during subdevice 0 */ + dht11_pin = device->gpiopin; + dht11Read(); piLock(LOCK_DEVICES); if (dht11_valid) { device->value = dht11_temperature * 1000; @@ -943,6 +950,7 @@ } piUnlock(LOCK_DEVICES); } else if (device->subdevice == 1) { + /* Data already present */ piLock(LOCK_DEVICES); if (dht11_valid) { device->value = dht11_humidity * 1000; @@ -952,6 +960,7 @@ device->present = DEVPRESENT_ERROR; } piUnlock(LOCK_DEVICES); + dht11_last = now; /* Okay for a new reading. */ } } break;