thermferm/devices.c

changeset 674
6cabc02f4c8d
parent 672
0c2c66920d79
child 684
b2265c7e5707
--- a/thermferm/devices.c	Thu Apr 11 15:01:46 2024 +0200
+++ b/thermferm/devices.c	Thu Apr 11 15:58:49 2024 +0200
@@ -65,12 +65,12 @@
 void dht11Read(char *address)
 {
     int        	temp, hum;
-    int		fd, rc, err;
+    int		fd, rc, err, oldstate;
     char	buffer[25], *dhtpath = NULL;
 
     dht11_temperature = -1;
     dht11_humidity = -1;
-    dht11_state = DEVPRESENT_UNDEF;
+    oldstate = dht11_state;
     dhtpath = xstrcpy((char *)"/sys/bus/iio/devices/");
     dhtpath = xstrcat(dhtpath, address);
     dhtpath = xstrcat(dhtpath, (char *)"/in_temp_input");
@@ -95,7 +95,7 @@
 	} else {
 		dht11_state = DEVPRESENT_ERROR;
 	}
-	syslog(LOG_NOTICE, "DHT11 read temperature: %d %s", err, strerror(err));
+	syslog(LOG_NOTICE, "DHT11 read temperature: %s", strerror(err));
     } else {
 	sscanf(buffer, "%d", &temp);
 	dht11_temperature = temp;
@@ -122,7 +122,7 @@
 	    } else {
 		    dht11_state = DEVPRESENT_ERROR;
 	    }
-	    syslog(LOG_NOTICE, "DHT11 read humidity: %d %s", err, strerror(err));
+	    syslog(LOG_NOTICE, "DHT11 read humidity: %s", strerror(err));
 	    dht11_temperature = -1;	/* Make invalid	*/
 	} else {
 	    sscanf(buffer, "%d", &hum);
@@ -134,7 +134,8 @@
 	dhtpath = NULL;
     }
 
-    syslog(LOG_NOTICE, "dht11 t:%d h:%d state:%d", dht11_temperature, dht11_humidity, dht11_state);
+    if (oldstate != dht11_state)
+	syslog(LOG_NOTICE, "dht11 t:%d h:%d state:%d", dht11_temperature, dht11_humidity, dht11_state);
 }
 
 
@@ -291,35 +292,11 @@
 #endif
 		if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
 		    if (strncmp(device->address, (char *)"3a", 2) == 0) {
-			/*
-			 * DS2413. First read state so that we can preserve the state of
-			 * the "other" PIO channel. To make things a bit more complicated
-			 * the bits in the state register differ from the output register.
-			 */
-//			uint8_t	state, output;
-
-//			if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
-//			    state = (unsigned int)rc;
-//			    output = (state & 0x01) + ((state & 0x04) >> 1);
-
-//			    if (device->subdevice == 0) {
-//				output = (output & 0xfe);
-//				output |= (value == 0) ? 0x01 : 0x00;
-//			    } else if (device->subdevice == 1) {
-//				output = (output & 0xfd);
-//				output |= (value == 0) ? 0x02 : 0x00;
-//			    } else {
-//				output = 0xff;
-//			    }
-
-//			    if ((write_w1(device->address, (char *)"output", output)) == 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]);
-				device->value = (value == 0) ? 0 : 1;
-				device->timestamp = time(NULL);
-//				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
-//			    }
-//			}
+		    	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]);
+			device->value = (value == 0) ? 0 : 1;
+			device->timestamp = time(NULL);
+//			pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		    }
 		}
 
@@ -367,8 +344,6 @@
     if (uuid == NULL)
 	return 0;
 
-//    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
-
     for (device = Config.devices; device; device = device->next) {
 	if (! strcmp(uuid, device->uuid)) {
 	    present = device->present;
@@ -377,13 +352,11 @@
 	    } else {
 		tmp = 0;
 	    }
-//	    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 	    *value = tmp;
 	    return present;
 	}
     }
 
-//    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
     return DEVPRESENT_NO;
 }
 
@@ -721,47 +694,14 @@
 #ifdef USE_SIMULATOR
     simulator_list	*simulator;
 #endif
-//    char		*addr = NULL, line1[60], line2[60], *p = NULL;
-//    FILE		*fp;
     int			found;
     time_t		now;
 
     my_devices_state = 1;
     syslog(LOG_NOTICE, "Thread my_devices_loop started");
-
-#ifdef HAVE_WIRINGPI_H
-//    if ((rc = piHiPri(50)))
-//    	syslog(LOG_NOTICE, "my_devices_loop: piHiPri(50) rc=%d", rc);
-#endif
-
     dht11_next = time(NULL);
 
     /*
-     * Set the temperature sensors to 12 bits resolution and write it in EEPROM
-     */
-//    for (device = Config.devices; device; device = device->next) {
-//	if ((device->type == DEVTYPE_W1) &&
-//	    ((strncmp(device->address, (char *)"10", 2) == 0) ||
-  //           (strncmp(device->address, (char *)"22", 2) == 0) ||
-//             (strncmp(device->address, (char *)"28", 2) == 0) ||
-//             (strncmp(device->address, (char *)"3b", 2) == 0) ||
-//             (strncmp(device->address, (char *)"42", 2) == 0))) {
-//	    addr = xstrcpy((char *)"/sys/bus/w1/devices/");
-//            addr = xstrcat(addr, device->address);
-//            addr = xstrcat(addr, (char *)"/w1_slave");
-//            if ((fp = fopen(addr, "w"))) {
-//		rc = fprintf(fp, "12\n0\n");	// According to the kernel documentation. Seems to work.
-//		fclose(fp);
-//		if (rc != 5) {
-//		    syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr);
-//                }
-//	    }
-//	    free(addr);
-//	    addr = NULL;
-//	}
-//    }
-
-    /*
      * Loop forever until the external shutdown variable is set.
      */
     for (;;) {
@@ -773,7 +713,6 @@
 
 	    if (my_devices_shutdown)
 		break;
-//syslog(LOG_NOTICE, "device type %d address %s-%d comment %s", device->type, device->address, device->subdevice, device->comment);
 
 	    switch (device->type) {
 		case DEVTYPE_W1:
@@ -789,7 +728,6 @@
 			    found = FALSE;
 			    for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
 				if (strcmp(device->address, dev_w1->address) == 0) {
-//				    syslog(LOG_NOTICE, "sensor %s value %d", dev_w1->address, dev_w1->value);
 				    found = TRUE;
 				    if ((dev_w1->value == -1) || (dev_w1->value < -55000)) {
 					if (device->present != DEVPRESENT_ERROR)

mercurial