Sat, 23 Aug 2014 12:33:14 +0200
Added Simulator editor
158 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2014 | |
3 | * | |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with thermferm; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
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 | 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 | 27 | #include "xutil.h" |
28 | ||
29 | ||
30 | extern int debug; | |
31 | extern sys_config Config; | |
32 | extern int my_shutdown; | |
33 | ||
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) { |
235 | 127 | syslog(LOG_WARNING, "dht11 sensor disappeared"); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
128 | } else { |
235 | 129 | syslog(LOG_WARNING, "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) |
235 | 156 | syslog(LOG_WARNING, "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 | 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; |
187
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
169 | time_t now; |
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
170 | #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
|
171 | int i, rc; |
187
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
172 | char buf[40]; |
3c8bf18fdf42
Updated conditional defines and dependencies
Michiel Broek <mbroek@mbse.eu>
parents:
185
diff
changeset
|
173 | #endif |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
174 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
175 | 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
|
176 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
177 | |
185 | 178 | 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
|
179 | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
180 | 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
|
181 | if (! strcmp(uuid, device->uuid)) { |
185 | 182 | /* |
183 | * Execute command if different then the old value. But also | |
212 | 184 | * every 2 minutes because commands can have temporary |
185 | * disconnects, or have radio problems. | |
185 | 186 | */ |
187 | if ((value != device->value) || (((int)now - (int)device->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
|
188 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
189 | #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
|
190 | rc = 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
191 | 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
|
192 | 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
|
193 | 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
|
194 | if (buf[i] == '-') |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
195 | buf[i] = ','; |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
196 | 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
|
197 | rc = toggleSwitch(buf); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
198 | disableTransmit(); |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
199 | 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
|
200 | if (debug) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
201 | 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
|
202 | piLock(LOCK_DEVICES); |
212 | 203 | device->value = value; |
204 | device->timestamp = time(NULL); | |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
205 | piUnlock(LOCK_DEVICES); |
212 | 206 | return rc; |
207 | } | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
208 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
209 | 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
|
210 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
211 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
212 | #endif |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
213 | 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
|
214 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
215 | } |
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
|
216 | #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
|
217 | 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
|
218 | |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
219 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
220 | #endif |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
221 | } else { |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
222 | return 0; |
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 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
225 | } |
185 | 226 | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
227 | return 0; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
228 | } |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
229 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
230 | |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
166
diff
changeset
|
231 | |
158 | 232 | /* |
233 | * Auto detect hotplugged or known to be present devices | |
234 | */ | |
235 | int devices_detect(void) | |
236 | { | |
237 | struct dirent *de; | |
238 | DIR *fd; | |
239 | devices_list *device, *ndev; | |
240 | int found, subdevices, i, rc = 0; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
241 | char buf[40]; |
158 | 242 | uuid_t uu; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
243 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
244 | int pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
245 | #endif |
158 | 246 | |
247 | /* | |
248 | * Scan for 1-wire devices | |
249 | */ | |
250 | if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { | |
251 | while ((de = readdir(fd))) { | |
252 | if (de->d_name[0] != '.') { | |
253 | found = FALSE; | |
254 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
255 | if (strcmp(device->address,de->d_name) == 0) { |
158 | 256 | found = TRUE; |
257 | break; | |
258 | } | |
259 | } | |
260 | ||
261 | if (found == FALSE) { | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
262 | strncpy(buf, de->d_name, 2); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
263 | buf[2] = '\0'; |
158 | 264 | subdevices = 1; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
265 | if (strcmp(buf, (char *)"29") == 0) |
158 | 266 | subdevices = 8; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
267 | if (strcmp(buf, (char *)"3a") == 0) |
158 | 268 | subdevices = 2; |
269 | for (i = 0; i < subdevices; i++) { | |
270 | ndev = (devices_list *)malloc(sizeof(devices_list)); | |
271 | ndev->next = NULL; | |
272 | ndev->version = 1; | |
273 | ndev->uuid = malloc(37); | |
274 | uuid_generate(uu); | |
275 | uuid_unparse(uu, ndev->uuid); | |
276 | ndev->type = DEVTYPE_W1; | |
277 | ndev->direction = DEVDIR_UNDEF; | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
278 | if (strcmp(buf, (char *)"10") == 0) { |
158 | 279 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
280 | ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
281 | } else if (strcmp(buf, (char *)"28") == 0) { |
158 | 282 | ndev->direction = DEVDIR_IN_ANALOG; |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
283 | ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
284 | } else if (strcmp(buf, (char *)"29") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
285 | ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
286 | } else if (strcmp(buf, (char *)"3a") == 0) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
287 | ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
288 | } else if (strcmp(buf, (char *)"w1") == 0) { |
158 | 289 | ndev->description = xstrcpy((char *)"Master System device"); |
290 | } else { | |
291 | ndev->description = xstrcpy((char *)"Unknown device family "); | |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
292 | ndev->description = xstrcat(ndev->description, buf); |
158 | 293 | } |
294 | ndev->value = ndev->inuse = 0; | |
295 | ndev->present = DEVPRESENT_YES; | |
296 | ndev->address = xstrcpy(de->d_name); | |
297 | ndev->subdevice = i; | |
298 | ndev->gpiopin = -1; | |
299 | ndev->comment = xstrcpy((char *)"Auto detected device"); | |
300 | ndev->timestamp = time(NULL); | |
301 | ||
302 | if (Config.devices == NULL) { | |
303 | Config.devices = ndev; | |
304 | } else { | |
305 | for (device = Config.devices; device; device = device->next) { | |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
306 | if (device->next == NULL) { |
158 | 307 | device->next = ndev; |
161
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
308 | break; |
493e39bb0a08
Small fixes for device configuration
Michiel Broek <mbroek@mbse.eu>
parents:
158
diff
changeset
|
309 | } |
158 | 310 | } |
311 | } | |
312 | rc++; | |
313 | } | |
314 | } | |
315 | } | |
316 | } | |
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
|
317 | closedir(fd); |
158 | 318 | } |
319 | ||
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
320 | #ifdef HAVE_WIRINGPI_H |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
321 | if (piBoardRev() == 2) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
322 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
323 | * Support rev B boards only |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
324 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
325 | found = FALSE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
326 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
327 | if (device->type == DEVTYPE_GPIO) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
328 | found = TRUE; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
329 | break; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
330 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
331 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
332 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
333 | if (found == FALSE) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
334 | /* |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
335 | * There were no GPIO devices found. |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
336 | */ |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
337 | subdevices = 12; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
338 | pin = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
339 | for (i = 0; i < subdevices; i++) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
340 | if (i == 8) |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
341 | pin = 17; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
342 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
343 | ndev = (devices_list *)malloc(sizeof(devices_list)); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
344 | ndev->next = NULL; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
345 | ndev->version = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
346 | ndev->uuid = malloc(37); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
347 | uuid_generate(uu); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
348 | uuid_unparse(uu, ndev->uuid); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
349 | ndev->type = DEVTYPE_GPIO; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
350 | ndev->value = digitalRead(pin); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
351 | 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
|
352 | ndev->address = xstrcpy((char *)"GPIO"); |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
353 | snprintf(buf, 39, "Raspberry GPIO %d", i); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
354 | ndev->description = xstrcpy(buf); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
355 | ndev->subdevice = i; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
356 | ndev->gpiopin = pin; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
357 | ndev->timestamp = time(NULL); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
358 | if (i == PANEL_LED) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
359 | ndev->direction = DEVDIR_OUT_BIN; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
360 | ndev->inuse = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
361 | ndev->comment = xstrcpy((char *)"Frontpanel LED"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
362 | } else if (i == PANEL_ENTER) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
363 | ndev->direction = DEVDIR_IN_BIN; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
364 | ndev->inuse = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
365 | ndev->comment = xstrcpy((char *)"Frontpanel Enter key"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
366 | } else if (i == PANEL_DOWN) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
367 | ndev->direction = DEVDIR_IN_BIN; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
368 | ndev->inuse = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
369 | ndev->comment = xstrcpy((char *)"Frontpanel Down key"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
370 | } else if (i == PANEL_UP) { |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
371 | ndev->direction = DEVDIR_IN_BIN; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
372 | ndev->inuse = 1; |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
373 | ndev->comment = xstrcpy((char *)"Frontpanel Up key"); |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
374 | } else if (i == 7) { |
162
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
375 | ndev->direction = DEVDIR_INTERN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
376 | ndev->inuse = 1; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
377 | ndev->comment = xstrcpy((char *)"1-Wire bus"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
378 | } else { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
379 | ndev->direction = DEVDIR_IN_BIN; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
380 | ndev->inuse = 0; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
381 | ndev->comment = xstrcpy((char *)"Raspberry GPIO"); |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
382 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
383 | pin++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
384 | |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
385 | if (Config.devices == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
386 | Config.devices = ndev; |
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 | for (device = Config.devices; device; device = device->next) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
389 | if (device->next == NULL) { |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
390 | device->next = ndev; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
391 | break; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
392 | } |
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 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
395 | rc++; |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
396 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
397 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
398 | } |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
399 | #endif |
6fc9e3f7962f
Added Raspberry GPIO devices
Michiel Broek <mbroek@mbse.eu>
parents:
161
diff
changeset
|
400 | |
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
|
401 | #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
|
402 | 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
|
403 | 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
|
404 | 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
|
405 | 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
|
406 | 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
|
407 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
408 | } |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
409 | |
b7c967359771
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 | 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
|
411 | 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
|
412 | 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
|
413 | 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
|
414 | 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
|
415 | 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
|
416 | 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
|
417 | 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
|
418 | 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
|
419 | 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
|
420 | 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
|
421 | 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
|
422 | 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
|
423 | 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
|
424 | 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
|
425 | 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
|
426 | 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
|
427 | 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
|
428 | 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
|
429 | 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
|
430 | 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
|
431 | break; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
432 | 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
|
433 | 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
|
434 | 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
|
435 | 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
|
436 | 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
|
437 | 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
|
438 | 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
|
439 | 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
|
440 | 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
|
441 | 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
|
442 | 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
|
443 | 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
|
444 | 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
|
445 | 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
|
446 | 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
|
447 | 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
|
448 | } |
b7c967359771
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 | |
b7c967359771
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 | 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
|
451 | 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
|
452 | } 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
|
453 | 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
|
454 | 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
|
455 | 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
|
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 | } |
b7c967359771
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 | 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
|
461 | } |
b7c967359771
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 | } |
b7c967359771
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 | #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
|
464 | |
158 | 465 | return rc; |
466 | } | |
467 | ||
468 | ||
469 | ||
470 | #ifdef HAVE_WIRINGPI_H | |
471 | PI_THREAD (my_devices_loop) | |
472 | #else | |
473 | void *my_devices_loop(void *threadid) | |
474 | #endif | |
475 | { | |
476 | devices_list *device; | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
477 | char *addr = NULL, line[60], *p = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
478 | FILE *fp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
479 | int temp, rc; |
242
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
480 | #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
|
481 | time_t now; |
242
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
482 | #endif |
158 | 483 | |
484 | syslog(LOG_NOTICE, "Thread my_devices_loop started"); | |
485 | ||
242
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
486 | #ifdef HAVE_WIRINGPI_H |
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
487 | if ((rc = piHiPri(10))) |
d3fe84ece349
Fixed conditionals on systems without wiringPi.
Michiel Broek <mbroek@mbse.eu>
parents:
240
diff
changeset
|
488 | 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
|
489 | #endif |
239
793c09ece542
Increase priority for the devices thread
Michiel Broek <mbroek@mbse.eu>
parents:
235
diff
changeset
|
490 | |
158 | 491 | /* |
492 | * Loop forever until the external shutdown variable is set. | |
493 | */ | |
494 | for (;;) { | |
495 | ||
496 | /* | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
497 | * Process all devices. |
158 | 498 | */ |
499 | for (device = Config.devices; device; device = device->next) { | |
500 | ||
501 | if (my_shutdown) | |
502 | break; | |
503 | ||
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
504 | switch (device->type) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
505 | case DEVTYPE_W1: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
506 | 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
|
507 | /* Read DS18B20 sensor */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
508 | addr = xstrcpy((char *)"/sys/bus/w1/devices/"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
509 | addr = xstrcat(addr, device->address); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
510 | addr = xstrcat(addr, (char *)"/w1_slave"); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
511 | if ((fp = fopen(addr, "r"))) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
512 | if (device->present != DEVPRESENT_YES) { |
235 | 513 | syslog(LOG_WARNING, "sensor %s is back", device->address); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
514 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
515 | piLock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
516 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
517 | device->present = DEVPRESENT_YES; |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
518 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
519 | piUnlock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
520 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
521 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
522 | /* |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
523 | * The output looks like: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
524 | * 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
|
525 | * 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
|
526 | */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
527 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
528 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
529 | if ((line[36] == 'Y') && (line[37] == 'E')) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
530 | /* CRC is Ok, continue */ |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
531 | fgets(line, 50, fp); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
532 | line[strlen(line)-1] = '\0'; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
533 | strtok(line, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
534 | p = strtok(NULL, (char *)"="); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
535 | rc = sscanf(p, "%d", &temp); |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
536 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
537 | piLock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
538 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
539 | if ((rc == 1) && (device->value != temp)) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
540 | device->value = temp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
541 | device->timestamp = time(NULL); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
542 | } |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
543 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
544 | piUnlock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
545 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
546 | } else { |
235 | 547 | syslog(LOG_WARNING, "sensor %s CRC error", device->address); |
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 | device->present = DEVPRESENT_ERROR; |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
552 | #ifdef HAVE_WIRINGPI_H |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
553 | piUnlock(LOCK_DEVICES); |
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
554 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
555 | } |
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
|
556 | fclose(fp); |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
557 | } else { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
558 | if (device->present != DEVPRESENT_NO) { |
235 | 559 | syslog(LOG_WARNING, "sensor %s is missing", 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_NO; |
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 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
568 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
569 | free(addr); |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
570 | addr = NULL; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
571 | } |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
572 | break; |
203 | 573 | |
574 | #ifdef HAVE_WIRINGPI_H | |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
575 | 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
|
576 | /* |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
577 | * 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
|
578 | */ |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
579 | now = time(NULL); |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
580 | if ((int)(now - dht11_last) > 2) { |
203 | 581 | dht11_pin = device->gpiopin; |
582 | dht11Read(); | |
220
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
583 | dht11_last = now; |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
584 | } |
382938c5fce2
Make sure we don't read the DHT11 within 2 seconds.
Michiel Broek <mbroek@mbse.eu>
parents:
215
diff
changeset
|
585 | if (device->subdevice == 0) { |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
586 | piLock(LOCK_DEVICES); |
203 | 587 | if (dht11_valid) { |
588 | device->value = dht11_temperature * 1000; | |
589 | device->timestamp = time(NULL); | |
590 | device->present = DEVPRESENT_YES; | |
591 | } else { | |
592 | device->present = DEVPRESENT_ERROR; | |
593 | } | |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
594 | piUnlock(LOCK_DEVICES); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
595 | } else if (device->subdevice == 1) { |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
596 | piLock(LOCK_DEVICES); |
203 | 597 | if (dht11_valid) { |
598 | device->value = dht11_humidity * 1000; | |
599 | device->timestamp = time(NULL); | |
600 | device->present = DEVPRESENT_YES; | |
601 | } else { | |
602 | device->present = DEVPRESENT_ERROR; | |
603 | } | |
240
6bdda35b4a13
Added thread locks for device processing
Michiel Broek <mbroek@mbse.eu>
parents:
239
diff
changeset
|
604 | piUnlock(LOCK_DEVICES); |
202
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
605 | } |
5d09ca728809
Code added on the wrong machine
Michiel Broek <mbroek@mbse.eu>
parents:
187
diff
changeset
|
606 | break; |
203 | 607 | #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
|
608 | #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
|
609 | 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
|
610 | #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
|
611 | 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
|
612 | #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
|
613 | if (device->subdevice == 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
|
614 | device->value = 20000; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
615 | device->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
|
616 | } else if (device->subdevice == 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
|
617 | device->value = 20125; |
b7c967359771
Added framework for a simulation of a fridge with heater to use as controlled fermentor
Michiel Broek <mbroek@mbse.eu>
parents:
242
diff
changeset
|
618 | device->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
|
619 | } else if (device->subdevice == 2) { |
b7c967359771
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 | device->value = 20250; |
b7c967359771
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 | device->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
|
622 | } |
b7c967359771
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 | #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
|
624 | 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
|
625 | #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
|
626 | 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
|
627 | #endif |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
628 | default: |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
629 | break; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
163
diff
changeset
|
630 | } |
212 | 631 | |
632 | /* | |
633 | * Delay a bit after procesing a device. | |
634 | */ | |
635 | usleep(10000); | |
636 | } | |
215
5ad534c79a22
Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
637 | /* |
5ad534c79a22
Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
638 | * 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
|
639 | */ |
5ad534c79a22
Do not use 100% cpu on a system without devices
Michiel Broek <mbroek@mbse.eu>
parents:
213
diff
changeset
|
640 | usleep(100000); |
158 | 641 | } |
642 | ||
643 | syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); | |
644 | return 0; | |
645 | } | |
646 | ||
647 | ||
648 |