mash/sensors.c

changeset 103
99c47a8a61cb
child 147
0e7c51a66b5e
equal deleted inserted replaced
102:e37b9c571a56 103:99c47a8a61cb
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
23 #include "mash.h"
24
25 #ifdef HAVE_WIRINGPI_H
26
27
28 extern bool debug;
29 extern sys_config Config;
30 extern int my_shutdown;
31
32
33
34 PI_THREAD (my_sensors_loop)
35 {
36 w1_therm *tmp1, *old1;
37 char *device, line[60], *p = NULL;
38 FILE *fp;
39 int temp, rc, deviation;
40
41 syslog(LOG_NOTICE, "Thread my_sensors_loop started");
42 if (debug)
43 fprintf(stdout, "Thread my_sensors_loop started\n");
44
45 /*
46 * Loop forever until the external shutdown variable is set.
47 */
48 for (;;) {
49 /*
50 * Here send our 1-wire sensors values
51 */
52 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
53 old1 = tmp1->next;
54
55 if (my_shutdown) {
56 syslog(LOG_NOTICE, "Thread my_sensors_loop stopped");
57 if (debug)
58 fprintf(stdout, "Thread my_sensors_loop stopped\n");
59 return 0;
60 }
61
62 /*
63 * Build path to the on-wire sensor
64 */
65 device = xstrcpy((char *)"/sys/bus/w1/devices/");
66 device = xstrcat(device, tmp1->master);
67 device = xstrcat(device, (char *)"/");
68 device = xstrcat(device, tmp1->name);
69 device = xstrcat(device, (char *)"/w1_slave");
70
71 /*
72 * Read sensor data
73 */
74 if ((fp = fopen(device, "r"))) {
75 /*
76 * The output looks like:
77 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
78 * 72 01 4b 46 7f ff 0e 10 57 t=23125
79 */
80 fgets(line, 50, fp);
81 line[strlen(line)-1] = '\0';
82 if ((line[36] == 'Y') && (line[37] == 'E')) {
83 /*
84 * CRC is Ok, continue
85 */
86 fgets(line, 50, fp);
87 line[strlen(line)-1] = '\0';
88 strtok(line, (char *)"=");
89 p = strtok(NULL, (char *)"=");
90 rc = sscanf(p, "%d", &temp);
91 if ((rc == 1) && (tmp1->lastval != temp)) {
92 /*
93 * It is possible to have read errors or extreme values.
94 * This can happen with bad connections so we compare the
95 * value with the previous one. If the difference is too
96 * much, we don't send that value. That also means that if
97 * the next value is ok again, it will be marked invalid too.
98 * Maximum error is 20 degrees for now.
99 */
100 deviation = 20000;
101 if ( (tmp1->lastval == 0) ||
102 (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation))) ) {
103 /*
104 * Temperature is changed and valid, set flag.
105 */
106 tmp1->update = TRUE;
107 } else {
108 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp);
109 if (debug) {
110 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp);
111 }
112 }
113 tmp1->lastval = temp;
114 }
115 } else {
116 syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name);
117 }
118 fclose(fp);
119 tmp1->present = 1;
120 } else {
121 tmp1->present = 0;
122 if (debug)
123 printf("sensor %s is missing\n", tmp1->name);
124 }
125
126 free(device);
127 device = NULL;
128 }
129 }
130 }
131
132
133
134 #endif

mercurial