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