Sat, 12 Jul 2014 22:32:29 +0200
Added units logging
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" | |
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; | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
67 | } else { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
68 | rc = 0; |
93 | 69 | } |
70 | } else { | |
71 | syslog(LOG_NOTICE, "sensor %s CRC error", address); | |
72 | rc = 1; | |
73 | } | |
74 | fclose(fp); | |
75 | } else { | |
76 | syslog(LOG_NOTICE, "sensor %s is missing", address); | |
77 | rc = 2; | |
78 | } | |
79 | ||
80 | free(device); | |
81 | device = NULL; | |
82 | ||
83 | return rc; | |
84 | } | |
85 | ||
86 | ||
87 | ||
88 | #ifdef HAVE_WIRINGPI_H | |
89 | PI_THREAD (my_units_loop) | |
90 | #else | |
91 | void *my_units_loop(void *threadid) | |
92 | #endif | |
93 | { | |
94 | units_list *unit; | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
95 | int rc, temp, deviation; |
93 | 96 | |
97 | /* | |
98 | * Initialize units for processing | |
99 | */ | |
100 | for (unit = Config.units; unit; unit = unit->next) { | |
101 | /* | |
102 | * Safety, turn everything off | |
103 | */ | |
104 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; | |
105 | if (unit->profile && (int)unit->prof_started && (unit->mode == UNITMODE_PROFILE)) { | |
106 | syslog(LOG_NOTICE, "Starting unit %s profile %s", unit->name, unit->profile); | |
107 | } else if (unit->mode == UNITMODE_BEER) { | |
108 | syslog(LOG_NOTICE, "Starting unit %s beer cooler at %.1f degrees", unit->name, unit->beer_set); | |
109 | } else if (unit->mode == UNITMODE_FRIDGE) { | |
110 | syslog(LOG_NOTICE, "Starting unit %s as refridgerator at %.1f degrees", unit->name, unit->fridge_set); | |
111 | } else if (unit->mode == UNITMODE_NONE) { | |
112 | syslog(LOG_NOTICE, "Starting unit %s in inactive state", unit->name); | |
113 | } else { | |
114 | syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); | |
115 | } | |
116 | } | |
117 | ||
118 | syslog(LOG_NOTICE, "Thread my_units_loop started"); | |
119 | if (debug) | |
120 | fprintf(stdout, "Thread my_units_loop started\n"); | |
121 | ||
122 | /* | |
123 | * Loop forever until the external shutdown variable is set. | |
124 | */ | |
125 | for (;;) { | |
126 | ||
127 | if (my_shutdown) | |
128 | break; | |
129 | ||
130 | /* | |
131 | * Update the state of all fermenter units | |
132 | */ | |
133 | for (unit = Config.units; unit; unit = unit->next) { | |
134 | ||
135 | if (my_shutdown) | |
136 | break; | |
137 | ||
138 | if (unit->air_address) { | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
139 | 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
|
140 | if (rc == 0) { |
93 | 141 | /* |
142 | * It is possible to have read errors or extreme values. | |
143 | * This can happen with bad connections so we compare the | |
144 | * value with the previous one. If the difference is too | |
145 | * much, we don't send that value. That also means that if | |
146 | * the next value is ok again, it will be marked invalid too. | |
147 | * Maximum error is 20 degrees for now. | |
148 | */ | |
149 | deviation = 20000; | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
150 | if ((unit->air_temperature == 0) || |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
151 | (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
|
152 | unit->air_temperature = temp; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
153 | unit->air_state = 0; |
93 | 154 | } else { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
155 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); |
93 | 156 | if (debug) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
157 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); |
93 | 158 | } |
159 | } | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
160 | } else if (rc == 2) { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
161 | unit->air_state = 1; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
162 | } else { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
163 | unit->air_state = 2; |
93 | 164 | } |
165 | } | |
166 | if (my_shutdown) | |
167 | break; | |
168 | ||
169 | if (unit->beer_address) { | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
170 | 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
|
171 | if (rc == 0) { |
93 | 172 | deviation = 20000; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
173 | if ((unit->beer_temperature == 0) || |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
174 | (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
|
175 | unit->beer_temperature = temp; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
176 | unit->beer_state = 0; |
93 | 177 | } else { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
178 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); |
93 | 179 | if (debug) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
180 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); |
93 | 181 | } |
182 | } | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
183 | } else if (rc == 2) { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
184 | unit->beer_state = 1; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
185 | } else { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
186 | unit->beer_state = 2; |
93 | 187 | } |
188 | } | |
189 | if (my_shutdown) | |
190 | break; | |
191 | ||
192 | ||
193 | } | |
194 | usleep(10000); | |
195 | } | |
196 | ||
197 | /* | |
198 | * Stop units processing in a neat way | |
199 | */ | |
200 | for (unit = Config.units; unit; unit = unit->next) { | |
201 | /* | |
202 | * Turn everything off | |
203 | */ | |
204 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; | |
205 | syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); | |
206 | } | |
207 | ||
208 | syslog(LOG_NOTICE, "Thread my_units_loop stopped"); | |
209 | if (debug) | |
210 | fprintf(stdout, "Thread my_units_loop stopped\n"); | |
211 | return 0; | |
212 | } | |
213 | ||
214 |