thermferm/devices.c

changeset 528
908ef02d5d22
parent 527
afd13a58c8c8
child 553
4091d4fe217f
equal deleted inserted replaced
527:afd13a58c8c8 528:908ef02d5d22
697 { 697 {
698 devices_list *device; 698 devices_list *device;
699 #ifdef USE_SIMULATOR 699 #ifdef USE_SIMULATOR
700 simulator_list *simulator; 700 simulator_list *simulator;
701 #endif 701 #endif
702 char *addr = NULL, line[60], *p = NULL; 702 char *addr = NULL, line1[60], line2[60], *p = NULL;
703 FILE *fp; 703 FILE *fp;
704 int temp, rc; 704 int temp, rc;
705 #ifdef HAVE_WIRINGPI_H 705 #ifdef HAVE_WIRINGPI_H
706 time_t now; 706 time_t now;
707 #endif 707 #endif
739 (strncmp(device->address, (char *)"42", 2) == 0)) { 739 (strncmp(device->address, (char *)"42", 2) == 0)) {
740 addr = xstrcpy((char *)"/sys/bus/w1/devices/"); 740 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
741 addr = xstrcat(addr, device->address); 741 addr = xstrcat(addr, device->address);
742 addr = xstrcat(addr, (char *)"/w1_slave"); 742 addr = xstrcat(addr, (char *)"/w1_slave");
743 if ((fp = fopen(addr, "r"))) { 743 if ((fp = fopen(addr, "r"))) {
744 if (device->present != DEVPRESENT_YES) { 744 fgets(line1, 50, fp); // Read 2 lines
745 syslog(LOG_NOTICE, "sensor %s is back", device->address); 745 fgets(line2, 50, fp);
746 #ifdef HAVE_WIRINGPI_H 746 fclose(fp);
747 piLock(LOCK_DEVICES); 747 // if (device->present != DEVPRESENT_YES) {
748 #endif 748 // syslog(LOG_NOTICE, "sensor %s is back", device->address);
749 device->present = DEVPRESENT_YES; 749 //#ifdef HAVE_WIRINGPI_H
750 #ifdef HAVE_WIRINGPI_H 750 // piLock(LOCK_DEVICES);
751 piUnlock(LOCK_DEVICES); 751 //#endif
752 #endif 752 // device->present = DEVPRESENT_YES;
753 } 753 //#ifdef HAVE_WIRINGPI_H
754 // piUnlock(LOCK_DEVICES);
755 //#endif
756 // }
754 /* 757 /*
755 * The output looks like: 758 * The output looks like:
756 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES 759 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
757 * 72 01 4b 46 7f ff 0e 10 57 t=23125 760 * 72 01 4b 46 7f ff 0e 10 57 t=23125
758 */ 761 */
759 fgets(line, 50, fp); 762 line1[strlen(line1)-1] = '\0';
760 line[strlen(line)-1] = '\0'; 763 if ((line1[36] == 'Y') && (line1[37] == 'E')) {
761 if ((line[36] == 'Y') && (line[37] == 'E')) {
762 /* CRC is Ok, continue */ 764 /* CRC is Ok, continue */
763 fgets(line, 50, fp); 765 if (device->present != DEVPRESENT_YES) {
764 line[strlen(line)-1] = '\0'; 766 syslog(LOG_NOTICE, "sensor %s is Ok", device->address);
765 strtok(line, (char *)"="); 767 #ifdef HAVE_WIRINGPI_H
768 piLock(LOCK_DEVICES);
769 #endif
770 device->present = DEVPRESENT_YES;
771 #ifdef HAVE_WIRINGPI_H
772 piUnlock(LOCK_DEVICES);
773 #endif
774 }
775
776 line2[strlen(line2)-1] = '\0';
777 strtok(line2, (char *)"=");
766 p = strtok(NULL, (char *)"="); 778 p = strtok(NULL, (char *)"=");
767 rc = sscanf(p, "%d", &temp); 779 rc = sscanf(p, "%d", &temp);
768 #ifdef HAVE_WIRINGPI_H
769 piLock(LOCK_DEVICES);
770 #endif
771 if ((rc == 1) && (device->value != temp)) { 780 if ((rc == 1) && (device->value != temp)) {
781 #ifdef HAVE_WIRINGPI_H
782 piLock(LOCK_DEVICES);
783 #endif
772 device->value = temp; 784 device->value = temp;
773 device->timestamp = time(NULL); 785 device->timestamp = time(NULL);
786 device->present = DEVPRESENT_YES;
787 #ifdef HAVE_WIRINGPI_H
788 piUnlock(LOCK_DEVICES);
789 #endif
774 } 790 }
775 #ifdef HAVE_WIRINGPI_H
776 piUnlock(LOCK_DEVICES);
777 #endif
778 } else { 791 } else {
779 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line); 792 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1);
780 #ifdef HAVE_WIRINGPI_H 793 #ifdef HAVE_WIRINGPI_H
781 piLock(LOCK_DEVICES); 794 piLock(LOCK_DEVICES);
782 #endif 795 #endif
783 device->present = DEVPRESENT_ERROR; 796 device->present = DEVPRESENT_ERROR;
784 #ifdef HAVE_WIRINGPI_H 797 #ifdef HAVE_WIRINGPI_H
785 piUnlock(LOCK_DEVICES); 798 piUnlock(LOCK_DEVICES);
786 #endif 799 #endif
787 } 800 }
788 fclose(fp);
789 } else { 801 } else {
790 if (device->present != DEVPRESENT_NO) { 802 if (device->present != DEVPRESENT_NO) {
791 syslog(LOG_NOTICE, "sensor %s is missing", device->address); 803 syslog(LOG_NOTICE, "sensor %s is missing", device->address);
792 #ifdef HAVE_WIRINGPI_H 804 #ifdef HAVE_WIRINGPI_H
793 piLock(LOCK_DEVICES); 805 piLock(LOCK_DEVICES);

mercurial