thermometers/main.c

changeset 16
f4cbe008da72
parent 15
01fec4ddad17
child 18
3f4823083b9d
--- a/thermometers/main.c	Tue Apr 29 20:57:16 2014 +0200
+++ b/thermometers/main.c	Fri May 02 21:04:20 2014 +0200
@@ -42,6 +42,7 @@
 
 extern bool		debug;
 extern sys_config	Config;
+extern int		lcdHandle;
 
 int server(void);
 void help(void);
@@ -122,10 +123,19 @@
 
 
 
+void stopLCD(void)
+{
+   lcdClear(lcdHandle);
+   setBacklight(0);
+}
+
+
+
 int main(int argc, char *argv[])
 {
     int		rc, c, i;
     pid_t	frk;
+    char	buf[80];
 
     while (1) {
 	int option_index = 0;
@@ -168,6 +178,16 @@
 	    signal(i, (void (*))die);
     }
 
+    if ((rc = initLCD (16, 2))) {
+	fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
+	return 1;
+    }
+
+    lcdPosition(lcdHandle, 0, 0);
+    lcdPuts(lcdHandle, "Thermometers");
+    lcdPosition(lcdHandle, 0, 1);
+    sprintf(buf, "Version %s", VERSION);
+    lcdPuts(lcdHandle, buf);
 
     if (debug) {
 	/*
@@ -193,6 +213,7 @@
 	    case -1:	
 		    	syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
 			syslog(LOG_NOTICE, "Finished, rc=1");
+			stopLCD();
 			exit(1);
 	    case 0:	/*
 			 * Run the daemon
@@ -237,7 +258,7 @@
     char                *id = NULL, *state = NULL;
     struct mosquitto    *mosq = NULL;
     char                hostname[256], buf[1024];
-    int                 temp, rc, deviation, keepalive = 60;
+    int                 temp, rc, deviation, keepalive = 60, lcdupdate;
     unsigned int        max_inflight = 20;
     char                err[1024];
     w1_therm		*tmp1, *old1;
@@ -357,6 +378,8 @@
 
     do {
 	if (status == STATUS_CONNACK_RECVD) {
+	    lcdupdate = FALSE;
+
 	    /*
 	     * Here send our 1-wire sensors values
 	     */
@@ -406,8 +429,9 @@
 			     * value with the previous one. If the difference is too
 			     * much, we don't send that value. That also means that if
 			     * the next value is ok again, it will be marked invalid too.
+			     * Maximum error is 20 degrees for now.
 			     */
-			    deviation = (temp + tmp1->lastval) / 10;
+			    deviation = 20000;
 			    if ((tmp1->lastval == 0) ||
 				(tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) {
 			    	/*
@@ -421,10 +445,13 @@
 				    	syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
 			    	}
 			    } else {
-				if (debug)
-				    syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp);
+				syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp);
+				if (debug) {
+				    fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp);
+				}
 			    }
 			    tmp1->lastval = temp;
+			    lcdupdate = TRUE;
 			}
 		    } else {
 			syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name);
@@ -442,7 +469,18 @@
 		free(alias);
 		alias = NULL;
 	    }
-	    usleep(100000);
+
+	    if (lcdupdate) {
+		lcdPosition(lcdHandle, 0, 0);
+		tmp1 = Config.w1therms;
+		snprintf(buf, 16, "%5.1f %cC %s            ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias);
+		lcdPuts(lcdHandle, buf);
+		old1 = tmp1->next;
+		tmp1 = old1;
+		lcdPosition(lcdHandle, 0, 1);
+		snprintf(buf, 16, "%5.1f %cC %s            ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias);
+		lcdPuts(lcdHandle, buf);
+	    }
 
 	    if (shutdown) {
 		/*
@@ -452,7 +490,13 @@
 		mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, true);
 		last_mid = mid_sent;
 		status = STATUS_WAITING;
+		lcdClear(lcdHandle);
+		lcdPosition(lcdHandle, 0, 0);
+		lcdPuts(lcdHandle, "Shuting down ...");
 	    }
+
+	    usleep(100000);
+
 	} else if (status == STATUS_WAITING) {
 	    if (debug)
 	    	fprintf(stdout, (char *)"Waiting\n");
@@ -473,6 +517,8 @@
     mosquitto_destroy(mosq);
     mosquitto_lib_cleanup();
 
+    stopLCD();
+
     return rc;
 }
 

mercurial