Better handling of a removed DS18B20 temperature sensor.

Sat, 20 Apr 2024 10:53:18 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 20 Apr 2024 10:53:18 +0200
changeset 690
3a5b019e9acc
parent 689
f94b525f7563
child 691
cfe13185fd02

Better handling of a removed DS18B20 temperature sensor.

thermferm/one-wire.c file | annotate | diff | comparison | revisions
--- a/thermferm/one-wire.c	Fri Apr 19 20:56:55 2024 +0200
+++ b/thermferm/one-wire.c	Sat Apr 20 10:53:18 2024 +0200
@@ -295,15 +295,17 @@
      */
     if (cur_w1 != NULL) {
 
-	if ((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) ||
-	    (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) {
+	if (((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) ||
+	     (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) &&
+	     (cur_w1->present == DEVPRESENT_YES)) {
 	    devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
 	    devfile = xstrcat(devfile, cur_w1->address);
 	    devfile = xstrcat(devfile, (char *)"/resolution");
 	    if ((fp = fopen(devfile, "r+"))) {
 		if ((fgets(buffer, 25, fp))) {
 		     sscanf(buffer, "%d", &value);
-		     if (value != W1_TEMP_RESOLUTION) {
+		     /* If device is removed, value is negative (errno?) */
+		     if ((value > 0) && (value != W1_TEMP_RESOLUTION)) {
 			syslog(LOG_NOTICE, "One-wire device %s set resolution from %d to %d", cur_w1->address, value, W1_TEMP_RESOLUTION);
 			fseek(fp, 0L, SEEK_SET);
 			sprintf(buffer, "%d", W1_TEMP_RESOLUTION);
@@ -347,7 +349,7 @@
 	    }
 	    free(devfile);
 	    devfile = NULL;
-	}
+	} /* if temperature sensor and present */
 
 	for (;;) {
 	    if (cur_w1->next != NULL) {

mercurial