thermferm/devices.c

Sat, 07 Mar 2015 13:36:45 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 07 Mar 2015 13:36:45 +0100
changeset 325
35b6f6605ae3
parent 324
940a668e568f
child 337
5d3670aafd1b
permissions
-rw-r--r--

More device locks in the network server

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
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
181 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
182 if (! strcmp(uuid, device->uuid)) {
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
183 /*
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
184 * Execute command if different then the old value. But also
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
185 * every 2 minutes because commands can have temporary
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
186 * disconnects, or have radio problems.
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
187 */
323
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
188 #ifdef HAVE_WIRINGPI_H
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
189 piLock(LOCK_DEVICES);
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
190 #endif
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
191 my_timestamp = device->timestamp;
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
192 my_value = device->value;
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
193 #ifdef HAVE_WIRINGPI_H
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
194 piUnlock(LOCK_DEVICES);
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
195 #endif
608592f74b10 Added extra lock
Michiel Broek <mbroek@mbse.eu>
parents: 268
diff changeset
196 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
197
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
198 #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
199 rc = 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
200 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
201 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
202 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
203 if (buf[i] == '-')
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
204 buf[i] = ',';
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
205 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
206 rc = toggleSwitch(buf);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
207 disableTransmit();
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);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
211 piLock(LOCK_DEVICES);
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
212 device->value = value;
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
213 device->timestamp = time(NULL);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
214 piUnlock(LOCK_DEVICES);
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
215 return rc;
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
216 }
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
217
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
218 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
219
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
220 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
221 #endif
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
222 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
223
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
224 }
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
225 #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
226 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
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 }
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
229 #endif
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
230 } else {
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
231 return 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
232 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
233 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
234 }
185
4f34271cf1e7 PID finetuning
Michiel Broek <mbroek@mbse.eu>
parents: 181
diff changeset
235
179
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
236 return 0;
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
237 }
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
238
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
239
417ee898fb02 Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents: 166
diff changeset
240
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
241 /*
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242 * Auto detect hotplugged or known to be present devices
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
243 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
244 int devices_detect(void)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
245 {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
246 struct dirent *de;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
247 DIR *fd;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
248 devices_list *device, *ndev;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
249 int found, subdevices, i, rc = 0;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
250 char buf[40];
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
251 uuid_t uu;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
252 #ifdef HAVE_WIRINGPI_H
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
253 int pin;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
254 #endif
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
255
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
256 /*
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
257 * Scan for 1-wire devices
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
258 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
259 if ((fd = opendir((char *)"/sys/bus/w1/devices"))) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
260 while ((de = readdir(fd))) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
261 if (de->d_name[0] != '.') {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
262 found = FALSE;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
263 for (device = Config.devices; device; device = device->next) {
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
264 if (strcmp(device->address,de->d_name) == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
265 found = TRUE;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
266 break;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
267 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
268 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
269
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
270 if (found == FALSE) {
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
271 strncpy(buf, de->d_name, 2);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
272 buf[2] = '\0';
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
273 subdevices = 1;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
274 if (strcmp(buf, (char *)"29") == 0)
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
275 subdevices = 8;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
276 if (strcmp(buf, (char *)"3a") == 0)
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
277 subdevices = 2;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
278 for (i = 0; i < subdevices; i++) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
279 ndev = (devices_list *)malloc(sizeof(devices_list));
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
280 ndev->next = NULL;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
281 ndev->version = 1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
282 ndev->uuid = malloc(37);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
283 uuid_generate(uu);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
284 uuid_unparse(uu, ndev->uuid);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
285 ndev->type = DEVTYPE_W1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
286 ndev->direction = DEVDIR_UNDEF;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
287 if (strcmp(buf, (char *)"10") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
288 ndev->direction = DEVDIR_IN_ANALOG;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
289 ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
290 } else if (strcmp(buf, (char *)"28") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
291 ndev->direction = DEVDIR_IN_ANALOG;
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
292 ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
293 } else if (strcmp(buf, (char *)"29") == 0) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
294 ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
295 } else if (strcmp(buf, (char *)"3a") == 0) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
296 ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
297 } else if (strcmp(buf, (char *)"w1") == 0) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
298 ndev->description = xstrcpy((char *)"Master System device");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
299 } else {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
300 ndev->description = xstrcpy((char *)"Unknown device family ");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
301 ndev->description = xstrcat(ndev->description, buf);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
302 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
303 ndev->value = ndev->inuse = 0;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
304 ndev->present = DEVPRESENT_YES;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
305 ndev->address = xstrcpy(de->d_name);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
306 ndev->subdevice = i;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
307 ndev->gpiopin = -1;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
308 ndev->comment = xstrcpy((char *)"Auto detected device");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
309 ndev->timestamp = time(NULL);
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
310
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
311 if (Config.devices == NULL) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
312 Config.devices = ndev;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
313 } else {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
314 for (device = Config.devices; device; device = device->next) {
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
315 if (device->next == NULL) {
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
316 device->next = ndev;
161
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
317 break;
493e39bb0a08 Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents: 158
diff changeset
318 }
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
319 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
320 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
321 rc++;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
322 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
323 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
324 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
325 }
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
326 closedir(fd);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
327 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
328
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
329 #ifdef HAVE_WIRINGPI_H
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
330 if (piBoardRev() == 2) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
331 /*
324
940a668e568f Update for newer boards
Michiel Broek <mbroek@mbse.eu>
parents: 323
diff changeset
332 * Support rev B and newer boards only
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
333 */
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
334 found = FALSE;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
335 for (device = Config.devices; device; device = device->next) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
336 if (device->type == DEVTYPE_GPIO) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
337 found = TRUE;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
338 break;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
339 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
340 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
341
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
342 if (found == FALSE) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
343 /*
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
344 * There were no GPIO devices found.
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
345 */
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
346 subdevices = 12;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
347 pin = 0;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
348 for (i = 0; i < subdevices; i++) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
349 if (i == 8)
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
350 pin = 17;
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 ndev = (devices_list *)malloc(sizeof(devices_list));
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
353 ndev->next = NULL;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
354 ndev->version = 1;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
355 ndev->uuid = malloc(37);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
356 uuid_generate(uu);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
357 uuid_unparse(uu, ndev->uuid);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
358 ndev->type = DEVTYPE_GPIO;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
359 ndev->value = digitalRead(pin);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
360 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
361 ndev->address = xstrcpy((char *)"GPIO");
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
362 snprintf(buf, 39, "Raspberry GPIO %d", i);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
363 ndev->description = xstrcpy(buf);
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
364 ndev->subdevice = i;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
365 ndev->gpiopin = pin;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
366 ndev->timestamp = time(NULL);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
367 if (i == PANEL_LED) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
368 ndev->direction = DEVDIR_OUT_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
369 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
370 ndev->comment = xstrcpy((char *)"Frontpanel LED");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
371 } else if (i == PANEL_ENTER) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
372 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
373 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
374 ndev->comment = xstrcpy((char *)"Frontpanel Enter key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
375 } else if (i == PANEL_DOWN) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
376 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
377 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
378 ndev->comment = xstrcpy((char *)"Frontpanel Down key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
379 } else if (i == PANEL_UP) {
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
380 ndev->direction = DEVDIR_IN_BIN;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
381 ndev->inuse = 1;
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
382 ndev->comment = xstrcpy((char *)"Frontpanel Up key");
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
383 } else if (i == 7) {
162
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
384 ndev->direction = DEVDIR_INTERN;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
385 ndev->inuse = 1;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
386 ndev->comment = xstrcpy((char *)"1-Wire bus");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
387 } else {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
388 ndev->direction = DEVDIR_IN_BIN;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
389 ndev->inuse = 0;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
390 ndev->comment = xstrcpy((char *)"Raspberry GPIO");
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
391 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
392 pin++;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
393
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
394 if (Config.devices == NULL) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
395 Config.devices = ndev;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
396 } else {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
397 for (device = Config.devices; device; device = device->next) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
398 if (device->next == NULL) {
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
399 device->next = ndev;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
400 break;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
401 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
402 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
403 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
404 rc++;
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
405 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
406 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
407 }
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
408 #endif
6fc9e3f7962f Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents: 161
diff changeset
409
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
410 #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
411 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
412 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
413 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
414 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
415 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
416 }
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
417 }
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
418
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
419 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
420 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
421 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
422 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
423 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
424 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
425 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
426 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
427 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
428 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
429 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
430 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
431 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
432 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
433 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
434 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
435 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
436 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
437 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
438 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
439 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
440 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
441 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
442 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
443 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
444 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
445 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
446 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
447 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
448 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
449 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
450 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
451 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
452 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
453 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
454 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
455 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
456 break;
b7c967359771 Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents: 242
diff changeset
457 }
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
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 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
460 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
461 } 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
462 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
463 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
464 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
465 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
466 }
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 }
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 }
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 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
470 }
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 }
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 #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
473
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
474 return rc;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
475 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
476
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
477
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
478
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
479 #ifdef HAVE_WIRINGPI_H
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
480 PI_THREAD (my_devices_loop)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
481 #else
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
482 void *my_devices_loop(void *threadid)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
483 #endif
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
484 {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
485 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
486 #ifdef USE_SIMULATOR
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
487 simulator_list *simulator;
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
488 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
489 char *addr = NULL, line[60], *p = NULL;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
490 FILE *fp;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
491 int temp, rc;
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
492 #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
493 time_t now;
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
494 #endif
158
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 syslog(LOG_NOTICE, "Thread my_devices_loop started");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
497
242
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
498 #ifdef HAVE_WIRINGPI_H
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
499 if ((rc = piHiPri(10)))
d3fe84ece349 Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents: 240
diff changeset
500 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
501 #endif
239
793c09ece542 Increase priority for the devices thread
Michiel Broek <mbroek@mbse.eu>
parents: 235
diff changeset
502
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
503 /*
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
504 * Loop forever until the external shutdown variable is set.
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
505 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
506 for (;;) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
507
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
508 /*
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
509 * Process all devices.
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
510 */
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
511 for (device = Config.devices; device; device = device->next) {
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
512
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
513 if (my_shutdown)
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
514 break;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
515
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
516 switch (device->type) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
517 case DEVTYPE_W1:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
518 if (strncmp(device->address, (char *)"28", 2) == 0) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
519 /* Read DS18B20 sensor */
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
520 addr = xstrcpy((char *)"/sys/bus/w1/devices/");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
521 addr = xstrcat(addr, device->address);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
522 addr = xstrcat(addr, (char *)"/w1_slave");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
523 if ((fp = fopen(addr, "r"))) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
524 if (device->present != DEVPRESENT_YES) {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
525 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
526 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
527 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
528 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
529 device->present = DEVPRESENT_YES;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
530 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
531 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
532 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
533 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
534 /*
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
535 * The output looks like:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
536 * 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
537 * 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
538 */
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
539 fgets(line, 50, fp);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
540 line[strlen(line)-1] = '\0';
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
541 if ((line[36] == 'Y') && (line[37] == 'E')) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
542 /* CRC is Ok, continue */
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
543 fgets(line, 50, fp);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
544 line[strlen(line)-1] = '\0';
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
545 strtok(line, (char *)"=");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
546 p = strtok(NULL, (char *)"=");
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
547 rc = sscanf(p, "%d", &temp);
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
548 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
549 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
550 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
551 if ((rc == 1) && (device->value != temp)) {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
552 device->value = temp;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
553 device->timestamp = time(NULL);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
554 }
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 } else {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
559 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
560 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
561 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
562 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
563 device->present = DEVPRESENT_ERROR;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
564 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
565 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
566 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
567 }
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
568 fclose(fp);
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
569 } else {
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
570 if (device->present != DEVPRESENT_NO) {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 262
diff changeset
571 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
572 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
573 piLock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
574 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
575 device->present = DEVPRESENT_NO;
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
576 #ifdef HAVE_WIRINGPI_H
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
577 piUnlock(LOCK_DEVICES);
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
578 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
579 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
580 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
581 free(addr);
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
582 addr = NULL;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
583 }
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
584 break;
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
585
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
586 #ifdef HAVE_WIRINGPI_H
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
587 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
588 /*
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
589 * 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
590 */
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
591 now = time(NULL);
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
592 if ((int)(now - dht11_last) > 2) {
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
593 dht11_pin = device->gpiopin;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
594 dht11Read();
220
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
595 dht11_last = now;
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
596 }
382938c5fce2 Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents: 215
diff changeset
597 if (device->subdevice == 0) {
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
598 piLock(LOCK_DEVICES);
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
599 if (dht11_valid) {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
600 device->value = dht11_temperature * 1000;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
601 device->timestamp = time(NULL);
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
602 device->present = DEVPRESENT_YES;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
603 } else {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
604 device->present = DEVPRESENT_ERROR;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
605 }
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
606 piUnlock(LOCK_DEVICES);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
607 } else if (device->subdevice == 1) {
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
608 piLock(LOCK_DEVICES);
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
609 if (dht11_valid) {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
610 device->value = dht11_humidity * 1000;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
611 device->timestamp = time(NULL);
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
612 device->present = DEVPRESENT_YES;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
613 } else {
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
614 device->present = DEVPRESENT_ERROR;
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
615 }
240
6bdda35b4a13 Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents: 239
diff changeset
616 piUnlock(LOCK_DEVICES);
202
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
617 }
5d09ca728809 Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents: 187
diff changeset
618 break;
203
47e5109c7f53 Added DHT11 sensor code
Michiel Broek <mbroek@mbse.eu>
parents: 202
diff changeset
619 #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
620 #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
621 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
622 #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
623 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
624 #endif
262
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
625 if (Config.simulators) {
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
626 simulator = Config.simulators;
d0014ccec615 Simulation of fridge cold loss to the room added for testing.
Michiel Broek <mbroek@mbse.eu>
parents: 259
diff changeset
627 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
628 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
629 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
630 } 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
631 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
632 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
633 } 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
634 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
635 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
636 }
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
637 }
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
638 #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
639 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
640 #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
641 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
642 #endif
164
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
643 default:
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
644 break;
f16def8472ba Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents: 163
diff changeset
645 }
212
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
646
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
647 /*
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
648 * Delay a bit after procesing a device.
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
649 */
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
650 usleep(10000);
a76cbe676bf1 Code cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 209
diff changeset
651 }
215
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
652 /*
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
653 * 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
654 */
5ad534c79a22 Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents: 213
diff changeset
655 usleep(100000);
158
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
656 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
657
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
658 syslog(LOG_NOTICE, "Thread my_devices_loop stopped");
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
659 return 0;
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
660 }
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
661
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
662
f1b7e2ef90be Added device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
663

mercurial