40 #endif |
40 #endif |
41 |
41 |
42 #ifdef HAVE_WIRINGPI_H |
42 #ifdef HAVE_WIRINGPI_H |
43 |
43 |
44 #define MAXTIMINGS 100 |
44 #define MAXTIMINGS 100 |
|
45 |
|
46 /* |
|
47 * Since kernel version 4 there is a module, and a dtoverlay so that the |
|
48 * temperature and humidity can simply read from the /sys filesystem. |
|
49 * But this isn't as reliable as this userspace function, so we leave |
|
50 * that here. |
|
51 */ |
45 |
52 |
46 int dht11_pin = -1; |
53 int dht11_pin = -1; |
47 int dht11_temperature = -1; |
54 int dht11_temperature = -1; |
48 int dht11_humidity = -1; |
55 int dht11_humidity = -1; |
49 int dht11_valid = FALSE; |
56 int dht11_valid = FALSE; |
927 * Make sure we don't read the sensor within 2 seconds. |
934 * Make sure we don't read the sensor within 2 seconds. |
928 * But we use 30 seconds interval. |
935 * But we use 30 seconds interval. |
929 */ |
936 */ |
930 now = time(NULL); |
937 now = time(NULL); |
931 if ((int)(now - dht11_last) > 30) { |
938 if ((int)(now - dht11_last) > 30) { |
932 dht11_pin = device->gpiopin; |
|
933 dht11Read(); |
|
934 dht11_last = now; |
|
935 if (device->subdevice == 0) { |
939 if (device->subdevice == 0) { |
|
940 /* Read once during subdevice 0 */ |
|
941 dht11_pin = device->gpiopin; |
|
942 dht11Read(); |
936 piLock(LOCK_DEVICES); |
943 piLock(LOCK_DEVICES); |
937 if (dht11_valid) { |
944 if (dht11_valid) { |
938 device->value = dht11_temperature * 1000; |
945 device->value = dht11_temperature * 1000; |
939 device->timestamp = time(NULL); |
946 device->timestamp = time(NULL); |
940 device->present = DEVPRESENT_YES; |
947 device->present = DEVPRESENT_YES; |
941 } else { |
948 } else { |
942 device->present = DEVPRESENT_ERROR; |
949 device->present = DEVPRESENT_ERROR; |
943 } |
950 } |
944 piUnlock(LOCK_DEVICES); |
951 piUnlock(LOCK_DEVICES); |
945 } else if (device->subdevice == 1) { |
952 } else if (device->subdevice == 1) { |
|
953 /* Data already present */ |
946 piLock(LOCK_DEVICES); |
954 piLock(LOCK_DEVICES); |
947 if (dht11_valid) { |
955 if (dht11_valid) { |
948 device->value = dht11_humidity * 1000; |
956 device->value = dht11_humidity * 1000; |
949 device->timestamp = time(NULL); |
957 device->timestamp = time(NULL); |
950 device->present = DEVPRESENT_YES; |
958 device->present = DEVPRESENT_YES; |
951 } else { |
959 } else { |
952 device->present = DEVPRESENT_ERROR; |
960 device->present = DEVPRESENT_ERROR; |
953 } |
961 } |
954 piUnlock(LOCK_DEVICES); |
962 piUnlock(LOCK_DEVICES); |
|
963 dht11_last = now; /* Okay for a new reading. */ |
955 } |
964 } |
956 } |
965 } |
957 break; |
966 break; |
958 |
967 |
959 case DEVTYPE_GPIO: |
968 case DEVTYPE_GPIO: |