-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHeating_ESP_control.ino
1017 lines (871 loc) · 30.7 KB
/
Heating_ESP_control.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is used to add connected functionality to
* thermostatic radiator valve (TRV) head Eqiva EQ-3 N.
* It enables connection via MQTT protocol to central smart home units such as Home Assistant.
* ESP8266 board NodeMCU D1 mini is connected to the original board of EQ-3.
*
* Connections: (also in folder /wiring/)
* Light sensor is connected like this:
* pin 3.3V - photoresistor - pin A0 - 10k resistor - pin GND
* H-bridge pins are connected via 27 Ohm resistors to T1+T2 and T3+T4 (probably not necessary, just in case to protect ESP pins)
* if the valve is turning the other way, switch the pins
* DS18B20 temp sensor is connected to 3.3V and GND, middle pin is connected via (+-)4.6k pullup
* HIGHLY recommended to use wires at least 15 cm long, otherwise the reading could be too high
* learned it the hard way
*
* Change config below according to your needs or enter using AP+webserver:
* SSID is "ESPvalve-" & random 4 hex letters
* default WiFi AP password: "WiFiAPpassword", look below
* access using [SSID].local or 192.168.4.1
* enter credentials, click the green button and enjoy (:
*
* @author Jan Smejkal
* This program is a part of a bachelor's thesis at FIT BUT Brno.
* 2021
*
*TODO:
* PID control - tune params (hard because I part stacks up when central heating is off)
* - test & tune
* Opens AP to login and change credentials of network & MQTT broker
* - done, possible extension: show list of available WiFi networks
* ^https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.i
*/
#include <ESP8266WiFi.h>
//HTTP server:
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include <ESPAsyncWebServer.h>//async in handlesettingsupdate cannot yield, but it has to or ESP sometimes panics into bootloop
#include <ESP8266mDNS.h>
#include <string.h>
//to store and load config of WiFi+MQTT to connect to in flash
#include <FS.h>
#include <ArduinoJson.h>
//for OTA updates
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//time for decalc every saturday
#include <NTPClient.h>
//DS18B20 reading using lib by Rob Tillaart
#include <DS18B20.h>
#include <OneWire.h>
//PID
#include "QuickPID.h"
//MQTT:
#include <PubSubClient.h>
//default credentials of network, optional - example inputs are in #IFNDEF below, can be also changed via web interface:
//just connect to the AP (using APSSID + APPSK) and enter 192.168.4.1 or SSID_OF_AP + ".local"
#include "credentials.h"
//temperature reading:
const int DSpin = D5;
OneWire oneWire(DSpin);
DS18B20 sensor(&oneWire);
//maximum number of letters - 1 for each credential stored in flash
#define SIZE_CREDENTIAL_UNIT 25
//credentials can be included in credentials.h, example entries below:
//WiFi client (STAtion) fallback credentials
#ifndef STASSID
#define STASSID "WiFiSSID"
#define STAPSK "WiFipassword"
#endif
#define WIFI_RETRIES 3
//WiFi host (AP) fallback credentials
#ifndef APSSID
#define APSSID "ESPValve"
#define APPSK "WiFiAPpassword"
#endif
//MQTT fallback credentials, should be included from credentials.h
#ifndef MQTTCRED
const char *mqttServer = "192.168.X.XXX"; //max len of ipv6 addr (just in case someone switches to IPv6, this project expects IPv4
const int mqttPort = 1883;
const char *mqttUser = "ESPclient";
const char *mqttPassword = "mqttpassword";
//password for uploading OTA updates to ESP
const char* OTApassword = "otapassword";
#endif
//how often to retry connecting to WiFi/MQTT:
#define RETRY_PERIOD 5000
unsigned long last_retry = 0;
/**
* Struct for loading/getting config to store it in flash in FS
*/
struct Config {
char ap_ssid [SIZE_CREDENTIAL_UNIT];
char ap_psk [SIZE_CREDENTIAL_UNIT];
char mqtt_server [40];
char mqtt_port [SIZE_CREDENTIAL_UNIT];
char mqtt_user [SIZE_CREDENTIAL_UNIT];
char mqtt_password [SIZE_CREDENTIAL_UNIT];
};
struct Config *loaded_config;
//pins to control H-bridge in order to control the valve motor:
const int T1 = D8;
const int T3 = D7;
//stores last direction of motor
#define DIR_OPEN 1
#define DIR_CLOSE 2
#define DIR_NONE 0
int dir = DIR_NONE;
//stores sum of time that motor moved in series in one direction
// so in summer it doesn't just close all the time
unsigned long cnt_same_direction = 0;
const unsigned long same_direction_threshold = 70 * 1000;
unsigned long time_until = 0;
#define T_SIZE 6
//recent temperatures from last measurements
double temps[T_SIZE] = {21, 21, 21, 21, 21, 21};
//determines how often in ms should the valve recalculate and react to temperature change
#define REACT_TIME 60 * 1000
unsigned long time_last_react;
double target_temp = 21.0;
bool heating_on = true;
/**
* PID part:
*using library available from https://github.com/Dlloydev/QuickPID
*/
int16_t Setpoint = target_temp * 100, Input, Output;
float Kp = 2.0, Ki = 0.007, Kd = 0.02;
float POn = 0.05; // Range is 0.0 to 1.0 (1.0 is 100% P on Error, 0% P on Measurement)
//initial tuning parameters:
QuickPID myQuickPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, POn, DIRECT);
/**
* Stores previous position of valve, used to determine motor movement
*0 = fully closed valve, 255 = open
*/
int16_t prev_position = 255;
/*
* window opened/closed detection:
*/
bool window_opened = false;
#define HYSTERESIS_OPENED_WINDOW 1
#define HYSTERESIS_CLOSED_WINDOW 0.25
/*
* NTP section
* update every 48 hrs
*/
#define NTP_PERIOD 1000*60*60*48
//local offset in s. 1hr=3600s
#define PragueOffset 3600 //UTC+1
WiFiUDP ntpUDP;
//parameters: ptr WiFiUDP, serveraddr , offset[s], update interval [ms]
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, NTP_PERIOD);
unsigned long last_ntp = 0;
//stores info that decalc happened this week
bool decalc_done = false;
//MQTT
//period between sending current temperature
#define MQTT_SEND_PERIOD 30*1000
unsigned long time_last_send = 0;
WiFiClient espClient;
PubSubClient client(espClient);
//HTTP server inspired from example AdvancedWebServer Copyright (c) 2015, Majenko Technologies
ESP8266WebServer server(80);
//pin from which we can read from photoresistor, higher value means more light detected
const int light_sensor = A0;
//Pin where signal from middle button is connected, used to detect falling edge (3.3V -> 0V)
//used to switch boost mode
const int button_pin = D1;
int prev_button = HIGH;
int act_button;
//boost mode variables:
//to store when boost started
long long boost_start = -1;//negative value means not active
//time boost should take, could be changed via incoming MQTT message
int boost_duration = 3*60*1000; //3 minutes in ms
/**
HTTP webpage inspired from user ACROBOTIC at https://www.youtube.com/watch?v=lyoBWH92svk or https://github.com/acrobotic/Ai_Tips_ESP8266/blob/master/wifi_modes_switch/wifi_modes_switch.ino
and design from w3schools at https://www.w3schools.com/css/tryit.asp?filename=trycss_forms
*/
char webpage[] PROGMEM = R"=====(
<html>
<head>
<style>
body {
font-family: 'Segoe UI', sans-serif;
}
input {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
button :hover {
background-color: #45a049;
}
form {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
</head>
<body>
<h2>ESP Heating Valve Credentials Update</h2>
<form>
<label for="WIFIssid">WiFi SSID</label>
<input value="" id="WIFIssid" placeholder="SSID of WiFi to connect to"/>
<label for="WIFIpassword">WiFi password</label>
<input value="" id="WIFIpassword" placeholder="password for WiFi to connect to"/>
<label for="MQTTip">MQTT IP address</label>
<input value="" id="MQTTip" placeholder="IP address of MQTT broker"/>
<label for="MQTTport">MQTT port</label>
<input value="" id="MQTTport" placeholder="port of MQTT broker, usually 1883"/>
<label for="MQTTusername">MQTT username (optional)</label>
<input value="" id="MQTTusername" placeholder="MQTT username"/>
<label for="MQTTpassword">MQTT password (optional)</label>
<input value="" id="MQTTpassword" placeholder="MQTT password"/>
<button onclick="save()">Save changes</button>
</form>
</body>
<script>
function save() {
var ap_ssid = document.getElementById("WIFIssid").value;
var ap_psk = document.getElementById("WIFIpassword").value;
var mqtt_server = document.getElementById("MQTTip").value;
var mqtt_port = document.getElementById("MQTTport").value;
var mqtt_user = document.getElementById("MQTTusername").value;
var mqtt_password = document.getElementById("MQTTpassword").value;
var data = {ap_ssid:ap_ssid, ap_psk:ap_psk, mqtt_server:mqtt_server, mqtt_port:mqtt_port, mqtt_user:mqtt_user, mqtt_password:mqtt_password};
var xhr = new XMLHttpRequest();
var url = "/settings";
xhr.onreadystatechange = function(){
if (this.onreadyState == 4 && this.status == 200) {
console.log(xhr.responseText);
}
};
xhr.open("POST", url, true);
xhr.send(JSON.stringify(data));
}
</script>
</html>
)=====";
/**
* Shows root webpage with config:
* - WiFi SSID+password
* - MQTT IP address
* could show list of APs nearby
*/
void HandleRoot(){
Serial.println("HandleRoot mainpage");
server.send_P(200, "text/html", webpage);
}
/**
* stores JSON data to flashsfile system and reboots.
*/
void HandleSettingsUpdate(){
Serial.println("HandleSettingsUpdate start");
//receive JSON data to data and parse it
String data = server.arg("plain");
DynamicJsonBuffer j_buffer;
JsonObject& j_object = j_buffer.parseObject(data);
yield();
//store parsed data to file
File config_file = SPIFFS.open("/config.json", "w");
if (!config_file){
Serial.println("ERR opening /config.json in HandleSettingsUpdate!!!");
return;
}
Serial.println("");
j_object.printTo(config_file);
config_file.flush();
config_file.close();
server.send(200, "application/json", "{\"status\":\"ok\"}");
yield();
Serial.println("Settings updated, Restarting in 2s ...");
delay(2000);
ESP.restart();
}
/**
* Starts HTTP servers listening for incoming requests.
*/
void InitWeb(){
server.on("/", HandleRoot);
server.on("/settings", HTTP_POST, HandleSettingsUpdate);
server.begin();
Serial.println("HTTP server started");
}
/**
* Loads config and stores it in variable struct Config.
* Inspired from ConfigFile example by Ivan Grokhotkov in Arduino IDE.
*/
struct Config *LoadConfig(const char *filename){
File config_file = SPIFFS.open("/config.json", "r");
if (config_file) {
struct Config *conf_ret = (struct Config*) malloc((sizeof(struct Config)));
size_t size = config_file.size();
std::unique_ptr<char[]> buf(new char[size]);
config_file.readBytes(buf.get(), size);
DynamicJsonBuffer j_buffer(200);
JsonObject &j_object = j_buffer.parseObject(buf.get());
if (j_object.success()){
strncpy(conf_ret->ap_ssid, j_object["ap_ssid"], SIZE_CREDENTIAL_UNIT-1);
conf_ret->ap_ssid[SIZE_CREDENTIAL_UNIT-1] = '\0';
strncpy(conf_ret->ap_psk, j_object["ap_psk"], SIZE_CREDENTIAL_UNIT-1);
conf_ret->ap_psk[SIZE_CREDENTIAL_UNIT-1] = '\0';
strncpy(conf_ret->mqtt_server, j_object["mqtt_server"], 39);
conf_ret->mqtt_server[39] = '\0';
strncpy(conf_ret->mqtt_port, j_object["mqtt_port"], SIZE_CREDENTIAL_UNIT-1);
conf_ret->mqtt_port[SIZE_CREDENTIAL_UNIT-1] = '\0';
strncpy(conf_ret->mqtt_user, j_object["mqtt_user"], SIZE_CREDENTIAL_UNIT-1);
conf_ret->mqtt_user[SIZE_CREDENTIAL_UNIT-1] = '\0';
strncpy(conf_ret->mqtt_password, j_object["mqtt_password"], SIZE_CREDENTIAL_UNIT-1);
conf_ret->mqtt_password[SIZE_CREDENTIAL_UNIT-1] = '\0';
Serial.println("Succesfully loaded json data from FS.");
return conf_ret;
} else {
Serial.println("Failed to parse config data.");
return NULL;
}
} else {
Serial.println("Failed to open config file");
return NULL;
}
}
/**
* Initializes WiFi connection and starts endpoint for OTA updates.
* If connection to WiFi as STA (client) is not successful, open AP to load new config.
*/
void WifiOtaON() {
//default is STATION+AP
//to configure WiFi credentials + IP address of server
WiFi.mode(WIFI_STA);
//firstly let's add random string to differentiate from other ESPValves around here:
char APssid[30] = APSSID;
if (strnlen(APssid, 25) < 24){
strcat(APssid, "-");
strcat(APssid, String(random(0xffff), HEX).c_str());
} else {
APssid[29] = '\0';
}
//secondly, start station/client:
if (loaded_config != NULL){
WiFi.begin(loaded_config->ap_ssid, loaded_config->ap_psk);
} else {
WiFi.begin(STASSID, STAPSK);
}
bool connect_success = false;
//try to connect for a few times:
for (int i = 0; i < WIFI_RETRIES; i++) {
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Connection as STA Failed! Trying again..");
} else {
connect_success = true;
break;
}
}
//if connection was unsuccessful, open AP:
if (connect_success != true) {
//start host/AP: change to AP+STA mode
WiFi.mode(WIFI_AP_STA);
//the AP start itself:
if (WiFi.softAP(APssid, APPSK)){
Serial.print("AP IP addr:");
Serial.println(WiFi.softAPIP());
}
}
//if mDNS not started, start it
if (!MDNS.begin(APssid)){
Serial.println("mDNS startup failed!");
} else {
Serial.print("MDNS responder started at name=");
Serial.println(APssid);
}
//set password for OTA uploading new code to ESPs
//inspired from Arduino basicOTA example from https://github.com/esp8266/Arduino/blob/master/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino
ArduinoOTA.setPassword(OTApassword);
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS unmount it using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
/**
* handles incoming MQTT messages.
*/
void handleMQTT(char* topic, byte* payload, unsigned int length){
char *pomtext = (char*) malloc( sizeof(char) * ( length + 1 ) );
Serial.print("\nMessage arrived, topic: ");
Serial.println(topic);
if (pomtext == NULL){
Serial.print("ERR MQTT text could not be allocated, discarding, length=");
Serial.println(length);
return;
}
unsigned i = 0;
for (; i < length; i++){
pomtext[i] = payload[i];
}
pomtext[i] = 0;
//determine which topic arrived and proccess incoming value:
if (!strcmp(topic, "living_room/valve1/desired_temp/set")){
target_temp = atof(pomtext);
Setpoint = target_temp * 100;
Serial.print("New target_temp=");
Serial.println(target_temp);
} else if (!strcmp(topic, "living_room/valve1/mode/set")){//off|heat
if (strncmp(pomtext, "heat", 4) == 0){
heating_on = true;
Serial.println("heat on");
} else if (strncmp(pomtext, "off", 3) == 0){
heating_on = false;
Serial.println("heat off");
}
} else if (!strcmp(topic, "living_room/valve1/boost/set")) {
if (strncmp(pomtext, "OFF", 3) == 0){
//feedback message recieved
client.publish("living_room/valve1/boost", "OFF");
//boost ON->OFF by MQTT
if (boost_start != -1){
boost_start = -1;
Serial.println("Boost mode turning OFF from MQTT");
} else {
Serial.println("Boost mode staying OFF from MQTT");
}
} else if (strncmp(pomtext, "ON", 2) == 0) {
//feedback message recieved
client.publish("living_room/valve1/boost", "ON");
//boost ON->ON by MQTT => set starttime to now
if (boost_start != -1){
boost_start = millis();
Serial.println("Boost mode prolonging ON from MQTT");
//boost OFF->ON by MQTT
} else {
boost_start = millis();
Serial.println("Boost mode turning ON from MQTT");
MotorOpen(same_direction_threshold);
}
} else {
Serial.println("Incoming Boost MQTT msg discarded, unknown value!");
}
} else {
Serial.print("Incoming MQTT msg discarded, topic=");
Serial.println(topic);
}
}
/**
* initializates MQTT connection. Subscribing happens in setup()
* inspired from https://techtutorialsx.com/2017/04/09/esp8266-connecting-to-mqtt-broker/
*/
bool MQTTinit(){
char m_server[40], m_user[SIZE_CREDENTIAL_UNIT], m_password[SIZE_CREDENTIAL_UNIT];
int m_port;
if (loaded_config != NULL){
Serial.println("Using MQTT config from json in FS:");
strncpy(m_server, loaded_config->mqtt_server, 39);
m_server[39] = '\0';
m_port = atoi(loaded_config->mqtt_port);
strncpy(m_user, loaded_config->mqtt_user, SIZE_CREDENTIAL_UNIT-1);
m_user[SIZE_CREDENTIAL_UNIT-1] = '\0';
strncpy(m_password, loaded_config->mqtt_password, SIZE_CREDENTIAL_UNIT-1);
m_password[SIZE_CREDENTIAL_UNIT-1] = '\0';
} else {
Serial.println("Loading default MQTT config");
strcat(m_server, mqttServer);
m_port = mqttPort;
strcpy(m_user, mqttUser);
strcpy(m_password, mqttPassword);
}
char mqttID[20]="ESPvalve-";
strcat(mqttID, String(random(0xffff), HEX).c_str());
client.setServer(m_server, m_port);
client.setCallback(handleMQTT);
unsigned cnt = 0;
if (client.connect(mqttID, m_user, m_password )) {
Serial.println("MQTT connected");
//SUB to topics valve is interested in to get info about for its operation (controls, weather info, ...)
client.subscribe("living_room/valve1/desired_temp/set");
client.subscribe("living_room/valve1/mode/set");
client.subscribe("living_room/valve1/boost/set");
//MQTT publish info window closed (default when just started)
client.publish("living_room/valve1/window_opened", "OFF");
return true;
} else {
Serial.print("failed with state ");
Serial.println(client.state());
return false;
}
}
/**
* gets temperature from DS temperature sensor.
* returns temperature as double.
*/
double getTemp() {
sensor.requestTemperatures();
while (!sensor.isConversionComplete()){
// wait until sensor is ready, yield to ESP for now
yield();
}
return sensor.getTempC();
}
/**
* Pushes back incoming temperature newtemp to the end of array, moving others to the left.
* parameter double newtemp - incoming temperature
*/
void TempsPushBack(double newtemp){
unsigned i = 0;
for (; i < T_SIZE - 1 ; i++){
temps[i] = temps[i+1];
}
temps[i] = newtemp;
}
/**
* Returns true if temperature drop is over hysteresis
* Excepts T_SIZE to be at least 6, MINIMUM is 4 for meaningful results or error
*/
bool OpenedWindow(){
if (T_SIZE < 4){
//failsafe
Serial.println("ERR: function OpenedWindow could not be correctly called, T_SIZE has to be >= 4");
return false;
}
if (window_opened == false){
if ( ((temps[0] + temps[1]) / 2) - ((temps[T_SIZE-2] + temps[T_SIZE-1]) / 2) >= HYSTERESIS_OPENED_WINDOW ){
return true;
}
//if window is already detected open, check if closed (last measurement is rising against previous two)
} else if ( ((temps[T_SIZE-2] + temps[T_SIZE-1]) / 2) - ((temps[T_SIZE-4] + temps[T_SIZE-3]) / 2) >= HYSTERESIS_CLOSED_WINDOW ){
return false;
} else {
return true;
}
return false;
}
/**
* Checks if it's time for decalc and if yes, then it does it.
* By default it decals every saturday between 11 and 12.
*/
bool Decalc(){
//getDay returns 0-6 starting Sunday
if (timeClient.getDay() == 6 && timeClient.getHours() == 11 && decalc_done == false){
decalc_done = true;
MotorOpen(same_direction_threshold);
delay(same_direction_threshold);
MotorClose(same_direction_threshold);
return true;
} else if (timeClient.getDay() == 6 && timeClient.getHours() > 12) {
decalc_done = false;
}
return false;
}
/**
* Opens motor and sets var time_until declaring how long should it be opening for.
* param time time in ms to open for.
* The time is in reality likely to be a few hundreds of ms longer before the movement is cancelled.
*/
void MotorOpen(unsigned time){
if (dir == DIR_OPEN && cnt_same_direction >= same_direction_threshold) {
Serial.println("NOT opening valve, over threshold, cnt=" + String(cnt_same_direction));
return;
}
analogWrite(T1, 700);
analogWrite(T3, 0);
if (dir == DIR_OPEN){
cnt_same_direction += time;
} else {
dir = DIR_OPEN;
cnt_same_direction = time;
}
time_until = millis()+time;
Serial.println("Opening valve @700/1023, should be opened in " + String(time) + "ms at " + String(time_until) + ".");
}
/**
* Closes motor and sets var time_until declaring how long should it be closing for.
* param time time in ms to close for. The time is in reality likely to be a few hundreds of ms longer.
*/
void MotorClose(unsigned time){
if (dir == DIR_CLOSE && cnt_same_direction >= same_direction_threshold) {
Serial.println("NOT closing valve, over threshold, cnt=" + String(cnt_same_direction));
return;
}
analogWrite(T1, 0);
analogWrite(T3, 700);
if (dir == DIR_CLOSE){
cnt_same_direction += time;
} else {
dir = DIR_CLOSE;
cnt_same_direction = time;
}
time_until = millis()+time;
Serial.println("Closing valve @700/1023, should be closed in " + String(time) + "ms at " + String(time_until) + ".");
}
/*
* Checks if Open/Close happened for desired time and should be stopped.
* returns true when canceling action, false when no action canceled.
*/
bool CheckMotorTimeUntil(){
if (time_until <= millis() - 50){
digitalWrite(T1, LOW);
digitalWrite(T3, LOW);
if (time_until > 0) {
Serial.print("Cancelling closing/opening, should have been closed/opened at T_U=" + String(time_until) + ", diff=");
Serial.println(String(millis()-time_until) + ", now=" + String(millis()));
}
time_until = 0;
return true;
}
return false;
}
/**
* Initializes all used parts of program.
* Tries to load config, tries connecting to WiFi, MQTT,
* starts webserver, mDNS responder, PID, resets valve state to open.
*/
void setup() {
// initialize GPIO pins:
pinMode(T1, OUTPUT);
pinMode(T3, OUTPUT);
//default PWM is 1000 Hz, which is hearable, changed to 32k out of hearing range
analogWriteFreq(32000);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(light_sensor, INPUT);
pinMode(button_pin, INPUT);
digitalWrite(LED_BUILTIN, LOW);
//initializes serial communication, accessible by cable:
Serial.begin(115200);
Serial.println("### Booting ###");
SPIFFS.begin();
loaded_config = LoadConfig("/config.json");
//starts WiFi STAtion, if it doesn't connect succesffully, starts AP to store new settings. and OTA updates endpoint and mDNS responder
WifiOtaON();
//initialize sensor and measure first temp:
sensor.begin();
temps[0] = getTemp();
for (int i=1; i<T_SIZE; i++){
temps[i] = temps[i-1];
}
timeClient.begin();
timeClient.update();
MQTTinit();
InitWeb();
//double to int conversion for PID which needs int => to not lose accuracy, multiply temperature by 100 to have 0.01 accuracy of input
Input = temps[T_SIZE-1] * 100;
Setpoint = target_temp * 100;
Serial.print("input (*100) = ");
Serial.println(Input);
Serial.print("setpoint (*100) = ");
Serial.println(Setpoint);
//turn the PID on
myQuickPID.SetMode(AUTOMATIC);
//set PID control variables (tune it)
myQuickPID.SetTunings(Kp, Ki, Kd, POn);
//wait for the original board to do its bootup...
delay(2000);
//set valve position to baseline (fully opened), helps with installation
MotorOpen(same_direction_threshold);
prev_position = 255;
time_last_react = 0;
Serial.println("Startup setup end");
}
/**
* Main loop of the program.
* If there is problem connecting to WiFI or MQTT, the LED is ON.
*/
void loop() {
char temps_text[32];
double temp;
bool gotTemp = false;
int16_t diff_position;
int light_value;
//Handle possible OTA updates:
ArduinoOTA.handle();
//check if it's time to stop motor movement:
CheckMotorTimeUntil();
/**
* Check WiFi or MQTT broker connection, if not connected to MQTT, try reconnecting again
* (WiFi reconnection happens automatically)
*/
if (millis() - last_retry > RETRY_PERIOD) {
//check WiFi
if (WiFi.status() != WL_CONNECTED ) {
digitalWrite(LED_BUILTIN, LOW);//LED ON
//check MQTT, if not, reconnect
} else if (!client.connected()) {
digitalWrite(LED_BUILTIN, LOW);//LED ON
MQTTinit();
//connections are OK, keep connection with MQTT broker alive:
} else {
digitalWrite(LED_BUILTIN, HIGH);//LED OFF
//keep MQTT connection and get possible messages with updates
client.loop();
}
last_retry = millis();
}
//handle MDNS resolve requests, if any
//configuration happened in MQTTinit()
MDNS.update();
//handle webserver requests, if any
server.handleClient();
//check if it's time to decalc and if so, do it
Decalc();
//Boost button press detection + action
act_button = digitalRead(button_pin);
//if button state changed:
if (act_button != prev_button) {
//falling edge => button was just pressed
if (prev_button == HIGH) {
//check if boost was OFF=> turn ON
if (boost_start == -1) {
boost_start = millis();
Serial.println("Boost mode ON from button");
MotorOpen(same_direction_threshold);
client.publish("living_room/valve1/boost", "ON");
//if boost is already happening, cancel it
} else {
boost_start = -1;
Serial.println("Boost mode OFF from button");
MotorClose(same_direction_threshold);
client.publish("living_room/valve1/boost", "OFF");
}
}
prev_button = act_button;
}
//check if boost should end by timeout:
if (millis() - boost_start > boost_duration && boost_start >= 0) {
client.publish("living_room/valve1/boost", "OFF");
Serial.println("Boost mode timeouted to OFF");
boost_start = -1;
}
/**
* Checks if it's time to stop motor movementt
* Called twice inside the loop so there is not that long possible delay
*/
CheckMotorTimeUntil();
//check if it is time to get temp and determine motor movement:
if (millis() - time_last_react > REACT_TIME) {
gotTemp = true;
temp = getTemp();
TempsPushBack(temp);
sprintf(temps_text, "Temps: %f %f %f %f", temps[0], temps[1], temps[2], temps[3]);
Serial.println(temps_text);
/**
* Experimental PID part
*/
Input = temps[T_SIZE-1]*100;
myQuickPID.Compute();
//check if heating is on/off and no motor movement is happening:
if (time_until == 0 && heating_on == true && boost_start < 0) {
//check opened window => close valve
if (OpenedWindow() == true) {
window_opened = true;
MotorClose(same_direction_threshold);
prev_position = 0;
//MQTT publish info about opened window
client.publish("living_room/valve1/window_opened", "ON");
//if window was open and is now closed
} else if (window_opened == true) {
window_opened = false;
//MQTT publish info window closed
client.publish("living_room/valve1/window_opened", "OFF");
//if temp is too low, continue heating from full blast:
if (temps[T_SIZE-1] + 3 < target_temp) {
MotorOpen(same_direction_threshold);
prev_position = 255;
}
//heating is ON and window is closed, so we can control heating:
} else {
diff_position = Output - prev_position;
//DBG output
Serial.print(Output); Serial.print(" ");
Serial.print(prev_position); Serial.print(" ");
Serial.print("abs=");
Serial.println(abs(diff_position));
Serial.print("diff=");
Serial.println(diff_position);
//Check if diff of new position is a bit significant:
if (abs(diff_position) >= 10) {
//move in the correct direction:
if (diff_position > 0) {
//if getting to fully open, open even more so we can be sure we hit edge
if (Output == 255){
MotorOpen(diff_position / 255.0 * same_direction_threshold + 5000);
} else {
MotorOpen(diff_position / 255.0 * same_direction_threshold);
}
} else {
//if getting to fully closed, close even more so we can be sure we hit edge
if (Output == 0) {
MotorClose(abs(diff_position) / 255.0 * same_direction_threshold + 5000);
} else {
MotorClose(abs(diff_position) / 255.0 * same_direction_threshold);
}
}
prev_position = Output;
}
char outputstr[16];//just to be sure in case int somehow becomes bigger than 16b
sprintf(outputstr, "%d", Output);
client.publish("living_room/valve1/PIDvalue", outputstr);
Serial.print("publish PID value=");
Serial.println(outputstr);
}
} else if (heating_on == false){
MotorClose(same_direction_threshold);
}
//log that reaction happened:
time_last_react = millis();
/*
* End of PID part
*/
}
/**
* MQTT temp sending, checks if previous block happend and if so, then it just gets recent temperature from there and doesn't get it from sensor
* that's because the sensor could get heated up from just recent sending of temp data.
*/
if (millis() - time_last_send > MQTT_SEND_PERIOD){
if (gotTemp == true){
temp = temps[T_SIZE-1];
} else {