Version 0.9.20b1. Removed debug logging from DHT11 reading. Fixed reading one-wire temperature sensors state when inserted or removed from the bus. Automatic backup device selection works now.

Sat, 06 Jul 2024 14:52:49 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 06 Jul 2024 14:52:49 +0200
changeset 733
16716484cbe1
parent 732
b0e99e30a008
child 734
58df0c367530

Version 0.9.20b1. Removed debug logging from DHT11 reading. Fixed reading one-wire temperature sensors state when inserted or removed from the bus. Automatic backup device selection works now.

configure file | annotate | diff | comparison | revisions
configure.ac file | annotate | diff | comparison | revisions
thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/one-wire.c file | annotate | diff | comparison | revisions
thermferm/websocket.c file | annotate | diff | comparison | revisions
--- a/configure	Tue May 07 14:11:31 2024 +0200
+++ b/configure	Sat Jul 06 14:52:49 2024 +0200
@@ -2104,7 +2104,7 @@
 
 
 PACKAGE="mbsePi-apps"
-VERSION="0.9.20a2"
+VERSION="0.9.20b1"
 COPYRIGHT="Copyright (C) 2014-2024 Michiel Broek, All Rights Reserved"
 CYEARS="2014-2024"
 
--- a/configure.ac	Tue May 07 14:11:31 2024 +0200
+++ b/configure.ac	Sat Jul 06 14:52:49 2024 +0200
@@ -8,7 +8,7 @@
 dnl General settings
 dnl After changeing the version number, run autoconf!
 PACKAGE="mbsePi-apps"
-VERSION="0.9.20a2"
+VERSION="0.9.20b1"
 COPYRIGHT="Copyright (C) 2014-2024 Michiel Broek, All Rights Reserved"
 CYEARS="2014-2024"
 AC_SUBST(PACKAGE)
--- a/thermferm/devices.c	Tue May 07 14:11:31 2024 +0200
+++ b/thermferm/devices.c	Sat Jul 06 14:52:49 2024 +0200
@@ -64,12 +64,11 @@
 void dht11Read(char *address)
 {
     int        	temp, hum;
-    int		fd, rc, err, oldstate;
+    int		fd, rc, err;
     char	buffer[25], *dhtpath = NULL;
 
     dht11_temperature = -1;
     dht11_humidity = -1;
-    oldstate = dht11_state;
     dhtpath = xstrcpy((char *)"/sys/bus/iio/devices/");
     dhtpath = xstrcat(dhtpath, address);
     dhtpath = xstrcat(dhtpath, (char *)"/in_temp_input");
@@ -94,7 +93,7 @@
 	} else {
 		dht11_state = DEVPRESENT_ERROR;
 	}
-	syslog(LOG_NOTICE, "DHT11 read temperature: %s", strerror(err));
+//	syslog(LOG_NOTICE, "DHT11 read temperature: %s", strerror(err));
     } else {
 	sscanf(buffer, "%d", &temp);
 	dht11_temperature = temp;
@@ -132,9 +131,6 @@
 	free(dhtpath);
 	dhtpath = NULL;
     }
-
-    if (oldstate != dht11_state)
-	syslog(LOG_NOTICE, "dht11 t:%d h:%d state:%d", dht11_temperature, dht11_humidity, dht11_state);
 }
 
 
@@ -249,8 +245,6 @@
 	return 0;
 
     now = time(NULL);
-//    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
-
     for (device = Config.devices; device; device = device->next) {
 	if (! strcmp(uuid, device->uuid)) {
 	    /*
@@ -276,16 +270,13 @@
 		    for (i = 0; i < strlen(buf); i++)
 			if (buf[i] == '-')
 			    buf[i] = ',';
-//		    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 	    	    enableTransmit(device->gpiopin);	    
 		    rc = toggleSwitch(buf);
 		    disableTransmit();
-//		    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 		    syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
                     device->value = value;
 		    device->timestamp = time(NULL);
 		    devices_ws();
-//		    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		    return rc;
 	        }
 
@@ -296,14 +287,12 @@
 		if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
 		    if (strncmp(device->address, (char *)"3a", 2) == 0) {
 //		    	syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
-//			pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			if (device->value != (value == 0) ? 0 : 1) {
 			    syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
 			    device->value = (value == 0) ? 0 : 1;
 			    device->timestamp = time(NULL);
 			    devices_ws();
 			}
-//			pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		    }
 		}
 
@@ -341,13 +330,11 @@
 		}
 #endif
 	    } else {
-//		pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		return 0;
 	    } // if ((test_value != my_value) || (((int)now - (int)my_timestamp) >= 120))
 	} // if (! strcmp(uuid, device->uuid))
     } // for (device = Config.devices; device; device = device->next)
 
-//    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
     return 0;
 }	   
 
@@ -392,6 +379,7 @@
     for (device = Config.devices; device; device = device->next) {
 	if (! strcmp(address, device->address)) {
 	    if (device->present != present) {
+		syslog(LOG_NOTICE, "device_present(%s, %s)", address, DEVPRESENT[present]);
 	    	device->present = present;
 		device->timestamp = time(NULL);
 		return 1;
@@ -495,6 +483,15 @@
 #endif
 
     /*
+     * First set all one-wire devices to not present.
+     */
+    for (device = Config.devices; device; device = device->next) {
+	if (device->type == DEVTYPE_W1) {
+	    device->present = DEVPRESENT_NO;
+	}
+    }
+
+    /*
      * Scan for 1-wire devices. These are already detected by the
      * one-wire thread that is already running. So we just check
      * these detected devices.
@@ -504,6 +501,7 @@
 	for (device = Config.devices; device; device = device->next) {
 	    if (strcmp(device->address, dev_w1->address) == 0) {
 		found = TRUE;
+		device->present = DEVPRESENT_YES;
 		break;
 	    }
 	}
@@ -886,19 +884,18 @@
 			    for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
 				if (strcmp(device->address, dev_w1->address) == 0) {
 				    found = TRUE;
-				    if ((dev_w1->value == -1) || (dev_w1->value < -55000)) {
-					if (device->present != DEVPRESENT_ERROR)
-					    syslog(LOG_NOTICE, "sensor %s value error %d, keep %d", device->address, dev_w1->value, device->value);
-					device->present = DEVPRESENT_ERROR;
-					changed = true;
-				    } else {
-					if (device->present != DEVPRESENT_YES) {
-					    syslog(LOG_NOTICE, "sensor %s value ok %d", device->address, dev_w1->value);
-					    changed = true;
-					    device->present = DEVPRESENT_YES;
-					}
+				    if (device->present == DEVPRESENT_YES) {
 					if (device->value != dev_w1->value) {
 					    changed = true;
+					    syslog(LOG_NOTICE, "Devices sensor %s value ok %d", device->address, dev_w1->value);
+					    device->value = dev_w1->value;
+					    device->timestamp = time(NULL);
+					}
+				    } else if ((device->present == DEVPRESENT_ERROR) && (dev_w1->value != -1) && (dev_w1->value >= -55000)) {
+					if (device->value != dev_w1->value) {
+					    /* Should not get here since the value is frozen */
+					    changed = true;
+					    syslog(LOG_NOTICE, "Devices sensor %s value error %d, keep %d", device->address, dev_w1->value, device->value);
 					    device->value = dev_w1->value;
 					    device->timestamp = time(NULL);
 					}
@@ -908,11 +905,9 @@
 
 			    if (found == FALSE) {
 				if (device->present != DEVPRESENT_NO) {
-				    syslog(LOG_NOTICE, "sensor %s is missing", device->address);
-//				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
+				    syslog(LOG_NOTICE, "Devices sensor %s not found", device->address);
 				    device->present = DEVPRESENT_NO;
 				    changed = true;
-//				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 			    }
 
@@ -931,7 +926,6 @@
 			    if (device->subdevice == 0) {
 				/* Read once during subdevice 0 */
 			    	dht11Read(device->address);
-//				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				device->present = dht11_state;
 			    	if (dht11_state == DEVPRESENT_YES) {
 				    if (device->value != dht11_temperature) {
@@ -940,10 +934,8 @@
 				    	device->timestamp = time(NULL);
 				    }
 			    	}
-//				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 			    } else if (device->subdevice == 1) {
 				/* Data already present, valid or not. */
-//				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				device->present = dht11_state;
 			    	if (dht11_state == DEVPRESENT_YES) {
 				    if (device->value != dht11_humidity) {
@@ -955,7 +947,6 @@
 			    	} else {
 				    dht11_next = now + 1;
 				}
-//				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 			    }
 			}
 			break;
@@ -963,14 +954,12 @@
 #ifdef HAVE_WIRINGPI_H
 		case DEVTYPE_GPIO:
 			if (device->direction == DEVDIR_IN_BIN) {
-//			    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			    if (device->value != digitalRead(device->gpiopin)) {
 				changed = true;
 			    	device->value = digitalRead(device->gpiopin);
 			    	device->offset = 0;
 			    	device->timestamp = time(NULL);
 			    }
-//			    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 			}
 			break;
 
@@ -978,7 +967,6 @@
 #ifdef USE_SIMULATOR
 		case DEVTYPE_SIM:
 			while (my_simulator_state == THREAD_PAUSE) { mDelay(20); };
-//			pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			for (simulator = Config.simulators; simulator; simulator = simulator->next) {
 			    int		val;
 
@@ -1033,7 +1021,6 @@
                                 }
 			    }
 			}
-//			pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 			break;
 #endif
 		default:
--- a/thermferm/one-wire.c	Tue May 07 14:11:31 2024 +0200
+++ b/thermferm/one-wire.c	Sat Jul 06 14:52:49 2024 +0200
@@ -142,7 +142,7 @@
     (char *)"Websocket"
 SM_EDECL
 
-    int			found, w, rc, value, conv_time;
+    int			found, w, rc, value;
     FILE		*fp;
     devices_list	*device;
     w1_list		*dev_w1, *n_w1, *cur_w1 = NULL;
@@ -182,7 +182,7 @@
 	    for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
 		if (strcmp(dev_w1->address, buffer) == 0) {
 		    found = TRUE;
-		    if (dev_w1->present != DEVPRESENT_YES) {
+		    if ((dev_w1->present == DEVPRESENT_UNDEF) || (dev_w1->present == DEVPRESENT_NO)) {
 			syslog(LOG_NOTICE, "One-wire device %s is back", buffer);
 			pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
 			dev_w1->present = DEVPRESENT_YES;
@@ -243,11 +243,11 @@
 	devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
 	devfile = xstrcat(devfile, dev_w1->address);
 	devfile = xstrcat(devfile, (char *)"/uevent");
-	if (file_exist(devfile, R_OK) && (dev_w1->present == DEVPRESENT_YES)) {
+	if (file_exist(devfile, R_OK) && ((dev_w1->present == DEVPRESENT_YES) || (dev_w1->present == DEVPRESENT_ERROR))) {
 	    /*
 	     * Gone missing
 	     */
-	    syslog(LOG_NOTICE, "One-wire device %s is missing", dev_w1->address);
+	    syslog(LOG_NOTICE, "One-wire device %s is missing from /sys/bus/w1/", dev_w1->address);
 	    pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
 	    dev_w1->present = DEVPRESENT_NO;
 	    dev_w1->timestamp = time(NULL);
@@ -306,7 +306,6 @@
 			    changed = true;
 			}
 
-//			pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			/*
 			 * Read PIOA or PIOB pin state bits
 			 */
@@ -315,7 +314,6 @@
 		    	else if (device->subdevice == 1)
 			    device->value = (state & 0x04) ? 0 : 1;
 		    	device->timestamp = time(NULL);
-//		    	pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		    }
 		    mDelay(20);
 		} else if ((strcmp(dev_w1->address, device->address) == 0) && (device->direction == DEVDIR_OUT_BIN)) {
@@ -394,21 +392,6 @@
 	    }
 
 	    /*
-	     * Set conversion wait time. Skip this??
-	     */
-	    conv_time = 760;
-	    devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
-	    devfile = xstrcat(devfile, cur_w1->address);
-	    devfile = xstrcat(devfile, (char *)"/conv_time");
-	    if ((fp = fopen(devfile, "r"))) {
-	    	if ((fgets(buffer, 25, fp))) {
-		    sscanf(buffer, "%d", &conv_time);
-	    	}
-	    	fclose(fp);
-	    }
-	    free(devfile);
-
-	    /*
 	     * Read temperature for one sensor.
 	     * Bulk read does not work for unknown reason.
 	     * It may be because there are also DS2413 devices.
@@ -417,7 +400,7 @@
 	    devfile = xstrcat(devfile, cur_w1->address);
 	    devfile = xstrcat(devfile, (char *)"/temperature");
 	    if ((fp = fopen(devfile, "r"))) {
-	    	mDelay(conv_time);
+		mDelay(750);
 	    	if ((fgets(buffer, 25, fp))) {
 		    sscanf(buffer, "%d", &value);
 		    if (cur_w1->value != value) {
@@ -427,12 +410,29 @@
 		    	    syslog(LOG_NOTICE, "One-wire device %s temperature read %d => %d", cur_w1->address, cur_w1->value, value);
 		    }
 		    cur_w1->value = value;		/* devices.c will pick this up */
+		    if (cur_w1->present != DEVPRESENT_YES) {
+			syslog(LOG_NOTICE, "One-wire device %s present to DEVPRESENT_YES", cur_w1->address);
+			changed = true;
+		    }
+		    cur_w1->present = DEVPRESENT_YES;
 	    	} else {
 		    syslog(LOG_NOTICE, "One-wire device %s temperature read error", cur_w1->address);
+		    if (cur_w1->present != DEVPRESENT_ERROR) {
+			changed = true;
+			syslog(LOG_NOTICE, "One-wire device %s present to DEVPRESENT_ERROR", cur_w1->address);
+		    }
+		    cur_w1->present = DEVPRESENT_ERROR;
 	    	}
 	    	fclose(fp);
+		device_present(cur_w1->address, cur_w1->present);
 	    } else {
 		syslog(LOG_NOTICE, "One-wire device %s open: %s", cur_w1->address, strerror(errno));
+		if (cur_w1->present != DEVPRESENT_NO) {
+		    changed = true;
+		    syslog(LOG_NOTICE, "One-wire device %s present to DEVPRESENT_NO", cur_w1->address);
+		}
+		cur_w1->present = DEVPRESENT_NO;
+		device_present(cur_w1->address, cur_w1->present);
 	    }
 	    free(devfile);
 	    devfile = NULL;
--- a/thermferm/websocket.c	Tue May 07 14:11:31 2024 +0200
+++ b/thermferm/websocket.c	Sat Jul 06 14:52:49 2024 +0200
@@ -344,7 +344,6 @@
 
 		memcpy(buf, in, len);
 		buf[len] = '\0';
-		syslog(LOG_NOTICE, "ws: received %ld bytes %s", (long)len, buf);
 		/*
 		 * These are send by bmsapp to bmsd. Then bmsd resends these via MQTT.
 		 * Do we want to change that? Or use it for the new web pages.
@@ -352,14 +351,15 @@
 		 * {"node":"rpi01","group_id":"fermenters","control":"rebirth"}
 		 */
 		if (strncmp(buf, (char *)"{\"type\":\"fermenter\",", 20) == 0) {
+		    syslog(LOG_NOTICE, "ws: received %ld bytes %s", (long)len, buf);
 		    fermenter_ws_receive(buf);
 		} else if (strncmp(buf, (char *)"{\"type\":\"device\",", 17) == 0) {
-
+		    syslog(LOG_NOTICE, "ws: received %ld bytes %s", (long)len, buf);
 		} else if (strncmp(buf, (char *)"{\"type\":\"global\",", 17) == 0) {
-
+		    syslog(LOG_NOTICE, "ws: received %ld bytes %s", (long)len, buf);
 #ifdef USE_SIMULATOR
 		} else if (strncmp(buf, (char *)"{\"type\":\"simulator\",", 20) == 0) {
-
+		    syslog(LOG_NOTICE, "ws: received %ld bytes %s", (long)len, buf);
 #endif
 		} else if (strncmp(buf, (char *)"{\"pong\":", 8) == 0) {
 		    pong_ws_receive(buf);

mercurial