121 for (i = 0; i < NSIG; i++) { |
147 for (i = 0; i < NSIG; i++) { |
122 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
148 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
123 signal(i, (void (*))die); |
149 signal(i, (void (*))die); |
124 } |
150 } |
125 |
151 |
|
152 if (wiringPiSetup () ) |
|
153 return 1; |
|
154 |
126 if ((rc = initLCD (16, 2))) { |
155 if ((rc = initLCD (16, 2))) { |
127 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
156 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
128 return 1; |
157 return 1; |
129 } |
158 } |
130 |
159 |
131 lcdPosition(lcdHandle, 0, 0); |
160 lcdPosition(lcdHandle, 0, 0); |
132 lcdPuts(lcdHandle, "Coolers"); |
161 lcdPuts(lcdHandle, "Coolers"); |
133 lcdPosition(lcdHandle, 0, 1); |
162 lcdPosition(lcdHandle, 0, 1); |
134 sprintf(buf, "Version %s", VERSION); |
163 sprintf(buf, "Version %s", VERSION); |
135 lcdPuts(lcdHandle, buf); |
164 lcdPuts(lcdHandle, buf); |
|
165 |
|
166 if (Config.tx433 != -1) { |
|
167 if (debug) |
|
168 fprintf(stdout, "Using 433 MHz transmitter on pin %d\n", Config.tx433); |
|
169 syslog(LOG_NOTICE, "Using 433 MHz transmitter on pin %d", Config.tx433); |
|
170 enableTransmit(Config.tx433); |
|
171 } |
136 |
172 |
137 if (debug) { |
173 if (debug) { |
138 /* |
174 /* |
139 * For debugging run in foreground. |
175 * For debugging run in foreground. |
140 */ |
176 */ |
200 |
236 |
201 int server(void) |
237 int server(void) |
202 { |
238 { |
203 char buf[1024]; |
239 char buf[1024]; |
204 w1_therm *tmp1, *old1; |
240 w1_therm *tmp1, *old1; |
|
241 rc_switch *tmp2, *old2; |
205 int rc, run = 0; |
242 int rc, run = 0; |
206 |
243 |
207 my_mosquitto_init(); |
244 my_mosquitto_init(); |
208 |
245 |
209 rc = piThreadCreate (my_sensors_loop); |
246 rc = piThreadCreate(my_sensors_loop); |
210 if (rc) { |
247 if (rc) { |
211 fprintf(stderr, "my_sensors_loop thread didn't start\n"); |
248 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); |
212 } else { |
249 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); |
213 if (debug) |
|
214 fprintf(stdout, "Thread my_sensors_loop started\n"); |
|
215 } |
250 } |
216 |
251 |
217 do { |
252 do { |
218 lcdupdate = FALSE; |
253 lcdupdate = FALSE; |
219 |
254 |
220 run = my_mosquitto_loop(); |
255 run = my_mosquitto_loop(); |
|
256 |
|
257 tmp1 = Config.w1therms; |
|
258 tmp2 = Config.rcswitch; |
|
259 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) { |
|
260 my_mosquitto_switch(tmp2->address, 0); |
|
261 coolerA = 0; |
|
262 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
|
263 } |
|
264 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) { |
|
265 my_mosquitto_switch(tmp2->address, 1); |
|
266 coolerA = 1; |
|
267 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
|
268 } |
|
269 old1 = tmp1->next; |
|
270 tmp1 = old1; |
|
271 old2 = tmp2->next; |
|
272 tmp2 = old2; |
|
273 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) { |
|
274 my_mosquitto_switch(tmp2->address, 0); |
|
275 coolerB = 0; |
|
276 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
|
277 } |
|
278 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) { |
|
279 my_mosquitto_switch(tmp2->address, 1); |
|
280 coolerB = 1; |
|
281 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
|
282 } |
221 |
283 |
222 if (run && lcdupdate) { |
284 if (run && lcdupdate) { |
223 lcdPosition(lcdHandle, 0, 0); |
285 lcdPosition(lcdHandle, 0, 0); |
224 tmp1 = Config.w1therms; |
286 tmp1 = Config.w1therms; |
225 snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, ' ', tmp1->alias); |
287 snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '+' : ' ', tmp1->alias); |
226 lcdPuts(lcdHandle, buf); |
288 lcdPuts(lcdHandle, buf); |
227 old1 = tmp1->next; |
289 old1 = tmp1->next; |
228 tmp1 = old1; |
290 tmp1 = old1; |
229 lcdPosition(lcdHandle, 0, 1); |
291 lcdPosition(lcdHandle, 0, 1); |
230 snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, ' ', tmp1->alias); |
292 snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '+' : ' ', tmp1->alias); |
231 lcdPuts(lcdHandle, buf); |
293 lcdPuts(lcdHandle, buf); |
232 } |
294 } |
|
295 usleep(100000); |
233 |
296 |
234 } while (run); |
297 } while (run); |
235 |
298 |
236 if (debug) |
299 if (debug) |
237 fprintf(stdout, (char *)"Out of loop\n"); |
300 fprintf(stdout, (char *)"Out of loop\n"); |
238 |
301 |
|
302 /* |
|
303 * Give threads time to cleanup |
|
304 */ |
|
305 usleep(1500000); |
|
306 |
|
307 if (Config.tx433 != -1) { |
|
308 stopRCswitch(); |
|
309 } |
|
310 |
239 my_mosquitto_exit(); |
311 my_mosquitto_exit(); |
240 stopLCD(); |
312 stopLCD(); |
|
313 disableTransmit(); |
|
314 |
|
315 if (debug) |
|
316 fprintf(stdout, "Goodbye\n"); |
241 |
317 |
242 return 0; |
318 return 0; |
243 } |
319 } |
244 |
320 |
245 #else |
321 #else |