Sensor reading is done in a thread

Tue, 06 May 2014 13:24:25 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 06 May 2014 13:24:25 +0200
changeset 27
4703cc10b99a
parent 26
9322c619c525
child 28
32ed1ea4d0b6

Sensor reading is done in a thread

Makefile.global.in file | annotate | diff | comparison | revisions
coolers/Makefile file | annotate | diff | comparison | revisions
coolers/coolers.c file | annotate | diff | comparison | revisions
coolers/mosquitto.c file | annotate | diff | comparison | revisions
coolers/sensors.c file | annotate | diff | comparison | revisions
coolers/sensors.h file | annotate | diff | comparison | revisions
--- a/Makefile.global.in	Mon May 05 23:33:31 2014 +0200
+++ b/Makefile.global.in	Tue May 06 13:24:25 2014 +0200
@@ -19,7 +19,7 @@
 TAR		= @TAR@
 RANLIB		= @RANLIB@
 
-CFLAGS		= @CFLAGS@ -DETCDIR=@prefix@/etc -DVARDIR=@prefix@/var
+CFLAGS		= @CFLAGS@ -DVARDIR=@prefix@/var
 CXXFLAGS	= @CXXFLAGS@
 LIBS		= @LIBS@
 
--- a/coolers/Makefile	Mon May 05 23:33:31 2014 +0200
+++ b/coolers/Makefile	Tue May 06 13:24:25 2014 +0200
@@ -5,7 +5,7 @@
 SRCS		= coolers.c mosquitto.c sensors.c
 HDRS		= coolers.h mosquitto.h sensors.h
 OBJS		= coolers.o mosquitto.o sensors.o
-SLIBS		= ../lib/libmbse.a
+SLIBS		= -lpthread ../lib/libmbse.a
 TARGET		= coolers
 OTHER		= Makefile
 
--- a/coolers/coolers.c	Mon May 05 23:33:31 2014 +0200
+++ b/coolers/coolers.c	Tue May 06 13:24:25 2014 +0200
@@ -202,14 +202,21 @@
 {
     char                buf[1024];
     w1_therm		*tmp1, *old1;
-    int			run = 0;
+    int			rc, run = 0;
 
     my_mosquitto_init();
 
+    rc = piThreadCreate (my_sensors_loop);
+    if (rc) {
+	fprintf(stderr, "my_sensors_loop thread didn't start\n");
+    } else {
+	if (debug)
+	    fprintf(stdout, "Thread my_sensors_loop started\n");
+    }
+
     do {
 	lcdupdate = FALSE;
 
-	my_sensors_loop();
 	run = my_mosquitto_loop();
 
 	if (run && lcdupdate) {
--- a/coolers/mosquitto.c	Mon May 05 23:33:31 2014 +0200
+++ b/coolers/mosquitto.c	Tue May 06 13:24:25 2014 +0200
@@ -276,6 +276,7 @@
 			    syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
 		    }
 		    tmp1->update = FALSE;
+		    lcdupdate = TRUE;
 		} 
 
 		free(alias);
--- a/coolers/sensors.c	Mon May 05 23:33:31 2014 +0200
+++ b/coolers/sensors.c	Tue May 06 13:24:25 2014 +0200
@@ -26,94 +26,107 @@
 #ifdef HAVE_WIRINGPI_H
 
 
-extern char		*myhostname;
 extern bool		debug;
 extern sys_config	Config;
-extern int		lcdHandle;
-extern int		lcdupdate;
+extern int		shutdown;
 
 
 
-void my_sensors_loop(void)
+PI_THREAD (my_sensors_loop)
 {
     w1_therm		*tmp1, *old1;
     char		*device, line[60], *p = NULL;
     FILE		*fp;
     int			temp, rc, deviation;
 
+    syslog(LOG_NOTICE, "Thread my_sensors_loop started");
+    if (debug)
+	fprintf(stdout, "Thread my_sensors_loop started\n");
+
     /*
-     * Here send our 1-wire sensors values
+     * Loop forever until the external shutdown variable is set.
      */
-    for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
-	old1 = tmp1->next;
+    for (;;) {
+    	/*
+    	 * Here send our 1-wire sensors values
+    	 */
+    	for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
+	    old1 = tmp1->next;
+
+	    if (shutdown) {
+		syslog(LOG_NOTICE, "Thread my_sensors_loop stopped");
+		if (debug)
+		    fprintf(stdout, "Thread my_sensors_loop stopped\n");
+	    	return 0;
+	    }
 
-	/*
-	 * Build path and alias topic
-	 */
-	device = xstrcpy((char *)"/sys/bus/w1/devices/");
-	device = xstrcat(device, tmp1->master);
-	device = xstrcat(device, (char *)"/");
-	device = xstrcat(device, tmp1->name);
-	device = xstrcat(device, (char *)"/w1_slave");
+	    /*
+	     * Build path to the on-wire sensor
+	     */
+	    device = xstrcpy((char *)"/sys/bus/w1/devices/");
+	    device = xstrcat(device, tmp1->master);
+	    device = xstrcat(device, (char *)"/");
+	    device = xstrcat(device, tmp1->name);
+	    device = xstrcat(device, (char *)"/w1_slave");
 
-	/*
-	 * Read sensor data
-	 */
-	if ((fp = fopen(device, "r"))) {
+	    /*
+	     * Read sensor data
+	     */
+	    if ((fp = fopen(device, "r"))) {
+		/*
+		 * The output looks like:
+		 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
+		 * 72 01 4b 46 7f ff 0e 10 57 t=23125
+		 */
+		fgets(line, 50, fp);
+		line[strlen(line)-1] = '\0';
+		if ((line[36] == 'Y') && (line[37] == 'E')) {
 		    /*
-		     * The output looks like:
-		     * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
-		     * 72 01 4b 46 7f ff 0e 10 57 t=23125
+		     * CRC is Ok, continue
 		     */
 		    fgets(line, 50, fp);
 		    line[strlen(line)-1] = '\0';
-		    if ((line[36] == 'Y') && (line[37] == 'E')) {
+		    strtok(line, (char *)"=");
+		    p = strtok(NULL, (char *)"=");
+		    rc = sscanf(p, "%d", &temp);
+		    if ((rc == 1) && (tmp1->lastval != temp)) {
 			/*
-			 * CRC is Ok, continue
+			 * It is possible to have read errors or extreme values.
+			 * This can happen with bad connections so we compare the
+			 * 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.
 			 */
-		    	fgets(line, 50, fp);
-			line[strlen(line)-1] = '\0';
-			strtok(line, (char *)"=");
-			p = strtok(NULL, (char *)"=");
-			rc = sscanf(p, "%d", &temp);
-			if ((rc == 1) && (tmp1->lastval != temp)) {
+			deviation = 20000;
+			if ( (tmp1->lastval == 0) ||
+			     (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation))) ) {
 			    /*
-			     * It is possible to have read errors or extreme values.
-			     * This can happen with bad connections so we compare the
-			     * 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.
+			     * Temperature is changed and valid, set flag.
 			     */
-			    deviation = 20000;
-			    if ((tmp1->lastval == 0) ||
-				(tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) {
-			    	/*
-			    	 * Temperature is changed and valid, update and publish this.
-			    	 */
-				tmp1->update = TRUE;
-			    } else {
-				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->update = TRUE;
+			} else {
+			    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);
+			tmp1->lastval = temp;
 		    }
-		    fclose(fp);
-		    tmp1->present = 1;
-	} else {
-		    tmp1->present = 0;
-		    if (debug)
-		    	printf("sensor %s is missing\n", tmp1->name);
-	}
+		} else {
+		    syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name);
+		}
+		fclose(fp);
+		tmp1->present = 1;
+	    } else {
+		tmp1->present = 0;
+		if (debug)
+		    printf("sensor %s is missing\n", tmp1->name);
+	    }
 
-	free(device);
-	device = NULL;
+	    free(device);
+	    device = NULL;
+    	}
     }
 }
 
--- a/coolers/sensors.h	Mon May 05 23:33:31 2014 +0200
+++ b/coolers/sensors.h	Tue May 06 13:24:25 2014 +0200
@@ -1,6 +1,10 @@
 #ifndef	MY_SENSORS_H
 #define MY_SENSORS_H
 
-void my_sensors_loop(void);
+#ifdef HAVE_WIRINGPI_H
+
+PI_THREAD (my_sensors_loop);
 
 #endif
+
+#endif

mercurial