-
Notifications
You must be signed in to change notification settings - Fork 3
/
DashioESP.cpp
1061 lines (900 loc) · 33.5 KB
/
DashioESP.cpp
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
/*
MIT License
Copyright (c) 2021 Craig Tuffnell, DashIO Connect Limited
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.
*/
#if defined ESP32 || defined ESP8266
#include "DashioESP.h"
#define WIFI_TIMEOUT_S 300 // Restart after 5 minutes
#ifdef ESP32
#define C64_MAX_LENGHT 1000
#elif ESP8266
#define C64_MAX_LENGHT 500
#endif
const int INCOMING_BUFFER_SIZE = 512;
// MQTT
const int MQTT_QOS = 2;
const int MQTT_RETRY_S = 10; // Retry after 10 seconds
const int MQTT_CLIENT_BUFFER_SIZE = 2048;
const int MQTT_SEND_WAIT_MS = 1000;
const int MQTT_SEND_BUFFER_MIN = 1024;
// BLE
const int BLE_MAX_SEND_MESSAGE_LENGTH = 185; // 185 for iPhone 6, but can be up to 517
// ---------------------------------------- WiFi ---------------------------------------
bool DashioWiFi::oneSecond = false;
DashioWiFi::DashioWiFi(DashioDevice *_dashioDevice) {
dashioDevice = _dashioDevice;
#ifdef ESP32
xTaskCreatePinnedToCore(this->wifiOneSecondTask, "OneSecTask", 2048, this, 0, &wifiOneSecTaskHandle, 0);
#elif ESP8266
timer.every(1000, onTimerCallback); // 1000ms
#endif
}
#ifdef ESP32
void DashioWiFi::wifiOneSecondTask(void *parameter) {
for(;;) {
oneSecond = true;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
#elif ESP8266
bool DashioWiFi::onTimerCallback(void *argument) { // Timer Interrupt
oneSecond = true;
return true; // to repeat the timer action - false to stop
}
#endif
void DashioWiFi::attachConnection(DashioTCP *_tcpConnection) {
tcpConnection = _tcpConnection;
}
void DashioWiFi::detachTcp() {
tcpConnection = nullptr;
}
void DashioWiFi::attachConnection(DashioMQTT *_mqttConnection) {
mqttConnection = _mqttConnection;
}
void DashioWiFi::detachMqtt() {
mqttConnection = nullptr;
}
void DashioWiFi::setOnConnectCallback(void (*connectCallback)(void)) {
wifiConnectCallback = connectCallback;
}
void DashioWiFi::begin(char *ssid, char *password) {
// Below is required to get WiFi to connect reliably on ESP32
WiFi.disconnect(true);
delay(1000);
WiFi.mode(WIFI_STA);
delay(1000);
WiFi.begin(ssid, password);
wifiConnectCount = 1;
}
void DashioWiFi::run() {
#ifdef ESP8266
timer.tick();
#endif
if (mqttConnection != nullptr) {
mqttConnection->run();
}
if (tcpConnection != nullptr) {
tcpConnection->run();
}
if (oneSecond) {
oneSecond = false;
// First, check WiFi and connect if necessary
if (WiFi.status() != WL_CONNECTED) {
wifiConnectCount++;
Serial.print(F("Connecting to Wi-Fi "));
Serial.println(String(wifiConnectCount));
if (mqttConnection != nullptr) {
mqttConnection->state = notReady;
}
if (wifiConnectCount > WIFI_TIMEOUT_S) { // If too many fails, restart the ESP32. Sometimes ESP32's WiFi gets tied up in a knot.
if (dashioDevice != nullptr) {
dashioDevice->onStatusCallback(wifiDisconnected);
} else if (mqttConnection != nullptr) { // TODO??? can remove in future
mqttConnection->dashioDevice->onStatusCallback(wifiDisconnected);
} else if (tcpConnection != nullptr) { // TODO??? can remove in future
tcpConnection->dashioDevice->onStatusCallback(wifiDisconnected);
}
ESP.restart();
}
} else {
// WiFi OK
if (wifiConnectCount > 0) {
wifiConnectCount = 0; // So that we only execute the following once after WiFI connection
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
if (wifiConnectCallback != nullptr) { // Deprtecated
wifiConnectCallback();
}
if (dashioDevice != nullptr) {
dashioDevice->onStatusCallback(wifiConnected);
} else if (mqttConnection != nullptr) { // TODO??? can remove in future
mqttConnection->dashioDevice->onStatusCallback(wifiConnected);
} else if (tcpConnection != nullptr) { // TODO??? can remove in future
tcpConnection->dashioDevice->onStatusCallback(wifiConnected);
}
if (tcpConnection != nullptr) {
tcpConnection->begin();
tcpConnection->setupmDNSservice(macAddress());
}
if (mqttConnection != nullptr) {
mqttConnection->begin();
}
}
if (mqttConnection != nullptr) {
#ifdef ESP32
if (mqttConnection->esp32_mqtt_blocking) {
mqttConnection->checkConnection();
}
#elif ESP8266
mqttConnection->checkConnection();
#endif
}
}
}
}
void DashioWiFi::end() {
if (tcpConnection != nullptr) {
tcpConnection->end();
}
if (mqttConnection != nullptr) {
mqttConnection->end();
}
WiFi.disconnect();
}
String DashioWiFi::macAddress() {
#ifdef ESP32
#if ESP_IDF_VERSION_MAJOR >= 5
return Network.macAddress();
#else
return WiFi.macAddress();
#endif
#elif ESP8266
return WiFi.macAddress();
#endif
}
String DashioWiFi::ipAddress() {
IPAddress ip = WiFi.localIP();
static char ipStr[16];
sprintf(ipStr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
return ipStr;
}
// --------------------------------------- Soft AP -------------------------------------
bool DashioSoftAP::begin(const String& password) {
IPAddress IP = {192, 168, 68, 100};
IPAddress NMask = {255, 255, 255, 0};
Serial.println(F("Starting soft-AP"));
#ifdef ESP32
bool result = WiFi.softAP("Dash_Provision", password.c_str(), 1, 0, 1);
#elif ESP8266
bool result = WiFi.softAP(F("Dash_Provision"), password, 1, 0, 1);
#endif
if (result == true) {
WiFi.softAPConfig(IP, IP, NMask);
Serial.print(F("AP IP address: "));
Serial.println(WiFi.softAPIP());
if (tcpConnection != nullptr) {
originalTCPport = tcpConnection->tcpPort;
tcpConnection->setPort(SOFT_AP_PORT);
tcpConnection->begin();
}
}
return result;
}
void DashioSoftAP::attachConnection(DashioTCP *_tcpConnection) {
tcpConnection = _tcpConnection;
}
void DashioSoftAP::end() {
WiFi.softAPdisconnect(false); // false = turn off soft AP
tcpConnection->setPort(originalTCPport);
}
bool DashioSoftAP::isConnected() {
return (WiFi.softAPgetStationNum() > 0);
}
void DashioSoftAP::run() {
if (tcpConnection != nullptr) {
tcpConnection->run();
}
}
// ---------------------------------------- TCP ----------------------------------------
#ifdef ESP32
DashioTCP::DashioTCP(DashioDevice *_dashioDevice, bool _printMessages, uint16_t _tcpPort, uint8_t _maxTCPclients) {
dashioDevice = _dashioDevice;
tcpPort = _tcpPort;
printMessages = _printMessages;
wifiServer = WiFiServer(_tcpPort);
maxTCPclients = _maxTCPclients;
tcpClients = new TCPclient[_maxTCPclients];
}
#elif ESP8266
DashioTCP::DashioTCP(DashioDevice *_dashioDevice, bool _printMessages, uint16_t _tcpPort, uint8_t _maxTCPclients) : wifiServer(_tcpPort) {
dashioDevice = _dashioDevice;
tcpPort = _tcpPort;
printMessages = _printMessages;
maxTCPclients = _maxTCPclients;
tcpClients = new TCPclient[_maxTCPclients];
}
#endif
void DashioTCP::setCallback(void (*processIncomingMessage)(MessageData *messageData)) {
processTCPmessageCallback = processIncomingMessage;
}
void DashioTCP::setPort(uint16_t _tcpPort) {
tcpPort = _tcpPort;
}
void DashioTCP::begin() {
wifiServer.begin(tcpPort);
}
uint8_t DashioTCP::hasClient() {
uint8_t rVal = 0;
for (int i = 0; i < maxTCPclients; i++) {
TCPclient *tcpClientPtr = &tcpClients[i];
if (tcpClientPtr->client) {
rVal = tcpClientPtr->client.connected();
}
}
return rVal;
}
void DashioTCP::sendMessage(const String& message, uint8_t index) {
if (index < maxTCPclients) {
WiFiClient *clientPtr = &tcpClients[index].client;
if (clientPtr->connected()) {
clientPtr->print(message);
if (printMessages) {
Serial.println(F("---- TCP Sent ----"));
Serial.println(message);
}
}
}
}
void DashioTCP::sendMessage(const String& message) {
for (int i = 0; i < maxTCPclients; i++) {
sendMessage(message, i);
}
}
void DashioTCP::setupmDNSservice(const String& id) {
char charBuf[id.length()];
id.toCharArray(charBuf, id.length() + 1);
if (!MDNS.begin(charBuf)) {
if (printMessages) {
Serial.println(F("Error starting mDNS"));
}
return;
}
if (printMessages) {
Serial.print(F("mDNS started: "));
Serial.println(String(tcpPort));
}
MDNS.addService("DashIO", "tcp", tcpPort);
}
void DashioTCP::end() {
for (int i = 0; i < maxTCPclients; i++) {
if (tcpClients[i].client) {
tcpClients[i].client.stop();
}
}
#ifdef ESP8266
MDNS.close();
#endif
MDNS.end();
}
void DashioTCP::processConfig(uint16_t index) {
sendMessage(dashioDevice->getC64ConfigBaseMessage(), index);
int c64Length = strlen_P(dashioDevice->configC64Str);
int length = 0;
String message = "";
for (int k = 0; k < c64Length; k++) {
char myChar = pgm_read_byte_near(dashioDevice->configC64Str + k);
message += myChar;
length++;
if (length == C64_MAX_LENGHT) {
sendMessage(message, index);
message = "";
length = 0;
}
}
message += String(END_DELIM);
sendMessage(message, index);
}
bool DashioTCP::checkTCP(int index) {
TCPclient *tcpClientPtr = &tcpClients[index];
if (tcpClientPtr->client.connected()) {
while (tcpClientPtr->client.available()>0) {
char c = tcpClientPtr->client.read();
if (tcpClientPtr->data.processChar(c)) {
tcpClientPtr->data.connectionHandle = index; // So we have a reference to the index in the MessageData
if (printMessages) {
Serial.println(tcpClientPtr->data.getReceivedMessageForPrint(dashioDevice->getControlTypeStr(tcpClientPtr->data.control)));
}
switch (tcpClientPtr->data.control) {
case who:
sendMessage(dashioDevice->getWhoMessage(), index);
break;
case connect:
sendMessage(dashioDevice->getConnectMessage(), index);
break;
case config:
dashioDevice->dashboardID = tcpClientPtr->data.idStr;
if (dashioDevice->configC64Str != nullptr) {
processConfig(index);
} else {
if (processTCPmessageCallback != nullptr) {
processTCPmessageCallback(&tcpClientPtr->data);
}
}
break;
default:
if (processTCPmessageCallback != nullptr) {
processTCPmessageCallback(&tcpClientPtr->data);
}
break;
}
}
}
return true;
} else {
return false;
}
}
void DashioTCP::run() {
WiFiClient newClient = wifiServer.accept();
if (newClient) {
for (int i = 0; i < maxTCPclients; ++i) {
if (!tcpClients[i].client) {
newClient.setTimeout(2000);
tcpClients[i].client = newClient;
break;
}
}
}
for (int i = 0; i < maxTCPclients; i++) {
if (!checkTCP(i)) {
if (tcpClients[i].client) {
tcpClients[i].client.stop(); // This doesn't every get called
// tcpClients[i].client = 0;
}
}
}
#ifdef ESP8266
MDNS.update();
#endif
}
// ---------------------------------------- MQTT ---------------------------------------
DashioMQTT::DashioMQTT(DashioDevice *_dashioDevice, bool _sendRebootAlarm, bool _printMessages) : mqttClient(MQTT_CLIENT_BUFFER_SIZE) {
dashioDevice = _dashioDevice;
sendRebootAlarm = _sendRebootAlarm;
printMessages = _printMessages;
#ifdef ESP32
xTaskCreatePinnedToCore(this->checkConnectionTask, "CheckConnTask", 10000, this, 0, &mqttConnectTaskHandle, 0);
#endif
}
DashioMQTT::DashioMQTT(DashioDevice *_dashioDevice, bool _sendRebootAlarm, bool _printMessages, int _mqttBufferSize) : mqttClient(MQTT_CLIENT_BUFFER_SIZE) {
dashioDevice = _dashioDevice;
sendRebootAlarm = _sendRebootAlarm;
printMessages = _printMessages;
if (_mqttBufferSize >= MQTT_SEND_BUFFER_MIN) {
mqttBuffersize = _mqttBufferSize;
mqttSendBuffer.reserve(_mqttBufferSize);
}
#ifdef ESP32
xTaskCreatePinnedToCore(this->checkConnectionTask, "TasCheckConnTaskk1", 10000, this, 0, &mqttConnectTaskHandle, 0);
#endif
}
MessageData DashioMQTT::data(MQTT_CONN, INCOMING_BUFFER_SIZE);
void DashioMQTT::messageReceivedMQTTCallback(MQTTClient *client, char *topic, char *payload, int payload_length) {
data.processMessage(String(payload)); // The message components are stored within the connection where the messageReceived flag is set
}
void DashioMQTT::checkAndSendMQTTbuffer() {
if (mqttSendBuffer.length() > 0) {
unsigned long timeNow = millis();
long timeSinceLastMessage = timeNow - lastSentMessageTime;
if (timeSinceLastMessage < 0) {timeSinceLastMessage = MQTT_SEND_WAIT_MS;}
if (timeSinceLastMessage >= MQTT_SEND_WAIT_MS) {
lastSentMessageTime = timeNow;
publishMessage(mqttSendBuffer, data_topic);
mqttSendBuffer.clear();
}
}
}
void DashioMQTT::publishMessage(const String& message, MQTTTopicType topic) {
if (mqttClient.connected()) {
String publishTopic = dashioDevice->getMQTTTopic(username, topic);
mqttClient.publish(publishTopic.c_str(), message.c_str(), false, MQTT_QOS);
if (printMessages) {
Serial.print(F("---- MQTT Sent ---- Topic: "));
Serial.println(publishTopic);
Serial.println(message);
}
}
}
void DashioMQTT::sendMessage(const String& message, MQTTTopicType topic) {
if (mqttBuffersize >= MQTT_SEND_BUFFER_MIN && topic == data_topic) {
if (message.length() < (mqttBuffersize - mqttSendBuffer.length())) {
mqttSendBuffer += message;
checkAndSendMQTTbuffer();
}
} else {
publishMessage(message, topic);
}
}
void DashioMQTT::sendAlarmMessage(const String& message) {
sendMessage(message, alarm_topic);
}
void DashioMQTT::sendWhoAnnounce() {
sendMessage(dashioDevice->getWhoMessage(), announce_topic);
}
void DashioMQTT::processConfig() {
sendMessage(dashioDevice->getC64ConfigBaseMessage());
int c64Length = strlen_P(dashioDevice->configC64Str);
int length = 0;
String message = "";
for (int k = 0; k < c64Length; k++) {
char myChar = pgm_read_byte_near(dashioDevice->configC64Str + k);
message += myChar;
length++;
if (length == (MQTT_CLIENT_BUFFER_SIZE / 2)) {
sendMessage(message);
message = "";
length = 0;
}
}
message += String(END_DELIM);
sendMessage(message);
}
void DashioMQTT::addDashStore(ControlType controlType, String controlID) {
DashStore tempDashStore[dashStoreSize];
if (dashStore != nullptr) {
for (int i=0; i<dashStoreSize; i++) {
tempDashStore[i] = dashStore[i];
}
delete[] dashStore;
}
dashStore = new DashStore[dashStoreSize + 1];
for (int i=0; i<dashStoreSize; i++) {
dashStore[i] = tempDashStore[i];
}
DashStore newDashStore = {controlType, controlID};
dashStore[dashStoreSize] = newDashStore;
dashStoreSize += 1;
}
void DashioMQTT::setupLWT() {
// Setup MQTT Last Will and Testament message (Optional). Default keep alive time is 10s
String willTopic = dashioDevice->getMQTTTopic(username, will_topic);
String offlineMessage = dashioDevice->getOfflineMessage();
mqttClient.setWill(willTopic.c_str(), offlineMessage.c_str(), false, MQTT_QOS);
if (printMessages) {
Serial.print(F("LWT topic: "));
Serial.println(willTopic);
Serial.print(F("LWT message: "));
Serial.println(offlineMessage);
}
}
void DashioMQTT::setCallback(void (*processIncomingMessage)(MessageData *messageData)) {
processMQTTmessageCallback = processIncomingMessage;
}
void DashioMQTT::setup(char *_username, char *_password) {
username = _username;
password = _password;
state = notReady;
}
void DashioMQTT::begin() {
if (wifiSetInsecure) {
wifiClient.setInsecure();
}
mqttClient.begin(mqttHost, mqttPort, wifiClient);
mqttClient.setOptions(10, true, 10000); // 10s timeout
mqttClient.onMessageAdvanced(messageReceivedMQTTCallback);
setupLWT(); // Once the deviceID is known
state = disconnected;
}
void DashioMQTT::onConnected() {
// Subscribe to private MQTT connection
String subscriberTopic = dashioDevice->getMQTTSubscribeTopic(username);
mqttClient.subscribe(subscriberTopic.c_str(), MQTT_QOS); // ... and subscribe
// Send MQTT ONLINE and WHO messages to connection (Optional)
// WHO is only required here if using the Dash server and it must be send to the ANNOUNCE topic
sendMessage(dashioDevice->getOnlineMessage());
sendMessage(dashioDevice->getWhoMessage(), announce_topic); // Update announce topic with new name
if (dashStore != nullptr) {
for (int i=0; i<dashStoreSize; i++) { // Announce control for data store on dash server
sendMessage(dashioDevice->getDataStoreEnableMessage(dashStore[i]), announce_topic);
}
}
if (reboot) {
reboot = false;
if (sendRebootAlarm) {
sendAlarmMessage(dashioDevice->getAlarmMessage("ALX", "System Reboot", dashioDevice->name));
}
}
dashioDevice->onStatusCallback(mqttConnected);
}
void DashioMQTT::hostConnect() {
Serial.print(F("Connecting MQTT..."));
state = connecting;
/* In case deviceID.c_str() gives trouble
int idLen = dashioDevice->deviceID.length() + 1;
char clientID[idLen];
dashioDevice->deviceID.toCharArray(clientID, idLen);
*/
if (mqttClient.connect(dashioDevice->deviceID.c_str(), username, password, false)) { // skip = false is the default. Used in order to establish and verify TLS connections manually before giving control to the MQTT client
if (printMessages) {
Serial.println(F("connected"));
}
state = serverConnected;
} else {
if (printMessages) {
Serial.print(F("failed - Try again in 10 seconds. E = "));
Serial.println(String(mqttClient.lastError()) + " R = " + mqttClient.returnCode());
// Invalid URL or port => E = -3 R = 0
// Invalid username or password => E = -10 R = 5
// Invalid SSL record => E = -5 R = 6
}
state = disconnected;
}
}
void DashioMQTT::checkConnection() {
// Check and connect MQTT as necessary
if (WiFi.status() == WL_CONNECTED) {
if (state == disconnected) {
if (mqttConnectCount == 0) {
hostConnect();
}
if (mqttConnectCount >= MQTT_RETRY_S) {
mqttConnectCount = 0;
dashioDevice->onStatusCallback(mqttDisconnected);
} else {
mqttConnectCount++;
}
} else if (state == serverConnected) {
mqttConnectCount = 0;
}
}
}
#ifdef ESP32
void DashioMQTT::checkConnectionTask(void * parameter) {
DashioMQTT *mqttConn = (DashioMQTT *) parameter;
for(;;) {
if (mqttConn != nullptr) {
if (!mqttConn->esp32_mqtt_blocking) {
mqttConn->checkConnection();
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
#endif
void DashioMQTT::run() {
if (mqttClient.connected()) {
mqttClient.loop();
if (state == serverConnected) {
onConnected();
state = subscribed;
}
if (data.messageReceived) {
data.messageReceived = false;
if (printMessages) {
Serial.println(data.getReceivedMessageForPrint(dashioDevice->getControlTypeStr(data.control)));
}
switch (data.control) {
case who:
sendMessage(dashioDevice->getWhoMessage());
break;
case connect:
sendMessage(dashioDevice->getConnectMessage());
break;
case config:
dashioDevice->dashboardID = data.idStr;
if (dashioDevice->configC64Str != nullptr) {
processConfig();
} else {
if (processMQTTmessageCallback != nullptr) {
processMQTTmessageCallback(&data);
}
}
break;
default:
if (processMQTTmessageCallback != nullptr) {
processMQTTmessageCallback(&data);
}
break;
}
}
data.checkBuffer();
checkAndSendMQTTbuffer();
} else {
if ((state == serverConnected) or (state == subscribed)) {
state = disconnected;
}
}
}
void DashioMQTT::end() {
sendMessage(dashioDevice->getOfflineMessage());
mqttClient.disconnect();
}
// ---------------------------------------- BLE ----------------------------------------
#ifdef ESP32
BLEclientHolder *DashioBLE::bleClients = nullptr;
uint8_t DashioBLE::maxBLEclients = 1;
bool DashioBLE::printMessages = false;
uint32_t DashioBLE::passKey = 0;
class ServerCallbacks: public NimBLEServerCallbacks {
public:
ServerCallbacks(DashioBLE * local_DashioBLE): local_DashioBLE(local_DashioBLE) { }
DashioBLE *local_DashioBLE = nullptr;
void onConnect(NimBLEServer* pServer, ble_gap_conn_desc *desc) {
ESP_LOGI(DTAG, "BLE Client Connected, handle: %d", desc->conn_handle);
if (!local_DashioBLE->setConnectionActive(desc->conn_handle)) {
ESP_LOGI(DTAG, "No connections left for handle: %d", desc->conn_handle);
}
if (pServer->getConnectedCount() < local_DashioBLE->maxBLEclients) {
NimBLEDevice::startAdvertising(); // Keep advertising for more connections
}
}
void onDisconnect(NimBLEServer* pServer, ble_gap_conn_desc *desc) {
ESP_LOGI(DTAG, "BLE Client Disconnected, handle: %d", desc->conn_handle);
local_DashioBLE->setConnectionInactive(desc->conn_handle);
}
// Security callback functions
uint32_t onPassKeyRequest() {
ESP_LOGI(DTAG,"Server Passkey Request: %d", local_DashioBLE->passKey);
return local_DashioBLE->passKey;
};
void onAuthenticationComplete(ble_gap_conn_desc *desc) {
if (desc->sec_state.authenticated) {
ESP_LOGI(DTAG, "BLE Authenticated");
} else {
ESP_LOGI(DTAG, "BLE Authentication FAIL");
local_DashioBLE->setConnectionAuthState(desc->conn_handle, BLE_AUTH_FAIL);
}
if (desc->sec_state.encrypted) {
ESP_LOGI(DTAG, "BLE Encrypted");
}
if (desc->sec_state.bonded) {
ESP_LOGI(DTAG, "BLE Bonded");
local_DashioBLE->setConnectionAuthState(desc->conn_handle, BLE_AUTH_REQ_CONN);
}
}
};
class CharacteristicCallbacks: public NimBLECharacteristicCallbacks {
public:
CharacteristicCallbacks(DashioBLE * local_DashioBLE): local_DashioBLE(local_DashioBLE) { }
DashioBLE *local_DashioBLE = nullptr;
void onWrite(NimBLECharacteristic *pCharacteristic, ble_gap_conn_desc *desc) { // BLE callback for when a message is received
std::string payload = pCharacteristic->getValue();
local_DashioBLE->data.processMessage(payload.c_str(), desc->conn_handle); /// The message components are stored within the connection where the messageReceived flag is set
}
};
DashioBLE::DashioBLE(DashioDevice *_dashioDevice, bool _printMessages) : data(BLE_CONN, INCOMING_BUFFER_SIZE) {
dashioDevice = _dashioDevice;
printMessages = _printMessages;
maxBLEclients = 1;
initialiseClientHolders();
}
DashioBLE::DashioBLE(DashioDevice *_dashioDevice, bool _printMessages, uint8_t _maxBLEclients) : data(BLE_CONN, INCOMING_BUFFER_SIZE) {
dashioDevice = _dashioDevice;
printMessages = _printMessages;
maxBLEclients = _maxBLEclients;
initialiseClientHolders();
}
void DashioBLE::bleNotifyValue(const String& message) {
pCharacteristic->setValue(message);
pCharacteristic->notify();
}
void DashioBLE::sendMessage(const String& message) {
if (isConnected()) {
int maxMessageLength = NimBLEDevice::getMTU() - 3;
if (message.length() <= maxMessageLength) {
bleNotifyValue(message);
} else {
int messageLength = message.length();
int numFullStrings = messageLength / maxMessageLength;
String subStr((char *)0);
subStr.reserve(maxMessageLength);
int start = 0;
for (unsigned int i = 0; i < numFullStrings; i++) {
subStr = message.substring(start, start + maxMessageLength);
bleNotifyValue(subStr);
start += maxMessageLength;
}
if (start < messageLength) {
subStr = message.substring(start);
bleNotifyValue(subStr);
}
}
if (printMessages) {
Serial.println(F("---- BLE Sent ----"));
Serial.println(message);
}
}
}
void DashioBLE::processConfig() {
sendMessage(dashioDevice->getC64ConfigBaseMessage());
int c64Length = strlen_P(dashioDevice->configC64Str);
int length = 0;
String message = "";
for (int k = 0; k < c64Length; k++) {
char myChar = pgm_read_byte_near(dashioDevice->configC64Str + k);
message += myChar;
length++;
if (length == C64_MAX_LENGHT) {
sendMessage(message);
message = "";
length = 0;
}
}
message += String(END_DELIM);
sendMessage(message);
}
void DashioBLE::run() {
if (secureBLE && (bleClients != nullptr)) {
for (int i = 0; i < maxBLEclients; i++) {
if (bleClients[i].authState == BLE_AUTH_REQ_CONN) {
bleClients[i].authState = BLE_AUTHENTICATED;
sendMessage(dashioDevice->getConnectMessage()); /// This wil go to all BLE clients as Arduino NimBLE doesn't yet allow messagein individual clients
break; // Remove break when NimBLE can send to specific client/connectionHandle
}
}
}
if (data.messageReceived) {
data.messageReceived = false;
if (printMessages) {
Serial.println(data.getReceivedMessageForPrint(dashioDevice->getControlTypeStr(data.control)));
}
bool startAuth = false;
switch (data.control) {
case who:
sendMessage(dashioDevice->getWhoMessage());
break;
case connect:
if (secureBLE && (bleClients != nullptr)) {
for (int i = 0; i < maxBLEclients; i++) {
if (bleClients[i].connectionHandle == data.connectionHandle) {
if (bleClients[i].authState == BLE_NOT_AUTH) {
startAuth = true;
}
}
}
}
if (startAuth) {
NimBLEDevice::startSecurity(data.connectionHandle);
} else {
sendMessage(dashioDevice->getConnectMessage());
}
break;
case config:
dashioDevice->dashboardID = data.idStr;
if (dashioDevice->configC64Str != nullptr) {
processConfig();
} else {
if (processBLEmessageCallback != nullptr) {
processBLEmessageCallback(&data);
}
}
break;
default:
if (processBLEmessageCallback != nullptr) {
processBLEmessageCallback(&data);
}
break;
}
}
data.checkBuffer();
}
void DashioBLE::end() {
NimBLEDevice::deinit(true);
}
void DashioBLE::setCallback(void (*processIncomingMessage)(MessageData *messageData)) {
processBLEmessageCallback = processIncomingMessage;
}
void DashBLE::setPassKey(uint32_t _passKey) {
passKey = _passKey;
secureBLE = (String(passKey).length() == 6);
// Setup BLE security (optional)
if (secureBLE) {
NimBLEDevice::setSecurityAuth(true, true, true); // bool bonding, bool mitm (man in the middle), bool sc
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY); // uint8_t iocap Sets the Input/Output capabilities of this device.
}
}
void DashioBLE::begin(uint32_t _passKey) {
String localName = F("DashIO_");
localName += dashioDevice->type;
NimBLEDevice::init(localName.c_str());
NimBLEDevice::setMTU(BLE_MAX_SEND_MESSAGE_LENGTH);
if ((String(_passKey).length() == 6)) {
setPassKey(_passKey);
} else if ((String(passKey).length() == 6)) {
setPassKey(passKey); // in case passKey was set BEFORE NimBLEDevice::init
}
// Setup server, service and characteristic
pServer = NimBLEDevice::createServer();
pServer->setCallbacks(new ServerCallbacks(this));
NimBLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID, NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::NOTIFY);
pCharacteristic->setCallbacks(new CharacteristicCallbacks(this));
pService->start();
// Setup BLE advertising
pAdvertising = NimBLEDevice::getAdvertising();
pAdvertising->addServiceUUID(BLEUUID(SERVICE_UUID));
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMaxPreferred(0x12);
pAdvertising->start();
}
String DashioBLE::macAddress() {
BLEAddress bdAddr = NimBLEDevice::getAddress();
return bdAddr.toString().c_str();
}
void DashioBLE::advertise() {
pAdvertising->start();
}
bool DashioBLE::isConnected() {
if (pServer != nullptr) {
return (pServer->getConnectedCount() > 0);
} else {