-
Notifications
You must be signed in to change notification settings - Fork 329
/
ESP8266_mqtt_termostat_fan.ino
764 lines (574 loc) · 18.5 KB
/
ESP8266_mqtt_termostat_fan.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
#define DEBUG //If you comment this line, the DPRINT & DPRINTLN lines are defined as blank.
#ifdef DEBUG //Macros are usually in all capital letters.
#define DPRINT(...) Serial.print(__VA_ARGS__) //DPRINT is a macro, debug print
#define DPRINTLN(...) Serial.println(__VA_ARGS__) //DPRINTLN is a macro, debug print with new line
#else
#define DPRINT(...) //now defines a blank line
#define DPRINTLN(...) //now defines a blank line
#endif
#include <ESP8266WiFi.h>
#include <MQTT.h>
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define AP_SSID "xxx"
#define AP_PASSWORD "xxx"
#define EIOTCLOUD_USERNAME "xxx"
#define EIOTCLOUD_PASSWORD "xxx"
#define TEMPSENDINTERVAL 30 // s
#define TEMP_HISTERESE 0.25
#define CONFIG_START 0
#define CONFIG_VERSION "v01"
#define AP_CONNECT_TIME 30 //s
#define EIOT_CLOUD_ADDRESS "cloud.iot-playground.com"
MQTT *pmyMqtt = NULL;
#define PARAM_SET_TEMP "Sensor.Parameter1"
#define PARAM_SWITCH "Sensor.Parameter2"
#define PARAM_FAN "Sensor.Parameter3"
#define PARAM_ACT_TEMP "Sensor.Parameter4"
#define ONE_WIRE_BUS_PIN D2
#define RELAY_PIN D1
#define CFG_BUTTON_PIN D3
#define WIFI_STATUS_PIN D0 // LED_BUILTIN
#define LED_PIN D5 //
#define MANUAL_BUTTON_PIN D6 //
float actTemp; // actual temperature
bool fanOld;
bool fan;
float setTempNew;
bool switchState;
int milCnt;
int secCnt;
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature DS18B20(&oneWire);
WiFiServer server(80);
struct StoreStruct {
// This is for mere detection if they are your settings
char version[4];
// The variables of your settings
uint moduleId; // module id
bool state; // state
uint setTemp; // set temp
//uint fanMode; // fanMode
//uint acMode; // ac mode
char ssid[20];
char pwd[20];
char clUser[20];
char clPwd[20];
} storage = {
CONFIG_VERSION,
0, // The default module 0
0, // off
22, // 22 C
//2, // fan 2
//0, // ac mode - cool
AP_SSID,
AP_PASSWORD,
EIOTCLOUD_USERNAME,
EIOTCLOUD_PASSWORD
};
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void loadConfig() {
// To make sure there are settings, and they are YOURS!
// If nothing is found it will use the default settings.
if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
for (unsigned int t=0; t<sizeof(storage); t++)
*((char*)&storage + t) = EEPROM.read(CONFIG_START + t);
}
void saveConfig() {
for (unsigned int t=0; t<sizeof(storage); t++)
EEPROM.write(CONFIG_START + t, *((char*)&storage + t));
EEPROM.commit();
}
void AP_Setup(void){
Serial.println("Setup mode");
WiFi.mode(WIFI_AP);
String clientName;
clientName += "Device-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
Serial.println("starting ap");
WiFi.softAP((char*) clientName.c_str(), "");
Serial.println("running server");
server.begin();
}
void AP_Loop(void){
bool inf_loop = true;
int val = 0;
WiFiClient client;
Serial.println("AP loop");
int i =0;
while(inf_loop){
while (!client){
Serial.print(".");
delay(100);
client = server.available();
if (i++ > 1 * 60 * 10 * 5) // 5 min
{
Serial.println("Exit AP loop");
return;
}
Serial.println(i);
}
String ssid = "";
String passwd = "";
String clUser = "";
String clPwd = "";
bool moduleReset = false;
// Read the first line of the request
String req = client.readStringUntil('\r');
client.flush();
// Prepare the response. Start with the common header:
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
if (req.indexOf("&") != -1){
int ptr1 = req.indexOf("ssid=", 0);
int ptr2 = req.indexOf("password=", ptr1);
int ptr3 = req.indexOf("cluser=", ptr2);
int ptr4 = req.indexOf("clpwd=", ptr3);
int ptr5 = req.indexOf(" HTTP/",ptr4);
int ptr6 = req.indexOf("modReset=", ptr4);
ssid = req.substring(ptr1+5, ptr2-1);
passwd = req.substring(ptr2+9, ptr3-1);
clUser = req.substring(ptr3+7, ptr4-1);
if (ptr6 != -1)
{
moduleReset = true;
clPwd = req.substring(ptr4+6, ptr6-1);
}
else
clPwd = req.substring(ptr4+6, ptr5);
val = -1;
}
if (val == -1){
strcpy(storage.ssid, ssid.c_str());
strcpy(storage.pwd, passwd.c_str());
strcpy(storage.clUser, clUser.c_str());
strcpy(storage.clPwd, clPwd.c_str());
if (moduleReset)
storage.moduleId = 0;
saveConfig();
s += "Setting OK";
s += "<br>"; // Go to the next line.
// s += ssid + "<br>";
// s += passwd + "<br>";
// s += clUser + "<br>";
// s += clPwd + "<br>";
s += "Continue / reboot";
inf_loop = false;
}
else{
String content="";
content += "<!DOCTYPE html>";
content += "<html>";
content += "<body>";
content += "<style>";
content += "body {";
content += "zoom: 300%;";
content += "}";
content += "input[type=text], input[type=password], select {";
content += "width: 100%;";
content += "padding: 12px 20px;";
content += "margin: 8px 0;";
content += "display: inline-block;";
content += "border: 1px solid #ccc;";
content += "border-radius: 4px;";
content += "box-sizing: border-box;";
content += "}";
content += "input[type=submit] {";
content += "width: 100%;";
content += "background-color: #4CAF50;";
content += "color: white;";
content += "padding: 14px 20px;";
content += "margin: 8px 0;";
content += "border: none;";
content += "border-radius: 4px;";
content += "cursor: pointer;";
content += "}";
content += "input[type=submit]:hover {";
content += "background-color: #45a049;";
content += "}";
content += "div {";
content += "border-radius: 5px;";
content += "background-color: #f2f2f2;";
content += "padding: 20px;";
content += "}";
content += "</style>";
content += "<div>";
content += "<form method=get>";
content += "<b>AP</b><br>";
content += "SSID:<br>";
content += "<input type='text' name='ssid' maxlength='19' value='"+ String(storage.ssid) +"'>";
content += "<br>";
content += "PASSWORD:<br>";
content += "<input type='password' name='password' maxlength='19' value='"+ String(storage.pwd) +"'>";
content += "<br><br>";
content += "<b>Cloud</b><br>";
content += "Username:<br>";
content += "<input type='text' name='cluser' maxlength='19' value='"+ String(storage.clUser) +"'>";
content += "<br>";
content += "PASSWORD:<br>";
content += "<input type='password' name='clpwd' maxlength='19' value='"+ String(storage.clPwd) +"'>";
content += "<br><br>";
content += "<input type='checkbox' name='modReset' >Reset module Id<br><br>";
content += "<input type='submit' value='Submit'>";
content += "</form>";
content += "</div>";
content += "</body>";
content += "</html>";
s += content;
}
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
client.stop();
}
}
void setupMode()
{
Serial.println("Enter SETUP mode.");
digitalWrite(WIFI_STATUS_PIN, HIGH); // Turn the LED off by making the voltage HIGH
AP_Setup();
AP_Loop();
ESP.reset();
}
void checkConfigButton()
{
if (digitalRead(CFG_BUTTON_PIN) == 0)
setupMode();
}
void myConnectedCb() {
#ifdef DEBUG
Serial.println("Connected to MQTT server");
#endif
if (storage.moduleId != 0)
{
pmyMqtt->subscribe("/" + String(storage.moduleId) + "/Sensor.Parameter1");
pmyMqtt->subscribe("/" + String(storage.moduleId) + "/Sensor.Parameter2");
}
}
void myDisconnectedCb() {
DPRINTLN("disconnected. try to reconnect...");
delay(500);
pmyMqtt->connect();
}
void myPublishedCb() {
DPRINTLN("published.");
}
void myTimeoutCb() {
DPRINTLN("MQTT timeout.");
}
void myDataCb(String& topic, String& data) {
DPRINT("Received topic:");
DPRINTLN(topic);
DPRINT(": ");
DPRINTLN(data);
if (topic == String("/"+String(storage.moduleId)+ "/Sensor.Parameter1"))
{
setTempNew = data.toFloat();
DPRINTLN("New set temp received: " + String(setTempNew));
}
else if (topic == String("/"+String(storage.moduleId)+ "/Sensor.Parameter2"))
{
switchState = (data == String("1"))? true: false;
DPRINTLN("switch state received: " + String(switchState));
}
}
void setupMQTT()
{
pmyMqtt = new MQTT("", EIOT_CLOUD_ADDRESS, 1883);
String clientName;
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
pmyMqtt->setClientId((char*) clientName.c_str());
DPRINT("MQTT client id:");
DPRINTLN(clientName);
// setup callbacks
pmyMqtt->onConnected(myConnectedCb);
pmyMqtt->onDisconnected(myDisconnectedCb);
pmyMqtt->onPublished(myPublishedCb);
pmyMqtt->onData(myDataCb);
pmyMqtt->onWaitOk(checkConfigButton);
DPRINTLN("connect mqtt...");
pmyMqtt->setUserPwd(storage.clUser, storage.clPwd);
pmyMqtt->connect();
delay(500);
}
void subscribeMQTT()
{
DPRINTLN("Suscribe: /"+String(storage.moduleId)+ "/Sensor.Parameter1");
pmyMqtt->subscribe("/"+String(storage.moduleId)+ "/Sensor.Parameter1");
DPRINTLN("Suscribe: /"+String(storage.moduleId)+ "/Sensor.Parameter2");
pmyMqtt->subscribe("/" + String(storage.moduleId) + "/Sensor.Parameter2");
}
void setup() {
#ifdef DEBUG //Macros are usually in all capital letters.
Serial.begin(115200);
delay(1000);
#endif
DPRINTLN("");
DPRINTLN("D0: " + String(D0));
DPRINTLN("D1: " + String(D1));
DPRINTLN("D2: " + String(D2));
DPRINTLN("D3: " + String(D3));
DPRINTLN("D4: " + String(D4));
DPRINTLN("D5: " + String(D5));
DPRINTLN("D6: " + String(D6));
DPRINTLN("D7: " + String(D7));
DS18B20.begin();
pinMode(RELAY_PIN, OUTPUT); // relay pin
digitalWrite(RELAY_PIN, LOW);
pinMode(LED_PIN, OUTPUT); // relay pin
digitalWrite(LED_PIN, LOW);
// config pin
pinMode(CFG_BUTTON_PIN, INPUT);
digitalWrite(CFG_BUTTON_PIN, HIGH);
pinMode(WIFI_STATUS_PIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(WIFI_STATUS_PIN, HIGH); // Turn the LED off by making the voltage HIGH
pinMode(MANUAL_BUTTON_PIN, INPUT_PULLUP);
//digitalWrite(MANUAL_BUTTON_PIN, HIGH);
EEPROM.begin(512);
loadConfig();
setTempNew = storage.setTemp;
switchState = storage.state;
// WIFI
WiFi.setAutoConnect ( true ); // Autoconnect to last known Wifi on startup
WiFi.setAutoReconnect ( true );
WiFi.mode(WIFI_STA);
WiFi.begin(storage.ssid, storage.pwd);
int i = 0;
DPRINT("Connecting to AP");
while (WiFi.status() != WL_CONNECTED && i++ < (AP_CONNECT_TIME*2) ) {
delay(500);
DPRINT(".");
}
DPRINTLN("");
if (!(i < (AP_CONNECT_TIME*2)))
setupMode();
DPRINTLN("WiFi connected");
DPRINT("IP address: ");
DPRINTLN(WiFi.localIP());
DPRINT("ModuleId: ");
DPRINTLN(storage.moduleId);
DPRINTLN("Connecting to MQTT server");
setupMQTT();
if (storage.moduleId == 0)
{
//create module
DPRINTLN("create new module");
storage.moduleId = pmyMqtt->NewModule();
// create parameter
DPRINTLN("create parameter: "+String(PARAM_SET_TEMP));
pmyMqtt->NewModuleParameter(storage.moduleId, PARAM_SET_TEMP);
// set IsCommand
DPRINTLN("set IsCommand parameter: "+String(PARAM_SET_TEMP));
pmyMqtt->SetParameterIsCommand(storage.moduleId, PARAM_SET_TEMP, true);
// set Description
DPRINTLN("set description: "+String(PARAM_SET_TEMP));
pmyMqtt->SetParameterDescription(storage.moduleId, PARAM_SET_TEMP, "Set.temp.");
// set Unit
DPRINTLN("set Unit: "+String(PARAM_SET_TEMP));
pmyMqtt->SetParameterUnit(storage.moduleId, PARAM_SET_TEMP, "°C");
// create parameter
DPRINTLN("create parameter: " + String(PARAM_SWITCH));
pmyMqtt->NewModuleParameter(storage.moduleId, PARAM_SWITCH);
// set IsCommand
DPRINTLN("set IsCommand parameter: " + String(PARAM_SWITCH));
pmyMqtt->SetParameterIsCommand(storage.moduleId, PARAM_SWITCH, true);
// create parameter
DPRINTLN("create parameter: " + String(PARAM_FAN));
pmyMqtt->NewModuleParameter(storage.moduleId, PARAM_FAN);
// set Description
DPRINTLN("set description: "+String(PARAM_FAN));
pmyMqtt->SetParameterDescription(storage.moduleId, PARAM_FAN, "Fan");
// set ParameterDBLogging
// DPRINTLN("set ParameterDBLogging parameter: " + String(PARAM_FAN));
// pmyMqtt->SetParameterDBLogging(storage.moduleId, PARAM_FAN, true);
// set ParameterChartSteps
DPRINTLN("set ParameterChartSteps parameter: " + String(PARAM_FAN));
pmyMqtt->SetParameterChartSteps(storage.moduleId, PARAM_FAN, true);
// create parameter
DPRINTLN("create parameter: "+String(PARAM_ACT_TEMP));
pmyMqtt->NewModuleParameter(storage.moduleId, PARAM_ACT_TEMP);
// set Description
DPRINTLN("set description: "+String(PARAM_ACT_TEMP));
pmyMqtt->SetParameterDescription(storage.moduleId, PARAM_ACT_TEMP, "Act.temp.");
// set Unit
DPRINTLN("set Unit: "+String(PARAM_ACT_TEMP));
pmyMqtt->SetParameterUnit(storage.moduleId, PARAM_ACT_TEMP, "°C");
// set ParameterDBLogging
DPRINTLN("set ParameterDBLogging parameter: " + String(PARAM_ACT_TEMP));
pmyMqtt->SetParameterDBLogging(storage.moduleId, PARAM_ACT_TEMP, true);
DPRINTLN("create parameter: Settings.ShowAdditionalParameters");
pmyMqtt->NewModuleParameter(storage.moduleId, "Settings.ShowAdditionalParameters");
pmyMqtt->SetParameterValue(storage.moduleId, "Settings.ShowAdditionalParameters", "1");
DPRINTLN("create parameter: Settings.SliderMinValue");
pmyMqtt->NewModuleParameter(storage.moduleId, "Settings.SliderMinValue");
pmyMqtt->SetParameterValue(storage.moduleId, "Settings.SliderMinValue", "25");
DPRINTLN("create parameter: Settings.SliderMaxValue");
pmyMqtt->NewModuleParameter(storage.moduleId, "Settings.SliderMaxValue");
pmyMqtt->SetParameterValue(storage.moduleId, "Settings.SliderMaxValue", "35");
// set module type
DPRINTLN("Set module type");
pmyMqtt->SetModuleType(storage.moduleId, "MT_DIMMER");
// save new module id
saveConfig();
}
do {
DS18B20.requestTemperatures();
actTemp = DS18B20.getTempCByIndex(0);
} while (actTemp == 85.0 || actTemp == (-127.0));
DPRINTLN("Temperature: " + String(actTemp));
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_ACT_TEMP, String(actTemp).c_str());
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_FAN , String(fanOld).c_str());
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_SWITCH, String(switchState).c_str());
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_SET_TEMP, String(setTempNew).c_str());
}
void readTemp()
{
float readTemp;
do {
DS18B20.requestTemperatures();
readTemp = DS18B20.getTempCByIndex(0);
} while (readTemp == 85.0 || readTemp == (-127.0));
actTemp += (readTemp - actTemp) / 100;
}
void setRelay()
{
if (fan)
digitalWrite(RELAY_PIN, HIGH);
else
digitalWrite(RELAY_PIN, LOW);
}
// the loop function runs over and over again forever
void loop() {
// WIFI check
if (WiFi.status() == WL_CONNECTED )
//if (pmyMqtt->isConnected() )
digitalWrite(WIFI_STATUS_PIN, LOW); // Turn the LED on (Note that LOW is the voltage level
else
digitalWrite(WIFI_STATUS_PIN, HIGH); // Turn the LED off by making the voltage HIGH
checkConfigButton();
if (WiFi.status() != WL_CONNECTED) { // FIX FOR USING 2.3.0 CORE (only .begin if not connected)
DPRINTLN("AP not connected");
pmyMqtt->onConnected(NULL);
pmyMqtt->onDisconnected(NULL);
pmyMqtt->onPublished(NULL);
pmyMqtt->disconnect();
delay(1000);
delete pmyMqtt;
delay(1000);
WiFi.begin(storage.ssid, storage.pwd);
while (WiFi.status() != WL_CONNECTED) {
int i =0;
DPRINTLN("Connecting to AP");
while (WiFi.status() != WL_CONNECTED && i++ < (AP_CONNECT_TIME*2) ) {
delay(500);
DPRINT(".");
}
}
setupMQTT();
delay(2000);
subscribeMQTT();
delay(2000);
}
if (!pmyMqtt->isConnected())
{
DPRINTLN("Re-connect MQTT");
pmyMqtt->disconnect();
delete pmyMqtt;
pmyMqtt = NULL;
setupMQTT();
subscribeMQTT();
}
// COUNTER
if (milCnt++ >= 100)
{
secCnt++;
readTemp();
}
if (secCnt >= TEMPSENDINTERVAL)
{
digitalWrite(WIFI_STATUS_PIN, HIGH);
delay(50);
secCnt = 0;
DPRINTLN("Temperature: " + String(actTemp));
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_ACT_TEMP, String(actTemp).c_str());
}
// if manual button pressed
if (digitalRead(MANUAL_BUTTON_PIN) == 0)
{
DPRINTLN("manual btn pressed");
while(digitalRead(MANUAL_BUTTON_PIN) == 0){
delay(50);
//DPRINT(".");
}
//DPRINTLN("manual btn pressed OK");
switchState =! storage.state;
}
if (switchState != storage.state)
{
digitalWrite(WIFI_STATUS_PIN, HIGH);
delay(50);
DPRINTLN("publish " + String(PARAM_SWITCH));
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_SWITCH, String(switchState).c_str());
storage.state = switchState;
saveConfig();
}
if (setTempNew != storage.setTemp)
{
digitalWrite(WIFI_STATUS_PIN, HIGH);
delay(50);
DPRINTLN("publish " + String(PARAM_SET_TEMP));
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_SET_TEMP, String(setTempNew).c_str());
storage.setTemp = setTempNew;
saveConfig();
}
if (switchState)
{
digitalWrite(LED_PIN, HIGH);
if (actTemp <= (setTempNew - TEMP_HISTERESE) && fan)
fan = false;
else if (actTemp >= (setTempNew + TEMP_HISTERESE) && !fan)
fan = true;
if (fan != fanOld)
{
DPRINTLN("Temperature: " + String(actTemp));
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_ACT_TEMP, String(actTemp).c_str());
fanOld = fan;
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_FAN , String(fan).c_str());
DPRINTLN("fan " + String(fan));
}
setRelay();
}
else
{
digitalWrite(LED_PIN, LOW);
fan = false;
if (fan != fanOld)
{
fanOld = fan;
pmyMqtt->SetParameterValue(storage.moduleId, PARAM_FAN , String(fan).c_str());
DPRINTLN("fan " + String(fan));
}
setRelay();
}
delay(10);
}