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