thermferm/devices.c

changeset 643
586d376ab629
parent 596
e3d3cde32847
child 644
07cc86900473
equal deleted inserted replaced
642:5cb83127d285 643:586d376ab629
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2014..2019 2 * Copyright (C) 2014..2024
3 * 3 *
4 * Michiel Broek <mbroek at mbse dot eu> 4 * Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * This file is part of the mbsePi-apps 6 * This file is part of the mbsePi-apps
7 * 7 *
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:

mercurial