thermferm/devices.c

Thu, 26 Mar 2015 16:40:53 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 26 Mar 2015 16:40:53 +0100
changeset 343
ba3a2d27d59e
parent 342
3bbc8f42adc0
child 344
acd840c9fcc0
permissions
-rw-r--r--

Initialize offset to zero with new detected 1-wire devices.

158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
325
35b6f6605ae3 More device locks in the network server
Michiel Broek <mbroek@mbse.eu>
parents: 324
diff changeset
2 * Copyright (C) 2014..2015
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
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 extern int debug;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 extern sys_config Config;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 extern int my_shutdown;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
35 #ifdef HAVE_WIRINGPI_H
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
36
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
37 #define MAXTIMINGS 100
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
38
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
39 int dht11_pin = -1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
40 int dht11_temperature = -1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
41 int dht11_humidity = -1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
42 int dht11_valid = FALSE;
220
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
43 time_t dht11_last = (time_t)0;
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
44
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
45
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
46 static uint8_t sizecvt(const int read_value) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
47 /*
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
48 * digitalRead() and friends from wiringpi are defined as returning a value
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
49 * < 256. However, they are returned as int() types. This is a safety function
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
50 */
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
51 if (read_value > 255 || read_value < 0) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
52 syslog(LOG_NOTICE, "invalid data from wiringPi library");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
53 }
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 return (uint8_t)read_value;
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
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
58
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
59
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
60 /*
213
2317b8d644fa Code cleanup, streamlined error messages.
Michiel Broek <mbroek@mbse.eu>
parents: 212
diff changeset
61 * DHT11 sensor read.
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
62 */
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
63 void dht11Read(void) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
64 int tries = 5;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
65 unsigned short got_correct_data = 0;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
66
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
67 if (dht11_pin == -1)
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
68 return;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
69
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
70 while (tries && !got_correct_data) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
71 uint8_t laststate = HIGH;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
72 uint8_t counter = 0;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
73 uint8_t j = 0, i = 0;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
74 int dht11_dat[5] = {0,0,0,0,0};
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
75
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
76 /*
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
77 * Select output mode to send the start signal.
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
78 */
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
79 pinMode(dht11_pin, OUTPUT);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
80 digitalWrite(dht11_pin, HIGH);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
81 usleep(1000);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
82
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
83 /*
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
84 * Low for at least 18 milliseconds
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
85 */
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
86 digitalWrite(dht11_pin, LOW);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
87 usleep(20000);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
88 digitalWrite(dht11_pin, HIGH);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
89 pinMode(dht11_pin, INPUT);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
90
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
91 /*
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
92 * Detect change and read data
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
93 */
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
94 for (i=0; i<MAXTIMINGS; i++) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
95 counter = 0;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
96 delayMicroseconds(10);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
97 while (sizecvt(digitalRead(dht11_pin)) == laststate) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
98 counter++;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
99 delayMicroseconds(1);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
100 if (counter == 255) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
101 break;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
102 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
103 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
104 laststate = sizecvt(digitalRead(dht11_pin));
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
105
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
106 if (counter == 255)
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
107 break;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
108
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
109 /*
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
110 * ignore first 3 transitions
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
111 */
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
112 if ((i >= 4) && (i%2 == 0)) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
113
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
114 // shove each bit into the storage bytes
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
115 dht11_dat[(int)((double)j/8)] <<= 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
116 if (counter > 16)
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
117 dht11_dat[(int)((double)j/8)] |= 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
118 j++;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
119 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
120 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
121
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
122 /*
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
123 * If there is no sensor, j = 0
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
124 */
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
125 if ((counter == 255) && (j == 0)) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
126 if (dht11_temperature != -1) {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
127 syslog(LOG_NOTICE, "dht11 sensor disappeared");
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
128 } else {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
129 syslog(LOG_NOTICE, "dht11 sensor not present");
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
130 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
131 dht11_temperature = -1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
132 dht11_humidity = -1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
133 dht11_valid = FALSE;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
134 return;
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 /*
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
138 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
139 * print it out if data is good
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
140 */
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
141 if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
142 got_correct_data = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
143
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
144 int h = dht11_dat[0] + dht11_dat[1];
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
145 int t = (dht11_dat[2] & 0x7F) + dht11_dat[3];
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
146
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
147 if ((dht11_dat[2] & 0x80) != 0)
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
148 t *= -1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
149
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
150 dht11_temperature = t;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
151 dht11_humidity = h;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
152 dht11_valid = TRUE;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
153 } else {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
154 tries--;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
155 if (tries == 0)
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
156 syslog(LOG_NOTICE, "dht11 data checksum was wrong 5 times");
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
157 usleep(100000);
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
158 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
159 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
160 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
161
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
162 #endif
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
163
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
165
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
166 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
167 {
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
168 devices_list *device;
323
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
169 time_t now, my_timestamp;
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
170 int my_value;
187
3c8bf18fdf42 Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents: 185
diff changeset
171 #ifdef HAVE_WIRINGPI_H
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
172 int i, rc;
187
3c8bf18fdf42 Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents: 185
diff changeset
173 char buf[40];
3c8bf18fdf42 Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents: 185
diff changeset
174 #endif
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
175
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
176 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
177 return 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
178
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
179 now = time(NULL);
181
4099412fca09 Fixed double commands to 433 MHz switches, they did not switch.
Michiel Broek <mbroek@mbse.eu>
parents: 179
diff changeset
180
337
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
181 #ifdef HAVE_WIRINGPI_H
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
182 piLock(LOCK_DEVICES);
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
183 #endif
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
184
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
185 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
186 if (! strcmp(uuid, device->uuid)) {
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
187 /*
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
188 * Execute command if different then the old value. But also
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
189 * every 2 minutes because commands can have temporary
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
190 * disconnects, or have radio problems.
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
191 */
323
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
192 my_timestamp = device->timestamp;
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
193 my_value = device->value;
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
194 if ((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
195
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
196 #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
197 rc = 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
198 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
199 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
200 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
201 if (buf[i] == '-')
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
202 buf[i] = ',';
337
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
203 piUnlock(LOCK_DEVICES);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
204 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
205 rc = toggleSwitch(buf);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
206 disableTransmit();
337
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
207 piLock(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
208 syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
209 if (debug)
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
210 fprintf(stdout, "RC433 command %s rc=%d\n", buf, rc);
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
211 device->value = value;
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
212 device->timestamp = time(NULL);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
213 piUnlock(LOCK_DEVICES);
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
214 return rc;
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
215 }
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
216
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
217 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
218
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
219 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
220 #endif
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
221 if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (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
222
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
223 }
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
224 #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
225 if ((device->type == DEVTYPE_SIM) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) {
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
226
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
227 }
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
228 #endif
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
229 } else {
337
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
230 #ifdef HAVE_WIRINGPI_H
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
231 piUnlock(LOCK_DEVICES);
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
232 #endif
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
233 return 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
234 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
235 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
236 }
337
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
237 #ifdef HAVE_WIRINGPI_H
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
238 piUnlock(LOCK_DEVICES);
5d3670aafd1b More global device locking
Michiel Broek <mbroek@mbse.eu>
parents: 325
diff changeset
239 #endif
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
240
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
241 return 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
242 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
243
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
244
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
245
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
246 /*
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
247 * Auto detect hotplugged or known to be present devices
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
248 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
249 int devices_detect(void)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
250 {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
251 struct dirent *de;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
252 DIR *fd;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
253 devices_list *device, *ndev;
340
5b49416eb116 Experimental idea
Michiel Broek <mbroek@mbse.eu>
parents: 338
diff changeset
254 int found, subdevices, ival, i, rc = 0;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
255 char buf[40];
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
256 uuid_t uu;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
257 #ifdef HAVE_WIRINGPI_H
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
258 int pin;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
259 #endif
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
260
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
261 /*
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
262 * Scan for 1-wire devices
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
263 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
264 if ((fd = opendir((char *)"/sys/bus/w1/devices"))) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
265 while ((de = readdir(fd))) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
266 if (de->d_name[0] != '.') {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
267 found = FALSE;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
268 for (device = Config.devices; device; device = device->next) {
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
269 if (strcmp(device->address,de->d_name) == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
270 found = TRUE;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
271 break;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
272 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
273 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
274
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
275 if (found == FALSE) {
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
276 strncpy(buf, de->d_name, 2);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
277 buf[2] = '\0';
340
5b49416eb116 Experimental idea
Michiel Broek <mbroek@mbse.eu>
parents: 338
diff changeset
278 sscanf(buf, "%02x", &ival);
5b49416eb116 Experimental idea
Michiel Broek <mbroek@mbse.eu>
parents: 338
diff changeset
279 syslog(LOG_NOTICE, "Scan 1-wire %02x %d", ival, ival);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
280 subdevices = 1;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
281 if (strcmp(buf, (char *)"29") == 0)
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
282 subdevices = 8;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
283 if (strcmp(buf, (char *)"3a") == 0)
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
284 subdevices = 2;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
285 for (i = 0; i < subdevices; i++) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
286 ndev = (devices_list *)malloc(sizeof(devices_list));
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
287 ndev->next = NULL;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
288 ndev->version = 1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
289 ndev->uuid = malloc(37);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
290 uuid_generate(uu);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
291 uuid_unparse(uu, ndev->uuid);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
292 ndev->type = DEVTYPE_W1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
293 ndev->direction = DEVDIR_UNDEF;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
294 if (strcmp(buf, (char *)"10") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
295 ndev->direction = DEVDIR_IN_ANALOG;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
296 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
297 } 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
298 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
299 ndev->description = xstrcpy((char *)"DS1820 Digital thermometer");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
300 } else if (strcmp(buf, (char *)"28") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
301 ndev->direction = DEVDIR_IN_ANALOG;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
302 ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
303 } else if (strcmp(buf, (char *)"29") == 0) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
304 ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
305 } else if (strcmp(buf, (char *)"3a") == 0) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
306 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
307 } 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
308 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
309 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
310 } 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
311 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
312 ndev->description = xstrcpy((char *)"DS28EA00 Digital thermometer");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
313 } else if (strcmp(buf, (char *)"w1") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
314 ndev->description = xstrcpy((char *)"Master System device");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
315 } else {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
316 ndev->description = xstrcpy((char *)"Unknown device family ");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
317 ndev->description = xstrcat(ndev->description, buf);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
318 }
343
ba3a2d27d59e Initialize offset to zero with new detected 1-wire devices.
Michiel Broek <mbroek@mbse.eu>
parents: 342
diff changeset
319 ndev->value = ndev->offset = ndev->inuse = 0;
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
320 ndev->present = DEVPRESENT_YES;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
321 ndev->address = xstrcpy(de->d_name);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
322 ndev->subdevice = i;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
323 ndev->gpiopin = -1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
324 ndev->comment = xstrcpy((char *)"Auto detected device");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
325 ndev->timestamp = time(NULL);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
326
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
327 if (Config.devices == NULL) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
328 Config.devices = ndev;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
329 } else {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
330 for (device = Config.devices; device; device = device->next) {
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
331 if (device->next == NULL) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
332 device->next = ndev;
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
333 break;
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
334 }
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
335 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
336 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
337 rc++;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
338 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
339 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
340 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
341 }
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
342 closedir(fd);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
343 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
344
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
345 #ifdef HAVE_WIRINGPI_H
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
346 if (piBoardRev() == 2) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
347 /*
324
940a668e568f Update for newer boards
Michiel Broek <mbroek@mbse.eu>
parents: 323
diff changeset
348 * Support rev B and newer boards only
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
349 */
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
350 found = FALSE;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
351 for (device = Config.devices; device; device = device->next) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
352 if (device->type == DEVTYPE_GPIO) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
353 found = TRUE;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
354 break;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
355 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
356 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
357
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
358 if (found == FALSE) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
359 /*
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
360 * There were no GPIO devices found.
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
361 */
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
362 subdevices = 12;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
363 pin = 0;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
364 for (i = 0; i < subdevices; i++) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
365 if (i == 8)
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
366 pin = 17;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
367
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
368 ndev = (devices_list *)malloc(sizeof(devices_list));
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
369 ndev->next = NULL;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
370 ndev->version = 1;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
371 ndev->uuid = malloc(37);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
372 uuid_generate(uu);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
373 uuid_unparse(uu, ndev->uuid);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
374 ndev->type = DEVTYPE_GPIO;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
375 ndev->value = digitalRead(pin);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
376 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
377 ndev->address = xstrcpy((char *)"GPIO");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
378 snprintf(buf, 39, "Raspberry GPIO %d", i);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
379 ndev->description = xstrcpy(buf);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
380 ndev->subdevice = i;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
381 ndev->gpiopin = pin;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
382 ndev->timestamp = time(NULL);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
383 if (i == PANEL_LED) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
384 ndev->direction = DEVDIR_OUT_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
385 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
386 ndev->comment = xstrcpy((char *)"Frontpanel LED");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
387 } else if (i == PANEL_ENTER) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
388 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
389 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
390 ndev->comment = xstrcpy((char *)"Frontpanel Enter key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
391 } else if (i == PANEL_DOWN) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
392 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
393 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
394 ndev->comment = xstrcpy((char *)"Frontpanel Down key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
395 } else if (i == PANEL_UP) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
396 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
397 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
398 ndev->comment = xstrcpy((char *)"Frontpanel Up key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
399 } else if (i == 7) {
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
400 ndev->direction = DEVDIR_INTERN;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
401 ndev->inuse = 1;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
402 ndev->comment = xstrcpy((char *)"1-Wire bus");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
403 } else {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
404 ndev->direction = DEVDIR_IN_BIN;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
405 ndev->inuse = 0;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
406 ndev->comment = xstrcpy((char *)"Raspberry GPIO");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
407 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
408 pin++;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
409
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
410 if (Config.devices == NULL) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
411 Config.devices = ndev;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
412 } else {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
413 for (device = Config.devices; device; device = device->next) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
414 if (device->next == NULL) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
415 device->next = ndev;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
416 break;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
417 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
418 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
419 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
420 rc++;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
421 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
422 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
423 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
424 #endif
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
425
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
426 #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
427 found = FALSE;
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
428 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
429 if (device->type == DEVTYPE_SIM) {
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
430 found = TRUE;
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
431 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
432 }
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
433 }
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
434
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
435 if (found == FALSE) {
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
436 subdevices = 5;
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
437 for (i = 0; i < subdevices; i++) {
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
438 ndev = (devices_list *)malloc(sizeof(devices_list));
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
439 ndev->next = NULL;
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
440 ndev->version = 1;
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
441 ndev->uuid = malloc(37);
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
442 uuid_generate(uu);
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
443 uuid_unparse(uu, ndev->uuid);
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
444 ndev->type = DEVTYPE_SIM;
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
445 ndev->value = ndev->offset = 0;
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
446 ndev->present = DEVPRESENT_YES;
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
447 ndev->subdevice = i;
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
448 ndev->gpiopin = -1;
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
449 ndev->comment = xstrcpy((char *)"Auto detected device");
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
450 ndev->timestamp = time(NULL);
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
451 ndev->inuse = 0;
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
452 switch (i) {
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
453 case 0: ndev->direction = DEVDIR_IN_ANALOG;
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
454 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
455 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
456 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
457 case 1: ndev->direction = DEVDIR_IN_ANALOG;
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
458 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
459 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
460 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
461 case 2: ndev->direction = DEVDIR_IN_ANALOG;
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
462 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
463 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
464 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
465 case 3: ndev->direction = DEVDIR_OUT_ANALOG;
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
466 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
467 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
468 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
469 case 4: ndev->direction = DEVDIR_OUT_ANALOG;
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
470 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
471 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
472 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
473 }
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
474
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
475 if (Config.devices == NULL) {
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
476 Config.devices = ndev;
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
477 } else {
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
478 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
479 if (device->next == NULL) {
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
480 device->next = ndev;
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
481 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
482 }
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
483 }
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
484 }
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
485 rc++;
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
486 }
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
487 }
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
488 #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
489
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
490 return rc;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
491 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
492
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
493
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
494
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
495 #ifdef HAVE_WIRINGPI_H
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
496 PI_THREAD (my_devices_loop)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
497 #else
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
498 void *my_devices_loop(void *threadid)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
499 #endif
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
500 {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
501 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
502 #ifdef USE_SIMULATOR
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
503 simulator_list *simulator;
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
504 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
505 char *addr = NULL, line[60], *p = NULL;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
506 FILE *fp;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
507 int temp, rc;
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
508 #ifdef HAVE_WIRINGPI_H
220
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
509 time_t now;
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
510 #endif
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
511
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
512 syslog(LOG_NOTICE, "Thread my_devices_loop started");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
513
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
514 #ifdef HAVE_WIRINGPI_H
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
515 if ((rc = piHiPri(10)))
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
516 syslog(LOG_NOTICE, "my_devices_loop: piHiPri(10) rc=%d", rc);
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
517 #endif
239
793c09ece542 Increase priority for the devices thread
Michiel Broek <mbroek@mbse.eu>
parents: 235
diff changeset
518
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
519 /*
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
520 * Loop forever until the external shutdown variable is set.
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
521 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
522 for (;;) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
523
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
524 /*
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
525 * Process all devices.
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
526 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
527 for (device = Config.devices; device; device = device->next) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
528
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
529 if (my_shutdown)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
530 break;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
531
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
532 switch (device->type) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
533 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
534 /*
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
535 * 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
536 * 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
537 */
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
538 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
539 (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
540 (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
541 (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
542 (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
543 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
544 addr = xstrcat(addr, device->address);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
545 addr = xstrcat(addr, (char *)"/w1_slave");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
546 if ((fp = fopen(addr, "r"))) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
547 if (device->present != DEVPRESENT_YES) {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
548 syslog(LOG_NOTICE, "sensor %s is back", device->address);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
549 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
550 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
551 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
552 device->present = DEVPRESENT_YES;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
553 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
554 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
555 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
556 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
557 /*
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
558 * The output looks like:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
559 * 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
560 * 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
561 */
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
562 fgets(line, 50, fp);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
563 line[strlen(line)-1] = '\0';
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
564 if ((line[36] == 'Y') && (line[37] == 'E')) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
565 /* CRC is Ok, continue */
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
566 fgets(line, 50, fp);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
567 line[strlen(line)-1] = '\0';
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
568 strtok(line, (char *)"=");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
569 p = strtok(NULL, (char *)"=");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
570 rc = sscanf(p, "%d", &temp);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
571 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
572 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
573 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
574 if ((rc == 1) && (device->value != temp)) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
575 device->value = temp;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
576 device->timestamp = time(NULL);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
577 }
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
578 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
579 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
580 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
581 } else {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
582 syslog(LOG_NOTICE, "sensor %s CRC error", device->address);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
583 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
584 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
585 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
586 device->present = DEVPRESENT_ERROR;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
587 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
588 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
589 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
590 }
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
591 fclose(fp);
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
592 } else {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
593 if (device->present != DEVPRESENT_NO) {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
594 syslog(LOG_NOTICE, "sensor %s is missing", device->address);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
595 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
596 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
597 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
598 device->present = DEVPRESENT_NO;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
599 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
600 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
601 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
602 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
603 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
604 free(addr);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
605 addr = NULL;
341
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
606 } /* if temperature sensor */
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
607 /*
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
608 * DS2408 8 Channel addressable switch
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
609 */
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
610 if (strncmp(device->address, (char *)"29", 2) == 0) {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
611 /*
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
612 * Always read current state and set these bits
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
613 * in the subdevices value. We do this even with
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
614 * the output bits, they should match.
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
615 */
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
616 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
617 addr = xstrcat(addr, device->address);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
618 addr = xstrcat(addr, (char *)"/state");
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
619 if ((fp = fopen(addr, "r"))) {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
620 if (device->present != DEVPRESENT_YES) {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
621 syslog(LOG_NOTICE, "DS2408 %s is back", device->address);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
622 #ifdef HAVE_WIRINGPI_H
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
623 piLock(LOCK_DEVICES);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
624 #endif
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
625 device->present = DEVPRESENT_YES;
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
626 #ifdef HAVE_WIRINGPI_H
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
627 piUnlock(LOCK_DEVICES);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
628 #endif
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
629 }
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
630
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
631 fclose(fp);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
632 } else {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
633 if (device->present != DEVPRESENT_NO) {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
634 syslog(LOG_NOTICE, "DS2408 %s is missing", device->address);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
635 #ifdef HAVE_WIRINGPI_H
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
636 piLock(LOCK_DEVICES);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
637 #endif
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
638 device->present = DEVPRESENT_NO;
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
639 #ifdef HAVE_WIRINGPI_H
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
640 piUnlock(LOCK_DEVICES);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
641 #endif
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
642 }
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
643 }
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
644 free(addr);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
645 addr = NULL;
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
646 }
342
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
647 /*
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
648 * DS2413 Dual channel addressable switch
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
649 */
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
650 unsigned char state;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
651
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
652 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
653 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
654 addr = xstrcat(addr, device->address);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
655 addr = xstrcat(addr, (char *)"/state");
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
656 if ((fp = fopen(addr, "r"))) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
657 if (device->present != DEVPRESENT_YES) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
658 syslog(LOG_NOTICE, "DS2413 %s is back", device->address);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
659 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
660 piLock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
661 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
662 device->present = DEVPRESENT_YES;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
663 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
664 piUnlock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
665 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
666 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
667 if ((fread(&state, 1, 1, fp)) == 1) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
668 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
669 piLock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
670 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
671 /*
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
672 * 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
673 */
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
674 if (device->subdevice == 0)
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
675 device->value = (state & 0x01) ? 0 : 1;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
676 else if (device->subdevice == 1)
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
677 device->value = (state & 0x04) ? 0 : 1;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
678 device->timestamp = time(NULL);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
679 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
680 piUnlock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
681 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
682 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
683 fclose(fp);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
684 } else {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
685 if (device->present != DEVPRESENT_NO) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
686 syslog(LOG_NOTICE, "DS2413 %s is missing", device->address);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
687 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
688 piLock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
689 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
690 device->present = DEVPRESENT_NO;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
691 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
692 piUnlock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
693 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
694 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
695 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
696 free(addr);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
697 addr = NULL;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
698 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
699
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
700 break;
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
701
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
702 #ifdef HAVE_WIRINGPI_H
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
703 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
704 /*
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
705 * Make sure we don't read the sensor withing 2 seconds.
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
706 */
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
707 now = time(NULL);
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
708 if ((int)(now - dht11_last) > 2) {
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
709 dht11_pin = device->gpiopin;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
710 dht11Read();
220
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
711 dht11_last = now;
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
712 }
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
713 if (device->subdevice == 0) {
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
714 piLock(LOCK_DEVICES);
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
715 if (dht11_valid) {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
716 device->value = dht11_temperature * 1000;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
717 device->timestamp = time(NULL);
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
718 device->present = DEVPRESENT_YES;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
719 } else {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
720 device->present = DEVPRESENT_ERROR;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
721 }
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
722 piUnlock(LOCK_DEVICES);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
723 } else if (device->subdevice == 1) {
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
724 piLock(LOCK_DEVICES);
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
725 if (dht11_valid) {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
726 device->value = dht11_humidity * 1000;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
727 device->timestamp = time(NULL);
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
728 device->present = DEVPRESENT_YES;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
729 } else {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
730 device->present = DEVPRESENT_ERROR;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
731 }
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
732 piUnlock(LOCK_DEVICES);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
733 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
734 break;
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
735 #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
736 #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
737 case DEVTYPE_SIM:
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
738 #ifdef HAVE_WIRINGPI_H
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
739 piLock(LOCK_DEVICES);
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
740 #endif
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
741 if (Config.simulators) {
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
742 simulator = Config.simulators;
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
743 if (device->subdevice == 0) {
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
744 device->value = (int)(simulator->room_temperature * 1000);
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
745 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
746 } else if (device->subdevice == 1) {
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
747 device->value = (int)(simulator->air_temperature * 1000);
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
748 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
749 } else if (device->subdevice == 2) {
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
750 device->value = (int)(simulator->beer_temperature * 1000);
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
751 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
752 }
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
753 }
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
754 #ifdef HAVE_WIRINGPI_H
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
755 piUnlock(LOCK_DEVICES);
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
756 #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
757 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
758 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
759 default:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
760 break;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
761 }
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
762
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
763 /*
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
764 * Delay a bit after procesing a device.
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
765 */
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
766 usleep(10000);
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
767 }
215
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
768 /*
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
769 * 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
770 */
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
771 usleep(100000);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
772 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
773
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
774 syslog(LOG_NOTICE, "Thread my_devices_loop stopped");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
775 return 0;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
776 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
777
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
778
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
779

mercurial