Don't block with a missing DHT11 sensor

Tue, 13 May 2014 16:46:36 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 13 May 2014 16:46:36 +0200
changeset 36
8701b4dab77b
parent 35
f3c5ae78b746
child 37
2e9b7c1be9a6

Don't block with a missing DHT11 sensor

lib/dht11.c file | annotate | diff | comparison | revisions
--- a/lib/dht11.c	Tue May 13 16:33:47 2014 +0200
+++ b/lib/dht11.c	Tue May 13 16:46:36 2014 +0200
@@ -120,17 +120,20 @@
 	if ((counter == 255) && (j == 0)) {
 	    if (dht11_temperature != -1) {
 		syslog(LOG_NOTICE, "dht11 sensor disappeared");
+	    } else {
+		syslog(LOG_NOTICE, "dht11 sensor not present");
 	    }
 	    dht11_temperature = -1;
 	    dht11_humidity = -1;
 	    dht11_valid = false;
-	} else {
+	    return;
+	} 
 
-	    /*
-	     * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
-	     * print it out if data is good
-	     */
-	    if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) {
+	/*
+	 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
+	 * print it out if data is good
+	 */
+	if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) {
 		got_correct_data = 1;
 
 		int h = dht11_dat[0] + dht11_dat[1];
@@ -144,12 +147,11 @@
 		dht11_temperature = t;
 		dht11_humidity = h;
 		dht11_valid = true;
-	    } else {
+	} else {
 		tries--;
 		if (tries == 0)
 		    syslog(LOG_INFO, "dht11 data checksum was wrong 5 times");
 		usleep(100000);
-	    }
 	}
     }
 }

mercurial