Skip to content

Commit e56ca41

Browse files
authored
Enable deactivation of BLE connect (1technophile#895)
Useful if you want to scan continuously for BLE device, in this case you should deactivate the BLE connect to avoid concurrency between scan and connect
1 parent 928f909 commit e56ca41

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

docs/use/ble.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ Once the forced scan has completed, the previous scan interval value will be res
9393

9494
The default value `TimeBtwRead` is set into config_BT.h or into your .ini file for platformio users.
9595

96+
If you want to scan continuously for BLE devices, for example for beacon location you can set the interval to 1ms:
97+
98+
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"interval":1}'`
99+
100+
In this case you should deactivate the BLE connection mechanism to avoid concurrency between scan and connections (see chapter below, bleconnect).
101+
96102
::: tip
97103
For certain devices like LYWSD03MMC OpenMQTTGateway use a connection (due to the fact that the advertized data are encrypted), this connection mechanism is launched after every `ScanBeforeConnect` per default, you can modify it by following the procedure below.
98104
:::
@@ -105,7 +111,7 @@ If you want to change the number of BLE scans that are done before a BLE connect
105111

106112
The BLE connect will be done every 30 * (`TimeBtwRead` + `Scan_duration`), 30 * (55000 + 10000) = 1950000ms
107113

108-
## Setting if the gateway publish all the BLE devices scanned or only the detected sensors
114+
## Setting if the gateway publishes all the BLE devices scanned or only the detected sensors
109115

110116
If you want to change this characteristic:
111117

@@ -117,6 +123,16 @@ With Home Assistant, this command is directly avalaible through MQTT auto discov
117123

118124
The gateway will publish only the detected sensors like Mi Flora, Mi jia, LYWSD03MMC... and not the other BLE devices. This is usefull if you don't use the gateway for presence detection but only to retrieve sensors data.
119125

126+
## Setting if the gateway connects to BLE devices eligibles on ESP32
127+
128+
If you want to change this characteristic:
129+
130+
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"bleconnect":false}'`
131+
132+
::: tip
133+
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
134+
:::
135+
120136
## Setting the minimum RSSI accepted to publish device data
121137

122138
If you want to change the minimum RSSI value accepted for a device to be published, you can change it by MQTT. For example if you want to set -80

main/ZgatewayBT.ino

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ void coreTask(void* pvParameters) {
744744
} else if (!ProcessLock) {
745745
BLEscan();
746746
// Launching a connect every BLEscanBeforeConnect
747-
if (!(scanCount % BLEscanBeforeConnect) || scanCount == 1)
747+
if ((!(scanCount % BLEscanBeforeConnect) || scanCount == 1) && bleConnect)
748748
BLEconnect();
749749
dumpDevices();
750750
}
@@ -1462,7 +1462,8 @@ void BTforceScan() {
14621462
BTtoMQTT();
14631463
Log.trace(F("Scan done" CR));
14641464
# ifdef ESP32
1465-
BLEconnect();
1465+
if (bleConnect)
1466+
BLEconnect();
14661467
# endif
14671468
} else {
14681469
Log.trace(F("Cannot launch scan due to other process running" CR));
@@ -1519,6 +1520,15 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
15191520
publishOnlySensors = (bool)BTdata["onlysensors"];
15201521
Log.notice(F("New value onlysensors: %T" CR), publishOnlySensors);
15211522
}
1523+
# ifdef ESP32
1524+
// Attempts to connect to elligible devices or not
1525+
if (BTdata.containsKey("bleconnect")) {
1526+
Log.trace(F("Do we initiate a connection to retrieve data" CR));
1527+
Log.trace(F("Previous value: %T" CR), bleConnect);
1528+
bleConnect = (bool)BTdata["bleconnect"];
1529+
Log.notice(F("New value bleConnect: %T" CR), bleConnect);
1530+
}
1531+
# endif
15221532
// MinRSSI set
15231533
if (BTdata.containsKey("minrssi")) {
15241534
// storing Min RSSI for further use if needed

main/ZmqttDiscovery.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,14 @@ void pubMqttDiscovery() {
709709
"", "", true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible,is a gateway entity, command topic
710710
"", "", "", "", true // device name, device manufacturer, device model, device mac, retain
711711
);
712+
createDiscovery("switch", //set Type
713+
"", "BT: Connect to devices", (char*)getUniqueId("bleconnect", "").c_str(), //set state_topic,name,uniqueId
714+
"", "", "", //set availability_topic,device_class,value_template,
715+
"{\"bleconnect\":true}", "{\"bleconnect\":false}", "", //set,payload_on,payload_off,unit_of_meas,
716+
0, //set off_delay
717+
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
718+
"", "", "", "", true // device name, device manufacturer, device model, device mac, retain
719+
);
712720
# endif
713721
# endif
714722
}

main/config_BT.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ extern void launchBTDiscovery();
3636
extern int btQueueBlocked;
3737
extern int btQueueLengthSum;
3838
extern int btQueueLengthCount;
39+
# ifndef AttemptBLECOnnect
40+
# define AttemptBLECOnnect true //do we by default attempt a BLE connection to sensors with ESP32
41+
# endif
42+
bool bleConnect = AttemptBLECOnnect;
3943
#endif
4044

4145
/*----------------------BT topics & parameters-------------------------*/

0 commit comments

Comments
 (0)