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