Updates for DHT11 reading

Fri, 22 Mar 2024 11:48:35 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 22 Mar 2024 11:48:35 +0100
changeset 643
586d376ab629
parent 642
5cb83127d285
child 644
07cc86900473

Updates for DHT11 reading

thermferm/devices.c file | annotate | diff | comparison | revisions
--- 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 <mbroek at mbse dot eu>
  *
@@ -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;

mercurial