thermometers/main.c

changeset 16
f4cbe008da72
parent 15
01fec4ddad17
child 18
3f4823083b9d
equal deleted inserted replaced
15:01fec4ddad17 16:f4cbe008da72
40 static bool shutdown = false; 40 static bool shutdown = false;
41 static pid_t pgrp, mypid; 41 static pid_t pgrp, mypid;
42 42
43 extern bool debug; 43 extern bool debug;
44 extern sys_config Config; 44 extern sys_config Config;
45 extern int lcdHandle;
45 46
46 int server(void); 47 int server(void);
47 void help(void); 48 void help(void);
48 void die(int); 49 void die(int);
49 50
120 printf("MQTT: %s\n", str); 121 printf("MQTT: %s\n", str);
121 } 122 }
122 123
123 124
124 125
126 void stopLCD(void)
127 {
128 lcdClear(lcdHandle);
129 setBacklight(0);
130 }
131
132
133
125 int main(int argc, char *argv[]) 134 int main(int argc, char *argv[])
126 { 135 {
127 int rc, c, i; 136 int rc, c, i;
128 pid_t frk; 137 pid_t frk;
138 char buf[80];
129 139
130 while (1) { 140 while (1) {
131 int option_index = 0; 141 int option_index = 0;
132 static struct option long_options[] = { 142 static struct option long_options[] = {
133 {"debug", 0, 0, 'c'}, 143 {"debug", 0, 0, 'c'},
166 for (i = 0; i < NSIG; i++) { 176 for (i = 0; i < NSIG; i++) {
167 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) 177 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
168 signal(i, (void (*))die); 178 signal(i, (void (*))die);
169 } 179 }
170 180
181 if ((rc = initLCD (16, 2))) {
182 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
183 return 1;
184 }
185
186 lcdPosition(lcdHandle, 0, 0);
187 lcdPuts(lcdHandle, "Thermometers");
188 lcdPosition(lcdHandle, 0, 1);
189 sprintf(buf, "Version %s", VERSION);
190 lcdPuts(lcdHandle, buf);
171 191
172 if (debug) { 192 if (debug) {
173 /* 193 /*
174 * For debugging run in foreground. 194 * For debugging run in foreground.
175 */ 195 */
191 frk = fork(); 211 frk = fork();
192 switch (frk) { 212 switch (frk) {
193 case -1: 213 case -1:
194 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); 214 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
195 syslog(LOG_NOTICE, "Finished, rc=1"); 215 syslog(LOG_NOTICE, "Finished, rc=1");
216 stopLCD();
196 exit(1); 217 exit(1);
197 case 0: /* 218 case 0: /*
198 * Run the daemon 219 * Run the daemon
199 */ 220 */
200 fclose(stdin); 221 fclose(stdin);
235 int server(void) 256 int server(void)
236 { 257 {
237 char *id = NULL, *state = NULL; 258 char *id = NULL, *state = NULL;
238 struct mosquitto *mosq = NULL; 259 struct mosquitto *mosq = NULL;
239 char hostname[256], buf[1024]; 260 char hostname[256], buf[1024];
240 int temp, rc, deviation, keepalive = 60; 261 int temp, rc, deviation, keepalive = 60, lcdupdate;
241 unsigned int max_inflight = 20; 262 unsigned int max_inflight = 20;
242 char err[1024]; 263 char err[1024];
243 w1_therm *tmp1, *old1; 264 w1_therm *tmp1, *old1;
244 char *device, *alias, line[60], *p = NULL; 265 char *device, *alias, line[60], *p = NULL;
245 FILE *fp; 266 FILE *fp;
355 if (debug) 376 if (debug)
356 fprintf(stdout, (char *)"Enter loop, connected %d\n", connected); 377 fprintf(stdout, (char *)"Enter loop, connected %d\n", connected);
357 378
358 do { 379 do {
359 if (status == STATUS_CONNACK_RECVD) { 380 if (status == STATUS_CONNACK_RECVD) {
381 lcdupdate = FALSE;
382
360 /* 383 /*
361 * Here send our 1-wire sensors values 384 * Here send our 1-wire sensors values
362 */ 385 */
363 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { 386 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
364 old1 = tmp1->next; 387 old1 = tmp1->next;
404 * It is possible to have read errors or extreme values. 427 * It is possible to have read errors or extreme values.
405 * This can happen with bad connections so we compare the 428 * This can happen with bad connections so we compare the
406 * value with the previous one. If the difference is too 429 * value with the previous one. If the difference is too
407 * much, we don't send that value. That also means that if 430 * much, we don't send that value. That also means that if
408 * the next value is ok again, it will be marked invalid too. 431 * the next value is ok again, it will be marked invalid too.
432 * Maximum error is 20 degrees for now.
409 */ 433 */
410 deviation = (temp + tmp1->lastval) / 10; 434 deviation = 20000;
411 if ((tmp1->lastval == 0) || 435 if ((tmp1->lastval == 0) ||
412 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) { 436 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) {
413 /* 437 /*
414 * Temperature is changed and valid, update and publish this. 438 * Temperature is changed and valid, update and publish this.
415 */ 439 */
419 mosquitto_reconnect(mosq); 443 mosquitto_reconnect(mosq);
420 else 444 else
421 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc); 445 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
422 } 446 }
423 } else { 447 } else {
424 if (debug) 448 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp);
425 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp); 449 if (debug) {
450 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp);
451 }
426 } 452 }
427 tmp1->lastval = temp; 453 tmp1->lastval = temp;
454 lcdupdate = TRUE;
428 } 455 }
429 } else { 456 } else {
430 syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name); 457 syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name);
431 } 458 }
432 fclose(fp); 459 fclose(fp);
440 free(device); 467 free(device);
441 device = NULL; 468 device = NULL;
442 free(alias); 469 free(alias);
443 alias = NULL; 470 alias = NULL;
444 } 471 }
445 usleep(100000); 472
473 if (lcdupdate) {
474 lcdPosition(lcdHandle, 0, 0);
475 tmp1 = Config.w1therms;
476 snprintf(buf, 16, "%5.1f %cC %s ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias);
477 lcdPuts(lcdHandle, buf);
478 old1 = tmp1->next;
479 tmp1 = old1;
480 lcdPosition(lcdHandle, 0, 1);
481 snprintf(buf, 16, "%5.1f %cC %s ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias);
482 lcdPuts(lcdHandle, buf);
483 }
446 484
447 if (shutdown) { 485 if (shutdown) {
448 /* 486 /*
449 * Final publish 0 to clients/<hostname>/thermometers/state 487 * Final publish 0 to clients/<hostname>/thermometers/state
450 */ 488 */
451 sprintf(buf, "0"); 489 sprintf(buf, "0");
452 mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, true); 490 mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, true);
453 last_mid = mid_sent; 491 last_mid = mid_sent;
454 status = STATUS_WAITING; 492 status = STATUS_WAITING;
493 lcdClear(lcdHandle);
494 lcdPosition(lcdHandle, 0, 0);
495 lcdPuts(lcdHandle, "Shuting down ...");
455 } 496 }
497
498 usleep(100000);
499
456 } else if (status == STATUS_WAITING) { 500 } else if (status == STATUS_WAITING) {
457 if (debug) 501 if (debug)
458 fprintf(stdout, (char *)"Waiting\n"); 502 fprintf(stdout, (char *)"Waiting\n");
459 if (last_mid_sent == last_mid && disconnect_sent == false) { 503 if (last_mid_sent == last_mid && disconnect_sent == false) {
460 mosquitto_disconnect(mosq); 504 mosquitto_disconnect(mosq);
471 515
472 mosquitto_loop_stop(mosq, false); 516 mosquitto_loop_stop(mosq, false);
473 mosquitto_destroy(mosq); 517 mosquitto_destroy(mosq);
474 mosquitto_lib_cleanup(); 518 mosquitto_lib_cleanup();
475 519
520 stopLCD();
521
476 return rc; 522 return rc;
477 } 523 }
478 524

mercurial