thermferm/thermferm.c

changeset 66
805f1d285acd
parent 64
2431965e8a8c
child 71
a09c5cdc4022
equal deleted inserted replaced
65:a08a1fce439e 66:805f1d285acd
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/ 21 *****************************************************************************/
22 22
23 #include "thermferm.h" 23 #include "thermferm.h"
24 24
25 #ifdef HAVE_WIRINGPI_H
26
27 25
28 int tempA = 80; 26 int tempA = 80;
29 int tempB = 80; 27 int tempB = 80;
30 int coolerA = 0; 28 int coolerA = 0;
31 int coolerB = 0; 29 int coolerB = 0;
36 bool my_shutdown = false; 34 bool my_shutdown = false;
37 static pid_t pgrp, mypid; 35 static pid_t pgrp, mypid;
38 36
39 extern bool debug; 37 extern bool debug;
40 extern sys_config Config; 38 extern sys_config Config;
39 #ifdef HAVE_WIRINGPI_H
41 extern int lcdHandle; 40 extern int lcdHandle;
42 extern unsigned char lcdbuf[MAX_LCDS][20][4]; 41 extern unsigned char lcdbuf[MAX_LCDS][20][4];
42 #endif
43 int lcdupdate; 43 int lcdupdate;
44 44
45 45
46 int server(void); 46 int server(void);
47 void help(void); 47 void help(void);
48 void die(int); 48 void die(int);
49 #ifdef HAVE_WIRINGPI_H
49 void sendRCswitch(char *, int); 50 void sendRCswitch(char *, int);
50 void stopLCD(void); 51 void stopLCD(void);
51 void stopRCswitch(void); 52 void stopRCswitch(void);
53 #endif
52 54
53 55
54 56
55 void help(void) 57 void help(void)
56 { 58 {
77 my_shutdown = true; 79 my_shutdown = true;
78 } 80 }
79 81
80 82
81 83
84 #ifdef HAVE_WIRINGPI_H
82 void sendRCswitch(char *address, int state) 85 void sendRCswitch(char *address, int state)
83 { 86 {
84 char *cmd = NULL; 87 char *cmd = NULL;
85 int rc; 88 int rc;
86 89
93 if (debug) 96 if (debug)
94 fprintf(stdout, "Switch %s rc=%d\n", cmd, rc); 97 fprintf(stdout, "Switch %s rc=%d\n", cmd, rc);
95 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc); 98 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
96 free(cmd); 99 free(cmd);
97 } 100 }
98 101 #endif
99 102
100 103
104 #ifdef HAVE_WIRINGPI_H
101 void stopLCD(void) 105 void stopLCD(void)
102 { 106 {
103 mb_lcdClear(lcdHandle); 107 mb_lcdClear(lcdHandle);
104 setBacklight(0); 108 setBacklight(0);
105 } 109 }
122 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc); 126 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
123 free(cmd); 127 free(cmd);
124 cmd = NULL; 128 cmd = NULL;
125 } 129 }
126 } 130 }
131 #endif
127 132
128 133
129 134
130 int main(int argc, char *argv[]) 135 int main(int argc, char *argv[])
131 { 136 {
132 int rc, c, i; 137 int rc, c, i;
133 pid_t frk; 138 pid_t frk;
139 #ifdef HAVE_WIRINGPI_H
134 char buf[80]; 140 char buf[80];
141 #endif
135 142
136 while (1) { 143 while (1) {
137 int option_index = 0; 144 int option_index = 0;
138 static struct option long_options[] = { 145 static struct option long_options[] = {
139 {"debug", 0, 0, 'c'}, 146 {"debug", 0, 0, 'c'},
172 for (i = 0; i < NSIG; i++) { 179 for (i = 0; i < NSIG; i++) {
173 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) 180 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
174 signal(i, (void (*))die); 181 signal(i, (void (*))die);
175 } 182 }
176 183
184 #ifdef HAVE_WIRINGPI_H
177 if (wiringPiSetup () ) 185 if (wiringPiSetup () )
178 return 1; 186 return 1;
179 187
180 if ((rc = initLCD (16, 2))) { 188 if ((rc = initLCD (16, 2))) {
181 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); 189 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
193 if (debug) 201 if (debug)
194 fprintf(stdout, "Using 433 MHz transmitter on pin %d\n", Config.tx433); 202 fprintf(stdout, "Using 433 MHz transmitter on pin %d\n", Config.tx433);
195 syslog(LOG_NOTICE, "Using 433 MHz transmitter on pin %d", Config.tx433); 203 syslog(LOG_NOTICE, "Using 433 MHz transmitter on pin %d", Config.tx433);
196 enableTransmit(Config.tx433); 204 enableTransmit(Config.tx433);
197 } 205 }
206 #endif
198 207
199 if (debug) { 208 if (debug) {
200 /* 209 /*
201 * For debugging run in foreground. 210 * For debugging run in foreground.
202 */ 211 */
218 frk = fork(); 227 frk = fork();
219 switch (frk) { 228 switch (frk) {
220 case -1: 229 case -1:
221 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); 230 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
222 syslog(LOG_NOTICE, "Finished, rc=1"); 231 syslog(LOG_NOTICE, "Finished, rc=1");
232 #ifdef HAVE_WIRINGPI_H
223 stopLCD(); 233 stopLCD();
234 #endif
224 exit(1); 235 exit(1);
225 case 0: /* 236 case 0: /*
226 * Run the daemon 237 * Run the daemon
227 */ 238 */
228 fclose(stdin); 239 fclose(stdin);
263 int server(void) 274 int server(void)
264 { 275 {
265 char buf[1024]; 276 char buf[1024];
266 time_t now, last = (time_t)0; 277 time_t now, last = (time_t)0;
267 w1_therm *tmp1; 278 w1_therm *tmp1;
279 #ifdef HAVE_WIRINGPI_H
268 rc_switch *tmp2; 280 rc_switch *tmp2;
269 int rc, run = 1, temp; 281 int rc;
282 #endif
283 int run = 1, temp;
270 284
271 if (lockprog((char *)"thermferm")) { 285 if (lockprog((char *)"thermferm")) {
272 syslog(LOG_NOTICE, "Can't lock"); 286 syslog(LOG_NOTICE, "Can't lock");
273 return 1; 287 return 1;
274 } 288 }
275 289
290 #ifdef HAVE_WIRINGPI_H
276 rc = piThreadCreate(my_sensors_loop); 291 rc = piThreadCreate(my_sensors_loop);
277 if (rc) { 292 if (rc) {
278 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); 293 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc);
279 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); 294 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc);
280 } 295 }
282 rc = piThreadCreate(my_server_loop); 297 rc = piThreadCreate(my_server_loop);
283 if (rc) { 298 if (rc) {
284 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); 299 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc);
285 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); 300 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc);
286 } 301 }
302 #endif
287 303
288 snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB"); 304 snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB");
289 logger((char *)"thermferm.log", (char *)"thermferm", buf); 305 logger((char *)"thermferm.log", (char *)"thermferm", buf);
290 306
291 do { 307 do {
293 309
294 if (my_shutdown) 310 if (my_shutdown)
295 run = 0; 311 run = 0;
296 312
297 tmp1 = Config.w1therms; 313 tmp1 = Config.w1therms;
314 #ifdef HAVE_WIRINGPI_H
298 tmp2 = Config.rcswitch; 315 tmp2 = Config.rcswitch;
316 #endif
299 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) { 317 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) {
300 coolerA = 0; 318 coolerA = 0;
301 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); 319 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
320 #ifdef HAVE_WIRINGPI_H
302 sendRCswitch(tmp2->address, 0); 321 sendRCswitch(tmp2->address, 0);
322 #endif
303 lcdupdate = TRUE; 323 lcdupdate = TRUE;
304 } 324 }
305 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) { 325 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) {
306 coolerA = 1; 326 coolerA = 1;
307 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); 327 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
328 #ifdef HAVE_WIRINGPI_H
308 sendRCswitch(tmp2->address, 1); 329 sendRCswitch(tmp2->address, 1);
330 #endif
309 lcdupdate = TRUE; 331 lcdupdate = TRUE;
310 } 332 }
311 if (tmp1->update) { 333 if (tmp1->update) {
312 tmp1->update = FALSE; 334 tmp1->update = FALSE;
313 lcdupdate = TRUE; 335 lcdupdate = TRUE;
314 } 336 }
315 tmp1 = tmp1->next; 337 tmp1 = tmp1->next;
338 #ifdef HAVE_WIRINGPI_H
316 tmp2 = tmp2->next; 339 tmp2 = tmp2->next;
340 #endif
317 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) { 341 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) {
318 coolerB = 0; 342 coolerB = 0;
319 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); 343 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
344 #ifdef HAVE_WIRINGPI_H
320 sendRCswitch(tmp2->address, 0); 345 sendRCswitch(tmp2->address, 0);
346 #endif
321 lcdupdate = TRUE; 347 lcdupdate = TRUE;
322 } 348 }
323 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) { 349 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) {
324 coolerB = 1; 350 coolerB = 1;
325 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); 351 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
352 #ifdef HAVE_WIRINGPI_H
326 sendRCswitch(tmp2->address, 1); 353 sendRCswitch(tmp2->address, 1);
354 #endif
327 lcdupdate = TRUE; 355 lcdupdate = TRUE;
328 } 356 }
329 if (tmp1->update) { 357 if (tmp1->update) {
330 tmp1->update = FALSE; 358 tmp1->update = FALSE;
331 lcdupdate = TRUE; 359 lcdupdate = TRUE;
332 } 360 }
333 361
362 #ifdef HAVE_WIRINGPI_H
334 if (run && lcdupdate) { 363 if (run && lcdupdate) {
335 lcdPosition(lcdHandle, 0, 0); 364 lcdPosition(lcdHandle, 0, 0);
336 tmp1 = Config.w1therms; 365 tmp1 = Config.w1therms;
337 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '-' : ' ', tmp1->alias); 366 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '-' : ' ', tmp1->alias);
338 mb_lcdPuts(lcdHandle, buf); 367 mb_lcdPuts(lcdHandle, buf);
340 tmp1 = tmp1->next; 369 tmp1 = tmp1->next;
341 lcdPosition(lcdHandle, 0, 1); 370 lcdPosition(lcdHandle, 0, 1);
342 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '-' : ' ', tmp1->alias); 371 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '-' : ' ', tmp1->alias);
343 mb_lcdPuts(lcdHandle, buf); 372 mb_lcdPuts(lcdHandle, buf);
344 } 373 }
374 #endif
345 375
346 now = time(NULL); 376 now = time(NULL);
347 if (((int)now - (int)last) > 60) { 377 if (((int)now - (int)last) > 60) {
348 last = now; 378 last = now;
349 tmp1 = Config.w1therms; 379 tmp1 = Config.w1therms;
365 /* 395 /*
366 * Give threads time to cleanup 396 * Give threads time to cleanup
367 */ 397 */
368 usleep(1500000); 398 usleep(1500000);
369 399
400 #ifdef HAVE_WIRINGPI_H
370 if (Config.tx433 != -1) { 401 if (Config.tx433 != -1) {
371 stopRCswitch(); 402 stopRCswitch();
372 } 403 }
373 404
374 stopLCD(); 405 stopLCD();
375 disableTransmit(); 406 disableTransmit();
407 #endif
376 408
377 wrconfig((char *)"thermferm.conf"); 409 wrconfig((char *)"thermferm.conf");
378 410
379 ulockprog((char *)"thermferm"); 411 ulockprog((char *)"thermferm");
380 412
382 fprintf(stdout, "Goodbye\n"); 414 fprintf(stdout, "Goodbye\n");
383 415
384 return 0; 416 return 0;
385 } 417 }
386 418
387 #else
388
389
390 int main(int argc, char *argv[])
391 {
392 fprintf(stderr, "Compiled on a system without a wiringPi library.\n");
393 fprintf(stderr, "This program is useless and will do nothing.\n");
394 return 0;
395 }
396
397
398 #endif

mercurial