Mon, 18 May 2015 21:19:06 +0200
Version 0.3.3, still not for production. Fixed warnings when the simulator code is compiled. Slowed the simulator air temperature change 60 times. More realistic temperature changes for the heater and cooler elements. Improved logic in the simulator.
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 | ||
144
3446371e0bdb
Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
26 | static int my_shutdown = FALSE; |
12 | 27 | static pid_t pgrp, mypid; |
6 | 28 | |
144
3446371e0bdb
Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
29 | extern int debug; |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
30 | extern sys_config Config; |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
31 | #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
|
32 | extern int lcdHandle; |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
33 | #endif |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
34 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
35 | int server(void); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
36 | void help(void); |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
37 | void die(int); |
6 | 38 | |
39 | ||
1 | 40 | void help(void) |
41 | { | |
12 | 42 | fprintf(stdout, "mbsePi-apps thermometers v%s starting\n\n", VERSION); |
1 | 43 | fprintf(stdout, "Usage: thermomeneters [-d] [-h]\n"); |
12 | 44 | fprintf(stdout, " -d --debug Debug and run in foreground\n"); |
1 | 45 | fprintf(stdout, " -h --help Display this help\n"); |
46 | } | |
47 | ||
48 | ||
49 | ||
50 | void die(int onsig) | |
51 | { | |
52 | switch (onsig) { | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
53 | case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); |
1 | 54 | break; |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
55 | case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); |
1 | 56 | break; |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
57 | case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); |
1 | 58 | break; |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
59 | default: syslog(LOG_NOTICE, "die() on signal %d", onsig); |
1 | 60 | } |
61 | ||
144
3446371e0bdb
Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
62 | my_shutdown = TRUE; |
1 | 63 | } |
64 | ||
65 | ||
66 | ||
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
67 | #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
|
68 | 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
|
69 | { |
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
|
70 | 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
|
71 | 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
|
72 | } |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
73 | #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
|
74 | |
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
|
75 | |
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
|
76 | |
1 | 77 | int main(int argc, char *argv[]) |
78 | { | |
12 | 79 | int rc, c, i; |
80 | pid_t frk; | |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
81 | #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
|
82 | char buf[80]; |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
83 | #endif |
1 | 84 | |
85 | while (1) { | |
86 | int option_index = 0; | |
87 | static struct option long_options[] = { | |
88 | {"debug", 0, 0, 'c'}, | |
89 | {"help", 0, 0, 'h'}, | |
90 | {0, 0, 0, 0} | |
91 | }; | |
92 | ||
93 | c = getopt_long(argc, argv, "dh", long_options, &option_index); | |
94 | if (c == -1) | |
95 | break; | |
96 | ||
97 | switch (c) { | |
144
3446371e0bdb
Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
98 | case 'd': debug = TRUE; |
1 | 99 | break; |
100 | case 'h': help(); | |
101 | return 1; | |
102 | } | |
103 | } | |
104 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
105 | 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
|
106 | syslog(LOG_NOTICE, "mbsePi-apps thermometers v%s starting", VERSION); |
12 | 107 | if (debug) |
108 | 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
|
109 | |
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
|
110 | 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
|
111 | fprintf(stderr, "Error reading configuration\n"); |
145 | 112 | syslog(LOG_NOTICE, "Error reading configuration, halted"); |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
113 | return 1; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
114 | } |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
115 | |
1 | 116 | /* |
117 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored | |
118 | * but that's live. This daemon should only be stopped by SIGTERM. | |
119 | * Don't catch SIGCHLD. | |
120 | */ | |
121 | for (i = 0; i < NSIG; i++) { | |
122 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) | |
123 | signal(i, (void (*))die); | |
124 | } | |
125 | ||
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
126 | #ifdef HAVE_WIRINGPI_H |
28
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
127 | |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
128 | if (wiringPiSetup () ) |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
129 | return 1; |
32ed1ea4d0b6
Coolers working with a hardcode reference
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
130 | |
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
|
131 | 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
|
132 | 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
|
133 | 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
|
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 | 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
|
137 | 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
|
138 | 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
|
139 | 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
|
140 | lcdPuts(lcdHandle, buf); |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
141 | #endif |
12 | 142 | |
143 | if (debug) { | |
144 | /* | |
145 | * For debugging run in foreground. | |
146 | */ | |
147 | rc = server(); | |
148 | } else { | |
149 | /* | |
150 | * Server initialization is complete. Now we can fork the | |
151 | * daemon and return to the user. We need to do a setpgrp | |
152 | * so that the daemon will no longer be assosiated with the | |
153 | * users control terminal. This is done before the fork, so | |
154 | * that the child will not be a process group leader. Otherwise, | |
155 | * if the child were to open a terminal, it would become | |
156 | * associated with that terminal as its control terminal. | |
157 | */ | |
158 | if ((pgrp = setpgid(0, 0)) == -1) { | |
159 | syslog(LOG_NOTICE, "setpgpid failed"); | |
160 | } | |
161 | ||
162 | frk = fork(); | |
163 | switch (frk) { | |
164 | case -1: | |
165 | syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); | |
166 | syslog(LOG_NOTICE, "Finished, rc=1"); | |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
167 | #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
|
168 | stopLCD(); |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
169 | #endif |
12 | 170 | exit(1); |
171 | case 0: /* | |
172 | * Run the daemon | |
173 | */ | |
174 | fclose(stdin); | |
175 | if (open("/dev/null", O_RDONLY) != 0) { | |
176 | syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); | |
177 | _exit(2); | |
178 | } | |
179 | fclose(stdout); | |
180 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { | |
181 | syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); | |
182 | _exit(2); | |
183 | } | |
184 | fclose(stderr); | |
185 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { | |
186 | syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); | |
187 | _exit(2); | |
188 | } | |
189 | mypid = getpid(); | |
190 | rc = server(); | |
191 | break; | |
192 | /* Not reached */ | |
193 | default: | |
194 | /* | |
195 | * Here we detach this process and let the child | |
196 | * run the deamon process. | |
197 | */ | |
198 | syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); | |
199 | exit(0); | |
200 | } | |
201 | } | |
202 | ||
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
203 | 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
|
204 | return rc; |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
205 | } |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
206 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
207 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
208 | |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
209 | int server(void) |
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
210 | { |
145 | 211 | int temp, rc = 0; |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
212 | #ifdef HAVE_WIRINGPI_H |
149
f019ea6a9d31
More small fixes, new dependencies generated
Michiel Broek <mbroek@mbse.eu>
parents:
145
diff
changeset
|
213 | char buf[1024]; |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
214 | int lcdupdate; |
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
215 | #endif |
145 | 216 | w1_therm *tmp1; |
217 | char *device, line[60], *p = NULL; | |
11
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
218 | FILE *fp; |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
219 | |
12 | 220 | if (debug) |
144
3446371e0bdb
Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
221 | fprintf(stdout, (char *)"Enter loop\n"); |
12 | 222 | |
6 | 223 | do { |
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 | lcdupdate = FALSE; |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
226 | #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
|
227 | |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
228 | /* |
12 | 229 | * 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
|
230 | */ |
145 | 231 | for (tmp1 = Config.w1therms; tmp1; tmp1 = tmp1->next) { |
11
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
232 | |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
233 | /* |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
234 | * 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
|
235 | */ |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
236 | 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
|
237 | 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
|
238 | device = xstrcat(device, (char *)"/"); |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
239 | 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
|
240 | device = xstrcat(device, (char *)"/w1_slave"); |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
241 | |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
242 | /* |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
243 | * Read sensor data |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
244 | */ |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
245 | 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
|
246 | /* |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
247 | * The output looks like: |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
248 | * 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
|
249 | * 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
|
250 | */ |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
251 | fgets(line, 50, fp); |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
252 | 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
|
253 | 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
|
254 | /* |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
255 | * CRC is Ok, continue |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
256 | */ |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
257 | fgets(line, 50, fp); |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
258 | 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
|
259 | strtok(line, (char *)"="); |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
260 | p = strtok(NULL, (char *)"="); |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
261 | 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
|
262 | 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
|
263 | tmp1->lastval = temp; |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
264 | #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
|
265 | lcdupdate = TRUE; |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
266 | #endif |
11
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
267 | } |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
268 | } else { |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
269 | 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
|
270 | } |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
271 | fclose(fp); |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
272 | tmp1->present = 1; |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
273 | } else { |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
274 | tmp1->present = 0; |
12 | 275 | if (debug) |
276 | 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
|
277 | } |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
278 | |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
279 | free(device); |
f78f313b1d34
Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents:
10
diff
changeset
|
280 | device = NULL; |
10 | 281 | } |
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
|
282 | |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
283 | #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
|
284 | 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
|
285 | 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
|
286 | 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
|
287 | 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
|
288 | lcdPuts(lcdHandle, buf); |
145 | 289 | tmp1 = tmp1->next; |
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
|
290 | 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
|
291 | 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
|
292 | 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
|
293 | } |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
294 | #endif |
8
e584bc0177df
Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents:
7
diff
changeset
|
295 | |
40
dafbbd5e9922
Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents:
28
diff
changeset
|
296 | if (my_shutdown) { |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
297 | #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
|
298 | 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
|
299 | 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
|
300 | lcdPuts(lcdHandle, "Shuting down ..."); |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
301 | #endif |
6 | 302 | } |
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
|
303 | |
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
|
304 | 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
|
305 | |
144
3446371e0bdb
Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents:
51
diff
changeset
|
306 | } while (! my_shutdown); |
12 | 307 | |
308 | if (debug) | |
309 | fprintf(stdout, (char *)"Out of loop\n"); | |
6 | 310 | |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
311 | #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
|
312 | stopLCD(); |
18
3f4823083b9d
wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents:
16
diff
changeset
|
313 | #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
|
314 | |
12 | 315 | return rc; |
1 | 316 | } |
317 |