thermferm/devices.c

Mon, 25 Mar 2024 17:14:56 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 25 Mar 2024 17:14:56 +0100
changeset 650
0b215e4b814e
parent 649
64cfc01ec024
child 652
16d3d4b58b5b
permissions
-rw-r--r--

Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.

158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
643
586d376ab629 Updates for DHT11 reading
Michiel Broek <mbroek@mbse.eu>
parents: 596
diff changeset
2 * Copyright (C) 2014..2024
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * Michiel Broek <mbroek at mbse dot eu>
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * This file is part of the mbsePi-apps
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This is free software; you can redistribute it and/or modify it
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * Free Software Foundation; either version 2, or (at your option) any
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * later version.
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 *
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * mbsePi-apps is distributed in the hope that it will be useful, but
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * General Public License for more details.
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 *
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 * along with thermferm; see the file COPYING. If not, write to the Free
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *****************************************************************************/
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
23 #include "thermferm.h"
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 #include "devices.h"
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
25 #include "rc-switch.h"
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
26 #include "panel.h"
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 #include "xutil.h"
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29
646
e3edc783006b Running threads and locking doesn't depend on wiringPi anymore.
Michiel Broek <mbroek@mbse.eu>
parents: 645
diff changeset
30 int my_devices_state = 0;
e3edc783006b Running threads and locking doesn't depend on wiringPi anymore.
Michiel Broek <mbroek@mbse.eu>
parents: 645
diff changeset
31
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 extern sys_config Config;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 extern int my_shutdown;
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
34 extern pthread_mutex_t mutexes[5];
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35
362
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
36 #ifdef USE_SIMULATOR
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
37
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
38 extern int SIMcooling;
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
39 extern int SIMheating;
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
40 extern int SIMfan;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
41 extern int SIMlight;
362
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
42
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
43 #endif
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
45
643
586d376ab629 Updates for DHT11 reading
Michiel Broek <mbroek@mbse.eu>
parents: 596
diff changeset
46 /*
586d376ab629 Updates for DHT11 reading
Michiel Broek <mbroek@mbse.eu>
parents: 596
diff changeset
47 * Since kernel version 4 there is a module, and a dtoverlay so that the
586d376ab629 Updates for DHT11 reading
Michiel Broek <mbroek@mbse.eu>
parents: 596
diff changeset
48 * temperature and humidity can simply read from the /sys filesystem.
586d376ab629 Updates for DHT11 reading
Michiel Broek <mbroek@mbse.eu>
parents: 596
diff changeset
49 */
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
50 int dht11_temperature = -1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
51 int dht11_humidity = -1;
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
52 int dht11_state = DEVPRESENT_UNDEF;
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
53 time_t dht11_next;
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
54
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
55
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
56
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
57 /*
213
2317b8d644fa Code cleanup, streamlined error messages.
Michiel Broek <mbroek@mbse.eu>
parents: 212
diff changeset
58 * DHT11 sensor read.
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
59 * Called at 30 seconds interval if all is well.
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
60 * If last read was an error, the interval is 2 seconds.
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
61 */
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
62 void dht11Read(char *address)
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
63 {
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
64 int temp, hum;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
65 int fd, rc, err;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
66 char buffer[25], *dhtpath = NULL;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
67
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
68 dht11_temperature = -1;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
69 dht11_humidity = -1;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
70 dht11_state = DEVPRESENT_UNDEF;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
71 dhtpath = xstrcpy((char *)"/sys/bus/iio/devices/");
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
72 dhtpath = xstrcat(dhtpath, address);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
73 dhtpath = xstrcat(dhtpath, (char *)"/in_temp_input");
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
74
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
75 fd = open(dhtpath, O_RDONLY);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
76 if (fd < 0) {
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
77 /*
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
78 * The sensor is gone.
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
79 */
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
80 err = errno;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
81 syslog(LOG_NOTICE, "DHT11 open temperature: %d %s", err, strerror(err));
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
82 free(dhtpath);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
83 dht11_state = DEVPRESENT_NO;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
84 return;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
85 }
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
86
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
87 rc = read(fd, buffer, 25);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
88 if (rc == -1) {
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
89 err = errno;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
90 if (err == 110) {
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
91 dht11_state = DEVPRESENT_NO; /* Device is gone */
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
92 } else {
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
93 dht11_state = DEVPRESENT_ERROR;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
94 }
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
95 syslog(LOG_NOTICE, "DHT11 read temperature: %d %s", err, strerror(err));
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
96 } else {
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
97 sscanf(buffer, "%d", &temp);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
98 dht11_temperature = temp;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
99 dht11_state = DEVPRESENT_YES;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
100 }
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
101 close(fd);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
102 free(dhtpath);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
103 dhtpath = NULL;
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
104
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
105 /*
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
106 * Only read humidity if state is DEVPRESENT_YES
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
107 */
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
108 if (dht11_state == DEVPRESENT_YES) {
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
109 dhtpath = xstrcpy((char *)"/sys/bus/iio/devices/");
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
110 dhtpath = xstrcat(dhtpath, address);
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
111 dhtpath = xstrcat(dhtpath, (char *)"/in_humidityrelative_input");
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
112
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
113 fd = open(dhtpath, O_RDONLY);
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
114 rc = read(fd, buffer, 25);
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
115 if (rc == -1) {
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
116 err = errno;
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
117 if (err == 110) {
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
118 dht11_state = DEVPRESENT_NO;
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
119 } else {
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
120 dht11_state = DEVPRESENT_ERROR;
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
121 }
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
122 syslog(LOG_NOTICE, "DHT11 read humidity: %d %s", err, strerror(err));
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
123 dht11_temperature = -1; /* Make invalid */
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
124 } else {
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
125 sscanf(buffer, "%d", &hum);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
126 dht11_humidity = hum;
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
127 dht11_state = DEVPRESENT_YES;
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
128 }
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
129 close(fd);
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
130 free(dhtpath);
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
131 dhtpath = NULL;
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
132 }
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
133
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
134 syslog(LOG_NOTICE, "dht11 t:%d h:%d state:%d", dht11_temperature, dht11_humidity, dht11_state);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
135 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
136
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
137
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
139 /*
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
140 * Read one byte from a 1-wire device like a DS2413
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
141 */
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
142 int read_w1(char *address, char *file)
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
143 {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
144 char *addr = NULL;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
145 int fn = -1, rc = -1, retries = 5;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
146 uint8_t val;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
147
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
148 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
149 addr = xstrcat(addr, address);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
150 addr = xstrcat(addr, (char *)"/");
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
151 addr = xstrcat(addr, file);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
152
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
153 if ((fn = open(addr, O_RDONLY)) >= 0) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
154
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
155 if ((lseek(fn, 0L, SEEK_SET)) == 0) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
156
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
157 while (retries--) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
158 if ((read(fn, &val, 1)) == 1) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
159 rc = (int)val;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
160 goto leave;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
161 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
162 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
163 syslog(LOG_NOTICE, "read_w1() read %s fatal: %s", addr, strerror(errno));
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
164
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
165 } else {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
166 syslog(LOG_NOTICE, "read_w1() lseek %s: %s", addr, strerror(errno));
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
167 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
168
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
169 } else {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
170 syslog(LOG_NOTICE, "read_w1() open %s: %s", addr, strerror(errno));
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
171 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
172
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
173 leave:
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
174 if (fn != -1) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
175 if ((close(fn)) == -1) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
176 syslog(LOG_NOTICE, "read_w1() close %s: %s", addr, strerror(errno));
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
177 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
178 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
179
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
180 free(addr);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
181 return rc;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
182 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
183
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
184
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
185
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
186 /*
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
187 * Write a byte to a 1-wire device like a DS2413
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
188 */
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
189 int write_w1(char *address, char *file, uint8_t val)
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
190 {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
191 char *addr = NULL;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
192 int fn = -1, rc = -1, retries = 5;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
193
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
194 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
195 addr = xstrcat(addr, address);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
196 addr = xstrcat(addr, (char *)"/");
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
197 addr = xstrcat(addr, file);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
198
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
199 if ((fn = open(addr, O_WRONLY)) >= 0) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
200
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
201 if ((lseek(fn, 0L, SEEK_SET)) == 0) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
202
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
203 while (retries--) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
204 if ((write(fn, &val, 1)) == 1) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
205 rc = 0;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
206 goto leave;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
207 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
208 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
209 syslog(LOG_NOTICE, "write_w1() write %s fatal: %s", addr, strerror(errno));
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
210
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
211 } else {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
212 syslog(LOG_NOTICE, "write_w1() lseek %s: %s", addr, strerror(errno));
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
213 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
214
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
215 } else {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
216 syslog(LOG_NOTICE, "write_w1() open %s: %s", addr, strerror(errno));
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
217 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
218
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
219 leave:
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
220 if (fn != -1) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
221 if ((close(fn)) == -1) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
222 syslog(LOG_NOTICE, "write_w1() close %s: %s", addr, strerror(errno));
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
223 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
224 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
225
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
226 free(addr);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
227 return rc;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
228 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
229
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
230
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
231
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
232 int device_out(char *uuid, int value)
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
233 {
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
234 devices_list *device;
323
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
235 time_t now, my_timestamp;
354
5ff387f4d6b7 Fixed compile issue on non-Pi systems
Michiel Broek <mbroek@mbse.eu>
parents: 351
diff changeset
236 int rc, my_value, test_value;
187
3c8bf18fdf42 Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents: 185
diff changeset
237 #ifdef HAVE_WIRINGPI_H
354
5ff387f4d6b7 Fixed compile issue on non-Pi systems
Michiel Broek <mbroek@mbse.eu>
parents: 351
diff changeset
238 int i;
187
3c8bf18fdf42 Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents: 185
diff changeset
239 char buf[40];
3c8bf18fdf42 Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents: 185
diff changeset
240 #endif
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
241
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
242 if (uuid == NULL)
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
243 return 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
244
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
245 now = time(NULL);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
246 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
337
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
247
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
248 for (device = Config.devices; device; device = device->next) {
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
249 if (! strcmp(uuid, device->uuid)) {
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
250 /*
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
251 * Execute command if different then the old value. But also
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
252 * every 2 minutes because commands can have temporary
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
253 * disconnects, or have radio problems.
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
254 */
323
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
255 my_timestamp = device->timestamp;
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
256 my_value = device->value;
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
257
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
258 if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN)) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
259 test_value = (value == 0) ? 0 : 1;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
260 } else {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
261 test_value = value;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
262 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
263
351
d8c410c320db Fixed auto device update timer
Michiel Broek <mbroek@mbse.eu>
parents: 348
diff changeset
264 if ((test_value != my_value) || (((int)now - (int)my_timestamp) >= 120)) {
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
265
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
266 #ifdef HAVE_WIRINGPI_H
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
267 rc = 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
268 if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) {
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
269 snprintf(buf, 39, "%s,%d", device->address, value ? 1:0);
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
270 for (i = 0; i < strlen(buf); i++)
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
271 if (buf[i] == '-')
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
272 buf[i] = ',';
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
273 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
274 enableTransmit(device->gpiopin);
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
275 rc = toggleSwitch(buf);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
276 disableTransmit();
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
277 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
278 syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
279 device->value = value;
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
280 device->timestamp = time(NULL);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
281 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
282 return rc;
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
283 }
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
284
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
285 if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) {
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
286
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
287 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
288 #endif
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
289 if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
290 if (strncmp(device->address, (char *)"3a", 2) == 0) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
291 /*
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
292 * DS2413. First read state so that we can preserve the state of
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
293 * the "other" PIO channel. To make things a bit more complicated
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
294 * the bits in the state register differ from the output register.
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
295 */
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
296 uint8_t state, output;
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
297
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
298 if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
299 state = (unsigned int)rc;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
300 output = (state & 0x01) + ((state & 0x04) >> 1);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
301
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
302 if (device->subdevice == 0) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
303 output = (output & 0xfe);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
304 output |= (value == 0) ? 0x01 : 0x00;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
305 } else if (device->subdevice == 1) {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
306 output = (output & 0xfd);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
307 output |= (value == 0) ? 0x02 : 0x00;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
308 } else {
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
309 output = 0xff;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
310 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
311
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
312 if ((write_w1(device->address, (char *)"output", output)) == 0) {
348
ffc4b8aa824f Changed Fan switch logic.
Michiel Broek <mbroek@mbse.eu>
parents: 347
diff changeset
313 syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
314 device->value = (value == 0) ? 0 : 1;
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
315 device->timestamp = time(NULL);
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
316 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
317 }
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
318 }
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
319 }
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
320
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
321 #ifdef USE_SIMULATOR
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
322 if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
323 if ((strcmp((char *)"SimCooler", device->address) == 0) || (strcmp((char *)"SimHeater", device->address) == 0) ||
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
324 (strcmp((char *)"SimFan" , device->address) == 0) || (strcmp((char *)"SimLight" , device->address) == 0)) {
362
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
325 if (value != device->value) {
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
326 syslog(LOG_NOTICE, "SIM %s value=%d", device->address, value);
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
327 }
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
328 device->value = value;
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
329 device->timestamp = time(NULL);
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
330 if (strcmp((char *)"SimCooler", device->address) == 0)
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
331 SIMcooling = value;
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
332 if (strcmp((char *)"SimHeater", device->address) == 0)
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
333 SIMheating = value;
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
334 if (strcmp((char *)"SimFan", device->address) == 0)
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
335 SIMfan = value;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
336 if (strcmp((char *)"SimLight", device->address) == 0)
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
337 SIMlight = value;
362
c92651a54969 Made the client-server protocol more robust. When a change to a unit is made using the web interface, the main process is stopped during the update. Splitted the PID in two PID's, one for heating and one for cooling. Adjusted the web edit scrreen for this, but there are still rough edges. Replaced the PID code, maybe this one works better for our purpose. The simulator air temperature changes on the simulator heater and cooler, but it is not realistic at all. This is a development version, do not use in production. The version is 0.3.0
Michiel Broek <mbroek@mbse.eu>
parents: 354
diff changeset
338 }
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
339 }
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
340 #endif
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
341 } else {
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
342 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
343 return 0;
583
9795a16de807 Attempt to better check the temperature sensors and lower the cpu load.
Michiel Broek <mbroek@mbse.eu>
parents: 573
diff changeset
344 } // if ((test_value != my_value) || (((int)now - (int)my_timestamp) >= 120))
9795a16de807 Attempt to better check the temperature sensors and lower the cpu load.
Michiel Broek <mbroek@mbse.eu>
parents: 573
diff changeset
345 } // if (! strcmp(uuid, device->uuid))
9795a16de807 Attempt to better check the temperature sensors and lower the cpu load.
Michiel Broek <mbroek@mbse.eu>
parents: 573
diff changeset
346 } // for (device = Config.devices; device; device = device->next)
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
347
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
348 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
349 return 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
350 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
351
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
352
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
353 /*
395
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
354 * Returns DEVPRESENT_NO if failed.
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
355 * Returns DEVPRESENT_YES if success, value contains new value.
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
356 */
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
357 int device_in(char *uuid, int *value)
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
358 {
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
359 devices_list *device;
395
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
360 int tmp, present;
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
361
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
362 if (uuid == NULL)
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
363 return 0;
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
364
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
365 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
366
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
367 for (device = Config.devices; device; device = device->next) {
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
368 if (! strcmp(uuid, device->uuid)) {
395
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
369 present = device->present;
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
370 if (present == DEVPRESENT_YES) {
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
371 tmp = device->value + device->offset;
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
372 } else {
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
373 tmp = 0;
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
374 }
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
375 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
395
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
376 *value = tmp;
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
377 return present;
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
378 }
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
379 }
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
380
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
381 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
395
e32e83550963 Switched to new device read code. Removed the incomplete support for the DS2408 1-wire chip.
Michiel Broek <mbroek@mbse.eu>
parents: 394
diff changeset
382 return DEVPRESENT_NO;
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
383 }
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
384
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
385
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
386
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
387
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
388 /*
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
389 * Auto detect hotplugged or known to be present devices
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
390 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
391 int devices_detect(void)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
392 {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
393 struct dirent *de;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
394 DIR *fd;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
395 devices_list *device, *ndev;
340
5b49416eb116 Experimental idea
Michiel Broek <mbroek@mbse.eu>
parents: 338
diff changeset
396 int found, subdevices, ival, i, rc = 0;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
397 char buf[40];
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
398 uuid_t uu;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
399 #ifdef HAVE_WIRINGPI_H
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
400 int pin;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
401 #endif
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
402
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
403 /*
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
404 * Scan for 1-wire devices
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
405 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
406 if ((fd = opendir((char *)"/sys/bus/w1/devices"))) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
407 while ((de = readdir(fd))) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
408 if (de->d_name[0] != '.') {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
409 found = FALSE;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
410 for (device = Config.devices; device; device = device->next) {
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
411 if (strcmp(device->address,de->d_name) == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
412 found = TRUE;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
413 break;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
414 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
415 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
416
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
417 if (found == FALSE) {
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
418 strncpy(buf, de->d_name, 2);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
419 buf[2] = '\0';
340
5b49416eb116 Experimental idea
Michiel Broek <mbroek@mbse.eu>
parents: 338
diff changeset
420 sscanf(buf, "%02x", &ival);
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
421 subdevices = 0;
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
422
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
423 /*
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
424 * ival is zero when a ghost sensor is detected.
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
425 */
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
426 if (ival > 0)
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
427 subdevices = 1;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
428 if (strcmp(buf, (char *)"3a") == 0)
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
429 subdevices = 2;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
430 for (i = 0; i < subdevices; i++) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
431 ndev = (devices_list *)malloc(sizeof(devices_list));
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
432 ndev->next = NULL;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
433 ndev->uuid = malloc(37);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
434 uuid_generate(uu);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
435 uuid_unparse(uu, ndev->uuid);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
436 ndev->type = DEVTYPE_W1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
437 ndev->direction = DEVDIR_UNDEF;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
438 if (strcmp(buf, (char *)"10") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
439 ndev->direction = DEVDIR_IN_ANALOG;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
440 ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer");
338
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
441 } else if (strcmp(buf, (char *)"22") == 0) {
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
442 ndev->direction = DEVDIR_IN_ANALOG;
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
443 ndev->description = xstrcpy((char *)"DS1822 Digital thermometer");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
444 } else if (strcmp(buf, (char *)"28") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
445 ndev->direction = DEVDIR_IN_ANALOG;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
446 ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
447 } else if (strcmp(buf, (char *)"3a") == 0) {
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
448 ndev->direction = DEVDIR_IN_BIN;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
449 ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch");
338
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
450 } else if (strcmp(buf, (char *)"3b") == 0) {
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
451 ndev->direction = DEVDIR_IN_ANALOG;
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
452 ndev->description = xstrcpy((char *)"DS1825 Digital thermometer");
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
453 } else if (strcmp(buf, (char *)"42") == 0) {
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
454 ndev->direction = DEVDIR_IN_ANALOG;
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
455 ndev->description = xstrcpy((char *)"DS28EA00 Digital thermometer");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
456 } else if (strcmp(buf, (char *)"w1") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
457 ndev->description = xstrcpy((char *)"Master System device");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
458 } else {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
459 ndev->description = xstrcpy((char *)"Unknown device family ");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
460 ndev->description = xstrcat(ndev->description, buf);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
461 }
343
ba3a2d27d59e Initialize offset to zero with new detected 1-wire devices.
Michiel Broek <mbroek@mbse.eu>
parents: 342
diff changeset
462 ndev->value = ndev->offset = ndev->inuse = 0;
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
463 ndev->present = DEVPRESENT_YES;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
464 ndev->address = xstrcpy(de->d_name);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
465 ndev->subdevice = i;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
466 ndev->gpiopin = -1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
467 ndev->comment = xstrcpy((char *)"Auto detected device");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
468 ndev->timestamp = time(NULL);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
469
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
470 syslog(LOG_NOTICE, "New W1 device %s, subdevice %d, %s", ndev->address, ndev->subdevice, ndev->description);
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
471
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
472 if (Config.devices == NULL) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
473 Config.devices = ndev;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
474 } else {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
475 for (device = Config.devices; device; device = device->next) {
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
476 if (device->next == NULL) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
477 device->next = ndev;
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
478 break;
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
479 }
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
480 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
481 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
482 rc++;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
483 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
484 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
485 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
486 }
166
c31ea86fec43 Forgot some close functions so tha program ran out of file descriptors. Better loggin for writing the configuration.
Michiel Broek <mbroek@mbse.eu>
parents: 164
diff changeset
487 closedir(fd);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
488 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
489
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
490 /*
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
491 * DHT11 as kernel module. The number after the @ is the hexadecimal
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
492 * number of the BCM gpio pin. Since we use pin 22 at connector pin 15
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
493 * the number is 16.
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
494 */
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
495 char *dhtaddr = NULL;
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
496 if ((fd = opendir((char *)"/sys/devices/platform/dht11@16"))) {
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
497 while ((de = readdir(fd))) {
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
498 if (de->d_name[0] != '.') {
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
499 if (strncmp(de->d_name, (char *)"iio", 3) == 0) {
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
500 dhtaddr = xstrcpy(de->d_name);
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
501 }
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
502 }
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
503 }
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
504 closedir(fd);
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
505 }
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
506 syslog(LOG_NOTICE, "%s", dhtaddr);
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
507
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
508 if (dhtaddr) {
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
509
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
510 found = FALSE;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
511 for (device = Config.devices; device; device = device->next) {
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
512 if (strcmp(device->address, dhtaddr) == 0) {
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
513 found = TRUE;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
514 break;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
515 }
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
516 }
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
517
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
518 if (found == FALSE) {
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
519 for (i = 0; i < 2; i++) {
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
520 ndev = (devices_list *)malloc(sizeof(devices_list));
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
521 ndev->next = NULL;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
522 ndev->uuid = malloc(37);
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
523 uuid_generate(uu);
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
524 uuid_unparse(uu, ndev->uuid);
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
525 ndev->type = DEVTYPE_DHT;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
526 ndev->direction = DEVDIR_IN_ANALOG;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
527 if (i == 0)
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
528 ndev->description = xstrcpy((char *)"DHT11 temperature sensor");
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
529 else
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
530 ndev->description = xstrcpy((char *)"DHT11 humidity sensor");
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
531 ndev->value = ndev->offset = ndev->inuse = 0;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
532 ndev->present = DEVPRESENT_YES;
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
533 ndev->address = xstrcpy(dhtaddr);
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
534 ndev->subdevice = i;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
535 ndev->gpiopin = -1;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
536 ndev->comment = xstrcpy((char *)"Auto detected device");
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
537 ndev->timestamp = time(NULL);
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
538
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
539 syslog(LOG_NOTICE, "New DHT11 device %s, subdevice %d, %s", ndev->address, ndev->subdevice, ndev->description);
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
540
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
541 if (Config.devices == NULL) {
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
542 Config.devices = ndev;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
543 } else {
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
544 for (device = Config.devices; device; device = device->next) {
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
545 if (device->next == NULL) {
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
546 device->next = ndev;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
547 break;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
548 }
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
549 }
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
550 }
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
551 rc++;
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
552 }
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
553 }
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
554 }
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
555
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
556 #ifdef HAVE_WIRINGPI_H
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
557 if (piBoardRev() == 2) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
558 /*
324
940a668e568f Update for newer boards
Michiel Broek <mbroek@mbse.eu>
parents: 323
diff changeset
559 * Support rev B and newer boards only
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
560 */
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
561 found = FALSE;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
562 for (device = Config.devices; device; device = device->next) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
563 if (device->type == DEVTYPE_GPIO) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
564 found = TRUE;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
565 break;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
566 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
567 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
568
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
569 if (found == FALSE) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
570 /*
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
571 * There were no GPIO devices found.
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
572 */
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
573 subdevices = 12;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
574 pin = 0;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
575 for (i = 0; i < subdevices; i++) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
576 if (i == 8)
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
577 pin = 17;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
578
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
579 ndev = (devices_list *)malloc(sizeof(devices_list));
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
580 ndev->next = NULL;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
581 ndev->uuid = malloc(37);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
582 uuid_generate(uu);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
583 uuid_unparse(uu, ndev->uuid);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
584 ndev->type = DEVTYPE_GPIO;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
585 ndev->value = digitalRead(pin);
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
586 ndev->offset = 0;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
587 ndev->present = DEVPRESENT_YES;
163
3d813570a5e3 LIST BUS now displays the new devices table. Adjusted the web screen
Michiel Broek <mbroek@mbse.eu>
parents: 162
diff changeset
588 ndev->address = xstrcpy((char *)"GPIO");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
589 snprintf(buf, 39, "Raspberry GPIO %d", i);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
590 ndev->description = xstrcpy(buf);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
591 ndev->subdevice = i;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
592 ndev->gpiopin = pin;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
593 ndev->timestamp = time(NULL);
393
220f2f30bf68 Fixed initialisation of new GPIO RS433 and DHT11 devices.
Michiel Broek <mbroek@mbse.eu>
parents: 392
diff changeset
594 if (i == 0) {
220f2f30bf68 Fixed initialisation of new GPIO RS433 and DHT11 devices.
Michiel Broek <mbroek@mbse.eu>
parents: 392
diff changeset
595 ndev->direction = DEVDIR_INTERN;
220f2f30bf68 Fixed initialisation of new GPIO RS433 and DHT11 devices.
Michiel Broek <mbroek@mbse.eu>
parents: 392
diff changeset
596 ndev->inuse = 1;
394
14231379bec2 Spelling correction.
Michiel Broek <mbroek@mbse.eu>
parents: 393
diff changeset
597 ndev->comment = xstrcpy((char *)"433 Mhz transmitter");
393
220f2f30bf68 Fixed initialisation of new GPIO RS433 and DHT11 devices.
Michiel Broek <mbroek@mbse.eu>
parents: 392
diff changeset
598 } else if (i == 3) {
220f2f30bf68 Fixed initialisation of new GPIO RS433 and DHT11 devices.
Michiel Broek <mbroek@mbse.eu>
parents: 392
diff changeset
599 ndev->direction = DEVDIR_INTERN;
220f2f30bf68 Fixed initialisation of new GPIO RS433 and DHT11 devices.
Michiel Broek <mbroek@mbse.eu>
parents: 392
diff changeset
600 ndev->inuse = 1;
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
601 ndev->comment = xstrcpy((char *)"DHT11 sensor");
393
220f2f30bf68 Fixed initialisation of new GPIO RS433 and DHT11 devices.
Michiel Broek <mbroek@mbse.eu>
parents: 392
diff changeset
602 } else if (i == PANEL_LED) {
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
603 ndev->direction = DEVDIR_OUT_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
604 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
605 ndev->comment = xstrcpy((char *)"Frontpanel LED");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
606 } else if (i == PANEL_ENTER) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
607 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
608 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
609 ndev->comment = xstrcpy((char *)"Frontpanel Enter key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
610 } else if (i == PANEL_DOWN) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
611 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
612 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
613 ndev->comment = xstrcpy((char *)"Frontpanel Down key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
614 } else if (i == PANEL_UP) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
615 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
616 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
617 ndev->comment = xstrcpy((char *)"Frontpanel Up key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
618 } else if (i == 7) {
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
619 ndev->direction = DEVDIR_INTERN;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
620 ndev->inuse = 1;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
621 ndev->comment = xstrcpy((char *)"1-Wire bus");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
622 } else {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
623 ndev->direction = DEVDIR_IN_BIN;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
624 ndev->inuse = 0;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
625 ndev->comment = xstrcpy((char *)"Raspberry GPIO");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
626 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
627 pin++;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
628
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
629 syslog(LOG_NOTICE, "New GPIO device %s, subdevice %d, %s", ndev->address, ndev->subdevice, ndev->description);
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
630
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
631 if (Config.devices == NULL) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
632 Config.devices = ndev;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
633 } else {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
634 for (device = Config.devices; device; device = device->next) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
635 if (device->next == NULL) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
636 device->next = ndev;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
637 break;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
638 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
639 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
640 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
641 rc++;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
642 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
643 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
644 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
645 #endif
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
646
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
647 #ifdef USE_SIMULATOR
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
648 found = 0;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
649 for (device = Config.devices; device; device = device->next) {
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
650 if (device->type == DEVTYPE_SIM) {
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
651 found++;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
652 }
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
653 }
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
654
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
655 /*
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
656 * Create simulated devices, or upgrade with new devices.
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
657 */
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
658 subdevices = 9;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
659 for (i = found; i < subdevices; i++) {
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
660 ndev = (devices_list *)malloc(sizeof(devices_list));
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
661 ndev->next = NULL;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
662 ndev->uuid = malloc(37);
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
663 uuid_generate(uu);
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
664 uuid_unparse(uu, ndev->uuid);
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
665 ndev->type = DEVTYPE_SIM;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
666 ndev->value = ndev->offset = 0;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
667 ndev->present = DEVPRESENT_YES;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
668 ndev->subdevice = i;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
669 ndev->gpiopin = -1;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
670 ndev->comment = xstrcpy((char *)"Auto detected device");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
671 ndev->timestamp = time(NULL);
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
672 ndev->inuse = 0;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
673 switch (i) {
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
674 case 0: ndev->direction = DEVDIR_IN_ANALOG;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
675 ndev->address = xstrcpy((char *)"SimRoomTemp");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
676 ndev->description = xstrcpy((char *)"Simulated room temperature");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
677 break;
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
678 case 1: ndev->direction = DEVDIR_IN_ANALOG;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
679 ndev->address = xstrcpy((char *)"SimAirTemp");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
680 ndev->description = xstrcpy((char *)"Simulated air temperature");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
681 break;
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
682 case 2: ndev->direction = DEVDIR_IN_ANALOG;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
683 ndev->address = xstrcpy((char *)"SimBeerTemp");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
684 ndev->description = xstrcpy((char *)"Simulated beer temperature");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
685 break;
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
686 case 3: ndev->direction = DEVDIR_OUT_BIN;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
687 ndev->address = xstrcpy((char *)"SimHeater");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
688 ndev->description = xstrcpy((char *)"Simulated heater");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
689 break;
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
690 case 4: ndev->direction = DEVDIR_OUT_BIN;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
691 ndev->address = xstrcpy((char *)"SimCooler");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
692 ndev->description = xstrcpy((char *)"Simulated cooler");
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
693 break;
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
694 case 5: ndev->direction = DEVDIR_IN_ANALOG;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
695 ndev->address = xstrcpy((char *)"SimRoomHum");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
696 ndev->description = xstrcpy((char *)"Simulated room humidity");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
697 break;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
698 case 6: ndev->direction = DEVDIR_IN_ANALOG;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
699 ndev->address = xstrcpy((char *)"SimChillerTemp");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
700 ndev->description = xstrcpy((char *)"Simulated Chiller temperature");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
701 break;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
702 case 7: ndev->direction = DEVDIR_OUT_BIN;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
703 ndev->address = xstrcpy((char *)"SimFan");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
704 ndev->description = xstrcpy((char *)"Simulated fan");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
705 break;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
706 case 8: ndev->direction = DEVDIR_OUT_BIN;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
707 ndev->address = xstrcpy((char *)"SimLight");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
708 ndev->description = xstrcpy((char *)"Simulated light");
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
709 break;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
710 }
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
711
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
712 syslog(LOG_NOTICE, "New Simulator device %s, subdevice %d, %s", ndev->address, ndev->subdevice, ndev->description);
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
713
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
714 if (Config.devices == NULL) {
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
715 Config.devices = ndev;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
716 } else {
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
717 for (device = Config.devices; device; device = device->next) {
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
718 if (device->next == NULL) {
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
719 device->next = ndev;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
720 break;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
721 }
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
722 }
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
723 }
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
724 rc++;
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
725 }
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
726 #endif
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
727
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
728 return rc;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
729 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
730
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
731
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
732
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
733 void *my_devices_loop(void *threadid)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
734 {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
735 devices_list *device;
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
736 #ifdef USE_SIMULATOR
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
737 simulator_list *simulator;
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
738 #endif
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
739 char *addr = NULL, line1[60], line2[60], *p = NULL;
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
740 FILE *fp;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
741 int temp, rc;
220
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
742 time_t now;
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
743
646
e3edc783006b Running threads and locking doesn't depend on wiringPi anymore.
Michiel Broek <mbroek@mbse.eu>
parents: 645
diff changeset
744 my_devices_state = 1;
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
745 syslog(LOG_NOTICE, "Thread my_devices_loop started");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
746
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
747 #ifdef HAVE_WIRINGPI_H
583
9795a16de807 Attempt to better check the temperature sensors and lower the cpu load.
Michiel Broek <mbroek@mbse.eu>
parents: 573
diff changeset
748 if ((rc = piHiPri(50)))
9795a16de807 Attempt to better check the temperature sensors and lower the cpu load.
Michiel Broek <mbroek@mbse.eu>
parents: 573
diff changeset
749 syslog(LOG_NOTICE, "my_devices_loop: piHiPri(50) rc=%d", rc);
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
750 #endif
239
793c09ece542 Increase priority for the devices thread
Michiel Broek <mbroek@mbse.eu>
parents: 235
diff changeset
751
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
752 dht11_next = time(NULL);
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
753
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
754 /*
588
322a29000fd7 Added sensor precision initialize. Changed PID logging.
Michiel Broek <mbroek@mbse.eu>
parents: 587
diff changeset
755 * Set the temperature sensors to 12 bits resolution and write it in EEPROM
587
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
756 */
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
757 for (device = Config.devices; device; device = device->next) {
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
758 if ((device->type == DEVTYPE_W1) &&
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
759 ((strncmp(device->address, (char *)"10", 2) == 0) ||
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
760 (strncmp(device->address, (char *)"22", 2) == 0) ||
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
761 (strncmp(device->address, (char *)"28", 2) == 0) ||
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
762 (strncmp(device->address, (char *)"3b", 2) == 0) ||
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
763 (strncmp(device->address, (char *)"42", 2) == 0))) {
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
764 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
765 addr = xstrcat(addr, device->address);
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
766 addr = xstrcat(addr, (char *)"/w1_slave");
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
767 if ((fp = fopen(addr, "w"))) {
588
322a29000fd7 Added sensor precision initialize. Changed PID logging.
Michiel Broek <mbroek@mbse.eu>
parents: 587
diff changeset
768 rc = fprintf(fp, "12\n0\n"); // According to the kernel documentation. Seems to work.
587
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
769 fclose(fp);
588
322a29000fd7 Added sensor precision initialize. Changed PID logging.
Michiel Broek <mbroek@mbse.eu>
parents: 587
diff changeset
770 if (rc != 5) {
322a29000fd7 Added sensor precision initialize. Changed PID logging.
Michiel Broek <mbroek@mbse.eu>
parents: 587
diff changeset
771 syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr);
587
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
772 }
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
773 }
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
774 free(addr);
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
775 addr = NULL;
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
776 }
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
777 }
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
778
b8580eedfcc7 Experimental 12 bits resolution init
Michiel Broek <mbroek@mbse.eu>
parents: 586
diff changeset
779 /*
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
780 * Loop forever until the external shutdown variable is set.
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
781 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
782 for (;;) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
783
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
784 /*
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
785 * Process all devices.
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
786 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
787 for (device = Config.devices; device; device = device->next) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
788
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
789 if (my_shutdown)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
790 break;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
791
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
792 switch (device->type) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
793 case DEVTYPE_W1:
338
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
794 /*
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
795 * Only tested with DS18B20 but from the kernel source this
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
796 * should work with all 1-wire thermometer sensors.
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
797 */
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
798 if ((strncmp(device->address, (char *)"10", 2) == 0) ||
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
799 (strncmp(device->address, (char *)"22", 2) == 0) ||
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
800 (strncmp(device->address, (char *)"28", 2) == 0) ||
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
801 (strncmp(device->address, (char *)"3b", 2) == 0) ||
8e29d142d67d Added support for all 1-wire temperature sensors that are in the Linux kernel source.
Michiel Broek <mbroek@mbse.eu>
parents: 337
diff changeset
802 (strncmp(device->address, (char *)"42", 2) == 0)) {
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
803 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
804 addr = xstrcat(addr, device->address);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
805 addr = xstrcat(addr, (char *)"/w1_slave");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
806 if ((fp = fopen(addr, "r"))) {
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
807 fgets(line1, 50, fp); // Read 2 lines
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
808 fgets(line2, 50, fp);
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
809 fclose(fp);
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
810 /*
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
811 * The output looks like:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
812 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
813 * 72 01 4b 46 7f ff 0e 10 57 t=23125
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
814 */
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
815 line1[strlen(line1)-1] = '\0';
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
816 if ((line1[36] == 'Y') && (line1[37] == 'E')) {
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
817 /* CRC is Ok, continue */
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
818 if (device->present != DEVPRESENT_YES) {
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
819 syslog(LOG_NOTICE, "sensor %s is Ok", device->address);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
820 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
821 device->present = DEVPRESENT_YES;
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
822 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
823 }
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
824
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
825 line2[strlen(line2)-1] = '\0';
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
826 strtok(line2, (char *)"=");
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
827 p = strtok(NULL, (char *)"=");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
828 rc = sscanf(p, "%d", &temp);
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
829 if ((rc == 1) && (device->value != temp)) {
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
830 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
589
230a502b75ec Final device thermometer error detection.
Michiel Broek <mbroek@mbse.eu>
parents: 588
diff changeset
831 if (temp < -55000) {
230a502b75ec Final device thermometer error detection.
Michiel Broek <mbroek@mbse.eu>
parents: 588
diff changeset
832 syslog(LOG_NOTICE, "sensor %s value error '%d` '%s`", device->address, temp, line1);
586
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
833 device->present = DEVPRESENT_ERROR;
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
834 } else {
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
835 device->value = temp;
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
836 device->timestamp = time(NULL);
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
837 device->present = DEVPRESENT_YES;
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
838 }
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
839 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
840 }
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
841 } else {
528
908ef02d5d22 Changed 1-wire logic a little bit
Michiel Broek <mbroek@mbse.eu>
parents: 527
diff changeset
842 syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
843 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
844 device->present = DEVPRESENT_ERROR;
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
845 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
846 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
847 } else {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
848 if (device->present != DEVPRESENT_NO) {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
849 syslog(LOG_NOTICE, "sensor %s is missing", device->address);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
850 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
851 device->present = DEVPRESENT_NO;
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
852 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
853 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
854 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
855 free(addr);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
856 addr = NULL;
341
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
857 } /* if temperature sensor */
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
858 /*
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
859 * DS2413 Dual channel addressable switch
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
860 */
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
861 if (strncmp(device->address, (char *)"3a", 2) == 0) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
862 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
863 addr = xstrcat(addr, device->address);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
864 addr = xstrcat(addr, (char *)"/state");
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
865
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
866 if ((access(addr, R_OK)) == 0) {
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
867 if (device->present != DEVPRESENT_YES) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
868 syslog(LOG_NOTICE, "DS2413 %s is back", device->address);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
869 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
870 device->present = DEVPRESENT_YES;
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
871 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
872 }
408
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
873 /*
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
874 * First make sure that if this device is configured as input
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
875 * to drive the output high.
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
876 */
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
877 if (device->direction == DEVDIR_IN_BIN) {
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
878 uint8_t state, output;
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
879
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
880 if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
881 state = (unsigned int)rc;
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
882 output = ((state & 0x02) >> 1) + ((state & 0x08) >> 2); /* Both latch states */
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
883 if (device->subdevice == 0) {
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
884 output = (output & 0xfe);
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
885 output |= 0x01;
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
886 } else if (device->subdevice == 1) {
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
887 output = (output & 0xfd);
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
888 output |= 0x02;
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
889 } else {
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
890 output = 0xff;
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
891 }
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
892 write_w1(device->address, (char *)"output", output);
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
893 }
ec507c1f1df7 Make sure that a DS2413 device used as input has the output latch turned off.
Michiel Broek <mbroek@mbse.eu>
parents: 395
diff changeset
894 }
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
895 if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
896 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
897 /*
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
898 * Read PIOA or PIOB pin state bits
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
899 */
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
900 if (device->subdevice == 0)
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
901 device->value = (rc & 0x01) ? 0 : 1;
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
902 else if (device->subdevice == 1)
347
0ab5c3fd7c77 Completed DS2413 device code and driver.
Michiel Broek <mbroek@mbse.eu>
parents: 344
diff changeset
903 device->value = (rc & 0x04) ? 0 : 1;
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
904 device->timestamp = time(NULL);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
905 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
906 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
907 } else {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
908 if (device->present != DEVPRESENT_NO) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
909 syslog(LOG_NOTICE, "DS2413 %s is missing", device->address);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
910 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
911 device->present = DEVPRESENT_NO;
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
912 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
913 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
914 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
915 free(addr);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
916 addr = NULL;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
917 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
918
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
919 break;
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
920
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
921 case DEVTYPE_DHT:
220
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
922 /*
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
923 * Don't read these to often, 2 seconds minimum delay.
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
924 * Delay 30 seconds if last update was successfull,
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
925 * else delay 2 seconds.
220
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
926 */
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
927 now = time(NULL);
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
928 if ((int)(now >= dht11_next)) {
586
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
929 if (device->subdevice == 0) {
643
586d376ab629 Updates for DHT11 reading
Michiel Broek <mbroek@mbse.eu>
parents: 596
diff changeset
930 /* Read once during subdevice 0 */
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
931 dht11Read(device->address);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
932 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
933 device->present = dht11_state;
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
934 if (dht11_state == DEVPRESENT_YES) {
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
935 device->value = dht11_temperature;
586
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
936 device->timestamp = time(NULL);
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
937 }
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
938 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
586
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
939 } else if (device->subdevice == 1) {
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
940 /* Data already present, valid or not. */
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
941 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
648
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
942 device->present = dht11_state;
62c5ed1b9cfd Better detection of the DHT11 sensors. Read DHT11 with errors detection and reapeated tries.
Michiel Broek <mbroek@mbse.eu>
parents: 647
diff changeset
943 if (dht11_state == DEVPRESENT_YES) {
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
944 device->value = dht11_humidity;
586
504463dad07d Changed 1-wire DS18B20 sensor error check.
Michiel Broek <mbroek@mbse.eu>
parents: 585
diff changeset
945 device->timestamp = time(NULL);
650
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
946 dht11_next = now + 29;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
947 } else {
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
948 dht11_next = now + 1;
0b215e4b814e Brought the retry attempts to read the DHT11 sensors to the main devices loop. The actual read function is now very simple. Called every 30 seconds when all is well, or 2 seconds if something is wrong.
Michiel Broek <mbroek@mbse.eu>
parents: 649
diff changeset
949 }
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
950 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
951 }
585
326cf2982eee More relaxed DHT11 readings
Michiel Broek <mbroek@mbse.eu>
parents: 584
diff changeset
952 }
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
953 break;
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
954
647
fcd85176ea2e Work to move dht userspace code to kernel module.
Michiel Broek <mbroek@mbse.eu>
parents: 646
diff changeset
955 #ifdef HAVE_WIRINGPI_H
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
956 case DEVTYPE_GPIO:
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
957 if (device->direction == DEVDIR_IN_BIN) {
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
958 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
959 device->value = digitalRead(device->gpiopin);
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
960 device->offset = 0;
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
961 device->timestamp = time(NULL);
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
962 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
392
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
963 }
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
964 break;
034746506c3d Fixed initialisation of new GPIO devices.
Michiel Broek <mbroek@mbse.eu>
parents: 362
diff changeset
965
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
966 #endif
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
967 #ifdef USE_SIMULATOR
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
968 case DEVTYPE_SIM:
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
969 pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
970 if (Config.simulators) {
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
971 simulator = Config.simulators;
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
972 if (device->subdevice == 0) {
482
68f4468bfc24 Simulate sensors resolution.
Michiel Broek <mbroek@mbse.eu>
parents: 437
diff changeset
973 device->value = (int)((int)(simulator->room_temperature * 1000) / 500) * 500;
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
974 device->timestamp = time(NULL);
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
975 } else if (device->subdevice == 1) {
506
cdcd07bbee30 New MQTT protocol
Michiel Broek <mbroek@mbse.eu>
parents: 482
diff changeset
976 device->value = (int)((int)(simulator->air_temperature * 1000) / 62.5) * 62.5;
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
977 device->timestamp = time(NULL);
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
978 } else if (device->subdevice == 2) {
506
cdcd07bbee30 New MQTT protocol
Michiel Broek <mbroek@mbse.eu>
parents: 482
diff changeset
979 device->value = (int)((int)(simulator->beer_temperature * 1000) / 62.5) * 62.5;
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
980 device->timestamp = time(NULL);
553
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
981 } else if (device->subdevice == 5) {
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
982 device->value = (int)((int)(simulator->room_humidity * 1000) / 500) * 500;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
983 device->timestamp = time(NULL);
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
984 } else if (device->subdevice == 6) {
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
985 device->value = (int)((int)(simulator->chiller_temperature * 1000) / 62.5) * 62.5;
4091d4fe217f Updated simulator with more simulated devices.
Michiel Broek <mbroek@mbse.eu>
parents: 528
diff changeset
986 device->timestamp = time(NULL);
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
987 }
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
988 }
645
49eb753a958b All thread locks from wiringPi to standard pthreads.
Michiel Broek <mbroek@mbse.eu>
parents: 644
diff changeset
989 pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
259
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
990 break;
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
991 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
992 default:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
993 break;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
994 }
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
995
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
996 /*
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
997 * Delay a bit after procesing a device.
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
998 */
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
999 usleep(10000);
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
1000 }
437
5664743eaf2f Stop devices thread if the program ends.
Michiel Broek <mbroek@mbse.eu>
parents: 408
diff changeset
1001 if (my_shutdown)
5664743eaf2f Stop devices thread if the program ends.
Michiel Broek <mbroek@mbse.eu>
parents: 408
diff changeset
1002 break;
215
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
1003 /*
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
1004 * Delay a bit after all devices
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
1005 */
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
1006 usleep(100000);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1007 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1008
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1009 syslog(LOG_NOTICE, "Thread my_devices_loop stopped");
646
e3edc783006b Running threads and locking doesn't depend on wiringPi anymore.
Michiel Broek <mbroek@mbse.eu>
parents: 645
diff changeset
1010 my_devices_state = 0;
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1011 return 0;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1012 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1013
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1014
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1015

mercurial