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