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