thermferm/thermferm.c

changeset 72
f7cb53c50ee1
parent 71
a09c5cdc4022
child 74
879bd09e2b96
equal deleted inserted replaced
71:a09c5cdc4022 72:f7cb53c50ee1
24 24
25 25
26 int current_unit = -1; 26 int current_unit = -1;
27 int tempA = 80; 27 int tempA = 80;
28 int tempB = 80; 28 int tempB = 80;
29 int coolerA = 0;
30 int coolerB = 0;
31 29
32 key_t key = 5680; /* key to be passed to shmget() */ 30 key_t key = 5680; /* key to be passed to shmget() */
33 int shmid; 31 int shmid;
34 32
35 bool my_shutdown = false; 33 bool my_shutdown = false;
81 } 79 }
82 80
83 81
84 82
85 #ifdef HAVE_WIRINGPI_H 83 #ifdef HAVE_WIRINGPI_H
86 void sendRCswitch(char *address, int state)
87 {
88 char *cmd = NULL;
89 int rc;
90
91 cmd = xstrcpy(address);
92 if (state)
93 cmd = xstrcat(cmd, (char *)",1");
94 else
95 cmd = xstrcat(cmd, (char *)",0");
96 rc = toggleSwitch(cmd);
97 if (debug)
98 fprintf(stdout, "Switch %s rc=%d\n", cmd, rc);
99 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
100 free(cmd);
101 }
102 #endif
103
104
105 #ifdef HAVE_WIRINGPI_H
106 void stopLCD(void) 84 void stopLCD(void)
107 { 85 {
108 mb_lcdClear(lcdHandle); 86 mb_lcdClear(lcdHandle);
109 setBacklight(0); 87 setBacklight(0);
110 }
111
112
113
114 void stopRCswitch(void)
115 {
116 rc_switch *tmp, *old;
117 char *cmd = NULL;
118 int rc;
119
120 for (tmp = Config.rcswitch; tmp; tmp = old) {
121 old = tmp->next;
122 cmd = xstrcpy(tmp->address);
123 cmd = xstrcat(cmd, (char *)",0");
124 rc = toggleSwitch(cmd);
125 if (debug)
126 fprintf(stdout, "Switch %s rc=%d\n", cmd, rc);
127 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
128 free(cmd);
129 cmd = NULL;
130 }
131 } 88 }
132 #endif 89 #endif
133 90
134 91
135 92
195 sprintf(buf, " Thermferm"); 152 sprintf(buf, " Thermferm");
196 mb_lcdPuts(lcdHandle, buf); 153 mb_lcdPuts(lcdHandle, buf);
197 lcdPosition(lcdHandle, 0, 1); 154 lcdPosition(lcdHandle, 0, 1);
198 sprintf(buf, " Version %s", VERSION); 155 sprintf(buf, " Version %s", VERSION);
199 mb_lcdPuts(lcdHandle, buf); 156 mb_lcdPuts(lcdHandle, buf);
200
201 if (Config.tx433 != -1) {
202 if (debug)
203 fprintf(stdout, "Using 433 MHz transmitter on pin %d\n", Config.tx433);
204 syslog(LOG_NOTICE, "Using 433 MHz transmitter on pin %d", Config.tx433);
205 enableTransmit(Config.tx433);
206 }
207 #endif 157 #endif
208 158
209 if (debug) { 159 if (debug) {
210 /* 160 /*
211 * For debugging run in foreground. 161 * For debugging run in foreground.
276 { 226 {
277 char buf[1024]; 227 char buf[1024];
278 time_t now, last = (time_t)0; 228 time_t now, last = (time_t)0;
279 w1_therm *tmp1; 229 w1_therm *tmp1;
280 #ifdef HAVE_WIRINGPI_H 230 #ifdef HAVE_WIRINGPI_H
281 rc_switch *tmp2;
282 int rc; 231 int rc;
283 #endif 232 #endif
284 int run = 1, temp; 233 int run = 1, temp;
285 234
286 if (lockprog((char *)"thermferm")) { 235 if (lockprog((char *)"thermferm")) {
300 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); 249 fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc);
301 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); 250 syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc);
302 } 251 }
303 #endif 252 #endif
304 253
305 snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB"); 254 snprintf(buf, 1023, "tempA,tempB");
306 logger((char *)"thermferm.log", (char *)"thermferm", buf); 255 logger((char *)"thermferm.log", (char *)"thermferm", buf);
307 256
308 do { 257 do {
309 lcdupdate = FALSE; 258 lcdupdate = FALSE;
310 259
311 if (my_shutdown) 260 if (my_shutdown)
312 run = 0; 261 run = 0;
313 262
314 tmp1 = Config.w1therms; 263 tmp1 = Config.w1therms;
315 #ifdef HAVE_WIRINGPI_H
316 tmp2 = Config.rcswitch;
317 #endif
318 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) {
319 coolerA = 0;
320 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
321 #ifdef HAVE_WIRINGPI_H
322 sendRCswitch(tmp2->address, 0);
323 #endif
324 lcdupdate = TRUE;
325 }
326 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) {
327 coolerA = 1;
328 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
329 #ifdef HAVE_WIRINGPI_H
330 sendRCswitch(tmp2->address, 1);
331 #endif
332 lcdupdate = TRUE;
333 }
334 if (tmp1->update) { 264 if (tmp1->update) {
335 tmp1->update = FALSE; 265 tmp1->update = FALSE;
336 lcdupdate = TRUE; 266 lcdupdate = TRUE;
337 } 267 }
338 tmp1 = tmp1->next; 268 tmp1 = tmp1->next;
339 #ifdef HAVE_WIRINGPI_H
340 tmp2 = tmp2->next;
341 #endif
342 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) {
343 coolerB = 0;
344 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
345 #ifdef HAVE_WIRINGPI_H
346 sendRCswitch(tmp2->address, 0);
347 #endif
348 lcdupdate = TRUE;
349 }
350 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) {
351 coolerB = 1;
352 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
353 #ifdef HAVE_WIRINGPI_H
354 sendRCswitch(tmp2->address, 1);
355 #endif
356 lcdupdate = TRUE;
357 }
358 if (tmp1->update) { 269 if (tmp1->update) {
359 tmp1->update = FALSE; 270 tmp1->update = FALSE;
360 lcdupdate = TRUE; 271 lcdupdate = TRUE;
361 } 272 }
362 273
363 #ifdef HAVE_WIRINGPI_H 274 #ifdef HAVE_WIRINGPI_H
364 if (run && lcdupdate) { 275 if (run && lcdupdate) {
365 lcdPosition(lcdHandle, 0, 0); 276 lcdPosition(lcdHandle, 0, 0);
366 tmp1 = Config.w1therms; 277 tmp1 = Config.w1therms;
367 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '-' : ' ', tmp1->alias); 278 snprintf(buf, 16, "%5.2f %cC %s ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias);
368 mb_lcdPuts(lcdHandle, buf); 279 mb_lcdPuts(lcdHandle, buf);
369 temp = tmp1->lastval; 280 temp = tmp1->lastval;
370 tmp1 = tmp1->next; 281 tmp1 = tmp1->next;
371 lcdPosition(lcdHandle, 0, 1); 282 lcdPosition(lcdHandle, 0, 1);
372 snprintf(buf, 16, "%5.2f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '-' : ' ', tmp1->alias); 283 snprintf(buf, 16, "%5.2f %cC %s ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias);
373 mb_lcdPuts(lcdHandle, buf); 284 mb_lcdPuts(lcdHandle, buf);
374 } 285 }
375 #endif 286 #endif
376 287
377 now = time(NULL); 288 now = time(NULL);
379 last = now; 290 last = now;
380 tmp1 = Config.w1therms; 291 tmp1 = Config.w1therms;
381 temp = tmp1->lastval; 292 temp = tmp1->lastval;
382 tmp1 = tmp1->next; 293 tmp1 = tmp1->next;
383 if (temp && tmp1->lastval && run) { 294 if (temp && tmp1->lastval && run) {
384 snprintf(buf, 1023, "%.2f,%s,%.2f,%s", temp / 1000.0, coolerA ? (char *)"on" : (char *)"off", 295 snprintf(buf, 1023, "%.2f,%.2f", temp / 1000.0, tmp1->lastval / 1000.0);
385 tmp1->lastval / 1000.0, coolerB ? (char *)"on" : (char *)"off");
386 logger((char *)"thermferm.log", (char *)"thermferm", buf); 296 logger((char *)"thermferm.log", (char *)"thermferm", buf);
387 } 297 }
388 } 298 }
389 usleep(100000); 299 usleep(100000);
390 300
397 * Give threads time to cleanup 307 * Give threads time to cleanup
398 */ 308 */
399 usleep(1500000); 309 usleep(1500000);
400 310
401 #ifdef HAVE_WIRINGPI_H 311 #ifdef HAVE_WIRINGPI_H
402 if (Config.tx433 != -1) {
403 stopRCswitch();
404 }
405
406 stopLCD(); 312 stopLCD();
407 disableTransmit();
408 #endif 313 #endif
409 314
410 wrconfig((char *)"thermferm.conf"); 315 wrconfig((char *)"thermferm.conf");
411 316
412 ulockprog((char *)"thermferm"); 317 ulockprog((char *)"thermferm");

mercurial