Changed 1-wire logic a little bit

Tue, 25 Jul 2017 21:08:26 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 25 Jul 2017 21:08:26 +0200
changeset 528
908ef02d5d22
parent 527
afd13a58c8c8
child 529
00b45f2aaac3

Changed 1-wire logic a little bit

thermferm/devices.c file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Tue Jul 25 17:39:42 2017 +0200
+++ b/thermferm/devices.c	Tue Jul 25 21:08:26 2017 +0200
@@ -699,7 +699,7 @@
 #ifdef USE_SIMULATOR
     simulator_list	*simulator;
 #endif
-    char		*addr = NULL, line[60], *p = NULL;
+    char		*addr = NULL, line1[60], line2[60], *p = NULL;
     FILE		*fp;
     int			temp, rc;
 #ifdef HAVE_WIRINGPI_H
@@ -741,42 +741,55 @@
 			    addr = xstrcat(addr, device->address);
 			    addr = xstrcat(addr, (char *)"/w1_slave");
 			    if ((fp = fopen(addr, "r"))) {
-				if (device->present != DEVPRESENT_YES) {
-				     syslog(LOG_NOTICE, "sensor %s is back", device->address);
-#ifdef HAVE_WIRINGPI_H
-				     piLock(LOCK_DEVICES);
-#endif
-				     device->present = DEVPRESENT_YES;
-#ifdef HAVE_WIRINGPI_H
-				     piUnlock(LOCK_DEVICES);
-#endif
-				}
+				fgets(line1, 50, fp);	// Read 2 lines
+				fgets(line2, 50, fp);
+				fclose(fp);
+//				if (device->present != DEVPRESENT_YES) {
+//				     syslog(LOG_NOTICE, "sensor %s is back", device->address);
+//#ifdef HAVE_WIRINGPI_H
+//				     piLock(LOCK_DEVICES);
+//#endif
+//				     device->present = DEVPRESENT_YES;
+//#ifdef HAVE_WIRINGPI_H
+//				     piUnlock(LOCK_DEVICES);
+//#endif
+//				}
 				/*
 				 * 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')) {
+				line1[strlen(line1)-1] = '\0';
+				if ((line1[36] == 'Y') && (line1[37] == 'E')) {
 				    /* CRC is Ok, continue */
-				    fgets(line, 50, fp);
-				    line[strlen(line)-1] = '\0';
-				    strtok(line, (char *)"=");
+				    if (device->present != DEVPRESENT_YES) {
+					syslog(LOG_NOTICE, "sensor %s is Ok", device->address);
+#ifdef HAVE_WIRINGPI_H
+					piLock(LOCK_DEVICES);
+#endif
+					device->present = DEVPRESENT_YES;
+#ifdef HAVE_WIRINGPI_H
+					piUnlock(LOCK_DEVICES);
+#endif
+				    }
+
+				    line2[strlen(line2)-1] = '\0';
+				    strtok(line2, (char *)"=");
 				    p = strtok(NULL, (char *)"=");
 				    rc = sscanf(p, "%d", &temp);
+				    if ((rc == 1) && (device->value != temp)) {
 #ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);
+					piLock(LOCK_DEVICES);
 #endif
-				    if ((rc == 1) && (device->value != temp)) {
 					device->value = temp;
 				    	device->timestamp = time(NULL);
-				    }
+					device->present = DEVPRESENT_YES;
 #ifdef HAVE_WIRINGPI_H
-				    piUnlock(LOCK_DEVICES);
+				    	piUnlock(LOCK_DEVICES);
 #endif
+				    }
 				} else {
-				    syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line);
+				    syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1);
 #ifdef HAVE_WIRINGPI_H
 				    piLock(LOCK_DEVICES);                
 #endif
@@ -785,7 +798,6 @@
 				    piUnlock(LOCK_DEVICES);              
 #endif
 				}
-				fclose(fp);
 			    } else {
 				if (device->present != DEVPRESENT_NO) {
 				    syslog(LOG_NOTICE, "sensor %s is missing", device->address);

mercurial