|
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 "thermferm.h" |
|
24 |
|
25 |
|
26 extern int debug; |
|
27 extern sys_config Config; |
|
28 extern int my_shutdown; |
|
29 |
|
30 extern const char UNITMODE[5][8]; |
|
31 |
|
32 |
|
33 |
|
34 int read_w1_28(char *address, int *val) |
|
35 { |
|
36 char *device, line[60], *p = NULL; |
|
37 FILE *fp; |
|
38 int rc = 0; |
|
39 |
|
40 device = xstrcpy((char *)"/sys/bus/w1/devices/"); |
|
41 device = xstrcat(device, address); |
|
42 device = xstrcat(device, (char *)"/w1_slave"); |
|
43 |
|
44 /* |
|
45 * Read sensor data |
|
46 */ |
|
47 if ((fp = fopen(device, "r"))) { |
|
48 /* |
|
49 * The output looks like: |
|
50 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES |
|
51 * 72 01 4b 46 7f ff 0e 10 57 t=23125 |
|
52 */ |
|
53 fgets(line, 50, fp); |
|
54 line[strlen(line)-1] = '\0'; |
|
55 if ((line[36] == 'Y') && (line[37] == 'E')) { |
|
56 /* |
|
57 * CRC is Ok, continue |
|
58 */ |
|
59 fgets(line, 50, fp); |
|
60 line[strlen(line)-1] = '\0'; |
|
61 strtok(line, (char *)"="); |
|
62 p = strtok(NULL, (char *)"="); |
|
63 rc = sscanf(p, "%d", val); |
|
64 if (rc != 1) { |
|
65 syslog(LOG_NOTICE, "sensor %s data parse error", address); |
|
66 rc = 3; |
|
67 } |
|
68 } else { |
|
69 syslog(LOG_NOTICE, "sensor %s CRC error", address); |
|
70 rc = 1; |
|
71 } |
|
72 fclose(fp); |
|
73 } else { |
|
74 syslog(LOG_NOTICE, "sensor %s is missing", address); |
|
75 rc = 2; |
|
76 } |
|
77 |
|
78 free(device); |
|
79 device = NULL; |
|
80 |
|
81 return rc; |
|
82 } |
|
83 |
|
84 |
|
85 |
|
86 #ifdef HAVE_WIRINGPI_H |
|
87 PI_THREAD (my_units_loop) |
|
88 #else |
|
89 void *my_units_loop(void *threadid) |
|
90 #endif |
|
91 { |
|
92 units_list *unit; |
|
93 int temp, deviation; |
|
94 |
|
95 /* |
|
96 * Initialize units for processing |
|
97 */ |
|
98 for (unit = Config.units; unit; unit = unit->next) { |
|
99 /* |
|
100 * Safety, turn everything off |
|
101 */ |
|
102 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; |
|
103 if (unit->profile && (int)unit->prof_started && (unit->mode == UNITMODE_PROFILE)) { |
|
104 syslog(LOG_NOTICE, "Starting unit %s profile %s", unit->name, unit->profile); |
|
105 } else if (unit->mode == UNITMODE_BEER) { |
|
106 syslog(LOG_NOTICE, "Starting unit %s beer cooler at %.1f degrees", unit->name, unit->beer_set); |
|
107 } else if (unit->mode == UNITMODE_FRIDGE) { |
|
108 syslog(LOG_NOTICE, "Starting unit %s as refridgerator at %.1f degrees", unit->name, unit->fridge_set); |
|
109 } else if (unit->mode == UNITMODE_NONE) { |
|
110 syslog(LOG_NOTICE, "Starting unit %s in inactive state", unit->name); |
|
111 } else { |
|
112 syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); |
|
113 } |
|
114 } |
|
115 |
|
116 syslog(LOG_NOTICE, "Thread my_units_loop started"); |
|
117 if (debug) |
|
118 fprintf(stdout, "Thread my_units_loop started\n"); |
|
119 |
|
120 /* |
|
121 * Loop forever until the external shutdown variable is set. |
|
122 */ |
|
123 for (;;) { |
|
124 |
|
125 if (my_shutdown) |
|
126 break; |
|
127 |
|
128 /* |
|
129 * Update the state of all fermenter units |
|
130 */ |
|
131 for (unit = Config.units; unit; unit = unit->next) { |
|
132 |
|
133 if (my_shutdown) |
|
134 break; |
|
135 |
|
136 if (unit->air_address) { |
|
137 if (read_w1_28(unit->air_address, &temp) == 0) { |
|
138 /* |
|
139 * It is possible to have read errors or extreme values. |
|
140 * This can happen with bad connections so we compare the |
|
141 * value with the previous one. If the difference is too |
|
142 * much, we don't send that value. That also means that if |
|
143 * the next value is ok again, it will be marked invalid too. |
|
144 * Maximum error is 20 degrees for now. |
|
145 */ |
|
146 deviation = 20000; |
|
147 if ((unit->air_temp == 0) || |
|
148 (unit->air_temp && (temp > (int)unit->air_temp - deviation) && (temp < ((int)unit->air_temp + deviation)))) { |
|
149 unit->air_temp = temp; |
|
150 } else { |
|
151 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temp, temp); |
|
152 if (debug) { |
|
153 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temp, temp); |
|
154 } |
|
155 } |
|
156 } |
|
157 } |
|
158 if (my_shutdown) |
|
159 break; |
|
160 |
|
161 if (unit->beer_address) { |
|
162 if (read_w1_28(unit->beer_address, &temp) == 0) { |
|
163 deviation = 20000; |
|
164 if ((unit->beer_temp == 0) || |
|
165 (unit->beer_temp && (temp > (int)unit->beer_temp - deviation) && (temp < ((int)unit->beer_temp + deviation)))) { |
|
166 unit->beer_temp = temp; |
|
167 } else { |
|
168 syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temp, temp); |
|
169 if (debug) { |
|
170 fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temp, temp); |
|
171 } |
|
172 } |
|
173 } |
|
174 } |
|
175 if (my_shutdown) |
|
176 break; |
|
177 |
|
178 |
|
179 } |
|
180 usleep(10000); |
|
181 } |
|
182 |
|
183 /* |
|
184 * Stop units processing in a neat way |
|
185 */ |
|
186 for (unit = Config.units; unit; unit = unit->next) { |
|
187 /* |
|
188 * Turn everything off |
|
189 */ |
|
190 unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; |
|
191 syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); |
|
192 } |
|
193 |
|
194 syslog(LOG_NOTICE, "Thread my_units_loop stopped"); |
|
195 if (debug) |
|
196 fprintf(stdout, "Thread my_units_loop stopped\n"); |
|
197 return 0; |
|
198 } |
|
199 |
|
200 |