Make sure we don't read the DHT11 within 2 seconds.

Mon, 11 Aug 2014 19:17:32 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 11 Aug 2014 19:17:32 +0200
changeset 220
382938c5fce2
parent 219
ae720212accc
child 221
91a5e7281c35

Make sure we don't read the DHT11 within 2 seconds.

thermferm/devices.c file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Mon Aug 11 18:38:57 2014 +0200
+++ b/thermferm/devices.c	Mon Aug 11 19:17:32 2014 +0200
@@ -40,6 +40,7 @@
 int             dht11_temperature = -1;
 int             dht11_humidity = -1;
 int             dht11_valid = FALSE;
+time_t		dht11_last = (time_t)0;
 
 
 static uint8_t sizecvt(const int read_value) {
@@ -405,6 +406,7 @@
     char		*addr = NULL, line[60], *p = NULL;
     FILE		*fp;
     int			temp, rc;
+    time_t		now;
 
     syslog(LOG_NOTICE, "Thread my_devices_loop started");
 
@@ -469,9 +471,16 @@
 
 #ifdef HAVE_WIRINGPI_H
 		case DEVTYPE_DHT:
-			if (device->subdevice == 0) {
+			/*
+			 * Make sure we don't read the sensor withing 2 seconds.
+			 */
+			now = time(NULL);
+			if ((int)(now - dht11_last) > 2) {
 			    dht11_pin = device->gpiopin;
 			    dht11Read();
+			    dht11_last = now;
+			}
+			if (device->subdevice == 0) {
 			    if (dht11_valid) {
 				device->value = dht11_temperature * 1000;
 				device->timestamp = time(NULL);

mercurial