thermometers/main.c

changeset 13
7f8432413321
parent 12
102c44bb8c9d
child 14
21901dc95501
equal deleted inserted replaced
12:102c44bb8c9d 13:7f8432413321
34 static int mid_sent = 0; 34 static int mid_sent = 0;
35 static int last_mid = -1; 35 static int last_mid = -1;
36 static int last_mid_sent = -1; 36 static int last_mid_sent = -1;
37 static bool connected = true; 37 static bool connected = true;
38 static bool disconnect_sent = false; 38 static bool disconnect_sent = false;
39 static bool connect_lost = false;
39 static bool shutdown = false; 40 static bool shutdown = false;
40 static pid_t pgrp, mypid; 41 static pid_t pgrp, mypid;
41 42
42 extern bool debug; 43 extern bool debug;
43 extern sys_config Config; 44 extern sys_config Config;
74 75
75 76
76 77
77 void my_connect_callback(struct mosquitto *mosq, void *obj, int result) 78 void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
78 { 79 {
80 if (connect_lost) {
81 connect_lost = false;
82 syslog(LOG_NOTICE, "Reconnect: %s", mosquitto_connack_string(result));
83 }
84
79 if (!result) { 85 if (!result) {
80 status = STATUS_CONNACK_RECVD; 86 status = STATUS_CONNACK_RECVD;
81 } else { 87 } else {
82 syslog(LOG_NOTICE, "my_connect_callback: %s\n", mosquitto_connack_string(result)); 88 syslog(LOG_NOTICE, "my_connect_callback: %s\n", mosquitto_connack_string(result));
83 } 89 }
92 connected = false; 98 connected = false;
93 } else { 99 } else {
94 /* 100 /*
95 * The remove server was brought down. We must keep running 101 * The remove server was brought down. We must keep running
96 */ 102 */
97 syslog(LOG_NOTICE, "Received DISCONNECT from %s but we want to run", Config.mosq_host); 103 syslog(LOG_NOTICE, "Received DISCONNECT from %s, connection lost", Config.mosq_host);
98 /* 104 connect_lost = true;
99 * We need a temp state
100 */
101 } 105 }
102 } 106 }
103 107
104 108
105 109
339 device = xstrcpy((char *)"/sys/bus/w1/devices/"); 343 device = xstrcpy((char *)"/sys/bus/w1/devices/");
340 device = xstrcat(device, tmp1->master); 344 device = xstrcat(device, tmp1->master);
341 device = xstrcat(device, (char *)"/"); 345 device = xstrcat(device, (char *)"/");
342 device = xstrcat(device, tmp1->name); 346 device = xstrcat(device, tmp1->name);
343 device = xstrcat(device, (char *)"/w1_slave"); 347 device = xstrcat(device, (char *)"/w1_slave");
344 alias = xstrcpy((char *)"sensor/temperature/"); 348 alias = xstrcpy((char *)"/raw/");
345 alias = xstrcat(alias, hostname); 349 alias = xstrcat(alias, hostname);
346 alias = xstrcat(alias, (char *)"/"); 350 alias = xstrcat(alias, (char *)"/thermometers/w1/");
347 alias = xstrcat(alias, tmp1->master); 351 alias = xstrcat(alias, tmp1->master);
348 alias = xstrcat(alias, (char *)"/"); 352 alias = xstrcat(alias, (char *)"/");
349 alias = xstrcat(alias, tmp1->alias); 353 alias = xstrcat(alias, tmp1->name);
354 alias = xstrcat(alias, (char *)"/temperature");
350 355
351 /* 356 /*
352 * Read sensor data 357 * Read sensor data
353 */ 358 */
354 if ((fp = fopen(device, "r"))) { 359 if ((fp = fopen(device, "r"))) {
381 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) { 386 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation)))) {
382 /* 387 /*
383 * Temperature is changed and valid, update and publish this. 388 * Temperature is changed and valid, update and publish this.
384 */ 389 */
385 sprintf(buf, "%.1f", temp / 1000.0); 390 sprintf(buf, "%.1f", temp / 1000.0);
386 if ((rc = mosquitto_publish(mosq, &mid_sent, alias, strlen(buf), buf, qos, 0))) { 391 if ((rc = mosquitto_publish(mosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
387 if (rc == MOSQ_ERR_NO_CONN) 392 if (rc == MOSQ_ERR_NO_CONN)
388 mosquitto_reconnect(mosq); 393 mosquitto_reconnect(mosq);
389 else 394 else
390 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc); 395 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
391 } 396 }

mercurial