thermferm/sensors.c

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

mercurial