-
Notifications
You must be signed in to change notification settings - Fork 34
/
MQTTstuff.ino
698 lines (607 loc) · 26.6 KB
/
MQTTstuff.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
/*
***************************************************************************
** Program : MQTTstuff
** Version : v0.10.4-alpha
**
** Copyright (c) 2021-2024 Robert van den Breemen
** Modified version from (c) 2020 Willem Aandewiel
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
#include <PubSubClient.h> // MQTT client publish and subscribe functionality
#define MQTTDebugTln(...) ({ if (bDebugMQTT) DebugTln(__VA_ARGS__); })
#define MQTTDebugln(...) ({ if (bDebugMQTT) Debugln(__VA_ARGS__); })
#define MQTTDebugTf(...) ({ if (bDebugMQTT) DebugTf(__VA_ARGS__); })
#define MQTTDebugf(...) ({ if (bDebugMQTT) Debugf(__VA_ARGS__); })
#define MQTTDebugT(...) ({ if (bDebugMQTT) DebugT(__VA_ARGS__); })
#define MQTTDebug(...) ({ if (bDebugMQTT) Debug(__VA_ARGS__); })
// Declare some variables within global scope
static IPAddress MQTTbrokerIP;
static char MQTTbrokerIPchar[20];
static PubSubClient MQTTclient(wifiClient);
int8_t reconnectAttempts = 0;
char lastMQTTtimestamp[15] = "";
enum states_of_MQTT { MQTT_STATE_INIT, MQTT_STATE_TRY_TO_CONNECT, MQTT_STATE_IS_CONNECTED, MQTT_STATE_WAIT_CONNECTION_ATTEMPT, MQTT_STATE_WAIT_FOR_RECONNECT, MQTT_STATE_ERROR };
enum states_of_MQTT stateMQTT = MQTT_STATE_INIT;
String MQTTclientId;
String MQTTPubNamespace = "";
String MQTTSubNamespace = "";
String NodeId = "";
//set command list
struct MQTT_set_cmd_t
{
const char* setcmd;
const char* otgwcmd;
const char* ottype;
};
const MQTT_set_cmd_t setcmds[] {
{ "command", "", "raw" },
{ "setpoint", "TT", "temp" },
{ "constant", "TC", "temp" },
{ "outside", "OT", "temp" },
{ "hotwater", "HW", "on" },
{ "gatewaymode", "GW", "on" },
{ "setback", "SB", "temp" },
{ "maxchsetpt", "SH", "temp" },
{ "maxdhwsetpt", "SW", "temp" },
{ "maxmodulation", "MM", "level" },
{ "ctrlsetpt", "CS", "temp" },
{ "ctrlsetpt2", "C2", "temp" },
{ "chenable", "CH", "on" },
{ "chenable2", "H2", "on" },
{ "ventsetpt", "VS", "level" },
{ "temperaturesensor", "TS", "function" },
{ "addalternative", "AA", "function" },
{ "delalternative", "DA", "function" },
{ "unknownid", "UI", "function" },
{ "knownid", "KI", "function" },
{ "priomsg", "PM", "function" },
{ "setresponse", "SR", "function" },
{ "clearrespons", "CR", "function" },
{ "resetcounter", "RS", "function" },
{ "ignoretransitations", "IT", "function" },
{ "overridehb", "OH", "function" },
{ "forcethermostat", "FT", "function" },
{ "voltageref", "VR", "function" },
{ "debugptr", "DP", "function" },
} ;
const int nrcmds = sizeof(setcmds) / sizeof(setcmds[0]);
// const char learnmsg[] { "LA", "PR=L", "LB", "PR=L", "LC", "PR=L", "LD", "PR=L", "LE", "PR=L", "LF", "PR=L", "GA", "PR=G", "GB", "PR=G", "VR", "PR=V", "GW", "PR=M", "IT", "PR=T", "SB", "PR=S", "HW", "PR=W" } ;
// const int nrlearnmsg = sizeof(learnmsg) / sizeof(learnmsg[0]);
//===========================================================================================
void startMQTT()
{
if (!settingMQTTenable) return;
stateMQTT = MQTT_STATE_INIT;
//setup for mqtt discovery
clearMQTTConfigDone();
NodeId = settingMQTTuniqueid;
MQTTPubNamespace = settingMQTTtopTopic + "/value/" + NodeId;
MQTTSubNamespace = settingMQTTtopTopic + "/set/" + NodeId;
handleMQTT(); //initialize the MQTT statemachine
// handleMQTT(); //then try to connect to MQTT
// handleMQTT(); //now you should be connected to MQTT ready to send
}
bool bHAcycle = false;
// handles MQTT subscribe incoming stuff
void handleMQTTcallback(char* topic, byte* payload, unsigned int length) {
if (bDebugMQTT) {
DebugT("Message arrived on topic ["); Debug(topic); Debug("] = [");
for (unsigned int i = 0; i < length; i++) {
Debug((char)payload[i]);
}
Debug("] ("); Debug(length); Debug(")"); Debugln(); DebugFlush();
}
//detect home assistant going down...
char msgPayload[50];
int msglen = min((int)(length)+1, (int)sizeof(msgPayload));
strlcpy(msgPayload, (char *)payload, msglen);
if (strcasecmp(topic, "homeassistant/status") == 0) {
//incoming message on status, detect going down
if (!settingMQTTharebootdetection) {
//So if the HA reboot detection is turned of, we will just look for HA going online.
//This means everytime there is "online" message, we will restart MQTT configuration, including the HA Auto Discovery.
bHAcycle = true;
}
if (strcasecmp(msgPayload, "offline") == 0){
//home assistant went down
DebugTln(F("Home Assistant went offline!"));
bHAcycle = true; //set flag, so it triggers when it goes back online
} else if ((strcasecmp(msgPayload, "online") == 0) && bHAcycle){
DebugTln(F("Home Assistant went online!"));
bHAcycle = false; //clear flag, so it does not trigger again
//restart stuff, to make sure it works correctly again
startMQTT(); // fixing some issues with hanging HA AutoDiscovery in some scenario's?
} else {
DebugTf(PSTR("Home Assistant Status=[%s] and HA cycle status [%s]\r\n"), msgPayload, CBOOLEAN(bHAcycle));
}
}
// parse the incoming topic and execute commands
char* token;
char otgwcmd[51]={0};
//first check toptopic part, it can include the seperator, e.g. "myHome/OTGW" or "OTGW""
if (strncmp(topic, settingMQTTtopTopic.c_str(), settingMQTTtopTopic.length()) != 0) {
MQTTDebugln(F("MQTT: wrong top topic"));
return;
} else {
//remove the top topic part
MQTTDebugTf(PSTR("Parsing topic: %s/"), settingMQTTtopTopic.c_str());
topic += settingMQTTtopTopic.length();
while (*topic == '/') {
topic++;
}
}
// naming convention /set/<node id>/<command>
token = strtok(topic, "/");
MQTTDebugf("%s/", token);
if (strcasecmp(token, "set") == 0) {
token = strtok(NULL, "/");
MQTTDebugf("%s/", token);
if (strcasecmp(token, CSTR(NodeId)) == 0) {
token = strtok(NULL, "/");
MQTTDebugf("%s", token);
if (token != NULL){
//loop thru command list
int i;
for (i=0; i<nrcmds; i++){
if (strcasecmp(token, setcmds[i].setcmd) == 0){
//found a match
if (strcasecmp(setcmds[i].ottype, "raw") == 0){
//raw command
snprintf(otgwcmd, sizeof(otgwcmd), "%s", msgPayload);
MQTTDebugf(" found command, sending payload [%s]\r\n", otgwcmd);
addOTWGcmdtoqueue((char *)otgwcmd, strlen(otgwcmd), true);
} else {
//all other commands are <otgwcmd>=<payload message>
snprintf(otgwcmd, sizeof(otgwcmd), "%s=%s", setcmds[i].otgwcmd, msgPayload);
MQTTDebugf(" found command, sending payload [%s]\r\n", otgwcmd);
addOTWGcmdtoqueue((char *)otgwcmd, strlen(otgwcmd), true);
}
break; //exit loop
}
}
if (i >= nrcmds){
//no match found
MQTTDebugln();
MQTTDebugTf(PSTR("No match found for command: [%s]\r\n"), token);
}
}
}
}
}
//===========================================================================================
void handleMQTT()
{
if (!settingMQTTenable) return;
DECLARE_TIMER_SEC(timerMQTTwaitforconnect, 42, CATCH_UP_MISSED_TICKS); // wait before trying to connect again
DECLARE_TIMER_SEC(timerMQTTwaitforretry, 3, CATCH_UP_MISSED_TICKS); // wait for retry
//State debug timers
DECLARE_TIMER_SEC(timerMQTTdebugwaitforreconnect, 13);
DECLARE_TIMER_SEC(timerMQTTdebugerrorstate, 13);
DECLARE_TIMER_SEC(timerMQTTdebugwaitconnectionattempt, 1);
DECLARE_TIMER_SEC(timerMQTTdebugisconnected, 60);
if (MQTTclient.connected()) MQTTclient.loop(); //always do a MQTTclient.loop() first
switch(stateMQTT)
{
case MQTT_STATE_INIT:
MQTTDebugTln(F("MQTT State: MQTT Initializing"));
WiFi.hostByName(CSTR(settingMQTTbroker), MQTTbrokerIP); // lookup the MQTTbroker convert to IP
sprintf(MQTTbrokerIPchar, "%d.%d.%d.%d", MQTTbrokerIP[0], MQTTbrokerIP[1], MQTTbrokerIP[2], MQTTbrokerIP[3]);
if (isValidIP(MQTTbrokerIP))
{
MQTTDebugTf(PSTR("[%s] => setServer(%s, %d)\r\n"), CSTR(settingMQTTbroker), MQTTbrokerIPchar, settingMQTTbrokerPort);
MQTTclient.disconnect();
MQTTclient.setServer(MQTTbrokerIPchar, settingMQTTbrokerPort);
MQTTclient.setCallback(handleMQTTcallback);
MQTTclient.setSocketTimeout(4);
MQTTclientId = String(_HOSTNAME) + WiFi.macAddress();
//skip try to connect
reconnectAttempts =0;
stateMQTT = MQTT_STATE_TRY_TO_CONNECT;
}
else
{ // invalid IP, then goto error state
MQTTDebugTf(PSTR("ERROR: [%s] => is not a valid URL\r\n"), CSTR(settingMQTTbroker));
stateMQTT = MQTT_STATE_ERROR;
//DebugTln(F("Next State: MQTT_STATE_ERROR"));
}
RESTART_TIMER(timerMQTTwaitforconnect);
break;
case MQTT_STATE_TRY_TO_CONNECT:
MQTTDebugTln(F("MQTT State: MQTT try to connect"));
MQTTDebugTf(PSTR("MQTT server is [%s], IP[%s]\r\n"), settingMQTTbroker.c_str(), MQTTbrokerIPchar);
MQTTDebugT(F("Attempting MQTT connection .. "));
reconnectAttempts++;
//If no username, then anonymous connection to broker, otherwise assume username/password.
if (settingMQTTuser.length() == 0)
{
MQTTDebug(F("without a Username/Password "));
if(!MQTTclient.connect(CSTR(MQTTclientId), CSTR(MQTTPubNamespace), 0, true, "offline")) PrintMQTTError();
}
else
{
MQTTDebugf("Username [%s] ", CSTR(settingMQTTuser));
if(!MQTTclient.connect(CSTR(MQTTclientId), CSTR(settingMQTTuser), CSTR(settingMQTTpasswd), CSTR(MQTTPubNamespace), 0, true, "offline")) PrintMQTTError();
}
//If connection was made succesful, move on to next state...
if (MQTTclient.connected())
{
reconnectAttempts = 0;
MQTTDebugln(F(" .. connected\r"));
Debugln(F("MQTT connected"));
stateMQTT = MQTT_STATE_IS_CONNECTED;
MQTTDebugTln(F("Next State: MQTT_STATE_IS_CONNECTED"));
// birth message, sendMQTT retains by default
sendMQTT(CSTR(MQTTPubNamespace), "online");
// First do AutoConfiguration for Homeassistant
// doAutoConfigure();
//Subscribe to topics
char topic[100];
strcpy(topic, CSTR(MQTTSubNamespace));
strlcat(topic, "/#", sizeof(topic));
MQTTDebugTf(PSTR("Subscribe to MQTT: TopicId [%s]\r\n"), topic);
if (MQTTclient.subscribe(topic)){
MQTTDebugTf(PSTR("MQTT: Subscribed successfully to TopicId [%s]\r\n"), topic);
}
else
{
MQTTDebugTf(PSTR("MQTT: Subscribe TopicId [%s] FAILED! \r\n"), topic);
PrintMQTTError();
}
MQTTclient.subscribe("homeassistant/status"); //start monitoring the status of homeassistant, if it goes down, then force a restart after it comes back online.
sendMQTTversioninfo();
}
else
{ // no connection, try again, do a non-blocking wait for 3 seconds.
MQTTDebugln(F(" .. \r"));
MQTTDebugTf(PSTR("failed, retrycount=[%d], rc=[%d] .. try again in 3 seconds\r\n"), reconnectAttempts, MQTTclient.state());
RESTART_TIMER(timerMQTTwaitforretry);
stateMQTT = MQTT_STATE_WAIT_CONNECTION_ATTEMPT; // if the re-connect did not work, then return to wait for reconnect
MQTTDebugTln(F("Next State: MQTT_STATE_WAIT_CONNECTION_ATTEMPT"));
}
//After 5 attempts... go wait for a while.
if (reconnectAttempts >= 5)
{
MQTTDebugTln(F("5 attempts have failed. Retry wait for next reconnect in 10 minutes\r"));
RESTART_TIMER(timerMQTTwaitforconnect);
stateMQTT = MQTT_STATE_WAIT_FOR_RECONNECT; // if the re-connect did not work, then return to wait for reconnect
MQTTDebugTln(F("Next State: MQTT_STATE_WAIT_FOR_RECONNECT"));
}
break;
case MQTT_STATE_IS_CONNECTED:
if DUE(timerMQTTdebugisconnected) MQTTDebugTln(F("MQTT State: MQTT is Connected"));
if (MQTTclient.connected())
{ //if the MQTT client is connected, then please do a .loop call...
MQTTclient.loop();
}
else
{ //else go and wait 10 minutes, before trying again.
RESTART_TIMER(timerMQTTwaitforconnect);
stateMQTT = MQTT_STATE_WAIT_FOR_RECONNECT;
MQTTDebugTln(F("Next State: MQTT_STATE_WAIT_FOR_RECONNECT"));
}
break;
case MQTT_STATE_WAIT_CONNECTION_ATTEMPT:
//do non-blocking wait for 3 seconds
if DUE(timerMQTTdebugwaitconnectionattempt) MQTTDebugTln(F("MQTT State: MQTT_WAIT_CONNECTION_ATTEMPT"));
if (DUE(timerMQTTwaitforretry))
{
//Try again... after waitforretry non-blocking delay
stateMQTT = MQTT_STATE_TRY_TO_CONNECT;
MQTTDebugTln(F("Next State: MQTT_STATE_TRY_TO_CONNECT"));
}
break;
case MQTT_STATE_WAIT_FOR_RECONNECT:
//do non-blocking wait for 10 minutes, then try to connect again.
if DUE(timerMQTTdebugwaitforreconnect) MQTTDebugTln(F("MQTT State: MQTT wait for reconnect"));
if (DUE(timerMQTTwaitforconnect))
{
//remember when you tried last time to reconnect
RESTART_TIMER(timerMQTTwaitforretry);
reconnectAttempts = 0;
stateMQTT = MQTT_STATE_TRY_TO_CONNECT;
MQTTDebugTln(F("Next State: MQTT_STATE_TRY_TO_CONNECT"));
}
break;
case MQTT_STATE_ERROR:
if DUE(timerMQTTdebugerrorstate) MQTTDebugTln(F("MQTT State: MQTT ERROR, wait for 10 minutes, before trying again"));
//wait for next retry
RESTART_TIMER(timerMQTTwaitforconnect);
stateMQTT = MQTT_STATE_WAIT_FOR_RECONNECT;
MQTTDebugTln(F("Next State: MQTT_STATE_WAIT_FOR_RECONNECT"));
break;
default:
MQTTDebugTln(F("MQTT State: default, this should NEVER happen!"));
//do nothing, this state should not happen
stateMQTT = MQTT_STATE_INIT;
DebugTln(F("Next State: MQTT_STATE_INIT"));
break;
}
statusMQTTconnection = MQTTclient.connected();
} // handleMQTT()
//===========================================================================================
String trimVal(char *in)
{
String Out = in;
Out.trim();
return Out;
} // trimVal()
void PrintMQTTError(){
MQTTDebugln();
switch (MQTTclient.state())
{
case MQTT_CONNECTION_TIMEOUT : MQTTDebugTln(F("Error: MQTT connection timeout"));break;
case MQTT_CONNECTION_LOST : MQTTDebugTln(F("Error: MQTT connections lost"));break;
case MQTT_CONNECT_FAILED : MQTTDebugTln(F("Error: MQTT connection failed"));break;
case MQTT_DISCONNECTED : MQTTDebugTln(F("Error: MQTT disconnected"));break;
case MQTT_CONNECTED : MQTTDebugTln(F("Error: MQTT connected"));break;
case MQTT_CONNECT_BAD_PROTOCOL : MQTTDebugTln(F("Error: MQTT connect bad protocol"));break;
case MQTT_CONNECT_BAD_CLIENT_ID : MQTTDebugTln(F("Error: MQTT connect bad client id"));break;
case MQTT_CONNECT_UNAVAILABLE : MQTTDebugTln(F("Error: MQTT connect unavailable"));break;
case MQTT_CONNECT_BAD_CREDENTIALS: MQTTDebugTln(F("Error: MQTT connect bad credentials"));break;
case MQTT_CONNECT_UNAUTHORIZED : MQTTDebugTln(F("Error: MQTT connect unauthorized"));break;
default: MQTTDebugTln(F("Error: MQTT unknown error"));
}
}
/*
topic: <string> , sensor topic, will be automatically prefixed with <mqtt topic>/value/<node_id>
json: <string> , payload to send
retain: <bool> , retain mqtt message
*/
void sendMQTTData(const String topic, const String json, const bool retain = false)
{
if (!settingMQTTenable) return;
sendMQTTData(CSTR(topic), CSTR(json), retain);
}
/*
topic: <string> , sensor topic, will be automatically prefixed with <mqtt topic>/value/<node_id>
json: <string> , payload to send
retain: <bool> , retain mqtt message
*/
void sendMQTTData(const char* topic, const char *json, const bool retain = false)
{
if (!settingMQTTenable) return;
if (!MQTTclient.connected()) {DebugTln(F("Error: MQTT broker not connected.")); PrintMQTTError(); return;}
if (!isValidIP(MQTTbrokerIP)) {DebugTln(F("Error: MQTT broker IP not valid.")); return;}
char full_topic[100];
snprintf(full_topic, sizeof(full_topic), "%s/", CSTR(MQTTPubNamespace));
strlcat(full_topic, topic, sizeof(full_topic));
MQTTDebugTf(PSTR("Sending MQTT: server %s:%d => TopicId [%s] --> Message [%s]\r\n"), settingMQTTbroker.c_str(), settingMQTTbrokerPort, full_topic, json);
if (!MQTTclient.publish(full_topic, json, retain)) PrintMQTTError();
feedWatchDog();//feed the dog
} // sendMQTTData()
/*
* topic: <string> , topic will be used as is (no prefixing), retained = true
* json: <string> , payload to send
*/
//===========================================================================================
void sendMQTT(String topic, String json){
if (!settingMQTTenable) return;
sendMQTT(CSTR(topic), CSTR(json), json.length());
}
void sendMQTT(const char* topic, const char *json, const size_t len)
{
if (!settingMQTTenable) return;
if (!MQTTclient.connected()) {DebugTln(F("Error: MQTT broker not connected.")); PrintMQTTError(); return;}
if (!isValidIP(MQTTbrokerIP)) {DebugTln(F("Error: MQTT broker IP not valid.")); return;}
MQTTDebugTf(PSTR("Sending MQTT: server %s:%d => TopicId [%s] --> Message [%s]\r\n"), settingMQTTbroker.c_str(), settingMQTTbrokerPort, topic, json);
if (MQTTclient.getBufferSize() < len) MQTTclient.setBufferSize(len); //resize buffer when needed
if (MQTTclient.beginPublish(topic, len, true)){
for (size_t i = 0; i<len; i++) {
if(!MQTTclient.write(json[i])) PrintMQTTError();
}
MQTTclient.endPublish();
} else PrintMQTTError();
feedWatchDog();
} // sendMQTTData()
//===========================================================================================
void resetMQTTBufferSize()
{
if (!settingMQTTenable) return;
MQTTclient.setBufferSize(256);
}
//===========================================================================================
bool splitLine(String sIn, char del, byte &cID, String &cKey, String &cVal) {
sIn.trim(); //trim spaces
cID = 39; // ID 39 is unused in the OT spec
cKey = "";
cVal = "";
int posC = sIn.indexOf("//");
if (posC > -1) sIn.remove(posC); // strip comments
if (sIn.length() <= 3) return false; //not enough buffer, skip split
unsigned int pos = sIn.indexOf(del); //determine first split point
if ((pos <= 0) || (pos == (sIn.length() - 1))) return false; // no key or no value
unsigned int pos2 = sIn.indexOf(del, pos+1); //determine second split point, starting from the first found
if ((pos2 <= 0) || (pos2 <= pos) || (pos2 == (sIn.length() - 1))) return false; // no key or no value
String sID = sIn.substring(0, pos);
sID.trim();
cID = (byte)sID.toInt();
cKey = sIn.substring(pos+1, pos2);
cKey.trim(); //before, and trim spaces
cVal = sIn.substring(pos2 + 1);
cVal.trim(); //after,and trim spaces
// Debugf("Split line into: [%d] [%s] [%s]\r\n", cID, CSTR(cKey), CSTR(cVal)); DebugFlush();
return true;
}
//===========================================================================================
bool getMQTTConfigDone(const uint8_t MSGid)
{
uint8_t group = MSGid & 0b11100000;
group = group>>5;
uint8_t index = MSGid & 0b00011111;
uint32_t result = bitRead(MQTTautoConfigMap[group], index);
MQTTDebugTf(PSTR("Reading bit %d from group %d for MSGid %d: result = %d\r\n"), index, group, MSGid, result);
if (result > 0) {
return true;
} else {
return false;
}
}
//===========================================================================================
bool setMQTTConfigDone(const uint8_t MSGid)
{
uint8_t group = MSGid & 0b11100000;
group = group>>5;
uint8_t index = MSGid & 0b00011111;
MQTTDebugTf(PSTR("Setting bit %d from group %d for MSGid %d\r\n"), index, group, MSGid);
MQTTDebugTf(PSTR("Value before setting bit %d\r\n"), MQTTautoConfigMap[group]);
if(bitSet(MQTTautoConfigMap[group], index) > 0) {
MQTTDebugTf(PSTR("Value after setting bit %d\r\n"), MQTTautoConfigMap[group]);
return true;
} else {
return false;
}
}
//===========================================================================================
void clearMQTTConfigDone()
{
memset(MQTTautoConfigMap, 0, sizeof(MQTTautoConfigMap));
}
//===========================================================================================
void doAutoConfigure(bool bForcaAll = false){
//force all sensors to be sent to auto configuration
for (int i=0; i<255; i++){
if ((getMQTTConfigDone((byte)i)==true) || bForcaAll) {
MQTTDebugTf(PSTR("Sending auto configuration for sensor %d\r\n"), i);
doAutoConfigureMsgid((byte)i);
doBackgroundTasks();
}
}
// bool success = doAutoConfigure("config"); // the string "config" should match every line non-comment in mqttha.cfg
}
//===========================================================================================
bool doAutoConfigureMsgid(byte OTid)
{
String cfgSensorId = "" ;
// check if foney dataid is called to do autoconfigure for temp sensors, call configsensors instead
if (OTid == OTGWdallasdataid) {
MQTTDebugTf(PSTR("Sending auto configuration for temp sensors %d\r\n"), OTid);
configSensors() ;
return true;
}
else return doAutoConfigureMsgid(OTid, cfgSensorId);
}
bool doAutoConfigureMsgid(byte OTid, String cfgSensorId )
{
bool _result = false;
if (!settingMQTTenable) {
return _result;
}
if (!MQTTclient.connected()) {
DebugTln(F("Error: MQTT broker not connected."));
return _result;
}
if (!isValidIP(MQTTbrokerIP)) {
DebugTln(F("Error: MQTT broker IP not valid."));
return _result;
}
byte lineID = 39; // 39 is unused in OT protocol so is a safe value
String sMsg = "";
String sTopic = "";
//Let's open the MQTT autoconfig file
File fh; //filehandle
const char *cfgFilename = "/mqttha.cfg";
LittleFS.begin();
if (!LittleFS.exists(cfgFilename)) {
DebugTln(F("Error: confuration file not found."));
return _result;
}
fh = LittleFS.open(cfgFilename, "r");
if (!fh) {
DebugTln(F("Error: could not open confuration file."));
return _result;
}
//Lets go read the config and send it out to MQTT line by line
while (fh.available())
{
//read file line by line, split and send to MQTT (topic, msg)
feedWatchDog(); //start with feeding the dog
String sLine = fh.readStringUntil('\n');
// DebugTf(PSTR("sline[%s]\r\n"), CSTR(sLine));
if (!splitLine(sLine, ';', lineID, sTopic, sMsg)) { //splitLine() also filters comments
//MQTTDebugTf(PSTR("Either comment or invalid config line: [%s]\r\n"), CSTR(sLine));
continue;
}
// DebugTf(PSTR("looking in config file line for %d: [%d][%s] \r\n"), OTid, lineID, CSTR(sTopic));
// check if this is the specific line we are looking for
if (lineID != OTid) continue;
MQTTDebugTf(PSTR("Found line in config file for %d: [%d][%s] \r\n"), OTid, lineID, CSTR(sTopic));
// discovery topic prefix
MQTTDebugTf(PSTR("sTopic[%s]==>"), CSTR(sTopic));
sTopic.replace("%homeassistant%", CSTR(settingMQTThaprefix));
/// node
sTopic.replace("%node_id%", CSTR(NodeId));
/// SensorId
sTopic.replace("%sensor_id%", CSTR(cfgSensorId));
MQTTDebugf("[%s]\r\n", CSTR(sTopic));
/// ----------------------
MQTTDebugTf(PSTR("sMsg[%s]==>"), CSTR(sMsg));
/// node
sMsg.replace("%node_id%", CSTR(NodeId));
/// SensorId
sMsg.replace("%sensor_id%", CSTR(cfgSensorId));
/// hostname
sMsg.replace("%hostname%", CSTR(settingHostname));
/// version
sMsg.replace("%version%", _VERSION);
// pub topics prefix
sMsg.replace("%mqtt_pub_topic%", CSTR(MQTTPubNamespace));
// sub topics
sMsg.replace("%mqtt_sub_topic%", CSTR(MQTTSubNamespace));
MQTTDebugf("[%s]\r\n", CSTR(sMsg));
DebugFlush();
//sendMQTT(CSTR(sTopic), CSTR(sMsg), (sTopic.length() + sMsg.length()+2));
sendMQTT(sTopic, sMsg);
resetMQTTBufferSize();
// delay(10);
_result = true;
// TODO: enable this break if we are sure the old config dump method is no longer needed
// break;
} // while available()
fh.close();
// HA discovery msg's are rather large, reset the buffer size to release some memory
resetMQTTBufferSize();
return _result;
}
void sensorAutoConfigure(byte dataid, bool finishflag , String cfgSensorId = "") {
// Special version of Autoconfigure for sensors
// dataid is a foney id, not used by OT
// check wheter MQTT topic needs to be configured
// cfgNodeId can be set to alternate NodeId to allow for multiple temperature sensors, should normally be NodeId
// When finishflag is true, check on dataid is already done and complete the config. On false do the config and leave completion to caller
if(getMQTTConfigDone(dataid)==false or !finishflag) {
MQTTDebugTf(PSTR("Need to set MQTT config for sensor id(%d)\r\n"),dataid);
bool success = doAutoConfigureMsgid(dataid,cfgSensorId);
if(success) {
MQTTDebugTf(PSTR("Successfully sent MQTT config for sensor id(%d)\r\n"),dataid);
if (finishflag) setMQTTConfigDone(dataid);
} else {
MQTTDebugTf(PSTR("Not able to complete MQTT configuration for sensor id(%d)\r\n"),dataid);
}
} else {
// MQTTDebugTf("No need to set MQTT config for sensor id(%d)\r\n",dataid);
}
}
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/