733 dht11_next = time(NULL); |
737 dht11_next = time(NULL); |
734 |
738 |
735 /* |
739 /* |
736 * Set the temperature sensors to 12 bits resolution and write it in EEPROM |
740 * Set the temperature sensors to 12 bits resolution and write it in EEPROM |
737 */ |
741 */ |
738 for (device = Config.devices; device; device = device->next) { |
742 // for (device = Config.devices; device; device = device->next) { |
739 if ((device->type == DEVTYPE_W1) && |
743 // if ((device->type == DEVTYPE_W1) && |
740 ((strncmp(device->address, (char *)"10", 2) == 0) || |
744 // ((strncmp(device->address, (char *)"10", 2) == 0) || |
741 (strncmp(device->address, (char *)"22", 2) == 0) || |
745 // (strncmp(device->address, (char *)"22", 2) == 0) || |
742 (strncmp(device->address, (char *)"28", 2) == 0) || |
746 // (strncmp(device->address, (char *)"28", 2) == 0) || |
743 (strncmp(device->address, (char *)"3b", 2) == 0) || |
747 // (strncmp(device->address, (char *)"3b", 2) == 0) || |
744 (strncmp(device->address, (char *)"42", 2) == 0))) { |
748 // (strncmp(device->address, (char *)"42", 2) == 0))) { |
745 addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
749 // addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
746 addr = xstrcat(addr, device->address); |
750 // addr = xstrcat(addr, device->address); |
747 addr = xstrcat(addr, (char *)"/w1_slave"); |
751 // addr = xstrcat(addr, (char *)"/w1_slave"); |
748 if ((fp = fopen(addr, "w"))) { |
752 // if ((fp = fopen(addr, "w"))) { |
749 rc = fprintf(fp, "12\n0\n"); // According to the kernel documentation. Seems to work. |
753 // rc = fprintf(fp, "12\n0\n"); // According to the kernel documentation. Seems to work. |
750 fclose(fp); |
754 // fclose(fp); |
751 if (rc != 5) { |
755 // if (rc != 5) { |
752 syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr); |
756 // syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr); |
753 } |
757 // } |
754 } |
758 // } |
755 free(addr); |
759 // free(addr); |
756 addr = NULL; |
760 // addr = NULL; |
757 } |
761 // } |
758 } |
762 // } |
759 |
763 |
760 /* |
764 /* |
761 * Loop forever until the external shutdown variable is set. |
765 * Loop forever until the external shutdown variable is set. |
762 */ |
766 */ |
763 for (;;) { |
767 for (;;) { |
779 if ((strncmp(device->address, (char *)"10", 2) == 0) || |
783 if ((strncmp(device->address, (char *)"10", 2) == 0) || |
780 (strncmp(device->address, (char *)"22", 2) == 0) || |
784 (strncmp(device->address, (char *)"22", 2) == 0) || |
781 (strncmp(device->address, (char *)"28", 2) == 0) || |
785 (strncmp(device->address, (char *)"28", 2) == 0) || |
782 (strncmp(device->address, (char *)"3b", 2) == 0) || |
786 (strncmp(device->address, (char *)"3b", 2) == 0) || |
783 (strncmp(device->address, (char *)"42", 2) == 0)) { |
787 (strncmp(device->address, (char *)"42", 2) == 0)) { |
784 addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
788 found = FALSE; |
785 addr = xstrcat(addr, device->address); |
789 for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) { |
786 addr = xstrcat(addr, (char *)"/w1_slave"); |
790 if (strcmp(device->address, dev_w1->address) == 0) { |
787 if ((fp = fopen(addr, "r"))) { |
791 syslog(LOG_NOTICE, "sensor %s value %d", dev_w1->address, dev_w1->value); |
788 fgets(line1, 50, fp); // Read 2 lines |
792 found = TRUE; |
789 fgets(line2, 50, fp); |
793 if ((dev_w1->value == -1) || (dev_w1->value < -55000)) { |
790 fclose(fp); |
794 syslog(LOG_NOTICE, "sensor %s value error %d", device->address, dev_w1->value); |
791 /* |
795 device->present = DEVPRESENT_ERROR; |
792 * The output looks like: |
796 } else { |
793 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
797 device->value = dev_w1->value; |
794 * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
798 device->timestamp = time(NULL); |
795 */ |
|
796 line1[strlen(line1)-1] = '\0'; |
|
797 if ((line1[36] == 'Y') && (line1[37] == 'E')) { |
|
798 /* CRC is Ok, continue */ |
|
799 if (device->present != DEVPRESENT_YES) { |
|
800 syslog(LOG_NOTICE, "sensor %s is Ok", device->address); |
|
801 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
|
802 device->present = DEVPRESENT_YES; |
799 device->present = DEVPRESENT_YES; |
803 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
|
804 } |
800 } |
805 |
|
806 line2[strlen(line2)-1] = '\0'; |
|
807 strtok(line2, (char *)"="); |
|
808 p = strtok(NULL, (char *)"="); |
|
809 rc = sscanf(p, "%d", &temp); |
|
810 if ((rc == 1) && (device->value != temp)) { |
|
811 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
|
812 if (temp < -55000) { |
|
813 syslog(LOG_NOTICE, "sensor %s value error '%d` '%s`", device->address, temp, line1); |
|
814 device->present = DEVPRESENT_ERROR; |
|
815 } else { |
|
816 device->value = temp; |
|
817 device->timestamp = time(NULL); |
|
818 device->present = DEVPRESENT_YES; |
|
819 } |
|
820 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
|
821 } |
|
822 } else { |
|
823 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1); |
|
824 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
|
825 device->present = DEVPRESENT_ERROR; |
|
826 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
|
827 } |
801 } |
828 } else { |
802 } |
|
803 |
|
804 if (found == FALSE) { |
829 if (device->present != DEVPRESENT_NO) { |
805 if (device->present != DEVPRESENT_NO) { |
830 syslog(LOG_NOTICE, "sensor %s is missing", device->address); |
806 syslog(LOG_NOTICE, "sensor %s is missing", device->address); |
831 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
807 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
832 device->present = DEVPRESENT_NO; |
808 device->present = DEVPRESENT_NO; |
833 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
809 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
834 } |
810 } |
835 } |
811 } |
836 free(addr); |
812 |
837 addr = NULL; |
|
838 } /* if temperature sensor */ |
813 } /* if temperature sensor */ |
839 |
814 |
840 break; |
815 break; |
841 |
816 |
842 case DEVTYPE_DHT: |
817 case DEVTYPE_DHT: |