thermferm/sensors.c

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

mercurial