thermferm/devices.c

changeset 674
6cabc02f4c8d
parent 672
0c2c66920d79
child 684
b2265c7e5707
equal deleted inserted replaced
673:0c084b876a2c 674:6cabc02f4c8d
63 * If last read was an error, the interval is 2 seconds. 63 * If last read was an error, the interval is 2 seconds.
64 */ 64 */
65 void dht11Read(char *address) 65 void dht11Read(char *address)
66 { 66 {
67 int temp, hum; 67 int temp, hum;
68 int fd, rc, err; 68 int fd, rc, err, oldstate;
69 char buffer[25], *dhtpath = NULL; 69 char buffer[25], *dhtpath = NULL;
70 70
71 dht11_temperature = -1; 71 dht11_temperature = -1;
72 dht11_humidity = -1; 72 dht11_humidity = -1;
73 dht11_state = DEVPRESENT_UNDEF; 73 oldstate = dht11_state;
74 dhtpath = xstrcpy((char *)"/sys/bus/iio/devices/"); 74 dhtpath = xstrcpy((char *)"/sys/bus/iio/devices/");
75 dhtpath = xstrcat(dhtpath, address); 75 dhtpath = xstrcat(dhtpath, address);
76 dhtpath = xstrcat(dhtpath, (char *)"/in_temp_input"); 76 dhtpath = xstrcat(dhtpath, (char *)"/in_temp_input");
77 77
78 fd = open(dhtpath, O_RDONLY); 78 fd = open(dhtpath, O_RDONLY);
93 if (err == 110) { 93 if (err == 110) {
94 dht11_state = DEVPRESENT_NO; /* Device is gone */ 94 dht11_state = DEVPRESENT_NO; /* Device is gone */
95 } else { 95 } else {
96 dht11_state = DEVPRESENT_ERROR; 96 dht11_state = DEVPRESENT_ERROR;
97 } 97 }
98 syslog(LOG_NOTICE, "DHT11 read temperature: %d %s", err, strerror(err)); 98 syslog(LOG_NOTICE, "DHT11 read temperature: %s", strerror(err));
99 } else { 99 } else {
100 sscanf(buffer, "%d", &temp); 100 sscanf(buffer, "%d", &temp);
101 dht11_temperature = temp; 101 dht11_temperature = temp;
102 dht11_state = DEVPRESENT_YES; 102 dht11_state = DEVPRESENT_YES;
103 } 103 }
120 if (err == 110) { 120 if (err == 110) {
121 dht11_state = DEVPRESENT_NO; 121 dht11_state = DEVPRESENT_NO;
122 } else { 122 } else {
123 dht11_state = DEVPRESENT_ERROR; 123 dht11_state = DEVPRESENT_ERROR;
124 } 124 }
125 syslog(LOG_NOTICE, "DHT11 read humidity: %d %s", err, strerror(err)); 125 syslog(LOG_NOTICE, "DHT11 read humidity: %s", strerror(err));
126 dht11_temperature = -1; /* Make invalid */ 126 dht11_temperature = -1; /* Make invalid */
127 } else { 127 } else {
128 sscanf(buffer, "%d", &hum); 128 sscanf(buffer, "%d", &hum);
129 dht11_humidity = hum; 129 dht11_humidity = hum;
130 dht11_state = DEVPRESENT_YES; 130 dht11_state = DEVPRESENT_YES;
132 close(fd); 132 close(fd);
133 free(dhtpath); 133 free(dhtpath);
134 dhtpath = NULL; 134 dhtpath = NULL;
135 } 135 }
136 136
137 syslog(LOG_NOTICE, "dht11 t:%d h:%d state:%d", dht11_temperature, dht11_humidity, dht11_state); 137 if (oldstate != dht11_state)
138 syslog(LOG_NOTICE, "dht11 t:%d h:%d state:%d", dht11_temperature, dht11_humidity, dht11_state);
138 } 139 }
139 140
140 141
141 142
142 /* 143 /*
289 290
290 } 291 }
291 #endif 292 #endif
292 if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) { 293 if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
293 if (strncmp(device->address, (char *)"3a", 2) == 0) { 294 if (strncmp(device->address, (char *)"3a", 2) == 0) {
294 /* 295 syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
295 * DS2413. First read state so that we can preserve the state of 296 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
296 * the "other" PIO channel. To make things a bit more complicated 297 device->value = (value == 0) ? 0 : 1;
297 * the bits in the state register differ from the output register. 298 device->timestamp = time(NULL);
298 */ 299 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
299 // uint8_t state, output;
300
301 // if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
302 // state = (unsigned int)rc;
303 // output = (state & 0x01) + ((state & 0x04) >> 1);
304
305 // if (device->subdevice == 0) {
306 // output = (output & 0xfe);
307 // output |= (value == 0) ? 0x01 : 0x00;
308 // } else if (device->subdevice == 1) {
309 // output = (output & 0xfd);
310 // output |= (value == 0) ? 0x02 : 0x00;
311 // } else {
312 // output = 0xff;
313 // }
314
315 // if ((write_w1(device->address, (char *)"output", output)) == 0) {
316 syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
317 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
318 device->value = (value == 0) ? 0 : 1;
319 device->timestamp = time(NULL);
320 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
321 // }
322 // }
323 } 300 }
324 } 301 }
325 302
326 #ifdef USE_SIMULATOR 303 #ifdef USE_SIMULATOR
327 if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) { 304 if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
365 int tmp, present; 342 int tmp, present;
366 343
367 if (uuid == NULL) 344 if (uuid == NULL)
368 return 0; 345 return 0;
369 346
370 // pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
371
372 for (device = Config.devices; device; device = device->next) { 347 for (device = Config.devices; device; device = device->next) {
373 if (! strcmp(uuid, device->uuid)) { 348 if (! strcmp(uuid, device->uuid)) {
374 present = device->present; 349 present = device->present;
375 if (present == DEVPRESENT_YES) { 350 if (present == DEVPRESENT_YES) {
376 tmp = device->value + device->offset; 351 tmp = device->value + device->offset;
377 } else { 352 } else {
378 tmp = 0; 353 tmp = 0;
379 } 354 }
380 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
381 *value = tmp; 355 *value = tmp;
382 return present; 356 return present;
383 } 357 }
384 } 358 }
385 359
386 // pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
387 return DEVPRESENT_NO; 360 return DEVPRESENT_NO;
388 } 361 }
389 362
390 363
391 364
719 devices_list *device; 692 devices_list *device;
720 w1_list *dev_w1; 693 w1_list *dev_w1;
721 #ifdef USE_SIMULATOR 694 #ifdef USE_SIMULATOR
722 simulator_list *simulator; 695 simulator_list *simulator;
723 #endif 696 #endif
724 // char *addr = NULL, line1[60], line2[60], *p = NULL;
725 // FILE *fp;
726 int found; 697 int found;
727 time_t now; 698 time_t now;
728 699
729 my_devices_state = 1; 700 my_devices_state = 1;
730 syslog(LOG_NOTICE, "Thread my_devices_loop started"); 701 syslog(LOG_NOTICE, "Thread my_devices_loop started");
731
732 #ifdef HAVE_WIRINGPI_H
733 // if ((rc = piHiPri(50)))
734 // syslog(LOG_NOTICE, "my_devices_loop: piHiPri(50) rc=%d", rc);
735 #endif
736
737 dht11_next = time(NULL); 702 dht11_next = time(NULL);
738
739 /*
740 * Set the temperature sensors to 12 bits resolution and write it in EEPROM
741 */
742 // for (device = Config.devices; device; device = device->next) {
743 // if ((device->type == DEVTYPE_W1) &&
744 // ((strncmp(device->address, (char *)"10", 2) == 0) ||
745 // (strncmp(device->address, (char *)"22", 2) == 0) ||
746 // (strncmp(device->address, (char *)"28", 2) == 0) ||
747 // (strncmp(device->address, (char *)"3b", 2) == 0) ||
748 // (strncmp(device->address, (char *)"42", 2) == 0))) {
749 // addr = xstrcpy((char *)"/sys/bus/w1/devices/");
750 // addr = xstrcat(addr, device->address);
751 // addr = xstrcat(addr, (char *)"/w1_slave");
752 // if ((fp = fopen(addr, "w"))) {
753 // rc = fprintf(fp, "12\n0\n"); // According to the kernel documentation. Seems to work.
754 // fclose(fp);
755 // if (rc != 5) {
756 // syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr);
757 // }
758 // }
759 // free(addr);
760 // addr = NULL;
761 // }
762 // }
763 703
764 /* 704 /*
765 * Loop forever until the external shutdown variable is set. 705 * Loop forever until the external shutdown variable is set.
766 */ 706 */
767 for (;;) { 707 for (;;) {
771 */ 711 */
772 for (device = Config.devices; device; device = device->next) { 712 for (device = Config.devices; device; device = device->next) {
773 713
774 if (my_devices_shutdown) 714 if (my_devices_shutdown)
775 break; 715 break;
776 //syslog(LOG_NOTICE, "device type %d address %s-%d comment %s", device->type, device->address, device->subdevice, device->comment);
777 716
778 switch (device->type) { 717 switch (device->type) {
779 case DEVTYPE_W1: 718 case DEVTYPE_W1:
780 /* 719 /*
781 * Only tested with DS18B20 but from the kernel source this 720 * Only tested with DS18B20 but from the kernel source this
787 (strncmp(device->address, (char *)"3b", 2) == 0) || 726 (strncmp(device->address, (char *)"3b", 2) == 0) ||
788 (strncmp(device->address, (char *)"42", 2) == 0)) { 727 (strncmp(device->address, (char *)"42", 2) == 0)) {
789 found = FALSE; 728 found = FALSE;
790 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) { 729 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
791 if (strcmp(device->address, dev_w1->address) == 0) { 730 if (strcmp(device->address, dev_w1->address) == 0) {
792 // syslog(LOG_NOTICE, "sensor %s value %d", dev_w1->address, dev_w1->value);
793 found = TRUE; 731 found = TRUE;
794 if ((dev_w1->value == -1) || (dev_w1->value < -55000)) { 732 if ((dev_w1->value == -1) || (dev_w1->value < -55000)) {
795 if (device->present != DEVPRESENT_ERROR) 733 if (device->present != DEVPRESENT_ERROR)
796 syslog(LOG_NOTICE, "sensor %s value error %d, keep %d", device->address, dev_w1->value, device->value); 734 syslog(LOG_NOTICE, "sensor %s value error %d, keep %d", device->address, dev_w1->value, device->value);
797 device->present = DEVPRESENT_ERROR; 735 device->present = DEVPRESENT_ERROR;

mercurial