327 if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { |
326 if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { |
328 snprintf(buf, 39, "%s,%d", device->address, value ? 1:0); |
327 snprintf(buf, 39, "%s,%d", device->address, value ? 1:0); |
329 for (i = 0; i < strlen(buf); i++) |
328 for (i = 0; i < strlen(buf); i++) |
330 if (buf[i] == '-') |
329 if (buf[i] == '-') |
331 buf[i] = ','; |
330 buf[i] = ','; |
332 piUnlock(LOCK_DEVICES); |
331 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
333 enableTransmit(device->gpiopin); |
332 enableTransmit(device->gpiopin); |
334 rc = toggleSwitch(buf); |
333 rc = toggleSwitch(buf); |
335 disableTransmit(); |
334 disableTransmit(); |
336 piLock(LOCK_DEVICES); |
335 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
337 syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc); |
336 syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc); |
338 device->value = value; |
337 device->value = value; |
339 device->timestamp = time(NULL); |
338 device->timestamp = time(NULL); |
340 piUnlock(LOCK_DEVICES); |
339 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
341 return rc; |
340 return rc; |
342 } |
341 } |
343 |
342 |
344 if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { |
343 if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { |
345 |
344 |
423 int tmp, present; |
418 int tmp, present; |
424 |
419 |
425 if (uuid == NULL) |
420 if (uuid == NULL) |
426 return 0; |
421 return 0; |
427 |
422 |
428 #ifdef HAVE_WIRINGPI_H |
423 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
429 piLock(LOCK_DEVICES); |
|
430 #endif |
|
431 |
424 |
432 for (device = Config.devices; device; device = device->next) { |
425 for (device = Config.devices; device; device = device->next) { |
433 if (! strcmp(uuid, device->uuid)) { |
426 if (! strcmp(uuid, device->uuid)) { |
434 present = device->present; |
427 present = device->present; |
435 if (present == DEVPRESENT_YES) { |
428 if (present == DEVPRESENT_YES) { |
436 tmp = device->value + device->offset; |
429 tmp = device->value + device->offset; |
437 } else { |
430 } else { |
438 tmp = 0; |
431 tmp = 0; |
439 } |
432 } |
440 #ifdef HAVE_WIRINGPI_H |
433 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
441 piUnlock(LOCK_DEVICES); |
|
442 #endif |
|
443 *value = tmp; |
434 *value = tmp; |
444 return present; |
435 return present; |
445 } |
436 } |
446 } |
437 } |
447 |
438 |
448 #ifdef HAVE_WIRINGPI_H |
439 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
449 piUnlock(LOCK_DEVICES); |
|
450 #endif |
|
451 |
|
452 return DEVPRESENT_NO; |
440 return DEVPRESENT_NO; |
453 } |
441 } |
454 |
442 |
455 |
443 |
456 |
444 |
813 line1[strlen(line1)-1] = '\0'; |
801 line1[strlen(line1)-1] = '\0'; |
814 if ((line1[36] == 'Y') && (line1[37] == 'E')) { |
802 if ((line1[36] == 'Y') && (line1[37] == 'E')) { |
815 /* CRC is Ok, continue */ |
803 /* CRC is Ok, continue */ |
816 if (device->present != DEVPRESENT_YES) { |
804 if (device->present != DEVPRESENT_YES) { |
817 syslog(LOG_NOTICE, "sensor %s is Ok", device->address); |
805 syslog(LOG_NOTICE, "sensor %s is Ok", device->address); |
818 #ifdef HAVE_WIRINGPI_H |
806 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
819 piLock(LOCK_DEVICES); |
|
820 #endif |
|
821 device->present = DEVPRESENT_YES; |
807 device->present = DEVPRESENT_YES; |
822 #ifdef HAVE_WIRINGPI_H |
808 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
823 piUnlock(LOCK_DEVICES); |
|
824 #endif |
|
825 } |
809 } |
826 |
810 |
827 line2[strlen(line2)-1] = '\0'; |
811 line2[strlen(line2)-1] = '\0'; |
828 strtok(line2, (char *)"="); |
812 strtok(line2, (char *)"="); |
829 p = strtok(NULL, (char *)"="); |
813 p = strtok(NULL, (char *)"="); |
830 rc = sscanf(p, "%d", &temp); |
814 rc = sscanf(p, "%d", &temp); |
831 if ((rc == 1) && (device->value != temp)) { |
815 if ((rc == 1) && (device->value != temp)) { |
832 #ifdef HAVE_WIRINGPI_H |
816 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
833 piLock(LOCK_DEVICES); |
|
834 #endif |
|
835 if (temp < -55000) { |
817 if (temp < -55000) { |
836 syslog(LOG_NOTICE, "sensor %s value error '%d` '%s`", device->address, temp, line1); |
818 syslog(LOG_NOTICE, "sensor %s value error '%d` '%s`", device->address, temp, line1); |
837 device->present = DEVPRESENT_ERROR; |
819 device->present = DEVPRESENT_ERROR; |
838 } else { |
820 } else { |
839 device->value = temp; |
821 device->value = temp; |
840 device->timestamp = time(NULL); |
822 device->timestamp = time(NULL); |
841 device->present = DEVPRESENT_YES; |
823 device->present = DEVPRESENT_YES; |
842 } |
824 } |
843 #ifdef HAVE_WIRINGPI_H |
825 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
844 piUnlock(LOCK_DEVICES); |
|
845 #endif |
|
846 } |
826 } |
847 } else { |
827 } else { |
848 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1); |
828 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1); |
849 #ifdef HAVE_WIRINGPI_H |
829 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
850 piLock(LOCK_DEVICES); |
|
851 #endif |
|
852 device->present = DEVPRESENT_ERROR; |
830 device->present = DEVPRESENT_ERROR; |
853 #ifdef HAVE_WIRINGPI_H |
831 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
854 piUnlock(LOCK_DEVICES); |
|
855 #endif |
|
856 } |
832 } |
857 } else { |
833 } else { |
858 if (device->present != DEVPRESENT_NO) { |
834 if (device->present != DEVPRESENT_NO) { |
859 syslog(LOG_NOTICE, "sensor %s is missing", device->address); |
835 syslog(LOG_NOTICE, "sensor %s is missing", device->address); |
860 #ifdef HAVE_WIRINGPI_H |
836 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
861 piLock(LOCK_DEVICES); |
|
862 #endif |
|
863 device->present = DEVPRESENT_NO; |
837 device->present = DEVPRESENT_NO; |
864 #ifdef HAVE_WIRINGPI_H |
838 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
865 piUnlock(LOCK_DEVICES); |
|
866 #endif |
|
867 } |
839 } |
868 } |
840 } |
869 free(addr); |
841 free(addr); |
870 addr = NULL; |
842 addr = NULL; |
871 } /* if temperature sensor */ |
843 } /* if temperature sensor */ |
878 addr = xstrcat(addr, (char *)"/state"); |
850 addr = xstrcat(addr, (char *)"/state"); |
879 |
851 |
880 if ((access(addr, R_OK)) == 0) { |
852 if ((access(addr, R_OK)) == 0) { |
881 if (device->present != DEVPRESENT_YES) { |
853 if (device->present != DEVPRESENT_YES) { |
882 syslog(LOG_NOTICE, "DS2413 %s is back", device->address); |
854 syslog(LOG_NOTICE, "DS2413 %s is back", device->address); |
883 #ifdef HAVE_WIRINGPI_H |
855 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
884 piLock(LOCK_DEVICES); |
|
885 #endif |
|
886 device->present = DEVPRESENT_YES; |
856 device->present = DEVPRESENT_YES; |
887 #ifdef HAVE_WIRINGPI_H |
857 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
888 piUnlock(LOCK_DEVICES); |
|
889 #endif |
|
890 } |
858 } |
891 /* |
859 /* |
892 * First make sure that if this device is configured as input |
860 * First make sure that if this device is configured as input |
893 * to drive the output high. |
861 * to drive the output high. |
894 */ |
862 */ |
909 } |
877 } |
910 write_w1(device->address, (char *)"output", output); |
878 write_w1(device->address, (char *)"output", output); |
911 } |
879 } |
912 } |
880 } |
913 if ((rc = read_w1(device->address, (char *)"state")) >= 0) { |
881 if ((rc = read_w1(device->address, (char *)"state")) >= 0) { |
914 #ifdef HAVE_WIRINGPI_H |
882 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
915 piLock(LOCK_DEVICES); |
|
916 #endif |
|
917 /* |
883 /* |
918 * Read PIOA or PIOB pin state bits |
884 * Read PIOA or PIOB pin state bits |
919 */ |
885 */ |
920 if (device->subdevice == 0) |
886 if (device->subdevice == 0) |
921 device->value = (rc & 0x01) ? 0 : 1; |
887 device->value = (rc & 0x01) ? 0 : 1; |
922 else if (device->subdevice == 1) |
888 else if (device->subdevice == 1) |
923 device->value = (rc & 0x04) ? 0 : 1; |
889 device->value = (rc & 0x04) ? 0 : 1; |
924 device->timestamp = time(NULL); |
890 device->timestamp = time(NULL); |
925 #ifdef HAVE_WIRINGPI_H |
891 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
926 piUnlock(LOCK_DEVICES); |
|
927 #endif |
|
928 } |
892 } |
929 } else { |
893 } else { |
930 if (device->present != DEVPRESENT_NO) { |
894 if (device->present != DEVPRESENT_NO) { |
931 syslog(LOG_NOTICE, "DS2413 %s is missing", device->address); |
895 syslog(LOG_NOTICE, "DS2413 %s is missing", device->address); |
932 #ifdef HAVE_WIRINGPI_H |
896 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
933 piLock(LOCK_DEVICES); |
|
934 #endif |
|
935 device->present = DEVPRESENT_NO; |
897 device->present = DEVPRESENT_NO; |
936 #ifdef HAVE_WIRINGPI_H |
898 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
937 piUnlock(LOCK_DEVICES); |
|
938 #endif |
|
939 } |
899 } |
940 } |
900 } |
941 free(addr); |
901 free(addr); |
942 addr = NULL; |
902 addr = NULL; |
943 } |
903 } |
954 if ((int)(now - dht11_last) > 30) { |
914 if ((int)(now - dht11_last) > 30) { |
955 if (device->subdevice == 0) { |
915 if (device->subdevice == 0) { |
956 /* Read once during subdevice 0 */ |
916 /* Read once during subdevice 0 */ |
957 dht11_pin = device->gpiopin; |
917 dht11_pin = device->gpiopin; |
958 dht11Read(); |
918 dht11Read(); |
959 piLock(LOCK_DEVICES); |
919 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
960 if (dht11_valid) { |
920 if (dht11_valid) { |
961 device->value = dht11_temperature * 1000; |
921 device->value = dht11_temperature * 1000; |
962 device->timestamp = time(NULL); |
922 device->timestamp = time(NULL); |
963 device->present = DEVPRESENT_YES; |
923 device->present = DEVPRESENT_YES; |
964 } else { |
924 } else { |
965 device->present = DEVPRESENT_ERROR; |
925 device->present = DEVPRESENT_ERROR; |
966 } |
926 } |
967 piUnlock(LOCK_DEVICES); |
927 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
968 } else if (device->subdevice == 1) { |
928 } else if (device->subdevice == 1) { |
969 /* Data already present */ |
929 /* Data already present */ |
970 piLock(LOCK_DEVICES); |
930 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
971 if (dht11_valid) { |
931 if (dht11_valid) { |
972 device->value = dht11_humidity * 1000; |
932 device->value = dht11_humidity * 1000; |
973 device->timestamp = time(NULL); |
933 device->timestamp = time(NULL); |
974 device->present = DEVPRESENT_YES; |
934 device->present = DEVPRESENT_YES; |
975 } else { |
935 } else { |
976 device->present = DEVPRESENT_ERROR; |
936 device->present = DEVPRESENT_ERROR; |
977 } |
937 } |
978 piUnlock(LOCK_DEVICES); |
938 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
979 dht11_last = now; /* Okay for a new reading. */ |
939 dht11_last = now; /* Okay for a new reading. */ |
980 } |
940 } |
981 } |
941 } |
982 break; |
942 break; |
983 |
943 |
984 case DEVTYPE_GPIO: |
944 case DEVTYPE_GPIO: |
985 if (device->direction == DEVDIR_IN_BIN) { |
945 if (device->direction == DEVDIR_IN_BIN) { |
986 piLock(LOCK_DEVICES); |
946 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
987 device->value = digitalRead(device->gpiopin); |
947 device->value = digitalRead(device->gpiopin); |
988 device->offset = 0; |
948 device->offset = 0; |
989 device->timestamp = time(NULL); |
949 device->timestamp = time(NULL); |
990 piUnlock(LOCK_DEVICES); |
950 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]); |
991 } |
951 } |
992 break; |
952 break; |
993 |
953 |
994 #endif |
954 #endif |
995 #ifdef USE_SIMULATOR |
955 #ifdef USE_SIMULATOR |
996 case DEVTYPE_SIM: |
956 case DEVTYPE_SIM: |
997 #ifdef HAVE_WIRINGPI_H |
957 pthread_mutex_lock(&mutexes[LOCK_DEVICES]); |
998 piLock(LOCK_DEVICES); |
|
999 #endif |
|
1000 if (Config.simulators) { |
958 if (Config.simulators) { |
1001 simulator = Config.simulators; |
959 simulator = Config.simulators; |
1002 if (device->subdevice == 0) { |
960 if (device->subdevice == 0) { |
1003 device->value = (int)((int)(simulator->room_temperature * 1000) / 500) * 500; |
961 device->value = (int)((int)(simulator->room_temperature * 1000) / 500) * 500; |
1004 device->timestamp = time(NULL); |
962 device->timestamp = time(NULL); |