Mon, 04 Aug 2014 23:34:22 +0200
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
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 | { | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
38 | devices_list *device; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
39 | int tmp; |
93 | 40 | |
164
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
41 | for (device = Config.devices; device; device = device->next) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
42 | if (strcmp(address, device->uuid) == 0) { |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
43 | tmp = device->value; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
44 | *val = tmp; |
f16def8472ba
Threads partly working via new devices interface
Michiel Broek <mbroek@mbse.eu>
parents:
106
diff
changeset
|
45 | return device->present; |
93 | 46 | } |
47 | } | |
48 | ||
170 | 49 | return DEVPRESENT_NO; |
93 | 50 | } |
51 | ||
52 | ||
53 | ||
54 | #ifdef HAVE_WIRINGPI_H | |
55 | PI_THREAD (my_units_loop) | |
56 | #else | |
57 | void *my_units_loop(void *threadid) | |
58 | #endif | |
59 | { | |
60 | units_list *unit; | |
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
61 | devices_list *device; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
62 | int rc, temp, deviation; |
93 | 63 | |
64 | /* | |
65 | * Initialize units for processing | |
66 | */ | |
67 | for (unit = Config.units; unit; unit = unit->next) { | |
68 | /* | |
69 | * Safety, turn everything off | |
70 | */ | |
71 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; | |
72 | if (unit->profile && (int)unit->prof_started && (unit->mode == UNITMODE_PROFILE)) { | |
73 | syslog(LOG_NOTICE, "Starting unit %s profile %s", unit->name, unit->profile); | |
74 | } else if (unit->mode == UNITMODE_BEER) { | |
75 | syslog(LOG_NOTICE, "Starting unit %s beer cooler at %.1f degrees", unit->name, unit->beer_set); | |
76 | } else if (unit->mode == UNITMODE_FRIDGE) { | |
77 | syslog(LOG_NOTICE, "Starting unit %s as refridgerator at %.1f degrees", unit->name, unit->fridge_set); | |
78 | } else if (unit->mode == UNITMODE_NONE) { | |
79 | syslog(LOG_NOTICE, "Starting unit %s in inactive state", unit->name); | |
80 | } else { | |
81 | syslog(LOG_NOTICE, "Starting unit %s in off state", unit->name); | |
82 | } | |
83 | } | |
84 | ||
85 | syslog(LOG_NOTICE, "Thread my_units_loop started"); | |
86 | if (debug) | |
87 | fprintf(stdout, "Thread my_units_loop started\n"); | |
88 | ||
89 | /* | |
90 | * Loop forever until the external shutdown variable is set. | |
91 | */ | |
92 | for (;;) { | |
93 | ||
94 | if (my_shutdown) | |
95 | break; | |
96 | ||
97 | /* | |
98 | * Update the state of all fermenter units | |
99 | */ | |
100 | for (unit = Config.units; unit; unit = unit->next) { | |
101 | ||
102 | if (my_shutdown) | |
103 | break; | |
104 | ||
105 | if (unit->air_address) { | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
106 | rc = read_w1_28(unit->air_address, &temp); |
170 | 107 | if (rc == DEVPRESENT_YES) { |
93 | 108 | /* |
109 | * It is possible to have read errors or extreme values. | |
110 | * This can happen with bad connections so we compare the | |
111 | * value with the previous one. If the difference is too | |
112 | * much, we don't send that value. That also means that if | |
113 | * the next value is ok again, it will be marked invalid too. | |
175
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
170
diff
changeset
|
114 | * Maximum error is 40 degrees for now. |
93 | 115 | */ |
175
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
170
diff
changeset
|
116 | deviation = 40000; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
117 | if ((unit->air_temperature == 0) || |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
118 | (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
|
119 | unit->air_temperature = temp; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
120 | unit->air_state = 0; |
93 | 121 | } else { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
122 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); |
93 | 123 | if (debug) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
124 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); |
93 | 125 | } |
126 | } | |
170 | 127 | } else if (rc == DEVPRESENT_ERROR) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
128 | unit->air_state = 1; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
129 | } else { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
130 | unit->air_state = 2; |
93 | 131 | } |
132 | } | |
133 | if (my_shutdown) | |
134 | break; | |
135 | ||
136 | if (unit->beer_address) { | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
137 | rc = read_w1_28(unit->beer_address, &temp); |
170 | 138 | if (rc == DEVPRESENT_YES) { |
175
b73490398368
Units editing is more or less complete. Changed structures for heating, cooling, fan and door. Updated the web interface to confuigure all these things.
Michiel Broek <mbroek@mbse.eu>
parents:
170
diff
changeset
|
139 | deviation = 40000; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
140 | if ((unit->beer_temperature == 0) || |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
141 | (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
|
142 | unit->beer_temperature = temp; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
143 | unit->beer_state = 0; |
93 | 144 | } else { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
145 | syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); |
93 | 146 | if (debug) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
147 | fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); |
93 | 148 | } |
149 | } | |
170 | 150 | } else if (rc == DEVPRESENT_ERROR) { |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
151 | unit->beer_state = 1; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
152 | } else { |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
93
diff
changeset
|
153 | unit->beer_state = 2; |
93 | 154 | } |
155 | } | |
156 | if (my_shutdown) | |
157 | break; | |
158 | ||
179
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
159 | device_out(unit->heater_address, unit->heater_state); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
160 | if (my_shutdown) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
161 | break; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
162 | device_out(unit->cooler_address, unit->cooler_state); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
163 | if (my_shutdown) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
164 | break; |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
165 | device_out(unit->fan_address, unit->fan_state); |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
166 | if (my_shutdown) |
417ee898fb02
Added PID implementation and 433 MHz radio control switches. Not reliable yet.
Michiel Broek <mbroek@mbse.eu>
parents:
175
diff
changeset
|
167 | break; |
93 | 168 | } |
169 | usleep(10000); | |
170 | } | |
171 | ||
172 | /* | |
173 | * Stop units processing in a neat way | |
174 | */ | |
175 | for (unit = Config.units; unit; unit = unit->next) { | |
176 | /* | |
177 | * Turn everything off | |
178 | */ | |
179 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = 0; | |
180 | syslog(LOG_NOTICE, "Stopped unit %s mode %s", unit->name, UNITMODE[unit->mode]); | |
181 | } | |
182 | ||
183 | syslog(LOG_NOTICE, "Thread my_units_loop stopped"); | |
184 | if (debug) | |
185 | fprintf(stdout, "Thread my_units_loop stopped\n"); | |
186 | return 0; | |
187 | } | |
188 | ||
189 |