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 EC-65K; 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 |
|
23 #include "../lib/mbselib.h" |
|
24 #include "main.h" |
|
25 |
|
26 |
|
27 #define STATUS_CONNECTING 0 |
|
28 #define STATUS_CONNACK_RECVD 1 |
|
29 #define STATUS_WAITING 2 |
|
30 |
|
31 /* Global variables for use in callbacks. */ |
|
32 static int qos = 0; |
|
33 static int status = STATUS_CONNECTING; |
|
34 static int mid_sent = 0; |
|
35 static int last_mid = -1; |
|
36 static int last_mid_sent = -1; |
|
37 static bool connected = true; |
|
38 static bool disconnect_sent = false; |
|
39 static bool connect_lost = false; |
|
40 static bool my_shutdown = false; |
|
41 static pid_t pgrp, mypid; |
|
42 |
|
43 extern bool debug; |
|
44 extern sys_config Config; |
|
45 #ifdef HAVE_WIRINGPI_H |
|
46 extern int lcdHandle; |
|
47 #endif |
|
48 |
|
49 int server(void); |
|
50 void help(void); |
|
51 void die(int); |
|
52 |
|
53 |
|
54 void help(void) |
|
55 { |
|
56 fprintf(stdout, "mbsePi-apps thermometers v%s starting\n\n", VERSION); |
|
57 fprintf(stdout, "Usage: thermomeneters [-d] [-h]\n"); |
|
58 fprintf(stdout, " -d --debug Debug and run in foreground\n"); |
|
59 fprintf(stdout, " -h --help Display this help\n"); |
|
60 } |
|
61 |
|
62 |
|
63 |
|
64 void die(int onsig) |
|
65 { |
|
66 switch (onsig) { |
|
67 case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); |
|
68 break; |
|
69 case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); |
|
70 break; |
|
71 case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); |
|
72 break; |
|
73 default: syslog(LOG_NOTICE, "die() on signal %d", onsig); |
|
74 } |
|
75 |
|
76 my_shutdown = true; |
|
77 } |
|
78 |
|
79 |
|
80 |
|
81 void my_connect_callback(struct mosquitto *mosq, void *obj, int result) |
|
82 { |
|
83 if (connect_lost) { |
|
84 connect_lost = false; |
|
85 syslog(LOG_NOTICE, "Reconnect: %s", mosquitto_connack_string(result)); |
|
86 } |
|
87 |
|
88 if (!result) { |
|
89 status = STATUS_CONNACK_RECVD; |
|
90 } else { |
|
91 syslog(LOG_NOTICE, "my_connect_callback: %s\n", mosquitto_connack_string(result)); |
|
92 } |
|
93 } |
|
94 |
|
95 |
|
96 |
|
97 void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc) |
|
98 { |
|
99 if (my_shutdown) { |
|
100 syslog(LOG_NOTICE, "Acknowledged DISCONNECT from %s", Config.mosq_host); |
|
101 connected = false; |
|
102 } else { |
|
103 /* |
|
104 * The remove server was brought down. We must keep running |
|
105 */ |
|
106 syslog(LOG_NOTICE, "Received DISCONNECT from %s, connection lost", Config.mosq_host); |
|
107 connect_lost = true; |
|
108 } |
|
109 } |
|
110 |
|
111 |
|
112 |
|
113 void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) |
|
114 { |
|
115 last_mid_sent = mid; |
|
116 } |
|
117 |
|
118 |
|
119 |
|
120 void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str) |
|
121 { |
|
122 syslog(LOG_NOTICE, "MQTT: %s", str); |
|
123 printf("MQTT: %s\n", str); |
|
124 } |
|
125 |
|
126 |
|
127 |
|
128 #ifdef HAVE_WIRINGPI_H |
|
129 void stopLCD(void) |
|
130 { |
|
131 lcdClear(lcdHandle); |
|
132 setBacklight(0); |
|
133 } |
|
134 #endif |
|
135 |
|
136 |
|
137 |
|
138 int main(int argc, char *argv[]) |
|
139 { |
|
140 int rc, c, i; |
|
141 pid_t frk; |
|
142 #ifdef HAVE_WIRINGPI_H |
|
143 char buf[80]; |
|
144 #endif |
|
145 |
|
146 while (1) { |
|
147 int option_index = 0; |
|
148 static struct option long_options[] = { |
|
149 {"debug", 0, 0, 'c'}, |
|
150 {"help", 0, 0, 'h'}, |
|
151 {0, 0, 0, 0} |
|
152 }; |
|
153 |
|
154 c = getopt_long(argc, argv, "dh", long_options, &option_index); |
|
155 if (c == -1) |
|
156 break; |
|
157 |
|
158 switch (c) { |
|
159 case 'd': debug = true; |
|
160 break; |
|
161 case 'h': help(); |
|
162 return 1; |
|
163 } |
|
164 } |
|
165 |
|
166 openlog("thermometers", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); |
|
167 syslog(LOG_NOTICE, "mbsePi-apps thermometers v%s starting", VERSION); |
|
168 if (debug) |
|
169 fprintf(stdout, "mbsePi-apps thermometers v%s starting\n", VERSION); |
|
170 |
|
171 if (rdconfig((char *)"thermometers.conf")) { |
|
172 fprintf(stderr, "Error reading configuration\n"); |
|
173 syslog(LOG_NOTICE, "halted"); |
|
174 return 1; |
|
175 } |
|
176 |
|
177 /* |
|
178 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
|
179 * but that's live. This daemon should only be stopped by SIGTERM. |
|
180 * Don't catch SIGCHLD. |
|
181 */ |
|
182 for (i = 0; i < NSIG; i++) { |
|
183 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
|
184 signal(i, (void (*))die); |
|
185 } |
|
186 |
|
187 #ifdef HAVE_WIRINGPI_H |
|
188 |
|
189 if (wiringPiSetup () ) |
|
190 return 1; |
|
191 |
|
192 if ((rc = initLCD (16, 2))) { |
|
193 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
|
194 return 1; |
|
195 } |
|
196 |
|
197 lcdPosition(lcdHandle, 0, 0); |
|
198 lcdPuts(lcdHandle, "Thermometers"); |
|
199 lcdPosition(lcdHandle, 0, 1); |
|
200 sprintf(buf, "Version %s", VERSION); |
|
201 lcdPuts(lcdHandle, buf); |
|
202 #endif |
|
203 |
|
204 if (debug) { |
|
205 /* |
|
206 * For debugging run in foreground. |
|
207 */ |
|
208 rc = server(); |
|
209 } else { |
|
210 /* |
|
211 * Server initialization is complete. Now we can fork the |
|
212 * daemon and return to the user. We need to do a setpgrp |
|
213 * so that the daemon will no longer be assosiated with the |
|
214 * users control terminal. This is done before the fork, so |
|
215 * that the child will not be a process group leader. Otherwise, |
|
216 * if the child were to open a terminal, it would become |
|
217 * associated with that terminal as its control terminal. |
|
218 */ |
|
219 if ((pgrp = setpgid(0, 0)) == -1) { |
|
220 syslog(LOG_NOTICE, "setpgpid failed"); |
|
221 } |
|
222 |
|
223 frk = fork(); |
|
224 switch (frk) { |
|
225 case -1: |
|
226 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); |
|
227 syslog(LOG_NOTICE, "Finished, rc=1"); |
|
228 #ifdef HAVE_WIRINGPI_H |
|
229 stopLCD(); |
|
230 #endif |
|
231 exit(1); |
|
232 case 0: /* |
|
233 * Run the daemon |
|
234 */ |
|
235 fclose(stdin); |
|
236 if (open("/dev/null", O_RDONLY) != 0) { |
|
237 syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); |
|
238 _exit(2); |
|
239 } |
|
240 fclose(stdout); |
|
241 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { |
|
242 syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); |
|
243 _exit(2); |
|
244 } |
|
245 fclose(stderr); |
|
246 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { |
|
247 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); |
|
248 _exit(2); |
|
249 } |
|
250 mypid = getpid(); |
|
251 rc = server(); |
|
252 break; |
|
253 /* Not reached */ |
|
254 default: |
|
255 /* |
|
256 * Here we detach this process and let the child |
|
257 * run the deamon process. |
|
258 */ |
|
259 syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); |
|
260 exit(0); |
|
261 } |
|
262 } |
|
263 |
|
264 syslog(LOG_NOTICE, "Finished, rc=%d", rc); |
|
265 return rc; |
|
266 } |
|
267 |
|
268 |
|
269 |
|
270 int server(void) |
|
271 { |
|
272 char *id = NULL, *state = NULL; |
|
273 struct mosquitto *mosq = NULL; |
|
274 char hostname[256], buf[1024]; |
|
275 int temp, rc, deviation, keepalive = 60; |
|
276 #ifdef HAVE_WIRINGPI_H |
|
277 int lcdupdate; |
|
278 #endif |
|
279 unsigned int max_inflight = 20; |
|
280 char err[1024]; |
|
281 w1_therm *tmp1, *old1; |
|
282 char *device, *alias, line[60], *p = NULL; |
|
283 FILE *fp; |
|
284 |
|
285 /* |
|
286 * Initialize mosquitto communication |
|
287 */ |
|
288 mosquitto_lib_init(); |
|
289 |
|
290 /* |
|
291 * Build MQTT id |
|
292 */ |
|
293 hostname[0] = '\0'; |
|
294 gethostname(hostname, 256); |
|
295 hostname[255] = '\0'; |
|
296 |
|
297 id = xstrcpy((char *)"thermometers/"); |
|
298 id = xstrcat(id, hostname); |
|
299 if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { |
|
300 /* |
|
301 * Enforce maximum client id length of 23 characters |
|
302 */ |
|
303 id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; |
|
304 } |
|
305 |
|
306 mosq = mosquitto_new(id, true, NULL); |
|
307 if(!mosq) { |
|
308 switch(errno) { |
|
309 case ENOMEM: |
|
310 syslog(LOG_NOTICE, "mosquitto_new: Out of memory"); |
|
311 break; |
|
312 case EINVAL: |
|
313 syslog(LOG_NOTICE, "mosquitto_new: Invalid id"); |
|
314 break; |
|
315 } |
|
316 mosquitto_lib_cleanup(); |
|
317 return 1; |
|
318 } |
|
319 |
|
320 if (debug) { |
|
321 mosquitto_log_callback_set(mosq, my_log_callback); |
|
322 } |
|
323 |
|
324 /* |
|
325 * Set our will |
|
326 */ |
|
327 state = xstrcpy((char *)"clients/"); |
|
328 state = xstrcat(state, hostname); |
|
329 state = xstrcat(state, (char *)"/thermometers/state"); |
|
330 sprintf(buf, "0"); |
|
331 if ((rc = mosquitto_will_set(mosq, state, strlen(buf), buf, qos, true))) { |
|
332 if (rc == MOSQ_ERR_INVAL) { |
|
333 syslog(LOG_NOTICE, "mosquitto_will_set: input parameters invalid"); |
|
334 } else if (rc == MOSQ_ERR_NOMEM) { |
|
335 syslog(LOG_NOTICE, "mosquitto_will_set: Out of Memory"); |
|
336 } else if (rc == MOSQ_ERR_PAYLOAD_SIZE) { |
|
337 syslog(LOG_NOTICE, "mosquitto_will_set: invalid payload size"); |
|
338 } |
|
339 mosquitto_lib_cleanup(); |
|
340 return rc; |
|
341 } |
|
342 |
|
343 mosquitto_max_inflight_messages_set(mosq, max_inflight); |
|
344 mosquitto_connect_callback_set(mosq, my_connect_callback); |
|
345 mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); |
|
346 mosquitto_publish_callback_set(mosq, my_publish_callback); |
|
347 |
|
348 if ((rc = mosquitto_connect(mosq, Config.mosq_host, Config.mosq_port, keepalive))) { |
|
349 if (rc == MOSQ_ERR_ERRNO) { |
|
350 strerror_r(errno, err, 1024); |
|
351 syslog(LOG_NOTICE, "mosquitto_connect: error: %s", err); |
|
352 } else { |
|
353 syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc); |
|
354 } |
|
355 mosquitto_lib_cleanup(); |
|
356 return rc; |
|
357 } |
|
358 syslog(LOG_NOTICE, "Connected with %s:%d", Config.mosq_host, Config.mosq_port); |
|
359 |
|
360 /* |
|
361 * Initialise is complete, report our presence state |
|
362 */ |
|
363 mosquitto_loop_start(mosq); |
|
364 sprintf(buf, "1"); |
|
365 rc = mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, 1); |
|
366 #ifdef HAVE_WIRINGPI_H |
|
367 // setBacklight(0); |
|
368 #endif |
|
369 |
|
370 /* |
|
371 * Report alias names |
|
372 */ |
|
373 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
|
374 old1 = tmp1->next; |
|
375 |
|
376 alias = xstrcpy((char *)"/raw/"); |
|
377 alias = xstrcat(alias, hostname); |
|
378 alias = xstrcat(alias, (char *)"/thermometers/w1/"); |
|
379 alias = xstrcat(alias, tmp1->master); |
|
380 alias = xstrcat(alias, (char *)"/"); |
|
381 alias = xstrcat(alias, tmp1->name); |
|
382 alias = xstrcat(alias, (char *)"/alias"); |
|
383 |
|
384 sprintf(buf, "%s", tmp1->alias); |
|
385 if ((rc = mosquitto_publish(mosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) { |
|
386 if (rc == MOSQ_ERR_NO_CONN) |
|
387 mosquitto_reconnect(mosq); |
|
388 else |
|
389 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc); |
|
390 } |
|
391 |
|
392 free(alias); |
|
393 alias = NULL; |
|
394 } |
|
395 |
|
396 if (debug) |
|
397 fprintf(stdout, (char *)"Enter loop, connected %d\n", connected); |
|
398 |
|
399 do { |
|
400 if (status == STATUS_CONNACK_RECVD) { |
|
401 #ifdef HAVE_WIRINGPI_H |
|
402 lcdupdate = FALSE; |
|
403 #endif |
|
404 |
|
405 /* |
|
406 * Here send our 1-wire sensors values |
|
407 */ |
|
408 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
|
409 old1 = tmp1->next; |
|
410 |
|
411 /* |
|
412 * Build path and alias topic |
|
413 */ |
|
414 device = xstrcpy((char *)"/sys/bus/w1/devices/"); |
|
415 device = xstrcat(device, tmp1->master); |
|
416 device = xstrcat(device, (char *)"/"); |
|
417 device = xstrcat(device, tmp1->name); |
|
418 device = xstrcat(device, (char *)"/w1_slave"); |
|
419 alias = xstrcpy((char *)"/raw/"); |
|
420 alias = xstrcat(alias, hostname); |
|
421 alias = xstrcat(alias, (char *)"/thermometers/w1/"); |
|
422 alias = xstrcat(alias, tmp1->master); |
|
423 alias = xstrcat(alias, (char *)"/"); |
|
424 alias = xstrcat(alias, tmp1->name); |
|
425 alias = xstrcat(alias, (char *)"/temperature"); |
|
426 |
|
427 /* |
|
428 * Read sensor data |
|
429 */ |
|
430 if ((fp = fopen(device, "r"))) { |
|
431 /* |
|
432 * The output looks like: |
|
433 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
|
434 * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
|
435 */ |
|
436 fgets(line, 50, fp); |
|
437 line[strlen(line)-1] = '\0'; |
|
438 if ((line[36] == 'Y') && (line[37] == 'E')) { |
|
439 /* |
|
440 * CRC is Ok, continue |
|
441 */ |
|
442 fgets(line, 50, fp); |
|
443 line[strlen(line)-1] = '\0'; |
|
444 strtok(line, (char *)"="); |
|
445 p = strtok(NULL, (char *)"="); |
|
446 rc = sscanf(p, "%d", &temp); |
|
447 if ((rc == 1) && (tmp1->lastval != temp)) { |
|
448 /* |
|
449 * It is possible to have read errors or extreme values. |
|
450 * This can happen with bad connections so we compare the |
|
451 * value with the previous one. If the difference is too |
|
452 * much, we don't send that value. That also means that if |
|
453 * the next value is ok again, it will be marked invalid too. |
|
454 * Maximum error is 20 degrees for now. |
|
455 */ |
|
456 deviation = 20000; |
|
457 if ((tmp1->lastval == 0) || |
|
458 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) { |
|
459 /* |
|
460 * Temperature is changed and valid, update and publish this. |
|
461 */ |
|
462 sprintf(buf, "%.1f", temp / 1000.0); |
|
463 if ((rc = mosquitto_publish(mosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) { |
|
464 if (rc == MOSQ_ERR_NO_CONN) |
|
465 mosquitto_reconnect(mosq); |
|
466 else |
|
467 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc); |
|
468 } |
|
469 } else { |
|
470 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp); |
|
471 if (debug) { |
|
472 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp); |
|
473 } |
|
474 } |
|
475 tmp1->lastval = temp; |
|
476 #ifdef HAVE_WIRINGPI_H |
|
477 lcdupdate = TRUE; |
|
478 #endif |
|
479 } |
|
480 } else { |
|
481 syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name); |
|
482 } |
|
483 fclose(fp); |
|
484 tmp1->present = 1; |
|
485 } else { |
|
486 tmp1->present = 0; |
|
487 if (debug) |
|
488 printf("sensor %s is missing\n", tmp1->name); |
|
489 } |
|
490 |
|
491 free(device); |
|
492 device = NULL; |
|
493 free(alias); |
|
494 alias = NULL; |
|
495 } |
|
496 |
|
497 #ifdef HAVE_WIRINGPI_H |
|
498 if (lcdupdate) { |
|
499 lcdPosition(lcdHandle, 0, 0); |
|
500 tmp1 = Config.w1therms; |
|
501 snprintf(buf, 16, "%5.1f %cC %s ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias); |
|
502 lcdPuts(lcdHandle, buf); |
|
503 old1 = tmp1->next; |
|
504 tmp1 = old1; |
|
505 lcdPosition(lcdHandle, 0, 1); |
|
506 snprintf(buf, 16, "%5.1f %cC %s ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias); |
|
507 lcdPuts(lcdHandle, buf); |
|
508 } |
|
509 #endif |
|
510 |
|
511 if (my_shutdown) { |
|
512 /* |
|
513 * Final publish 0 to clients/<hostname>/thermometers/state |
|
514 */ |
|
515 sprintf(buf, "0"); |
|
516 mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, true); |
|
517 last_mid = mid_sent; |
|
518 status = STATUS_WAITING; |
|
519 #ifdef HAVE_WIRINGPI_H |
|
520 lcdClear(lcdHandle); |
|
521 lcdPosition(lcdHandle, 0, 0); |
|
522 lcdPuts(lcdHandle, "Shuting down ..."); |
|
523 #endif |
|
524 } |
|
525 |
|
526 usleep(100000); |
|
527 |
|
528 } else if (status == STATUS_WAITING) { |
|
529 if (debug) |
|
530 fprintf(stdout, (char *)"Waiting\n"); |
|
531 if (last_mid_sent == last_mid && disconnect_sent == false) { |
|
532 mosquitto_disconnect(mosq); |
|
533 disconnect_sent = true; |
|
534 } |
|
535 usleep(100000); |
|
536 } |
|
537 rc = MOSQ_ERR_SUCCESS; |
|
538 |
|
539 } while (rc == MOSQ_ERR_SUCCESS && connected); |
|
540 |
|
541 if (debug) |
|
542 fprintf(stdout, (char *)"Out of loop\n"); |
|
543 |
|
544 mosquitto_loop_stop(mosq, false); |
|
545 mosquitto_destroy(mosq); |
|
546 mosquitto_lib_cleanup(); |
|
547 |
|
548 #ifdef HAVE_WIRINGPI_H |
|
549 stopLCD(); |
|
550 #endif |
|
551 |
|
552 return rc; |
|
553 } |
|
554 |
|