-
Notifications
You must be signed in to change notification settings - Fork 2
/
HeatingController.ino
409 lines (373 loc) · 12.8 KB
/
HeatingController.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*********
Andy Pugh 2020
Based on work by Rui Saontos: https://randomnerdtutorials.com
*********/
// Load Wi-Fi library
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include "ESP_Mail_Client.h"
#include "time.h"
#include "EEPROM.h"
// https://github.com/cybergibbons/DS2482_OneWire _NOT_ the standard library
// If using the DS2482. Otherwise use the standard OneWire lib
#include <OneWire.h>
#include <DallasTemperature.h>
#include "config.h"
#include "globals.h"
#define __P(f_, ...) snprintf (buffer, 200, (f_), ##__VA_ARGS__) ; content += buffer ;
//#define __P(f_, ...) snprintf (buffer, 200, (f_), ##__VA_ARGS__) ; Serial.println(buffer);
#ifdef USE_DS2482
OneWire oneWire(0); //I2C device address 0
#else
OneWire oneWire(23); //Bitbanged device, pin 23
#endif
DallasTemperature sensors(&oneWire);
// Set web server port number to 80
AsyncWebServer server(80);
int error_flag = 2; // 1 flash is just a heartbeat. use 2 to send reboot email
void setup() {
int connection_count;
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE);
pinMode(0, INPUT_PULLUP);
// Get setup from eeprom
read_EEPROM(EEPROM_BASE, true);
read_EEPROM(CREDENTIALS_BASE, false);
// Connect to Wi-Fi network with SSID and password
Serial.print(" Connecting to ");
Serial.println(ssid);
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(hostname);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA);
pinMode(BLINK_LED, OUTPUT);
pins[BLINK_LED] = 'x';
do {
if (! digitalRead(0)){ // boot button pressed
Serial.println("boot button");
AP_mode();
}
digitalWrite(BLINK_LED, HIGH);
delay(250);
WiFi.begin(ssid, password);
Serial.print(WiFi.status());
digitalWrite(BLINK_LED, LOW);
delay(250);
connection_count++;
} while (WiFi.waitForConnectResult() != WL_CONNECTED && connection_count < 60);
// Print local IP address
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("MAC: ");
Serial.println(WiFi.macAddress());
Serial.printf("esp_idf_version %s\n", esp_get_idf_version());
configTime(0, 3600, ntpServer);
setenv ("TZ", TZstr, 1);
tzset (); // save the TZ variable
// Get programming from eeprom)
for (int i = 0; i < num_zones; i++) {
zones[i].hours= EEPROM.read(i * 8 + 0) * 0x1000000L + EEPROM.read(i * 8 + 1) * 0x10000L + EEPROM.read(i * 8 + 2) * 0x100L + EEPROM.read(i * 8 + 3) * 0x1L;
zones[i].on_temp = EEPROM.read(i * 8 + 4); // 0 to 128 resolution 0.5 to allow for Farenheit
if (zones[i].on_temp > 250) zones[i].on_temp = (units) ? 140 : 40; // in case if unintialised eeprom
zones[i].off_temp = EEPROM.read(i * 8 + 5); // 0 to 128 resolution 0.5 to allow for Farenheit
if (zones[i].off_temp > 250) zones[i].off_temp = (units) ? 80 : 10;
}
for (int i = 0; i < num_boilers; i++) {
pinMode(boilers[i].out_pin, OUTPUT);
pins[boilers[i].out_pin] = 'x';
digitalWrite(boilers[i].out_pin, HIGH);
}
for (int i = 0; i < num_zones; i++) {
pinMode(zones[i].out_pin, OUTPUT);
pins[zones[i].out_pin] = 'x';
digitalWrite(zones[i].out_pin, HIGH);
}
for (int i = 0; i < num_zones; i++) pinMode(zones[i].in_pin, INPUT_PULLUP);
for (int i = 0; i < num_pumps; i++) {
pinMode(pumps[i].out_pin, OUTPUT);
pins[pumps[i].out_pin] = 'x';
digitalWrite(pumps[i].out_pin, HIGH);
}
// Find which DS2482 channel each DS18B20 sensor can be found on
num_sensors = 0;
#ifdef USE_DS2482
if (ds2482_reset >= 0){
// pinMode(ds2482_reset, OUTPUT);
//digitalWrite(ds2482_reset, HIGH);
// delay(1000);
// oneWire.deviceReset();
// delay(1000);
// digitalWrite(ds2482_reset, LOW); // USE NC terminals, but sense is opposite for single relay
pins[ds2482_reset] = 'x';
// delay(2000);
}
sensors.begin();
delay(1000);
Serial.println("Searching for DS18B20 Sensors on DS2482");
if (!oneWire.checkPresence()){
Serial.println("DS2482 NOT found.");
}else{
Serial.println("DS2482 found.");
for (byte c = 0; c < 8; c++) {
oneWire.setChannel(c);
if (oneWire.wireReset()){
sensors.begin();
Serial.printf("Checking channel %d\n", c);
for (int j = sensors.getDS18Count() - 1; j >= 0; j--){
sensors.getAddress(all_sensors[num_sensors].address, j);
byte *a = all_sensors[num_sensors].address;
sprintf(all_sensors[num_sensors].str_address,"%02X%02X%02X%02X%02X%02X%02X%02X",
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
sprintf(all_sensors[num_sensors].dot_address,"%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x",
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
Serial.printf("Found device addr %s\n", all_sensors[num_sensors].dot_address);
all_sensors[num_sensors].channel = c;
num_sensors = min(max_sensors, ++num_sensors);
}
for (int z = 0; z < num_zones; z++) {
zones[z].temp=-127;
if (sensors.isConnected(zones[z].sensor.address) && zones[z].sensor.channel >= 0) {
Serial.printf(" Sensor for zone %i found\n", z);
zones[z].sensor.channel = c;
}
}
for (int z = 0; z < num_boilers; z++) {
zones[z].temp=-127;
if (sensors.isConnected(boilers[z].f_sensor.address) && boilers[z].f_sensor.channel >= 0) {
Serial.printf(" Flow sensor for boiler %i found\n", z);
boilers[z].f_sensor.channel = c;
}
if (sensors.isConnected(boilers[z].r_sensor.address) && boilers[z].r_sensor.channel >= 0) {
Serial.printf(" Return sensor for boiler %i found\n", z);
boilers[z].r_sensor.channel = c;
}
}
}
}
}
#else
sensors.begin;
Serial.println("Searching for DS18B20 Sensors");
DeviceAddress addr;
while (! oneWire.search(addr)){
oneWire.reset();
delay(250);
}
oneWire.reset_search();
while (oneWire.search(all_sensors[num_sensors].address)){
byte *a = all_sensors[num_sensors].address;
sprintf(all_sensors[num_sensors].str_address,"%02X%02X%02X%02X%02X%02X%02X%02X",
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
sprintf(all_sensors[num_sensors].dot_address,"%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x",
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
Serial.printf("Found device addr %s\n", all_sensors[num_sensors].dot_address);
all_sensors[num_sensors].channel = 0;
num_sensors = min(max_sensors, ++num_sensors);
}
for (int z = 0; z < num_zones; z++) {
zones[z].temp=-127;
if (sensors.isConnected(zones[z].sensor.address) && zones[z].sensor.channel >= 0) {
Serial.printf(" Sensor for zone %i found\n", z);
}
}
#endif
Serial.printf("Found a total of %i sensors\n", num_sensors);
// Set unused pins to their default state
for(int i = 0; i < 39; i++){
if (pins[i] == '0') {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
} else if (pins[i] == '1') {
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
} else if (pins[i] == 'i') {
pinMode(i, INPUT);
}
}
// Configure and start web server
// HTTP request pages are separate files
// And they seem to need to be .h as .cpp doesn't compile
#include "FrontPage.h"
#include "handleProgramming.h"
#include "handleSettings.h"
AsyncElegantOTA.begin(&server);
server.begin();
}
bool temp_valid(int z) {
if (! units) {
if (zones[z].temp > 80 || zones[z].temp < -100) return 0;
} else {
if (zones[z].temp > 180 || zones[z].temp < -100) return 0;
}
return 1;
}
void loop() {
int t_fault = 0;
int z = -1; // zone index
int h = -1; // hour index
char header[21] = {0}; int r = 0; // header buffer and index, ignore all but the first 20 chars
AsyncElegantOTA.loop();
do_status(); // blink-codes and connection housekeeping
getLocalTime(&timeinfo);
for (z = 0; z < num_zones; z++) {
float demand_temp;
bool off_flag = 0;
zones[z].temp = get_temp(zones[z].sensor);
if ((bitRead(zones[z].hours, timeinfo.tm_hour) || bitRead(zones[z].hours, 24)) && ! bitRead(zones[z].hours, 25)) {
demand_temp = zones[z].on_temp / 2.0;
} else {
demand_temp = zones[z].off_temp / 2.0;
off_flag = true; // Used to reset valve-stuck-closed
}
if ( ! temp_valid(z)) {
t_fault++;
// Handle default behaviour in faulted state
if (zones[z].default_state) {
if (off_flag) {
demand_temp = -250;
} else {
demand_temp = +250;
}
} else {
demand_temp = -250;
}
if (error_flag != 6){
zones[z].timeout = time(NULL);
error_flag = 6;
} else {
if (time(NULL) - zones[z].timeout > TIMEOUT){
#ifdef USE_DS2482
if (ds2482_reset >= 0){
Serial.printf("Resetting DS2482 HIGH on pin %i\n", ds2482_reset);
oneWire.deviceReset();
digitalWrite(ds2482_reset, HIGH);
delay(10000);
digitalWrite(ds2482_reset, LOW);
}
#endif
Serial.println("Resetting oneWire");
oneWire.reset();
zones[z].timeout = time(NULL);
}
}
}
/*************************************
Valve control state machine
*************************************/
switch (zones[z].state) {
case 0: // closed
if (zones[z].temp < demand_temp - hyst) {
digitalWrite(zones[z].out_pin, LOW);
zones[z].state = 1;
zones[z].timeout = time(NULL);
Serial.printf("Turning On zone %d\n", z);
}
break;
case 1: // valve opening
if (! digitalRead(zones[z].in_pin)) { // valve has opened
bitSet(zone_on, z);
zones[z].state = 2;
break;
}
if (time(NULL) - zones[z].timeout > timeoutTime) {
error_flag = 5;
zones[z].state = 5;
}
break;
case 2: // Valve opened
if (zones[z].temp > demand_temp + hyst) {
digitalWrite(zones[z].out_pin, HIGH);
zones[z].state = 3;
zones[z].timeout = time(NULL);
bitClear(zone_on, z);
Serial.printf("Turning Off zone %d\n", z);
}
break;
case 3: // valve closing
if (digitalRead(zones[z].in_pin)) { // valve has closed
zones[z].state = 0;
break;
}
if (time(NULL) - zones[z].timeout > timeoutTime) { // timeout closing
zones[z].state = 4;
error_flag = 4;
}
case 4: // Stuck Open fault: We can still control temp, and it might close
if (digitalRead(zones[z].in_pin)) { // valve has finally closed
bitClear(zone_on, z);
zones[z].state = 0;
error_flag = 1;
break;
}
if (zones[z].temp > demand_temp + hyst) {
bitClear(zone_on, z);
} else if (zones[z].temp < demand_temp - hyst) {
bitSet(zone_on, z);
}
break;
case 5: // stuck closed fault
digitalWrite(zones[z].out_pin, HIGH); // avoid overheating motor
bitClear(zone_on, z); // turn off the pump
if (off_flag) {
zones[z].state = 0;
error_flag = 1;
}
break;
default:
error_flag = 10;
}
}
if (t_fault == 0 && error_flag == 6) error_flag = 1;
/*********************************
Boiler State Machine
*********************************/
for (int i = 0; i < num_boilers; i++) {
boilers[i].f_temp = get_temp(boilers[i].f_sensor);
boilers[i].r_temp = get_temp(boilers[i].r_sensor);
switch (boilers[i].state) {
case 0: // boiler off
if (zone_on && boilers[i].mask) {
bitSet(boiler_on, i);
digitalWrite(boilers[i].out_pin, LOW);
boilers[i].state = 1;
}
break;
case 1: // boiler on
if (! zone_on && boilers[i].mask) {
boilers[i].run_on_timer = time(NULL);
bitClear(boiler_on, i);
bitSet(run_on, i);
boilers[i].state = 2;
digitalWrite(boilers[i].out_pin, HIGH);
}
break;
case 2: // run-on timer
// check for timeout _or_ re-activation
if (((time(NULL) - boilers[i].run_on_timer) > run_on_time) || (zone_on && boilers[i].mask)) {
bitClear(run_on, i);
boilers[i].state = 0;
break;
}
break;
default:
error_flag = 10;
}
}
/****************************
Pump Control
****************************/
for (int i = 0; i < num_pumps; i++) {
if ((pumps[i].mask & zone_on) || (pumps[i].run_on_mask & run_on)) {
digitalWrite(pumps[i].out_pin, LOW);
} else {
digitalWrite(pumps[i].out_pin, HIGH);
}
}
}