set BLE scans interval per mqtt and on demand BLE scans
#373 to change the BLE scan interval publish a payload {"interval":66666} in ms to "home/OpenMQTTGateway/commands/MQTTtoBT/set" to generate a scan publish a 0 interval payload {"interval":0}
This commit is contained in:
1 parent
8aad413ce3
commit
56bfef8e9c
2 files changed
+55
-30
No files matched your search
+50
-25
@@ -32,7 +32,6 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
||||
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
vector<BLEdevice> devices;
|
||||
|
||||
#ifdef ESP32
|
||||
@@ -126,6 +125,9 @@ vector<BLEdevice> devices;
|
||||
};
|
||||
|
||||
void setupBT(){
|
||||
BLEinterval = TimeBtw_Read;
|
||||
trc(F("BLEinterval btw scans"));
|
||||
trc(BLEinterval);
|
||||
#ifdef multiCore
|
||||
// we setup a task with priority one to avoid conflict with other gateways
|
||||
xTaskCreatePinnedToCore(
|
||||
@@ -150,37 +152,37 @@ vector<BLEdevice> devices;
|
||||
|
||||
while(true){
|
||||
trc(taskMessage);
|
||||
delay(TimeBtw_Read);
|
||||
|
||||
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);
|
||||
delay(BLEinterval);
|
||||
BLEscan();
|
||||
}
|
||||
}
|
||||
boolean BTtoMQTT(){ // for on demande BLE scans
|
||||
BLEscan();
|
||||
}
|
||||
#else
|
||||
boolean BTtoMQTT(){
|
||||
unsigned long now = millis();
|
||||
if (now > (timeBLE + TimeBtw_Read)) {
|
||||
if (now > (timeBLE + BLEinterval)) {
|
||||
timeBLE = now;
|
||||
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);
|
||||
BLEscan();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
void BLEscan(){
|
||||
|
||||
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);
|
||||
}
|
||||
#else // arduino or ESP8266 working with HM10/11
|
||||
|
||||
#include <SoftwareSerial.h>
|
||||
@@ -200,6 +202,9 @@ vector<BLEdevice> devices;
|
||||
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_Read;
|
||||
trc(F("BLEinterval btw scans"));
|
||||
trc(BLEinterval);
|
||||
softserial.begin(9600);
|
||||
softserial.print(F("AT+ROLE1"));
|
||||
delay(100);
|
||||
@@ -225,14 +230,14 @@ vector<BLEdevice> devices;
|
||||
returnedString = returnedString + String(a,HEX);
|
||||
}
|
||||
|
||||
if (millis() > (timebt + TimeBtw_Read)) {//retriving data
|
||||
if (millis() > (timebt + BLEinterval)) {//retriving data
|
||||
timebt = millis();
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
if (returnedString != "") {
|
||||
size_t pos = 0;
|
||||
while ((pos = returnedString.lastIndexOf(delimiter)) != -1) {
|
||||
while ((pos = returnedString.lastIndexOf(BLEdelimiter)) != -1) {
|
||||
#if defined(ESP8266)
|
||||
yield();
|
||||
#endif
|
||||
@@ -487,24 +492,44 @@ void haRoomPresence(JsonObject& HomePresence){
|
||||
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); //TO DO catch mac adress > 12
|
||||
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); //TO DO catch mac adress > 12
|
||||
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
|
||||
}
|
||||
}
|
||||
dumpDevices();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -25,21 +25,21 @@
|
||||
*/
|
||||
/*----------------------BT topics & parameters-------------------------*/
|
||||
#define subjectBTtoMQTT Base_Topic Gateway_Name "/BTtoMQTT/"
|
||||
#define subjectMQTTtoRF Base_Topic Gateway_Name "/commands/MQTTto433"
|
||||
#define subjectMQTTtoBTset Base_Topic Gateway_Name "/commands/MQTTtoBT/set"
|
||||
#define TimeBtw_Read 55555 //define the time between 2 scans
|
||||
#define TimeBtw_Read 55555 //define default time between 2 scans
|
||||
#define Scan_duration 10 //define the time for a scan
|
||||
#define HM-10
|
||||
#define HM-10
|
||||
//#define HM-11 // uncomment this line if you use HM-11 and comment the line above
|
||||
//#define HM_BLUE_LED_STOP true //uncomment to stop the blue led light of HM1X
|
||||
#define delimiter "4f4b2b444953413a"
|
||||
#define delimiter_length 16
|
||||
#define BLEdelimiter "4f4b2b444953413a"
|
||||
#define pubBLEServiceData true // comment if you don't want to publish service data (in case you are having too heavy service data) https://github.com/1technophile/OpenMQTTGateway/issues/318#issuecomment-446064707
|
||||
|
||||
/*-------------------HOME ASSISTANT ROOM PRESENCE ----------------------*/
|
||||
// if not commented Home presence integration with HOME ASSISTANT is activated
|
||||
#define subjectHomePresence Base_Topic "home_presence/" Gateway_Name // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name)
|
||||
|
||||
unsigned int BLEinterval ; //time between 2 scans
|
||||
|
||||
struct BLEdevice{
|
||||
char macAdr[13];
|
||||
boolean isDisc;
|
||||
|
||||
Reference in new issue
Block a user