thermferm/devices.c

Thu, 26 Mar 2015 20:34:53 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 26 Mar 2015 20:34:53 +0100
changeset 344
acd840c9fcc0
parent 343
ba3a2d27d59e
child 347
0ab5c3fd7c77
permissions
-rw-r--r--

Fixed Offset label in device editor. Added code to edit pin states for DS2408 and DS2413 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");
344
acd840c9fcc0 Fixed Offset label in device editor. Added code to edit pin states for DS2408 and DS2413 1-wire devices.
Michiel Broek <mbroek@mbse.eu>
parents: 343
diff changeset
305 ndev->direction = DEVDIR_IN_BIN;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
306 } else if (strcmp(buf, (char *)"3a") == 0) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
307 ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch");
344
acd840c9fcc0 Fixed Offset label in device editor. Added code to edit pin states for DS2408 and DS2413 1-wire devices.
Michiel Broek <mbroek@mbse.eu>
parents: 343
diff changeset
308 ndev->direction = DEVDIR_IN_BIN;
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
309 } 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
310 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
311 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
312 } 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
313 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
314 ndev->description = xstrcpy((char *)"DS28EA00 Digital thermometer");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
315 } else if (strcmp(buf, (char *)"w1") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
316 ndev->description = xstrcpy((char *)"Master System device");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
317 } else {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
318 ndev->description = xstrcpy((char *)"Unknown device family ");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
319 ndev->description = xstrcat(ndev->description, buf);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
320 }
343
ba3a2d27d59e Initialize offset to zero with new detected 1-wire devices.
Michiel Broek <mbroek@mbse.eu>
parents: 342
diff changeset
321 ndev->value = ndev->offset = ndev->inuse = 0;
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
322 ndev->present = DEVPRESENT_YES;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
323 ndev->address = xstrcpy(de->d_name);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
324 ndev->subdevice = i;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
325 ndev->gpiopin = -1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
326 ndev->comment = xstrcpy((char *)"Auto detected device");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
327 ndev->timestamp = time(NULL);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
328
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
329 if (Config.devices == NULL) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
330 Config.devices = ndev;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
331 } else {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
332 for (device = Config.devices; device; device = device->next) {
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
333 if (device->next == NULL) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
334 device->next = ndev;
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
335 break;
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
336 }
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
337 }
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 rc++;
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 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
342 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
343 }
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
344 closedir(fd);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
345 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
346
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
347 #ifdef HAVE_WIRINGPI_H
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
348 if (piBoardRev() == 2) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
349 /*
324
940a668e568f Update for newer boards
Michiel Broek <mbroek@mbse.eu>
parents: 323
diff changeset
350 * Support rev B and newer boards only
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
351 */
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
352 found = FALSE;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
353 for (device = Config.devices; device; device = device->next) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
354 if (device->type == DEVTYPE_GPIO) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
355 found = TRUE;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
356 break;
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 }
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 if (found == FALSE) {
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 * There were no GPIO devices found.
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
363 */
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
364 subdevices = 12;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
365 pin = 0;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
366 for (i = 0; i < subdevices; i++) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
367 if (i == 8)
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
368 pin = 17;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
369
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
370 ndev = (devices_list *)malloc(sizeof(devices_list));
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
371 ndev->next = NULL;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
372 ndev->version = 1;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
373 ndev->uuid = malloc(37);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
374 uuid_generate(uu);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
375 uuid_unparse(uu, ndev->uuid);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
376 ndev->type = DEVTYPE_GPIO;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
377 ndev->value = digitalRead(pin);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
378 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
379 ndev->address = xstrcpy((char *)"GPIO");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
380 snprintf(buf, 39, "Raspberry GPIO %d", i);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
381 ndev->description = xstrcpy(buf);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
382 ndev->subdevice = i;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
383 ndev->gpiopin = pin;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
384 ndev->timestamp = time(NULL);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
385 if (i == PANEL_LED) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
386 ndev->direction = DEVDIR_OUT_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
387 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
388 ndev->comment = xstrcpy((char *)"Frontpanel LED");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
389 } else if (i == PANEL_ENTER) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
390 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
391 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
392 ndev->comment = xstrcpy((char *)"Frontpanel Enter key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
393 } else if (i == PANEL_DOWN) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
394 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
395 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
396 ndev->comment = xstrcpy((char *)"Frontpanel Down key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
397 } else if (i == PANEL_UP) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
398 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
399 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
400 ndev->comment = xstrcpy((char *)"Frontpanel Up key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
401 } else if (i == 7) {
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
402 ndev->direction = DEVDIR_INTERN;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
403 ndev->inuse = 1;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
404 ndev->comment = xstrcpy((char *)"1-Wire bus");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
405 } else {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
406 ndev->direction = DEVDIR_IN_BIN;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
407 ndev->inuse = 0;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
408 ndev->comment = xstrcpy((char *)"Raspberry GPIO");
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 pin++;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
411
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
412 if (Config.devices == NULL) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
413 Config.devices = ndev;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
414 } else {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
415 for (device = Config.devices; device; device = device->next) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
416 if (device->next == NULL) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
417 device->next = ndev;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
418 break;
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 }
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 rc++;
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 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
425 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
426 #endif
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
427
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
428 #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
429 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
430 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
431 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
432 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
433 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
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 }
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
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 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
438 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
439 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
440 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
441 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
442 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
443 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
444 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
445 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
446 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
447 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
448 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
449 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
450 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
451 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
452 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
453 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
454 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
455 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
456 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
457 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
458 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
459 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
460 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
461 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
462 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
463 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
464 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
465 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
466 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
467 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
468 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
469 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
470 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
471 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
472 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
473 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
474 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
475 }
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
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 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
478 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
479 } 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
480 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
481 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
482 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
483 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
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 }
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 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
488 }
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 }
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
490 #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
491
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
492 return rc;
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
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
496
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
497 #ifdef HAVE_WIRINGPI_H
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
498 PI_THREAD (my_devices_loop)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
499 #else
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
500 void *my_devices_loop(void *threadid)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
501 #endif
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
502 {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
503 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
504 #ifdef USE_SIMULATOR
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
505 simulator_list *simulator;
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
506 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
507 char *addr = NULL, line[60], *p = NULL;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
508 FILE *fp;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
509 int temp, rc;
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
510 #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
511 time_t now;
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
512 #endif
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
513
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
514 syslog(LOG_NOTICE, "Thread my_devices_loop started");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
515
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
516 #ifdef HAVE_WIRINGPI_H
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
517 if ((rc = piHiPri(10)))
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
518 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
519 #endif
239
793c09ece542 Increase priority for the devices thread
Michiel Broek <mbroek@mbse.eu>
parents: 235
diff changeset
520
158
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 * Loop forever until the external shutdown variable is set.
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 for (;;) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
525
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
526 /*
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
527 * Process all devices.
158
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 for (device = Config.devices; device; device = device->next) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
530
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
531 if (my_shutdown)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
532 break;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
533
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
534 switch (device->type) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
535 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
536 /*
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 * 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
538 * 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
539 */
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 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
541 (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
542 (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
543 (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
544 (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
545 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
546 addr = xstrcat(addr, device->address);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
547 addr = xstrcat(addr, (char *)"/w1_slave");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
548 if ((fp = fopen(addr, "r"))) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
549 if (device->present != DEVPRESENT_YES) {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
550 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
551 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
552 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
553 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
554 device->present = DEVPRESENT_YES;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
555 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
556 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
557 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
558 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
559 /*
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
560 * The output looks like:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
561 * 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
562 * 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
563 */
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
564 fgets(line, 50, fp);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
565 line[strlen(line)-1] = '\0';
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
566 if ((line[36] == 'Y') && (line[37] == 'E')) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
567 /* CRC is Ok, continue */
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
568 fgets(line, 50, fp);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
569 line[strlen(line)-1] = '\0';
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
570 strtok(line, (char *)"=");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
571 p = strtok(NULL, (char *)"=");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
572 rc = sscanf(p, "%d", &temp);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
573 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
574 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
575 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
576 if ((rc == 1) && (device->value != temp)) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
577 device->value = temp;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
578 device->timestamp = time(NULL);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
579 }
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
580 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
581 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
582 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
583 } else {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
584 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
585 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
586 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
587 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
588 device->present = DEVPRESENT_ERROR;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
589 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
590 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
591 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
592 }
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
593 fclose(fp);
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
594 } else {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
595 if (device->present != DEVPRESENT_NO) {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
596 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
597 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
598 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
599 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
600 device->present = DEVPRESENT_NO;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
601 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
602 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
603 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
604 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
605 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
606 free(addr);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
607 addr = NULL;
341
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
608 } /* if temperature sensor */
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 * DS2408 8 Channel addressable switch
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 if (strncmp(device->address, (char *)"29", 2) == 0) {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
613 /*
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
614 * Always read current state and set these bits
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
615 * 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
616 * the output bits, they should match.
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
617 */
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
618 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
619 addr = xstrcat(addr, device->address);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
620 addr = xstrcat(addr, (char *)"/state");
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
621 if ((fp = fopen(addr, "r"))) {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
622 if (device->present != DEVPRESENT_YES) {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
623 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
624 #ifdef HAVE_WIRINGPI_H
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
625 piLock(LOCK_DEVICES);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
626 #endif
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
627 device->present = DEVPRESENT_YES;
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
628 #ifdef HAVE_WIRINGPI_H
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
629 piUnlock(LOCK_DEVICES);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
630 #endif
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
631 }
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
632
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
633 fclose(fp);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
634 } else {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
635 if (device->present != DEVPRESENT_NO) {
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
636 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
637 #ifdef HAVE_WIRINGPI_H
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
638 piLock(LOCK_DEVICES);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
639 #endif
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
640 device->present = DEVPRESENT_NO;
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
641 #ifdef HAVE_WIRINGPI_H
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
642 piUnlock(LOCK_DEVICES);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
643 #endif
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
644 }
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
645 }
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
646 free(addr);
cfc952a68d4a Possible part of DS2408 code added.
Michiel Broek <mbroek@mbse.eu>
parents: 340
diff changeset
647 addr = NULL;
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
648 }
342
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 * DS2413 Dual channel addressable switch
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 unsigned char state;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
653
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
654 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
655 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
656 addr = xstrcat(addr, device->address);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
657 addr = xstrcat(addr, (char *)"/state");
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
658 if ((fp = fopen(addr, "r"))) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
659 if (device->present != DEVPRESENT_YES) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
660 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
661 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
662 piLock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
663 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
664 device->present = DEVPRESENT_YES;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
665 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
666 piUnlock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
667 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
668 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
669 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
670 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
671 piLock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
672 #endif
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 * 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
675 */
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
676 if (device->subdevice == 0)
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
677 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
678 else if (device->subdevice == 1)
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
679 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
680 device->timestamp = time(NULL);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
681 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
682 piUnlock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
683 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
684 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
685 fclose(fp);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
686 } else {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
687 if (device->present != DEVPRESENT_NO) {
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
688 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
689 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
690 piLock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
691 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
692 device->present = DEVPRESENT_NO;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
693 #ifdef HAVE_WIRINGPI_H
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
694 piUnlock(LOCK_DEVICES);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
695 #endif
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
696 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
697 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
698 free(addr);
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
699 addr = NULL;
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
700 }
3bbc8f42adc0 Added device read for the DS2413 dual channel PIO
Michiel Broek <mbroek@mbse.eu>
parents: 341
diff changeset
701
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
702 break;
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
703
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
704 #ifdef HAVE_WIRINGPI_H
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
705 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
706 /*
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
707 * 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
708 */
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
709 now = time(NULL);
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
710 if ((int)(now - dht11_last) > 2) {
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
711 dht11_pin = device->gpiopin;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
712 dht11Read();
220
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
713 dht11_last = now;
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
714 }
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
715 if (device->subdevice == 0) {
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
716 piLock(LOCK_DEVICES);
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
717 if (dht11_valid) {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
718 device->value = dht11_temperature * 1000;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
719 device->timestamp = time(NULL);
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
720 device->present = DEVPRESENT_YES;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
721 } else {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
722 device->present = DEVPRESENT_ERROR;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
723 }
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
724 piUnlock(LOCK_DEVICES);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
725 } else if (device->subdevice == 1) {
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
726 piLock(LOCK_DEVICES);
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
727 if (dht11_valid) {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
728 device->value = dht11_humidity * 1000;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
729 device->timestamp = time(NULL);
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
730 device->present = DEVPRESENT_YES;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
731 } else {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
732 device->present = DEVPRESENT_ERROR;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
733 }
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
734 piUnlock(LOCK_DEVICES);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
735 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
736 break;
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
737 #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
738 #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
739 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
740 #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
741 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
742 #endif
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
743 if (Config.simulators) {
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
744 simulator = Config.simulators;
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
745 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
746 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
747 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
748 } 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
749 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
750 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
751 } 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
752 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
753 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
754 }
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
755 }
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 #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
757 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
758 #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
759 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
760 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
761 default:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
762 break;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
763 }
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
764
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 * Delay a bit after procesing a device.
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
767 */
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
768 usleep(10000);
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
769 }
215
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 * 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
772 */
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
773 usleep(100000);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
774 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
775
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
776 syslog(LOG_NOTICE, "Thread my_devices_loop stopped");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
777 return 0;
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
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
780
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
781

mercurial