thermometers/thermometers.c

Tue, 29 Jul 2014 20:42:02 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 29 Jul 2014 20:42:02 +0200
changeset 144
3446371e0bdb
parent 51
a03b6dac5398
child 145
1396350141cf
permissions
-rw-r--r--

Removed code dependencies to mosquitto and owfs

1
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * Copyright (C) 2014
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * Michiel Broek <mbroek at mbse dot eu>
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * This file is part of the mbsePi-apps
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This is free software; you can redistribute it and/or modify it
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * Free Software Foundation; either version 2, or (at your option) any
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * later version.
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 *
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * mbsePi-apps is distributed in the hope that it will be useful, but
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * General Public License for more details.
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 *
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 * along with EC-65K; see the file COPYING. If not, write to the Free
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *****************************************************************************/
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
27 static pid_t pgrp, mypid;
6
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
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
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
38
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
39
1
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40 void help(void)
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 {
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
42 fprintf(stdout, "mbsePi-apps thermometers v%s starting\n\n", VERSION);
1
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 fprintf(stdout, "Usage: thermomeneters [-d] [-h]\n");
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
44 fprintf(stdout, " -d --debug Debug and run in foreground\n");
1
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 fprintf(stdout, " -h --help Display this help\n");
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 }
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 void die(int onsig)
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 {
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 }
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61
144
3446371e0bdb Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
62 my_shutdown = TRUE;
1
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 }
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 int main(int argc, char *argv[])
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78 {
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
79 int rc, c, i;
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 while (1) {
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 int option_index = 0;
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 static struct option long_options[] = {
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 {"debug", 0, 0, 'c'},
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89 {"help", 0, 0, 'h'},
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 {0, 0, 0, 0}
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 };
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 c = getopt_long(argc, argv, "dh", long_options, &option_index);
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 if (c == -1)
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 break;
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99 break;
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
100 case 'h': help();
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
101 return 1;
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 }
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 }
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
107 if (debug)
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
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");
e584bc0177df Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
112 syslog(LOG_NOTICE, "halted");
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
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 /*
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118 * but that's live. This daemon should only be stopped by SIGTERM.
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 * Don't catch SIGCHLD.
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 */
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 for (i = 0; i < NSIG; i++) {
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 signal(i, (void (*))die);
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 }
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
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
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
142
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
143 if (debug) {
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
144 /*
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
145 * For debugging run in foreground.
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
146 */
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
147 rc = server();
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
148 } else {
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
149 /*
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
150 * Server initialization is complete. Now we can fork the
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
151 * daemon and return to the user. We need to do a setpgrp
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
152 * so that the daemon will no longer be assosiated with the
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
153 * users control terminal. This is done before the fork, so
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
154 * that the child will not be a process group leader. Otherwise,
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
155 * if the child were to open a terminal, it would become
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
156 * associated with that terminal as its control terminal.
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
157 */
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
158 if ((pgrp = setpgid(0, 0)) == -1) {
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
159 syslog(LOG_NOTICE, "setpgpid failed");
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
160 }
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
161
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
162 frk = fork();
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
163 switch (frk) {
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
164 case -1:
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
165 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
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
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
170 exit(1);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
171 case 0: /*
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
172 * Run the daemon
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
173 */
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
174 fclose(stdin);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
175 if (open("/dev/null", O_RDONLY) != 0) {
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
176 syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed");
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
177 _exit(2);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
178 }
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
179 fclose(stdout);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
180 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
181 syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed");
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
182 _exit(2);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
183 }
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
184 fclose(stderr);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
185 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
186 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed");
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
187 _exit(2);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
188 }
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
189 mypid = getpid();
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
190 rc = server();
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
191 break;
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
192 /* Not reached */
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
193 default:
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
194 /*
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
195 * Here we detach this process and let the child
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
196 * run the deamon process.
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
197 */
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
198 syslog(LOG_NOTICE, "Starting daemon with pid %d", frk);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
199 exit(0);
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
200 }
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
201 }
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
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 {
e584bc0177df Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
211 char hostname[256], buf[1024];
144
3446371e0bdb Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
212 int temp, rc = 0, deviation;
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
213 #ifdef HAVE_WIRINGPI_H
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
10
5600a1789644 Loop runs
Michiel Broek <mbroek@mbse.eu>
parents: 8
diff changeset
216 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
217 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
218 FILE *fp;
8
e584bc0177df Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
219
6
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
220 /*
8
e584bc0177df Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
221 * Build MQTT id
e584bc0177df Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
222 */
6
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
223 hostname[0] = '\0';
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
224 gethostname(hostname, 256);
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
225 hostname[255] = '\0';
8
e584bc0177df Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
226
6
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
227 /*
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
228 * Initialise is complete, report our presence state
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
229 */
19
eaa03048c269 Turned LCD backlight off for now
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
230 #ifdef HAVE_WIRINGPI_H
45
053c4657105f Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents: 40
diff changeset
231 // setBacklight(0);
19
eaa03048c269 Turned LCD backlight off for now
Michiel Broek <mbroek@mbse.eu>
parents: 18
diff changeset
232 #endif
6
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
233
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
234 if (debug)
144
3446371e0bdb Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
235 fprintf(stdout, (char *)"Enter loop\n");
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
236
6
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
237 do {
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
238 #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
239 lcdupdate = FALSE;
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
240 #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
241
8
e584bc0177df Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
242 /*
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
243 * 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
244 */
10
5600a1789644 Loop runs
Michiel Broek <mbroek@mbse.eu>
parents: 8
diff changeset
245 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
5600a1789644 Loop runs
Michiel Broek <mbroek@mbse.eu>
parents: 8
diff changeset
246 old1 = tmp1->next;
11
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
247
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
248 /*
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
249 * 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
250 */
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
251 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
252 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
253 device = xstrcat(device, (char *)"/");
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
254 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
255 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
256 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
257 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
258 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
259 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
260 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
261 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
262 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
263
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
264 /*
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
265 * Read sensor data
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
266 */
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
267 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
268 /*
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
269 * The output looks like:
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
270 * 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
271 * 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
272 */
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
273 fgets(line, 50, fp);
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
274 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
275 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
276 /*
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
277 * CRC is Ok, continue
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 fgets(line, 50, fp);
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
280 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
281 strtok(line, (char *)"=");
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
282 p = strtok(NULL, (char *)"=");
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
283 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
284 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
285 /*
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
286 * 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
287 * 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
288 * 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
289 * 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
290 * 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
291 * 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
292 */
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
293 deviation = 20000;
11
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
294 if ((tmp1->lastval == 0) ||
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
295 (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
296 /*
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
297 * 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
298 */
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
299 sprintf(buf, "%.1f", temp / 1000.0);
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
300 } 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
301 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
302 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
303 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
304 }
11
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
305 }
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
306 tmp1->lastval = temp;
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
307 #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
308 lcdupdate = TRUE;
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
309 #endif
11
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
310 }
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
311 } else {
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
312 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
313 }
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
314 fclose(fp);
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
315 tmp1->present = 1;
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
316 } else {
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
317 tmp1->present = 0;
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
318 if (debug)
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
319 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
320 }
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
321
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
322 free(device);
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
323 device = NULL;
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
324 free(alias);
f78f313b1d34 Working reading thermometers and sending data to a MQTT server.
Michiel Broek <mbroek@mbse.eu>
parents: 10
diff changeset
325 alias = NULL;
10
5600a1789644 Loop runs
Michiel Broek <mbroek@mbse.eu>
parents: 8
diff changeset
326 }
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
327
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
328 #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
329 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
330 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
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 }
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
340 #endif
8
e584bc0177df Server communication seems ok, can handle server shutdowns too.
Michiel Broek <mbroek@mbse.eu>
parents: 7
diff changeset
341
40
dafbbd5e9922 Prepared unix socket communication
Michiel Broek <mbroek@mbse.eu>
parents: 28
diff changeset
342 if (my_shutdown) {
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
343 #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
344 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
345 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
346 lcdPuts(lcdHandle, "Shuting down ...");
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
347 #endif
6
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
348 }
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
349
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
350 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
351
144
3446371e0bdb Removed code dependencies to mosquitto and owfs
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
352 } while (! my_shutdown);
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
353
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
354 if (debug)
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
355 fprintf(stdout, (char *)"Out of loop\n");
6
9db76e20e21e Can connect and disconnect
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
356
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
357 #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
358 stopLCD();
18
3f4823083b9d wirinPi code is now conditional
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
359 #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
360
12
102c44bb8c9d Deamon code added
Michiel Broek <mbroek@mbse.eu>
parents: 11
diff changeset
361 return rc;
1
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
362 }
91065eba8e1b Added two sources
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
363

mercurial