Mosquitto init does several tries before giving up. The DHT11 sensor now uses a temperature offset too.

Tue, 13 May 2014 16:33:47 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 13 May 2014 16:33:47 +0200
changeset 35
f3c5ae78b746
parent 34
38e3e3a49320
child 36
8701b4dab77b

Mosquitto init does several tries before giving up. The DHT11 sensor now uses a temperature offset too.

coolers/mosquitto.c file | annotate | diff | comparison | revisions
dht11/dht11.c file | annotate | diff | comparison | revisions
--- a/coolers/mosquitto.c	Mon May 12 19:45:11 2014 +0200
+++ b/coolers/mosquitto.c	Tue May 13 16:33:47 2014 +0200
@@ -100,7 +100,7 @@
 {
     char                *id = NULL, *state = NULL;
     char                buf[1024];
-    int                 rc, keepalive = 60;
+    int                 try, rc, keepalive = 60;
     unsigned int        max_inflight = 20;
     char                err[1024];
     w1_therm		*tmp1, *old1;
@@ -169,13 +169,21 @@
     mosquitto_disconnect_callback_set(mymosq, my_disconnect_callback);
     mosquitto_publish_callback_set(mymosq, my_publish_callback);
 
-    if ((rc = mosquitto_connect(mymosq, Config.mosq_host, Config.mosq_port, keepalive))) {
-	if (rc == MOSQ_ERR_ERRNO) {
-	    strerror_r(errno, err, 1024);
-	    syslog(LOG_NOTICE, "mosquitto_connect: error: %s", err);
-	} else {
-	    syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc);
-	}
+    try = 10; rc = -1;
+    while (try && rc) {
+    	if ((rc = mosquitto_connect(mymosq, Config.mosq_host, Config.mosq_port, keepalive))) {
+	    if (rc == MOSQ_ERR_ERRNO) {
+	    	strerror_r(errno, err, 1024);
+	    	syslog(LOG_NOTICE, "mosquitto_connect: error: %s, try=%d", err, 11-try);
+	    } else {
+	    	syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc);
+	    }
+	    sleep(2);
+	    try--;
+    	}
+    }
+    if (rc) {
+	syslog(LOG_NOTICE, "mosquitto_connect: too many tries, giving up");
 	mosquitto_lib_cleanup();
 	return rc;
     }
--- a/dht11/dht11.c	Mon May 12 19:45:11 2014 +0200
+++ b/dht11/dht11.c	Tue May 13 16:33:47 2014 +0200
@@ -36,7 +36,7 @@
     if (wiringPiSetup() == -1)
 	return 0;
 
-    dht11Init(PIN, 0, 6);
+    dht11Init(PIN, 1, 6);
     dht11Read();
     if (dht11_valid) {
 	fprintf(stdout, "DHT11: temperature %d degrees, humidity %d%%\n", dht11_temperature, dht11_humidity);

mercurial