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