-
Notifications
You must be signed in to change notification settings - Fork 781
/
ZgatewayBT.ino
912 lines (817 loc) · 32.7 KB
/
ZgatewayBT.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
/*
OpenMQTTGateway - ESP8266 or Arduino program for home automation
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal/BLE and a MQTT broker
Send and receiving command by MQTT
This gateway enables to:
- publish MQTT data to a different topic related to BLE devices rssi signal
- publish MQTT data related to mi flora temperature, moisture, fertility and lux
- publish MQTT data related to mi jia indoor temperature & humidity sensor
Copyright: (c)Florian ROBERT
Copyright: (c)Sven Henkel for Mi Flora Battery reading (https://github.com/sidddy/flora)
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd https://github.com/dinosd/BLE_PROXIMITY for inspiring me how to implement the gateway
*/
#include "User_config.h"
#ifdef ZgatewayBT
#include <vector>
using namespace std;
vector<BLEdeviceDisc> devices;
void setWorBMac(char * mac , bool isWhite){
bool foundMac = false;
for(vector<BLEdeviceDisc>::iterator p = devices.begin(); p != devices.end(); ++p){
if((strcmp(p->macAdr,mac) == 0)){
p->isWhtL = isWhite;
p->isBlkL = !isWhite;
foundMac = true;
}
}
if (!foundMac) {
BLEdeviceDisc device;
strcpy( device.macAdr,mac );
device.isDisc = false;
device.isWhtL = isWhite;
device.isBlkL = !isWhite;
devices.push_back(device);
}
}
bool oneWhite(){
for(vector<BLEdeviceDisc>::iterator p = devices.begin(); p != devices.end(); ++p){
if(p->isWhtL) return true;
}
return false;
}
bool isWhite(char * mac){
for(vector<BLEdeviceDisc>::iterator p = devices.begin(); p != devices.end(); ++p){
if((strcmp(p->macAdr,mac) == 0)){
return p->isWhtL;
}
}
return false;
}
bool isBlack(char * mac){
for(vector<BLEdeviceDisc>::iterator p = devices.begin(); p != devices.end(); ++p){
if((strcmp(p->macAdr,mac) == 0)){
return p->isBlkL;
}
}
return false;
}
bool isDiscovered(char * mac){
for(vector<BLEdeviceDisc>::iterator p = devices.begin(); p != devices.end(); ++p){
if((strcmp(p->macAdr,mac) == 0)){
return p->isDisc;
}
}
return false;
}
void dumpDevices(){
trc("dumpDevices list");
for(vector<BLEdeviceDisc>::iterator p = devices.begin(); p != devices.end(); ++p){
trc(p->macAdr);
trc(p->isDisc);
trc(p->isWhtL);
trc(p->isBlkL);
trc(p->devTyp);
}
}
void strupp(char* beg)
{
while (*beg = toupper(*beg))
++beg;
}
void MiFloraDiscovery(char * mac){
trc(F("MiFloraDiscovery"));
#ifdef ZmqttDiscovery
#define MiFloraparametersCount 4
char * MiFlorasensor[MiFloraparametersCount][8] = {
{"sensor", "MiFlora-lux", mac, "illuminance","{{ value_json.lux | is_defined }}","", "", "lu"} ,
{"sensor", "MiFlora-tem", mac,"temperature","{{ value_json.tem | is_defined }}","", "", "°C"} ,
{"sensor", "MiFlora-fer", mac,"","{{ value_json.fer | is_defined }}","", "", ""} ,
{"sensor", "MiFlora-moi", mac,"","{{ value_json.moi | is_defined }}","", "", "%"}
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
};
for (int i=0;i<MiFloraparametersCount;i++){
trc(F("CreateDiscoverySensor"));
trc(MiFlorasensor[i][1]);
String discovery_topic = String(subjectBTtoMQTT) + String(mac);
String unique_id = String(mac) + "-" + MiFlorasensor[i][1];
createDiscovery(MiFlorasensor[i][0],
(char *)discovery_topic.c_str(), MiFlorasensor[i][1], (char *)unique_id.c_str(),
will_Topic, MiFlorasensor[i][3], MiFlorasensor[i][4],
MiFlorasensor[i][5], MiFlorasensor[i][6], MiFlorasensor[i][7],
0,"","",true,"");
}
#endif
BLEdeviceDisc device;
strcpy( device.macAdr, mac );
strcpy( device.devTyp, "MIFLO");
device.isDisc = true;
device.isWhtL = false;
device.isBlkL = false;
devices.push_back(device);
}
void MiJiaDiscovery(char * mac){
trc(F("MiJiaDiscovery"));
#ifdef ZmqttDiscovery
#define MiJiaparametersCount 3
char * MiJiasensor[MiJiaparametersCount][8] = {
{"sensor", "MiJia-batt", mac, "battery","{{ value_json.batt | is_defined }}","", "", "V"} ,
{"sensor", "MiJia-tem", mac,"temperature","{{ value_json.tem | is_defined }}","", "", "°C"} ,
{"sensor", "MiJia-hum", mac,"humidity","{{ value_json.hum | is_defined }}","", "", "%"}
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
};
for (int i=0;i<MiJiaparametersCount;i++){
trc(F("CreateDiscoverySensor"));
trc(MiJiasensor[i][1]);
String discovery_topic = String(subjectBTtoMQTT) + String(mac);
String unique_id = String(mac) + "-" + MiJiasensor[i][1];
createDiscovery(MiJiasensor[i][0],
(char *)discovery_topic.c_str(), MiJiasensor[i][1], (char *)unique_id.c_str(),
will_Topic, MiJiasensor[i][3], MiJiasensor[i][4],
MiJiasensor[i][5], MiJiasensor[i][6], MiJiasensor[i][7],
0,"","",true,"");
}
#endif
BLEdeviceDisc device;
strcpy( device.macAdr, mac );
strcpy( device.devTyp, "MIJIA");
device.isDisc = true;
device.isWhtL = false;
device.isBlkL = false;
devices.push_back(device);
}
void LYWSD02Discovery(char * mac){
trc(F("LYWSD02Discovery"));
#ifdef ZmqttDiscovery
#define LYWSD02parametersCount 3
char * LYWSD02sensor[LYWSD02parametersCount][8] = {
{"sensor", "LYWSD02-batt", mac, "battery","{{ value_json.batt | is_defined }}","", "", "V"} ,
{"sensor", "LYWSD02-tem", mac,"temperature","{{ value_json.tem | is_defined }}","", "", "°C"} ,
{"sensor", "LYWSD02-hum", mac,"humidity","{{ value_json.hum | is_defined }}","", "", "%"}
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
};
for (int i=0;i<LYWSD02parametersCount;i++){
trc(F("CreateDiscoverySensor"));
trc(LYWSD02sensor[i][1]);
String discovery_topic = String(subjectBTtoMQTT) + String(mac);
String unique_id = String(mac) + "-" + LYWSD02sensor[i][1];
createDiscovery(LYWSD02sensor[i][0],
(char *)discovery_topic.c_str(), LYWSD02sensor[i][1], (char *)unique_id.c_str(),
will_Topic, LYWSD02sensor[i][3], LYWSD02sensor[i][4],
LYWSD02sensor[i][5], LYWSD02sensor[i][6], LYWSD02sensor[i][7],
0,"","",true,"");
}
#endif
BLEdeviceDisc device;
strcpy( device.macAdr, mac );
strcpy( device.devTyp, "LYWSD");
device.isDisc = true;
device.isWhtL = false;
device.isBlkL = false;
devices.push_back(device);
}
void CLEARGRASSTRHDiscovery(char * mac){
trc(F("CLEARGRASSTRHDiscovery"));
#ifdef ZmqttDiscovery
#define CLEARGRASSTRHparametersCount 3
char * CLEARGRASSTRHsensor[CLEARGRASSTRHparametersCount][8] = {
{"sensor", "CLEARGRASSTRH-batt", mac, "battery","{{ value_json.batt | is_defined }}","", "", "V"} ,
{"sensor", "CLEARGRASSTRH-tem", mac,"temperature","{{ value_json.tem | is_defined }}","", "", "°C"} ,
{"sensor", "CLEARGRASSTRH-hum", mac,"humidity","{{ value_json.hum | is_defined }}","", "", "%"}
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
};
for (int i=0;i<CLEARGRASSTRHparametersCount;i++){
trc(F("CreateDiscoverySensor"));
trc(CLEARGRASSTRHsensor[i][1]);
String discovery_topic = String(subjectBTtoMQTT) + String(mac);
String unique_id = String(mac) + "-" + CLEARGRASSTRHsensor[i][1];
createDiscovery(CLEARGRASSTRHsensor[i][0],
(char *)discovery_topic.c_str(), CLEARGRASSTRHsensor[i][1], (char *)unique_id.c_str(),
will_Topic, CLEARGRASSTRHsensor[i][3], CLEARGRASSTRHsensor[i][4],
CLEARGRASSTRHsensor[i][5], CLEARGRASSTRHsensor[i][6], CLEARGRASSTRHsensor[i][7],
0,"","",true,"");
}
#endif
BLEdeviceDisc device;
trc(F("mac"));
strcpy( device.macAdr, mac );
trc(F("name"));
strcpy( device.devTyp, "CLEARG");
trc(F("others"));
device.isDisc = true;
trc(F("others1"));
device.isWhtL = false;
trc(F("others2"));
device.isBlkL = false;
trc(F("others3"));
devices.push_back(device);
}
#ifdef ESP32
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
// core task implementation thanks to https://techtutorialsx.com/2017/05/09/esp32-running-code-on-a-specific-core/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
//Time used to wait for an interval before resending BLE infos
unsigned long timeBLE= 0;
//core on which the BLE detection task will run
static int taskCore = 0;
//wifi or ble usage, as ESP32 has only one antenna we monitor the wifi or ble usage to avoid 2 tasks using both
unsigned long bleUseCount = 0;
// the remote service we wish to connect to
static BLEUUID serviceUUID("00001204-0000-1000-8000-00805f9b34fb");
// the characteristic of the remote service we are interested in
static BLEUUID uuid_version_battery("00001a02-0000-1000-8000-00805f9b34fb");
static BLEUUID uuid_write_mode("00001a00-0000-1000-8000-00805f9b34fb");
BLEClient* getClient(BLEAddress Address) {
BLEClient* Client = BLEDevice::createClient();
if (!Client->connect(Address)) {
trc("- Connection failed, skipping");
return nullptr;
}
trc("- Connection successful");
return Client;
}
BLERemoteService* getService(BLEClient* Client) {
BLERemoteService* Service = nullptr;
try {
Service = Client->getService(serviceUUID);
}
catch (...) {
// something went wrong
}
if (Service == nullptr) {
trc("- Failed to find data service");
}
else {
trc("- Found data service");
}
return Service;
}
bool forceServiceDataMode(BLERemoteService* Service) {
BLERemoteCharacteristic* Characteristic;
// get device mode characteristic, needs to be changed to read data
trc("- Force device in data mode");
Characteristic = nullptr;
try {
Characteristic = Service->getCharacteristic(uuid_write_mode);
}
catch (...) {
// something went wrong
}
if (Characteristic == nullptr) {
trc("-- Failed, skipping device");
return false;
}
// write the magic data
uint8_t buf[2] = {0xA0, 0x1F};
Characteristic->writeValue(buf, 2, true);
delay(500);
return true;
}
int readFloraBatteryCharacteristic(BLERemoteService* Service) {
BLERemoteCharacteristic* Characteristic = nullptr;
int batteryValue = -1;
// get the device battery characteristic
trc("- Access battery characteristic from device");
try {
Characteristic = Service->getCharacteristic(uuid_version_battery);
}
catch (...) {
// something went wrong
}
if (Characteristic == nullptr) {
trc("-- Failed, skipping battery level");
return batteryValue;
}
// read characteristic value
trc("- Read value from characteristic");
std::string value;
try{
value = Characteristic->readValue();
}
catch (...) {
// something went wrong
trc("-- Failed, skipping battery level");
return batteryValue;
}
const char *val2 = value.c_str();
batteryValue = val2[0];
char buffer[64];
trc("-- Battery: ");
trc(batteryValue);
snprintf(buffer, 64, "%d", batteryValue);
return batteryValue;
}
bool processService(BLERemoteService* Service, char* deviceMacAddress) {
trc("Processing service");
// set device in data mode
if (!forceServiceDataMode(Service)) {
return false;
}
int batteryValue;
batteryValue = readFloraBatteryCharacteristic(Service);
if(batteryValue != -1){
trc(F("Creating BLE buffer"));
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
JsonObject& BLEdata = jsonBuffer.createObject();
BLEdata.set("batt", (int)batteryValue);
String mac_adress = String(deviceMacAddress);
mac_adress.replace(":","");
String mactopic = subjectBTtoMQTT + mac_adress;
pub((char *)mactopic.c_str(),BLEdata); // publish device even if there is no service data
return true;
}else{
return false;
}
}
bool processDevice(BLEAddress Address, char* deviceMacAddress) {
trc("Processing device at ");
trc(deviceMacAddress);
// connect to ble server
BLEClient* Client = getClient(Address);
if (Client == nullptr) {
return false;
}
trc("get service");
// connect data service
BLERemoteService* Service = getService(Client);
if (Service == nullptr) {
Client->disconnect();
return false;
}
trc("process data");
// process devices data
bool success = processService(Service, deviceMacAddress);
trc("disconnect");
// disconnect from device
Client->disconnect();
return success;
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
trc(F("Creating BLE buffer"));
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
JsonObject& BLEdata = jsonBuffer.createObject();
String mac_adress = advertisedDevice.getAddress().toString().c_str();
BLEdata.set("id", (char *)mac_adress.c_str());
mac_adress.replace(":","");
mac_adress.toUpperCase();
String mac_adress_upper = mac_adress;
BLEAddress Address((char *)mac_adress_upper.c_str());
mac_adress.replace(":","");
String mactopic = subjectBTtoMQTT + mac_adress;
char mac[mac_adress.length()+1];
mac_adress.toCharArray(mac,mac_adress.length()+1);
trc("device detected");
trc(mac);
if((!oneWhite() || isWhite(mac)) && !isBlack(mac)){ //if not black listed mac we go AND if we have no white mac or this mac is white we go out
if (advertisedDevice.haveName()) BLEdata.set("name", (char *)advertisedDevice.getName().c_str());
if (advertisedDevice.haveManufacturerData()) BLEdata.set("manufacturerdata", (char *)advertisedDevice.getManufacturerData().c_str());
if (advertisedDevice.haveRSSI()) BLEdata.set("rssi", (int) advertisedDevice.getRSSI());
if (advertisedDevice.haveTXPower()) BLEdata.set("txpower", (int8_t) advertisedDevice.getTXPower());
#ifdef subjectHomePresence
if (advertisedDevice.haveRSSI()) haRoomPresence(BLEdata);// this device has an rssi in consequence we can use it for home assistant room presence component
#endif
if (advertisedDevice.haveServiceData()){
trc(F("Get services data :"));
int serviceDataCount = advertisedDevice.getServiceDataCount();
trc(serviceDataCount);
for (int j = 0; j < serviceDataCount;j++){
std::string serviceData = advertisedDevice.getServiceData(j);
int serviceDataLength = serviceData.length();
String returnedString = "";
for (int i=0; i<serviceDataLength; i++)
{
int a = serviceData[i];
if (a < 16) {
returnedString = returnedString + "0";
}
returnedString = returnedString + String(a,HEX);
}
char service_data[returnedString.length()+1];
returnedString.toCharArray(service_data,returnedString.length()+1);
service_data[returnedString.length()] = '\0';
#ifdef pubBLEServiceData
BLEdata.set("servicedata", service_data);
BLEdata.set("servicedatauuid", (char *)advertisedDevice.getServiceDataUUID(j).toString().c_str());
#endif
pub((char *)mactopic.c_str(),BLEdata);
if (strstr(BLEdata["servicedatauuid"].as<char*>(),"fe95") != NULL){
trc("Processing BLE device data");
int pos = -1;
pos = strpos(service_data,"209800");
if (pos != -1){
trc(F("mi flora data reading"));
//example "servicedata":"71209800bc63b6658d7cc40d0910023200"
//if(!isDiscovered(mac)) MiFloraDiscovery(mac);
process_data(pos - 24,service_data,mac);
}
pos = -1;
pos = strpos(service_data,"20aa01");
if (pos != -1){
trc(F("mi jia data reading"));
//if(!isDiscovered(mac)) MiJiaDiscovery(mac);
process_data(pos - 26,service_data,mac);
}
pos = -1;
pos = strpos(service_data,"205b04");
if (pos != -1){
trc(F("LYWSD02 data reading"));
//example "servicedata":"70205b04b96ab883c8593f09041002e000"
//if(!isDiscovered(mac)) LYWSD02Discovery(mac);
process_data(pos - 24,service_data,mac);
}
pos = -1;
pos = strpos(service_data,"304703");
if (pos != -1){
trc(F("ClearGrass T RH data reading"));
//example "servicedata":"5030470340743e10342d58041002d6000a100164"
//if(!isDiscovered(mac)) CLEARGRASSTRHDiscovery(mac);
process_data(pos - 26,service_data,mac);
}
}
}
}else{
pub((char *)mactopic.c_str(),BLEdata); // publish device even if there is no service data
}
}else{
trc(F("Filtered mac device"));
}
dumpDevices();
}
};
void BLEscan(){
taskInProgress = true;
bleUseCount++;
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed=1;
TIMERG0.wdt_wprotect=0;
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
MyAdvertisedDeviceCallbacks myCallbacks;
pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks);
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
BLEScanResults foundDevices = pBLEScan->start(Scan_duration);
taskInProgress = false;
}
void BLEread(){
taskInProgress = true;
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed=1;
TIMERG0.wdt_wprotect=0;
BLEDevice::init("");
BLEDevice::setPower(ESP_PWR_LVL_P7);
trc(F("mi flora battery reading"));
String mac_adress_upper = "C4:7C:8D:65:B6:63";
BLEAddress Address((char *)mac_adress_upper.c_str());
processDevice(Address,(char *)mac_adress_upper.c_str() );
taskInProgress = false;
}
void coreTask( void * pvParameters ){
String taskMessage = "BLEscan running on core ";
taskMessage = taskMessage + xPortGetCoreID();
while(true){
trc(taskMessage);
delay(BLEinterval);
BLEscan();
}
}
void coreTask2( void * pvParameters ){
String taskMessage = "BLEread running on core ";
taskMessage = taskMessage + xPortGetCoreID();
while(true){
trc(taskMessage);
delay(TimeBtw_Reads);
BLEread();
}
}
void setupBT(){
BLEinterval = TimeBtw_Scans;
trc(F("BLEinterval btw scans"));
trc(BLEinterval);
trc(F("BLEinterval btw reads"));
trc(TimeBtw_Reads);
// we setup a task with priority one to avoid conflict with other gateways
xTaskCreatePinnedToCore(
coreTask, /* Function to implement the task */
"coreTask", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
2, /* Priority of the task */
NULL, /* Task handle. */
taskCore); /* Core where the task should run */
xTaskCreatePinnedToCore(
coreTask2, /* Function to implement the task */
"coreTask2", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
NULL, /* Task handle. */
taskCore ); /* Core where the task should run */
trc(F("ZgatewayBT multicore ESP32 setup done "));
}
bool BTtoMQTT(){ // for on demand BLE scans
#ifndef ESP32
BLEscan();
#endif
}
#else // arduino or ESP8266 working with HM10/11
#include <SoftwareSerial.h>
#define STRING_MSG "OK+DISC:"
#define QUESTION_MSG "AT+DISA?"
#define RESPONSE_MSG "OK+DISIS"
#define RESP_END_MSG "OK+DISCE"
#define SETUP_MSG "OK+RESET"
SoftwareSerial softserial(BT_RX, BT_TX);
String returnedString = "";
unsigned long timebt = 0;
// this struct define which parts of the hexadecimal chain we extract and what to do with these parts
struct decompose d[6] = {{"mac",16,12,true},{"typ",28,2,false},{"rsi",30,2,false},{"rdl",32,2,false},{"sty",44,4,true},{"rda",34,60,false}};
void setupBT() {
BLEinterval = TimeBtw_Scans;
trc(F("BLEinterval btw scans"));
trc(BLEinterval);
softserial.begin(9600);
softserial.print(F("AT+ROLE1"));
delay(100);
softserial.print(F("AT+IMME1"));
delay(100);
softserial.print(F("AT+RESET"));
delay(100);
#ifdef HM_BLUE_LED_STOP
softserial.print(F("AT+PIO11")); // When not connected (as in BLE mode) the LED is off. When connected the LED is solid on.
#endif
delay(100);
trc(F("ZgatewayBT HM1X setup done "));
}
bool BTtoMQTT() {
//extract serial data from module in hexa format
while (softserial.available() > 0) {
int a = softserial.read();
if (a < 16) {
returnedString = returnedString + "0";
}
returnedString = returnedString + String(a,HEX);
}
if (millis() > (timebt + BLEinterval)) {//retriving data
timebt = millis();
#if defined(ESP8266)
yield();
#endif
if (returnedString != "") {
size_t pos = 0;
while ((pos = returnedString.lastIndexOf(BLEdelimiter)) != -1) {
#if defined(ESP8266)
yield();
#endif
String token = returnedString.substring(pos);
returnedString.remove(pos,returnedString.length() );
char token_char[token.length()+1];
token.toCharArray(token_char, token.length()+1);
trc(token);
if ( token.length() > 60){// we extract data only if we have detailled infos
for(int i =0; i<6;i++)
{
extract_char(token_char,d[i].extract,d[i].start, d[i].len ,d[i].reverse, false);
if (i == 3) d[5].len = (int)strtol(d[i].extract, NULL, 16) * 2; // extracting the length of the rest data
}
if((strlen(d[0].extract)) == 12) // if a mac adress is detected we publish it
{
trc(F("Creating BLE buffer"));
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
JsonObject& BLEdata = jsonBuffer.createObject();
strupp(d[0].extract);
if(isBlack(d[0].extract)) return false; //if black listed mac we go out
if(oneWhite() && !isWhite(d[0].extract)) return false; //if we have at least one white mac and this mac is not white we go out
#ifdef subjectHomePresence
String HomePresenceId;
for (int i = 0; i<12; i++){
HomePresenceId += String(d[0].extract[i]);
if(((i-1) % 2 == 0) && (i!=11)) HomePresenceId += ":";
}
trc(F("HomePresenceId"));
trc(HomePresenceId);
BLEdata.set("id", (char *)HomePresenceId.c_str());
#endif
String topic = subjectBTtoMQTT + String(d[0].extract);
int rssi = (int)strtol(d[2].extract, NULL, 16) - 256;
BLEdata.set("rssi", (int)rssi);
#ifdef subjectHomePresence
haRoomPresence(BLEdata);// this device has an rssi in consequence we can use it for home assistant room presence component
#endif
String Service_data(d[5].extract);
Service_data = Service_data.substring(14);
#ifdef pubBLEServiceData
BLEdata.set("servicedata", (char *)Service_data.c_str());
#endif
pub((char *)topic.c_str(),BLEdata);
int pos = -1;
pos = strpos(d[5].extract,"209800");
if (pos != -1) {
trc("mi flora data reading");
#ifdef ZmqttDiscovery
if(!isDiscovered(d[0].extract)) MiFloraDiscovery(d[0].extract);
#endif
bool result = process_data(pos - 38,(char *)Service_data.c_str(),d[0].extract);
}
pos = -1;
pos = strpos(d[5].extract,"20aa01");
//example "servicedata":"5020aa0194dfaa33342d580d1004e3002c02"
if (pos != -1){
trc("mi jia data reading");
#ifdef ZmqttDiscovery
if(!isDiscovered(d[0].extract)) MiJiaDiscovery(d[0].extract);
#endif
bool result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract);
}
pos = -1;
pos = strpos(d[5].extract,"205b04");
//example "servicedata":"141695fe70205b0461298882c8593f09061002d002"
if (pos != -1){
trc("LYWSD02 data reading");
#ifdef ZmqttDiscovery
if(!isDiscovered(d[0].extract)) LYWSD02Discovery(d[0].extract);
#endif
bool result = process_data(pos - 38,(char *)Service_data.c_str(),d[0].extract);
}
pos = -1;
pos = strpos(d[5].extract,"304703");
if (pos != -1){
trc("CLEARGRASSTRH data reading");
#ifdef ZmqttDiscovery
if(!isDiscovered(d[0].extract)) CLEARGRASSTRHDiscovery(d[0].extract);
#endif
bool result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract);
}
return true;
}
}
}
returnedString = ""; //init data string
return false;
}
softserial.print(F(QUESTION_MSG));
return false;
}else{
return false;
}
}
#endif
bool process_data(int offset, char * rest_data, char * mac_adress){
trc(F("Creating BLE buffer"));
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
JsonObject& BLEdata = jsonBuffer.createObject();
trc("rest_data");
trc(rest_data);
int data_length = 0;
switch (rest_data[51 + offset]) {
case '1' :
case '2' :
case '3' :
case '4' :
data_length = ((rest_data[51 + offset] - '0') * 2)+1;
trc("data_length");
trc(data_length);
break;
default:
trc("can't read data_length");
return false;
}
char rev_data[data_length];
char data[data_length];
memcpy( rev_data, &rest_data[52 + offset], data_length );
rev_data[data_length] = '\0';
// reverse data order
revert_hex_data(rev_data, data, data_length);
double value = strtol(data, NULL, 16);
trc(value);
char val[12];
String mactopic(mac_adress);
mactopic = subjectBTtoMQTT + mactopic;
// second value
char val2[12];
trc("rest_data");
trc(rest_data);
// Mi flora provides tem(perature), (earth) moi(sture), fer(tility) and lux (illuminance)
// Mi Jia provides tem(perature), batt(erry) and hum(idity)
// following the value of digit 47 we determine the type of data we get from the sensor
switch (rest_data[47 + offset]) {
case '9' :
BLEdata.set("fer", (double)value);
break;
case '4' :
if (value > 65000) value = value - 65535;
BLEdata.set("tem", (double)value/10);
break;
case '6' :
if (value > 65000) value = value - 65535;
BLEdata.set("hum", (double)value/10);
break;
case '7' :
BLEdata.set("lux", (double)value);
break;
case '8' :
BLEdata.set("moi", (double)value);
break;
case 'a' : // batteryLevel
BLEdata.set("batt", (double)value);
break;
case 'd' : // temp+hum
char tempAr[8];
// humidity
memcpy(tempAr, data, 4);
tempAr[4] = '\0';
value = strtol(tempAr, NULL, 16);
if (value > 65000) value = value - 65535;
BLEdata.set("hum", (double)value/10);
// temperature
memcpy(tempAr, &data[4], 4);
tempAr[4] = '\0';
value = strtol(tempAr, NULL, 16);
if (value > 65000) value = value - 65535;
BLEdata.set("tem", (double)value/10);
break;
default:
trc("can't read values");
return false;
}
pub((char *)mactopic.c_str(),BLEdata);
return true;
}
#ifdef subjectHomePresence
void haRoomPresence(JsonObject& HomePresence){
int BLErssi = HomePresence["rssi"];
trc(F("BLErssi"));
trc(BLErssi);
int txPower = HomePresence["txpower"]|0;
if (txPower == 0) txPower = -59; //if tx power is not found we set a default calibration value
trc(F("txPower"));
trc(txPower);
double ratio = BLErssi*1.0/txPower;
double distance;
if (ratio < 1.0) {
distance = pow(ratio,10);
}else{
distance = (0.89976)* pow(ratio,7.7095) + 0.111;
}
HomePresence["distance"] = distance;
trc(F("BLE DISTANCE :"));
trc(distance);
pub(subjectHomePresence,HomePresence);
}
#endif
void MQTTtoBT(char * topicOri, JsonObject& BTdata) { // json object decoding
if (strcmp(topicOri,subjectMQTTtoBTset) == 0){
trc(F("MQTTtoBT json set"));
// Black list & white list set
int WLsize = BTdata["white-list"].size();
if(WLsize > 0){
trc(F("WL set"));
for (int i = 0; i < WLsize; i++){
const char * whiteMac = BTdata["white-list"][i];
setWorBMac((char *)whiteMac, true);
}
}
int BLsize = BTdata["black-list"].size();
if(BLsize > 0){
trc(F("BL set"));
for (int i = 0; i < BLsize; i++){
const char * blackMac = BTdata["black-list"][i];
setWorBMac((char *)blackMac,false);
}
}
if (BLsize > 0 || WLsize > 0) dumpDevices();
// Scan interval set
if (BTdata.containsKey("interval")){
trc(F("BLE interval set"));
// storing BLE interval for further use if needed
unsigned int prevBLEinterval = BLEinterval;
// set BLE interval if present if not setting default value
BLEinterval = (unsigned int)BTdata["interval"]|prevBLEinterval;
trc(BLEinterval);
if (BLEinterval == 0) {
if(BTtoMQTT())// as BLEinterval is = to 0 we can launch the loop and the scan will execute immediately
trc(F("Scan done"));
BLEinterval = prevBLEinterval; // as 0 was just used as a command we recover previous scan duration
}
}
}
}
#endif