-
Notifications
You must be signed in to change notification settings - Fork 14
/
esp32_iGrill.ino
1638 lines (1499 loc) · 59.5 KB
/
esp32_iGrill.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
/*iGrill BLE Client
In order to setup the iGrill BLE Client or force the device back into configuration mode to update WiFi or MQTT settings you will need to
press the reset button on the device twice to trigger the double reset detector.
You can configure the Number of seconds to wait for the second reset by changing the DRD_TIMEOUT variable (Default 10s).
Once the device has booted up in configuration mode you will need to enter a password to enter the configuration/setup panel.
From this configuration panel you can configure the device to connect to your home WiFi network as well as configure the MQTT Server Information.
NOTE: You can enter in information to connect to two wifi networks, and the device will automatically choose the one with the best signal.
If you only have a single network you want to use this device from you can leave the second SSID and Password field empty.
The Credentials will then be saved into LittleFS / SPIFFS file and be used to connect to the MQTT Server and publish the iGrill Topics
************************************
* Configuration Portal Information *
************************************
Wifi SSID Name: iGrillClient_<ESP32_Chip_ID>
Wifi Password: igrill_client
NOTE: You can change this from the default by editing the code @ Line 58 before uploading to the device.
Wifi MQTT handling based on the example provided by the ESP_WifiManager Library for handling Wifi/MQTT Setup (https://github.com/khoih-prog/ESP_WiFiManager)
*/
#include "config.h" //iGrill BLE Client Configurable Settings
#include <ArduinoJson.h>
#include <esp_wifi.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h> //for MQTT
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#include <ESP_WiFiManager.h> //https://github.com/khoih-prog/ESP_WiFiManager
#include <BLEDevice.h>
#include <LittleFS.h> // https://github.com/espressif/arduino-esp32/tree/master/libraries/LittleFS
FS* filesystem = &LittleFS;
const int BUTTON_PIN = 27;
const int RED_LED = 26;
const int BLUE_LED = 25;
#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector
DoubleResetDetector* drd = NULL;
uint32_t timer = millis();
const char* CONFIG_FILE = "/ConfigMQTT.json";
// Indicates whether ESP has WiFi credentials saved from previous session
bool initialConfig = false; //default false
//Setting up variables for MQTT Info in Config Portal
char custom_MQTT_SERVER[custom_MQTT_SERVER_LEN];
char custom_MQTT_SERVERPORT[custom_MQTT_PORT_LEN];
char custom_MQTT_USERNAME[custom_MQTT_USERNAME_LEN];
char custom_MQTT_PASSWORD[custom_MQTT_PASSWORD_LEN];
char custom_MQTT_BASETOPIC[custom_MQTT_BASETOPIC_LEN];
char customhtml[24] = "type=\"checkbox\""; //Used for Metric Degrees checkbox in Config Portal
bool USE_METRIC_DEGREES = false; //Default to Imperial Degrees
char customhtmlNoMultiWifi[24] = "type=\"checkbox\""; //Used for MultiWifi checkbox in Config Portal
bool NO_MULTI_WIFI = false; // Default use MultiWifi
// SSID and PW for Config Portal
String ssid = "iGrillClient_" + String(ESP_getChipId(), HEX);
const char* password = "igrill_client";
// SSID and PW for your Router
String Router_SSID;
String Router_Pass;
typedef struct
{
char wifi_ssid[SSID_MAX_LEN];
char wifi_pw [PASS_MAX_LEN];
} WiFi_Credentials;
typedef struct
{
String wifi_ssid;
String wifi_pw;
} WiFi_Credentials_String;
typedef struct
{
WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS];
} WM_Config;
WM_Config WM_config;
// Use USE_DHCP_IP == true for dynamic DHCP IP, false to use static IP which you have to change accordingly to your network
#if (defined(USE_STATIC_IP_CONFIG_IN_CP) && !USE_STATIC_IP_CONFIG_IN_CP)
// Force DHCP to be true
#if defined(USE_DHCP_IP)
#undef USE_DHCP_IP
#endif
#define USE_DHCP_IP true
#else
#define USE_DHCP_IP true
#endif
#if ( USE_DHCP_IP || ( defined(USE_STATIC_IP_CONFIG_IN_CP) && !USE_STATIC_IP_CONFIG_IN_CP ) )
// Use DHCP
#warning Using DHCP IP
IPAddress stationIP = IPAddress(0, 0, 0, 0);
IPAddress gatewayIP = IPAddress(192, 168, 2, 1);
IPAddress netMask = IPAddress(255, 255, 255, 0);
#else
// Use static IP
#warning Using static IP
IPAddress stationIP = IPAddress(192, 168, 2, 232);
IPAddress gatewayIP = IPAddress(192, 168, 2, 1);
IPAddress netMask = IPAddress(255, 255, 255, 0);
#endif
IPAddress dns1IP = gatewayIP;
IPAddress dns2IP = IPAddress(8, 8, 8, 8);
IPAddress APStaticIP = IPAddress(192, 168, 100, 1);
IPAddress APStaticGW = IPAddress(192, 168, 100, 1);
IPAddress APStaticSN = IPAddress(255, 255, 255, 0);
WiFi_AP_IPConfig WM_AP_IPconfig;
WiFi_STA_IPConfig WM_STA_IPconfig;
// Create an ESP32 WiFiClient class to connect to the MQTT server
WiFiClient *client = nullptr;
PubSubClient *mqtt_client = nullptr;
//iGrill BLE Client Logging Function
void IGRILLLOGGER(String logMsg, int requiredLVL)
{
if(requiredLVL <= IGRILL_DEBUG_LVL)
Serial.printf("[II] %s\n", logMsg.c_str());
}
#pragma region iGrill_BLE_Variables
static const uint8_t chalBuf[] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; //iGrill Authentcation Payload
static BLEUUID GENERIC_ATTRIBUTE("00001801-0000-1000-8000-00805f9b34fb"); //UNUSED
static BLEUUID GENERIC_SERVICE_GUID("00001800-0000-1000-8000-00805f9b34fb"); //UNUSED
static BLEUUID BATTERY_SERVICE_UUID("0000180f-0000-1000-8000-00805f9b34fb"); //iGrill BLE Battery Service
static BLEUUID BATTERY_LEVEL("00002A19-0000-1000-8000-00805F9B34FB"); //iGrill BLE Battery Level Characteristic
static BLEUUID AUTH_SERVICE_UUID("64ac0000-4a4b-4b58-9f37-94d3c52ffdf7"); //iGrill BLE Auth Service
static BLEUUID FIRMWARE_VERSION("64ac0001-4a4b-4b58-9f37-94d3c52ffdf7"); // //iGrill BLE Characteristic used getting device firmware info
static BLEUUID APP_CHALLENGE("64ac0002-4a4b-4b58-9f37-94d3c52ffdf7"); //iGrill BLE Characteristic used for Authentication
static BLEUUID DEVICE_CHALLENGE("64ac0003-4a4b-4b58-9f37-94d3c52ffdf7"); //iGrill BLE Characteristic used for Authentication
static BLEUUID DEVICE_RESPONSE("64ac0004-4a4b-4b58-9f37-94d3c52ffdf7"); //iGrill BLE Characteristic used for Authentication
static BLEUUID MINI_SERVICE_UUID("63C70000-4A82-4261-95FF-92CF32477861"); //iGrill mini Service
static BLEUUID MINIV2_SERVICE_UUID("9d610c43-ae1d-41a9-9b09-3c7ecd5c6035"); //iGrill mini v2 Service
static BLEUUID SERVICE_UUID("A5C50000-F186-4BD6-97F2-7EBACBA0D708"); //iGrillv2 Service
static BLEUUID V202_SERVICE_UUID("ADA7590F-2E6D-469E-8F7B-1822B386A5E9"); //iGrillv202 Service
static BLEUUID V3_SERVICE_UUID("6E910000-58DC-41C7-943F-518B278CEA88"); //iGrillv3 Service
static BLEUUID IGRILL_TEMP_UNITS("06EF0001-2E06-4B79-9E33-FCE2C42805EC"); //iGrill BLE Characteristic for Setting Temperature Units 0=IMPERIAL 1=METRIC
static BLEUUID PROBE1_TEMPERATURE("06EF0002-2E06-4B79-9E33-FCE2C42805EC"); //iGrill BLE Characteristic for Probe1 Temperature
static BLEUUID PROBE1_THRESHOLD("06ef0003-2e06-4b79-9e33-fce2c42805ec"); //iGrill BLE Characteristic for Probe1 Notification Threshhold (NOT IMPLEMENTED)
static BLEUUID PROBE2_TEMPERATURE("06ef0004-2e06-4b79-9e33-fce2c42805ec"); //iGrill BLE Characteristic for Probe2 Temperature
static BLEUUID PROBE2_THRESHOLD("06ef0005-2e06-4b79-9e33-fce2c42805ec"); //iGrill BLE Characteristic for Probe2 Notification Threshhold (NOT IMPLEMENTED)
static BLEUUID PROBE3_TEMPERATURE("06ef0006-2e06-4b79-9e33-fce2c42805ec"); //iGrill BLE Characteristic for Probe3 Temperature
static BLEUUID PROBE3_THRESHOLD("06ef0007-2e06-4b79-9e33-fce2c42805ec"); //iGrill BLE Characteristic for Probe3 Notification Threshhold (NOT IMPLEMENTED)
static BLEUUID PROBE4_TEMPERATURE("06ef0008-2e06-4b79-9e33-fce2c42805ec"); //iGrill BLE Characteristic for Probe4 Temperature
static BLEUUID PROBE4_THRESHOLD("06ef0009-2e06-4b79-9e33-fce2c42805ec"); //iGrill BLE Characteristic for Probe4 Notification Threshhold (NOT IMPLEMENTED)
static BLEUUID PROPANE_SERVICE("f5d40000-3548-4c22-9947-f3673fce3cd9");//iGrill v3 Propane Service
static BLEUUID PROPANE_LEVEL_SENSOR("f5d40001-3548-4c22-9947-f3673fce3cd9"); //iGrill v3 Propane sensor
static bool doConnect = false; //bool for determining if an iGrill Device has been found
static bool connected = false; //bool for determining if we are connected and Authenticated to an iGrill Device
static bool reScan = false; //bool for determining if we need to do a BLE Scan (i.e. iGrill Device is no longer available or iGrill wasnt powered on before we finished initial scan)
static BLEClient* iGrillClient = nullptr;
static BLERemoteCharacteristic* authRemoteCharacteristic = nullptr;
static BLERemoteCharacteristic* batteryCharacteristic = nullptr;
static BLERemoteCharacteristic* deviceTempUnitsCharacteristic = nullptr;
static BLERemoteCharacteristic* probe1TempCharacteristic = nullptr;
static BLERemoteCharacteristic* probe2TempCharacteristic = nullptr;
static BLERemoteCharacteristic* probe3TempCharacteristic = nullptr;
static BLERemoteCharacteristic* probe4TempCharacteristic = nullptr;
static BLERemoteCharacteristic* propanelevelCharacteristic = nullptr;
static BLERemoteService* iGrillAuthService = nullptr; //iGrill BLE Service used for authenticating to the device (Needed to read probe values)
static BLERemoteService* iGrillService = nullptr; //iGrill BLE Service used for reading probes
static BLERemoteService* iGrillBattService = nullptr; //iGrill BLE Service used to read battery level
static BLERemoteService* iGrillPropaneService = nullptr; //iGrill BLE Service used to read propane level from iGrillv3 devices
static BLEAdvertisedDevice* myDevice;
static String deviceStr ="";
static String iGrillMac="";
static String iGrillModel="";
static bool has_propane_sensor=false;
#define DELETE(ptr) { if (ptr != nullptr) {delete ptr; ptr = nullptr;} }
#pragma endregion
#pragma region iGrill_BLE_Functions
//The registered iGrill Characteristics call this function when their values change
//We then determine the BLEUUID to figure out how to correctly parse the info then print it to Serial and send to an MQTT server
static void notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify)
{
if(PROBE1_TEMPERATURE.equals(pBLERemoteCharacteristic->getUUID()))
{
if(pData[1] ==248) //This is the value set when probe is unplugged
{
IGRILLLOGGER(" ! Probe 1 Disconnected!", 2);
publishProbeTemp(1,-100); //We use -100 as the lowest supported temp via the probes is -58
}
else
{
short t = (pData[1] << 8) | pData[0];
IGRILLLOGGER(" * Probe 1 Temp: " + String(t), 2);
publishProbeTemp(1, t);
}
}
else if(PROBE2_TEMPERATURE.equals(pBLERemoteCharacteristic->getUUID()))
{
if(pData[1] ==248) //This is the value set when probe is unplugged
{
IGRILLLOGGER(" ! Probe 2 Disconnected!",2);
publishProbeTemp(2,-100); //We use -100 as the lowest supported temp via the probes is -58
}
else
{
short t = (pData[1] << 8) | pData[0];
IGRILLLOGGER(" * Probe 2 Temp: " + String(t), 2);
publishProbeTemp(2, t);
}
}
else if(PROBE3_TEMPERATURE.equals(pBLERemoteCharacteristic->getUUID()))
{
if(pData[1] ==248) //This is the value set when probe is unplugged
{
IGRILLLOGGER(" ! Probe 3 Disconnected!",2);
publishProbeTemp(3,-100); //We use -100 as the lowest supported temp via the probes is -58
}
else
{
short t = (pData[1] << 8) | pData[0];
IGRILLLOGGER(" * Probe 3 Temp: " + String(t), 2);
publishProbeTemp(3, t);
}
}
else if(PROBE4_TEMPERATURE.equals(pBLERemoteCharacteristic->getUUID()))
{
if(pData[1] ==248) //This is the value set when probe is unplugged
{
IGRILLLOGGER(" ! Probe 4 Disconnected!",2);
publishProbeTemp(4,-100); //We use -100 as the lowest supported temp via the probes is -58
}
else
{
short t = (pData[1] << 8) | pData[0];
IGRILLLOGGER(" * Probe 4 Temp: " + String(t), 2);
publishProbeTemp(4, t);
}
}
else if(BATTERY_LEVEL.equals(pBLERemoteCharacteristic->getUUID()))
{
IGRILLLOGGER(" %% Battery Level: " + String(pData[0]) + "%% ",2);
publishBattery(pData[0]);
}
else if(PROPANE_LEVEL_SENSOR.equals(pBLERemoteCharacteristic->getUUID()))
{
//WE RECIEVED A NEW PROPANE LEVEL EVENT
IGRILLLOGGER(" %% Propane Level: " + String(pData[0]*25) + "%% ",2);
publishPropaneLevel(pData[0]*25);
}
}
class MyClientCallback : public BLEClientCallbacks
{
void onConnect(BLEClient* pclient)
{
}
void onDisconnect(BLEClient* pclient)
{
IGRILLLOGGER(" - iGrill Disconnected!", 1);
IGRILLLOGGER(" - Freeing Memory....", 1);
String availTopic = (String)custom_MQTT_BASETOPIC + "/binary_sensor/igrill_"+ iGrillMac+"/connected";
mqtt_client->publish(availTopic.c_str(),"offline");
publishProbeTemp(1,-100);
if((iGrillModel != "iGrill_mini") && (iGrillModel != "iGrill_mini_v2"))
{
publishProbeTemp(2,-100);
publishProbeTemp(3,-100);
publishProbeTemp(4,-100);
}
delay(100);
//Lost Connection to Device Resetting all Variables....
connected = false; //No longer connected to iGrill
//Free up memory
DELETE(iGrillClient);
// the service references are owned by iGrillClient and are deleted by it's destructor
authRemoteCharacteristic = nullptr;
batteryCharacteristic = nullptr;
probe1TempCharacteristic = nullptr;
probe2TempCharacteristic = nullptr;
probe3TempCharacteristic = nullptr;
probe4TempCharacteristic = nullptr;
propanelevelCharacteristic = nullptr;
deviceTempUnitsCharacteristic = nullptr;
iGrillAuthService = nullptr;
iGrillService = nullptr;
iGrillBattService = nullptr;
iGrillPropaneService = nullptr;
has_propane_sensor=false;
// deviceStr =""; //Reset the Device String used for MQTT publishing
// iGrillMac=""; //Reset the iGrillMac String used for MQTT publishing
// iGrillModel=""; //Reset the iGrillModel String used for MQTT publishing
// disconnectMQTT();
IGRILLLOGGER(" - Done!", 1);
reScan = true; //Set the BLE rescan flag to true to initiate a new scan
}
};
static MyClientCallback oMyClientCallback;
//Set iGrill Temperature Units to Match what has been selected in the Web Configuration Portal
void setDeviceTemperatureUnits()
{
uint8_t IMPERIAL_UNITS = 0;
uint8_t METRIC_UNITS = 1;
IGRILLLOGGER(" - Setting Device Temp Units...",1);
if (iGrillService == nullptr)
{
IGRILLLOGGER(" - Setting Device Temp Units Failed!",1);
iGrillClient->disconnect();
}
else
{
try
{
deviceTempUnitsCharacteristic = iGrillService->getCharacteristic(IGRILL_TEMP_UNITS);
if (deviceTempUnitsCharacteristic == nullptr)
{
IGRILLLOGGER(" - Setting Device Temp Units Failed!", 1);
iGrillClient->disconnect();
}
if(USE_METRIC_DEGREES)
{
//SET IGRILL DEVICE TO METRIC UNITS
if (deviceTempUnitsCharacteristic->canWrite())
deviceTempUnitsCharacteristic->writeValue(METRIC_UNITS, true);
else
IGRILLLOGGER(" - Cannot WRITE Device Temp Units!", 1);
}
else
{
//SET IGRILL DEVICE TO IMPERIAL UNITS
if (deviceTempUnitsCharacteristic->canWrite())
deviceTempUnitsCharacteristic->writeValue(IMPERIAL_UNITS, true);
else
IGRILLLOGGER(" - Cannot WRITE Device Temp Units!", 1);
}
delay(1*1000);
if (deviceTempUnitsCharacteristic->canRead())
{
uint8_t tempUnits = deviceTempUnitsCharacteristic->readUInt8();
if(tempUnits == 0)
IGRILLLOGGER(" ** Temperature Units: IMPERIAL", 1);
if(tempUnits == 1)
IGRILLLOGGER(" ** Temperature Units: METRIC", 1);
}
}
catch(...)
{
IGRILLLOGGER(" - Setting Device Temp Units Failed!",1);
iGrillClient->disconnect();
}
}
}
//Register Callback for iGrill Probes
void setupProbes()
{
IGRILLLOGGER(" - Setting up Probes...",1);
if (iGrillService == nullptr)
{
IGRILLLOGGER(" - Setting up Probes Failed!",1);
iGrillClient->disconnect();
}
else
{
try
{
probe1TempCharacteristic = iGrillService->getCharacteristic(PROBE1_TEMPERATURE);
if(probe1TempCharacteristic->canNotify())
{
probe1TempCharacteristic->registerForNotify(notifyCallback);
IGRILLLOGGER(" -- Probe 1 Setup!",1);
}
if((iGrillModel != "iGrill_mini") && (iGrillModel != "iGrill_mini_v2"))
{
probe2TempCharacteristic = iGrillService->getCharacteristic(PROBE2_TEMPERATURE);
if(probe2TempCharacteristic->canNotify())
{
probe2TempCharacteristic->registerForNotify(notifyCallback);
IGRILLLOGGER(" -- Probe 2 Setup!",1);
}
probe3TempCharacteristic = iGrillService->getCharacteristic(PROBE3_TEMPERATURE);
if(probe3TempCharacteristic->canNotify())
{
probe3TempCharacteristic->registerForNotify(notifyCallback);
IGRILLLOGGER(" -- Probe 3 Setup!",1);
}
probe4TempCharacteristic = iGrillService->getCharacteristic(PROBE4_TEMPERATURE);
if(probe4TempCharacteristic->canNotify())
{
probe4TempCharacteristic->registerForNotify(notifyCallback);
IGRILLLOGGER(" -- Probe 4 Setup!",1);
}
}
}
catch(...)
{
IGRILLLOGGER(" - Setting up Probes Failed!",1);
iGrillClient->disconnect();
}
}
}
//Read and Publish iGrill System Info
void getiGrillInfo()
{
try
{
std::string fwVersion = iGrillAuthService->getCharacteristic(FIRMWARE_VERSION)->readValue();
if(deviceStr == "")
setDeviceJSONObject(fwVersion.c_str(), myDevice->getAddress().toString().c_str());
publishSystemInfo();
}
catch(...)
{
IGRILLLOGGER("Error obtaining Firmware Info", 1);
iGrillClient->disconnect();
}
}
//Register Callback for iGrill Device Propane Level
bool setupPropaneCharacteristic()
{
IGRILLLOGGER(" - Setting up Propane Level Characteristic...",1);
try
{
propanelevelCharacteristic = iGrillPropaneService->getCharacteristic(PROPANE_LEVEL_SENSOR);
if (propanelevelCharacteristic == nullptr)
{
IGRILLLOGGER(" - Setting up Propane Level Characteristic Failed!", 1);
iGrillClient->disconnect();
return false;
}
if (propanelevelCharacteristic->canNotify())
{
propanelevelCharacteristic->registerForNotify(notifyCallback);
IGRILLLOGGER(" -- Propane Level Setup!",1);
}
if (propanelevelCharacteristic->canRead())
{
uint8_t value = propanelevelCharacteristic->readUInt8();
IGRILLLOGGER(" %% Propane Level: " + String(value*25) + "%", 2);
publishPropaneLevel(value*25);
}
return true;
}
catch(...)
{
IGRILLLOGGER(" - Setting up Propane Level Characteristic Failed!", 1);
iGrillClient->disconnect();
return false;
}
}
//Register Callback for iGrill Device Battery Level
bool setupBatteryCharacteristic()
{
IGRILLLOGGER(" - Setting up Battery Characteristic...",1);
try
{
batteryCharacteristic = iGrillBattService->getCharacteristic(BATTERY_LEVEL);
if (batteryCharacteristic == nullptr)
{
IGRILLLOGGER(" - Setting up Battery Characteristic Failed!", 1);
iGrillClient->disconnect();
return false;
}
if (batteryCharacteristic->canNotify())
{
batteryCharacteristic->registerForNotify(notifyCallback);
IGRILLLOGGER(" -- Battery Setup!",1);
}
if (batteryCharacteristic->canRead())
{
uint8_t value = batteryCharacteristic->readUInt8();
IGRILLLOGGER(" %% Battery Level: " + String(value) + "%", 2);
publishBattery(value);
}
return true;
}
catch(...)
{
IGRILLLOGGER(" - Setting up Battery Characteristic Failed!", 1);
iGrillClient->disconnect();
return false;
}
}
//BLE Security Callbacks used for pairing
class MySecurity : public BLESecurityCallbacks
{
uint32_t onPassKeyRequest()
{
return 000000;
}
void onPassKeyNotify(uint32_t pass_key)
{
IGRILLLOGGER(" - The passkey Notify number: " + String(pass_key),0);
}
bool onConfirmPIN(uint32_t pass_key)
{
IGRILLLOGGER(" - The passkey YES/NO number: " + String(pass_key), 0);
vTaskDelay(5000);
return true;
}
bool onSecurityRequest()
{
IGRILLLOGGER(" - Security Request", 0);
return true;
}
void onAuthenticationComplete(esp_ble_auth_cmpl_t auth_cmpl)
{
Serial.printf("[II] - iGrill Pair Status: %s\n", auth_cmpl.success ? "Paired" : "Disconnected");
}
};
static MySecurity oMySecurity;
/*This function does the following
- Connects and Pairs to the iGrill Device
- Sets up the iGrill Authentication Service
1. Write a challenge (in this case 16 bytes of 0) to APP_CHALLENGE Characteristic
2. Read DEVICE_CHALLENGE Characteristic Value
3. Write the DEVICE_CHALLENGE Characteristic Value to DEVICE_RESPONSE Characteristic
** Since our first 8 bytes are 0, we dont need to do any crypto operations. We can just hand back the same encrypted value we get and we're authenticated.**
- Sets up iGrillService (Needed to read probe temps)
- Sets up iGrillBattService (Needed to read Battery Level)
- Sets up iGrillPropaneService (Needed to read Propane Level from v3 Devices)
*/
bool connectToServer()
{
String temp = "Connecting to iGrill Device: " + String(myDevice->getAddress().toString().c_str());
IGRILLLOGGER(temp ,1);
//Setting up the iGrill Pairing Paramaters (Without Bonding you can't read the Temp Probes)
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
BLEDevice::setSecurityCallbacks(&oMySecurity);
BLESecurity security;
security.setAuthenticationMode(ESP_LE_AUTH_BOND);
security.setCapability(ESP_IO_CAP_NONE);
security.setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
//End of the iGrill Pairing Paramaters
iGrillClient = BLEDevice::createClient(); //Creating the iGrill Client
if(iGrillClient == nullptr)
{
reScan = true;
IGRILLLOGGER("Connection Failed!", 1);
return false;
}
delay(1*1000);
if(!iGrillClient->connect(myDevice)) //Connecting to the iGrill Device
{
DELETE(iGrillClient);
reScan = true;
IGRILLLOGGER("Connection Failed!",1);
return false;
}
IGRILLLOGGER(" - Created client",1);
iGrillClient->setClientCallbacks(&oMyClientCallback);
// Connect to the remote BLE Server.
IGRILLLOGGER(" - Connected to iGrill BLE Server",1);
delay(1*1000);
// Obtain a reference to the authentication service we are after in the remote BLE server.
IGRILLLOGGER(" - Performing iGrill App Authentication",1);
iGrillAuthService = iGrillClient->getService(AUTH_SERVICE_UUID);
delay(1*1000);
if (iGrillAuthService == nullptr)
{
IGRILLLOGGER(" - Authentication Failed (iGrillAuthService is null)",1);
iGrillClient->disconnect();
return false;
}
delay(2*1000);
if(iGrillClient->isConnected())
{
try
{
// Obtain a reference to the characteristic in the service of the remote BLE server.
authRemoteCharacteristic = iGrillAuthService->getCharacteristic(APP_CHALLENGE);
if (authRemoteCharacteristic == nullptr)
{
IGRILLLOGGER(" - Authentication Failed (authRemoteCharacteristic is null)!",1);
return false;
}
//Start of Authentication Sequence
IGRILLLOGGER(" - Writing iGrill App Challenge...",1);
authRemoteCharacteristic->writeValue((uint8_t*)chalBuf, sizeof(chalBuf), true);
delay(500);
IGRILLLOGGER(" - Reading iGrill Device Challenge",1);
std::string encrypted_device_challenge = iGrillAuthService->getCharacteristic(DEVICE_CHALLENGE)->readValue();
delay(500);
IGRILLLOGGER(" - Writing Encrypted iGrill Device Challenge...",1);
iGrillAuthService->getCharacteristic(DEVICE_RESPONSE)->writeValue(encrypted_device_challenge, true);
//End of Authentication Sequence
IGRILLLOGGER(" - Authentication Complete",1);
if(iGrillModel == "iGrill_mini")
{
iGrillService = iGrillClient->getService(MINI_SERVICE_UUID); //Obtain a reference for the Main iGrill Service that we use for Temp Probes
}
else if(iGrillModel == "iGrill_mini_v2")
{
iGrillService = iGrillClient->getService(MINIV2_SERVICE_UUID); //Obtain a reference for the Main iGrill Service that we use for Temp Probes
}
else if(iGrillModel == "iGrillv2")
{
iGrillService = iGrillClient->getService(SERVICE_UUID); //Obtain a reference for the Main iGrill Service that we use for Temp Probes
}
else if(iGrillModel == "iGrillv202")
{
iGrillService = iGrillClient->getService(V202_SERVICE_UUID); //Obtain a reference for the Main iGrill Service that we use for Temp Probes
}
else if(iGrillModel == "iGrillv3")
{
iGrillService = iGrillClient->getService(V3_SERVICE_UUID); //Obtain a reference for the Main iGrill Service that we use for Temp Probes
delay(1*1000);
iGrillPropaneService = iGrillClient->getService(PROPANE_SERVICE); //Obtain a reference for the iGrill Propane Service that we use for Propane Level
if(iGrillPropaneService == nullptr)
{
IGRILLLOGGER(" - Unable to Setup Propane Level Service (iGrillPropaneService is null)",2);
}
else
{
IGRILLLOGGER(" - Propane Level Service Setup!",1);
has_propane_sensor = true;
}
}
delay(1*1000);
iGrillBattService = iGrillClient->getService(BATTERY_SERVICE_UUID); //Obtain a reference for the iGrill Battery Service that we use for Getting the Battery Level
delay(1*1000);
connected = true;
reScan = false;
return true;
}
catch(...)
{
IGRILLLOGGER(" - Authentication Failed!",1);
iGrillClient->disconnect();
return false;
}
}
else
{
IGRILLLOGGER(" - Authentication Failed (lost connection to device)!",1);
iGrillClient->disconnect();
return false;
}
}
//Scan for BLE servers and find the first one that advertises the service we are looking for.
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
//This Callback is called for each advertising BLE server.
void onResult(BLEAdvertisedDevice advertisedDevice)
{
// We have found a device, let us now see if it contains the iGrill service
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(MINI_SERVICE_UUID))
{
BLEDevice::getScan()->stop();
DELETE(myDevice); // delete old stuff (don't need it anymore)
myDevice = new BLEAdvertisedDevice(advertisedDevice);
iGrillModel = "iGrill_mini";
doConnect = true;
}
else if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(MINIV2_SERVICE_UUID))
{
BLEDevice::getScan()->stop();
DELETE(myDevice); // delete old stuff (don't need it anymore)
myDevice = new BLEAdvertisedDevice(advertisedDevice);
iGrillModel = "iGrill_mini_v2";
doConnect = true;
}
else if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(SERVICE_UUID))
{
BLEDevice::getScan()->stop();
DELETE(myDevice); // delete old stuff (don't need it anymore)
myDevice = new BLEAdvertisedDevice(advertisedDevice);
iGrillModel = "iGrillv2";
doConnect = true;
}
else if(advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(V202_SERVICE_UUID))
{
BLEDevice::getScan()->stop();
DELETE(myDevice); // delete old stuff (don't need it anymore)
myDevice = new BLEAdvertisedDevice(advertisedDevice);
iGrillModel = "iGrillv202";
doConnect = true;
}
else if(advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(V3_SERVICE_UUID))
{
IGRILLLOGGER(" - iGrillv3 device discovered",2);
BLEDevice::getScan()->stop();
DELETE(myDevice); // delete old stuff (don't need it anymore)
myDevice = new BLEAdvertisedDevice(advertisedDevice);
iGrillModel = "iGrillv3";
doConnect = true;
}
}
};
static MyAdvertisedDeviceCallbacks oMyAdvertisedDeviceCallbacks;
#pragma endregion
#pragma region WIFI_Fuctions
void initAPIPConfigStruct(WiFi_AP_IPConfig &in_WM_AP_IPconfig)
{
in_WM_AP_IPconfig._ap_static_ip = APStaticIP;
in_WM_AP_IPconfig._ap_static_gw = APStaticGW;
in_WM_AP_IPconfig._ap_static_sn = APStaticSN;
}
void initSTAIPConfigStruct(WiFi_STA_IPConfig &in_WM_STA_IPconfig)
{
in_WM_STA_IPconfig._sta_static_ip = stationIP;
in_WM_STA_IPconfig._sta_static_gw = gatewayIP;
in_WM_STA_IPconfig._sta_static_sn = netMask;
#if USE_CONFIGURABLE_DNS
in_WM_STA_IPconfig._sta_static_dns1 = dns1IP;
in_WM_STA_IPconfig._sta_static_dns2 = dns2IP;
#endif
}
void displayIPConfigStruct(WiFi_STA_IPConfig in_WM_STA_IPconfig)
{
LOGERROR3(F("stationIP ="), in_WM_STA_IPconfig._sta_static_ip, ", gatewayIP =", in_WM_STA_IPconfig._sta_static_gw);
LOGERROR1(F("netMask ="), in_WM_STA_IPconfig._sta_static_sn);
#if USE_CONFIGURABLE_DNS
LOGERROR3(F("dns1IP ="), in_WM_STA_IPconfig._sta_static_dns1, ", dns2IP =", in_WM_STA_IPconfig._sta_static_dns2);
#endif
}
void configWiFi(WiFi_STA_IPConfig in_WM_STA_IPconfig)
{
#if USE_CONFIGURABLE_DNS
// Set static IP, Gateway, Subnetmask, DNS1 and DNS2. New in v1.0.5
WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn, in_WM_STA_IPconfig._sta_static_dns1, in_WM_STA_IPconfig._sta_static_dns2);
#else
// Set static IP, Gateway, Subnetmask, Use auto DNS1 and DNS2.
WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn);
#endif
}
uint8_t connectWiFi()
{
uint8_t status;
LOGERROR(F("ConnectWiFi with :"));
for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++)
{
if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) )
{
LOGERROR3(F("* Using SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw );
LOGERROR(F("Connecting Wifi..."));
WiFi.mode(WIFI_STA);
status = WiFi.begin(WM_config.WiFi_Creds[i].wifi_ssid, WM_config.WiFi_Creds[i].wifi_pw);
status = WiFi.waitForConnectResult();
if ( WiFi.isConnected() )
{
LOGERROR(F("WiFi connected"));
LOGERROR3(F("SSID:"), WiFi.SSID(), F(",RSSI="), WiFi.RSSI());
LOGERROR3(F("Channel:"), WiFi.channel(), F(",IP address:"), WiFi.localIP() );
return status;
}
else
LOGERROR(F("No success, trying next credentials"));
}
}
LOGERROR(F("WiFi not connected"));
return status;
}
uint8_t connectMultiWiFi()
{
uint8_t status;
LOGERROR(F("ConnectMultiWiFi with :"));
if ( (Router_SSID != "") && (Router_Pass != "") )
{
LOGERROR3(F("* Flash-stored Router_SSID = "), Router_SSID, F(", Router_Pass = "), Router_Pass );
}
for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++)
{
// Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8)
if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) )
{
LOGERROR3(F("* Additional SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw );
}
}
LOGERROR(F("Connecting MultiWifi..."));
WiFi.mode(WIFI_STA);
#if !USE_DHCP_IP
configWiFi(WM_STA_IPconfig);
#endif
int i = 0;
status = wifiMulti.run();
delay(WIFI_MULTI_1ST_CONNECT_WAITING_MS);
while ( ( i++ < 20 ) && ( status != WL_CONNECTED ) )
{
status = wifiMulti.run();
if ( status == WL_CONNECTED )
break;
else
delay(WIFI_MULTI_CONNECT_WAITING_MS);
}
if ( status == WL_CONNECTED )
{
LOGERROR1(F("WiFi connected after time: "), i);
LOGERROR3(F("SSID:"), WiFi.SSID(), F(",RSSI="), WiFi.RSSI());
LOGERROR3(F("Channel:"), WiFi.channel(), F(",IP address:"), WiFi.localIP() );
}
else
LOGERROR(F("WiFi not connected"));
return status;
}
void heartBeatPrint()
{
static int num = 1;
if(mqtt_client != nullptr) //We have to check and see if we have a mqtt client created as this doesnt happen until the device is connected to an igrill device.
{
if(!mqtt_client->connected())
{
IGRILLLOGGER("MQTT Disconnected",2);
connectMQTT();
}
}
if(!connected) //If are not connected to an iGrill Device we need to initate a new BLE Scan to try and find a device.
{
if(num%3 == 0) //Only attempt to re-scan every 3th time we make it to the check unconnected (If using default this will be twice a min.)
reScan = true; //Set the BLE rescan flag to true to initiate a new scan
}
num++;
IGRILLLOGGER(String("free heap memory: ") + String(ESP.getFreeHeap()), 2);
}
void check_WiFi()
{
if ( (WiFi.status() != WL_CONNECTED) )
{
IGRILLLOGGER("WiFi lost.", 0);
disconnectMQTT();
if (NO_MULTI_WIFI)
{
IGRILLLOGGER("Call connectWiFi in loop", 0);
connectWiFi();
}
else
{
IGRILLLOGGER("Call connectMultiWiFi in loop", 0);
connectMultiWiFi();
}
}
}
void wifi_manager()
{
IGRILLLOGGER("\nConfig Portal requested.", 0);
digitalWrite(LED_BUILTIN, LED_ON); // turn the LED on by making the voltage LOW to tell us we are in configuration mode.
ESP_WiFiManager ESP_wifiManager("ESP32_iGrillClient");
IGRILLLOGGER("Opening Configuration Portal. ", 0);
Router_SSID = ESP_wifiManager.WiFi_SSID();
Router_Pass = ESP_wifiManager.WiFi_Pass();
//Check if there is stored WiFi router/password credentials.
if ( !initialConfig && (Router_SSID != "") && (Router_Pass != "") )
{
//If valid AP credential and not DRD, set timeout 120s.
ESP_wifiManager.setConfigPortalTimeout(120);
IGRILLLOGGER("Got stored Credentials. Timeout 120s", 0);
}
else
{
//If not found, device will remain in configuration mode until switched off via webserver.
ESP_wifiManager.setConfigPortalTimeout(0);
IGRILLLOGGER("No timeout : ", 0);
if (initialConfig)
IGRILLLOGGER("DRD or No stored Credentials..", 0);
else
IGRILLLOGGER("No stored Credentials.", 0);
}
//Local intialization. Once its business is done, there is no need to keep it around
// Extra parameters to be configured
// After connecting, parameter.getValue() will get you the configured value
// Format: <ID> <Placeholder text> <default value> <length> <custom HTML> <label placement>
// (*** we are not using <custom HTML> and <label placement> ***)
ESP_WMParameter MQTT_SERVER_FIELD(MQTT_SERVER_Label, "MQTT SERVER", custom_MQTT_SERVER, custom_MQTT_SERVER_LEN);// MQTT_SERVER
ESP_WMParameter MQTT_SERVERPORT_FIELD(MQTT_SERVERPORT_Label, "MQTT SERVER PORT", custom_MQTT_SERVERPORT, custom_MQTT_PORT_LEN + 1);// MQTT_SERVERPORT
ESP_WMParameter MQTT_USERNAME_FIELD(MQTT_USERNAME_Label, "MQTT USERNAME", custom_MQTT_USERNAME, custom_MQTT_USERNAME_LEN);// MQTT_USERNAME
ESP_WMParameter MQTT_PASSWORD_FIELD(MQTT_PASSWORD_Label, "MQTT PASSWORD", custom_MQTT_PASSWORD, custom_MQTT_PASSWORD_LEN);// MQTT_PASSWORD
ESP_WMParameter MQTT_BASETOPIC_FIELD(MQTT_BASETOPIC_Label, "MQTT BASE TOPIC", custom_MQTT_BASETOPIC, custom_MQTT_BASETOPIC_LEN);// MQTT_BASETOPIC
if (USE_METRIC_DEGREES)
strcat(customhtml, " checked"); //check the box so the portal shows the correct state
ESP_WMParameter USE_METRIC_DEGREES_CHECKBOX(USE_METRIC_DEGREES_Label, "Use Metric Degrees (°C)", "T", 2, customhtml, WFM_LABEL_AFTER);
if (NO_MULTI_WIFI)
strcat(customhtmlNoMultiWifi, " checked"); //check the box so the portal shows the correct state
ESP_WMParameter NO_MULTI_WIFI_CHECKBOX(NO_MULTI_WIFI_Label, "Don't use MultWifi", "T", 2, customhtmlNoMultiWifi, WFM_LABEL_AFTER);
ESP_wifiManager.addParameter(&MQTT_SERVER_FIELD);
ESP_wifiManager.addParameter(&MQTT_SERVERPORT_FIELD);
ESP_wifiManager.addParameter(&MQTT_USERNAME_FIELD);
ESP_wifiManager.addParameter(&MQTT_PASSWORD_FIELD);
ESP_wifiManager.addParameter(&MQTT_BASETOPIC_FIELD);
ESP_wifiManager.addParameter(&USE_METRIC_DEGREES_CHECKBOX);
ESP_wifiManager.addParameter(&NO_MULTI_WIFI_CHECKBOX);
ESP_wifiManager.setMinimumSignalQuality(-1);
ESP_wifiManager.setConfigPortalChannel(0); // Set config portal channel, Use 0 => random channel from 1-13
#if !USE_DHCP_IP
#if USE_CONFIGURABLE_DNS
// Set static IP, Gateway, Subnetmask, DNS1 and DNS2. New in v1.0.5
ESP_wifiManager.setSTAStaticIPConfig(stationIP, gatewayIP, netMask, dns1IP, dns2IP);
#else
// Set static IP, Gateway, Subnetmask, Use auto DNS1 and DNS2.
ESP_wifiManager.setSTAStaticIPConfig(stationIP, gatewayIP, netMask);
#endif
#endif
#if USING_CORS_FEATURE
ESP_wifiManager.setCORSHeader("Your Access-Control-Allow-Origin");
#endif
// Start an access point and goes into a blocking loop awaiting configuration.
// Once the user leaves the portal with the exit button processing will continue
if (!ESP_wifiManager.startConfigPortal((const char *) ssid.c_str(), password))
{
IGRILLLOGGER("Not connected to WiFi but continuing anyway.", 0);
}
else
{
IGRILLLOGGER("WiFi Connected!", 0);
}
// Only clear then save data if CP entered and with new valid Credentials
// No CP => stored getSSID() = ""
if ( String(ESP_wifiManager.getSSID(0)) != "" && String(ESP_wifiManager.getPW(0)) != "" )
{
memset(&WM_config, 0, sizeof(WM_config));
for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++)
{
String tempSSID = ESP_wifiManager.getSSID(i);
String tempPW = ESP_wifiManager.getPW(i);
if (strlen(tempSSID.c_str()) < sizeof(WM_config.WiFi_Creds[i].wifi_ssid) - 1)
strcpy(WM_config.WiFi_Creds[i].wifi_ssid, tempSSID.c_str());
else
strncpy(WM_config.WiFi_Creds[i].wifi_ssid, tempSSID.c_str(), sizeof(WM_config.WiFi_Creds[i].wifi_ssid) - 1);
if (strlen(tempPW.c_str()) < sizeof(WM_config.WiFi_Creds[i].wifi_pw) - 1)
strcpy(WM_config.WiFi_Creds[i].wifi_pw, tempPW.c_str());
else
strncpy(WM_config.WiFi_Creds[i].wifi_pw, tempPW.c_str(), sizeof(WM_config.WiFi_Creds[i].wifi_pw) - 1);
if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) )
{
LOGERROR3(F("* Add SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw );
wifiMulti.addAP(WM_config.WiFi_Creds[i].wifi_ssid, WM_config.WiFi_Creds[i].wifi_pw);
}
}
ESP_wifiManager.getSTAStaticIPConfig(WM_STA_IPconfig);
displayIPConfigStruct(WM_STA_IPconfig);
saveConfigData();
}