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:
1technophile committed 2019-04-28 15:06:23 +02:00
1 parent 8aad413ce3
commit 56bfef8e9c
2 files changed
+54 -29

No files matched your search

+50 -25
View File
@@ -32,7 +32,6 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
#include <vector> #include <vector>
using namespace std; using namespace std;
vector<BLEdevice> devices; vector<BLEdevice> devices;
#ifdef ESP32 #ifdef ESP32
@@ -126,6 +125,9 @@ vector<BLEdevice> devices;
}; };
void setupBT(){ void setupBT(){
BLEinterval = TimeBtw_Read;
trc(F("BLEinterval btw scans"));
trc(BLEinterval);
#ifdef multiCore #ifdef multiCore
// we setup a task with priority one to avoid conflict with other gateways // we setup a task with priority one to avoid conflict with other gateways
xTaskCreatePinnedToCore( xTaskCreatePinnedToCore(
@@ -150,7 +152,25 @@ vector<BLEdevice> devices;
while(true){ while(true){
trc(taskMessage); trc(taskMessage);
delay(TimeBtw_Read); delay(BLEinterval);
BLEscan();
}
}
boolean BTtoMQTT(){ // for on demande BLE scans
BLEscan();
}
#else
boolean BTtoMQTT(){
unsigned long now = millis();
if (now > (timeBLE + BLEinterval)) {
timeBLE = now;
BLEscan();
return true;
}
return false;
}
#endif
void BLEscan(){
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed=1; TIMERG0.wdt_feed=1;
@@ -163,24 +183,6 @@ vector<BLEdevice> devices;
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
BLEScanResults foundDevices = pBLEScan->start(Scan_duration); BLEScanResults foundDevices = pBLEScan->start(Scan_duration);
} }
}
#else
boolean BTtoMQTT(){
unsigned long now = millis();
if (now > (timeBLE + TimeBtw_Read)) {
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);
return true;
}
return false;
}
#endif
#else // arduino or ESP8266 working with HM10/11 #else // arduino or ESP8266 working with HM10/11
#include <SoftwareSerial.h> #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}}; 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() { void setupBT() {
BLEinterval = TimeBtw_Read;
trc(F("BLEinterval btw scans"));
trc(BLEinterval);
softserial.begin(9600); softserial.begin(9600);
softserial.print(F("AT+ROLE1")); softserial.print(F("AT+ROLE1"));
delay(100); delay(100);
@@ -225,14 +230,14 @@ vector<BLEdevice> devices;
returnedString = returnedString + String(a,HEX); returnedString = returnedString + String(a,HEX);
} }
if (millis() > (timebt + TimeBtw_Read)) {//retriving data if (millis() > (timebt + BLEinterval)) {//retriving data
timebt = millis(); timebt = millis();
#if defined(ESP8266) #if defined(ESP8266)
yield(); yield();
#endif #endif
if (returnedString != "") { if (returnedString != "") {
size_t pos = 0; size_t pos = 0;
while ((pos = returnedString.lastIndexOf(delimiter)) != -1) { while ((pos = returnedString.lastIndexOf(BLEdelimiter)) != -1) {
#if defined(ESP8266) #if defined(ESP8266)
yield(); yield();
#endif #endif
@@ -487,24 +492,44 @@ void haRoomPresence(JsonObject& HomePresence){
pub(subjectHomePresence,HomePresence); pub(subjectHomePresence,HomePresence);
} }
#endif #endif
void MQTTtoBT(char * topicOri, JsonObject& BTdata) { // json object decoding void MQTTtoBT(char * topicOri, JsonObject& BTdata) { // json object decoding
if (strcmp(topicOri,subjectMQTTtoBTset) == 0){ if (strcmp(topicOri,subjectMQTTtoBTset) == 0){
trc(F("MQTTtoBT json set")); trc(F("MQTTtoBT json set"));
// Black list & white list set
int WLsize = BTdata["white-list"].size(); int WLsize = BTdata["white-list"].size();
if(WLsize > 0){ if(WLsize > 0){
trc(F("WL set"));
for (int i = 0; i < WLsize; i++){ for (int i = 0; i < WLsize; i++){
const char * whiteMac = BTdata["white-list"][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(); int BLsize = BTdata["black-list"].size();
if(BLsize > 0){ if(BLsize > 0){
trc(F("BL set"));
for (int i = 0; i < BLsize; i++){ for (int i = 0; i < BLsize; i++){
const char * blackMac = BTdata["black-list"][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();
} }
} }
+4 -4
View File
@@ -25,21 +25,21 @@
*/ */
/*----------------------BT topics & parameters-------------------------*/ /*----------------------BT topics & parameters-------------------------*/
#define subjectBTtoMQTT Base_Topic Gateway_Name "/BTtoMQTT/" #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 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 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-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 HM_BLUE_LED_STOP true //uncomment to stop the blue led light of HM1X
#define delimiter "4f4b2b444953413a" #define BLEdelimiter "4f4b2b444953413a"
#define delimiter_length 16
#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 #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 ----------------------*/ /*-------------------HOME ASSISTANT ROOM PRESENCE ----------------------*/
// if not commented Home presence integration with HOME ASSISTANT is activated // 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) #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{ struct BLEdevice{
char macAdr[13]; char macAdr[13];
boolean isDisc; boolean isDisc;