wdt tip add and presence detection improvments
-improve distance measurement -add wdt prevent tip -add conditional macro haroompresence
This commit is contained in:
1 parent
62b16f8f66
commit
ed44418a7f
1 file changed
+37
-26
+37
-26
@@ -41,6 +41,8 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
|||||||
#include <BLEUtils.h>
|
#include <BLEUtils.h>
|
||||||
#include <BLEScan.h>
|
#include <BLEScan.h>
|
||||||
#include <BLEAdvertisedDevice.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
|
//Time used to wait for an interval before resending BLE infos
|
||||||
unsigned long timeBLE= 0;
|
unsigned long timeBLE= 0;
|
||||||
@@ -54,25 +56,17 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
|||||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
|
||||||
JsonObject& BLEdata = jsonBuffer.createObject();
|
JsonObject& BLEdata = jsonBuffer.createObject();
|
||||||
String mac_adress = advertisedDevice.getAddress().toString().c_str();
|
String mac_adress = advertisedDevice.getAddress().toString().c_str();
|
||||||
BLEdata.set("id", mac_adress);
|
BLEdata.set("id", (char *)mac_adress.c_str());
|
||||||
mac_adress.replace(":","");
|
mac_adress.replace(":","");
|
||||||
mac_adress.toUpperCase();
|
mac_adress.toUpperCase();
|
||||||
String mactopic = subjectBTtoMQTT + mac_adress;
|
String mactopic = subjectBTtoMQTT + mac_adress;
|
||||||
if (advertisedDevice.haveName()){
|
if (advertisedDevice.haveName()) BLEdata.set("name", (char *)advertisedDevice.getName().c_str());
|
||||||
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.haveManufacturerData()){
|
if (advertisedDevice.haveTXPower()) BLEdata.set("txpower", (int8_t) advertisedDevice.getTXPower());
|
||||||
BLEdata.set("manufacturerdata", (char *)advertisedDevice.getManufacturerData().c_str());
|
#ifdef subjectHomePresence
|
||||||
}
|
if (advertisedDevice.haveRSSI()) haRoomPresence(BLEdata);// this device has an rssi in consequence we can use it for home assistant room presence component
|
||||||
if (advertisedDevice.haveRSSI()){
|
#endif
|
||||||
BLEdata.set("rssi", (int) advertisedDevice.getRSSI());
|
|
||||||
#ifdef subjectHomePresence
|
|
||||||
haRoomPresence(BLEdata);// this device has an rssi in consequence we can use it for home assistant room presence component
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
if (advertisedDevice.haveTXPower()){
|
|
||||||
BLEdata.set("txpower", (int8_t) advertisedDevice.getTXPower());
|
|
||||||
}
|
|
||||||
if (advertisedDevice.haveServiceData()){
|
if (advertisedDevice.haveServiceData()){
|
||||||
|
|
||||||
char mac[mac_adress.length()+1];
|
char mac[mac_adress.length()+1];
|
||||||
@@ -147,6 +141,11 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
|||||||
while(true){
|
while(true){
|
||||||
trc(taskMessage);
|
trc(taskMessage);
|
||||||
delay(TimeBtw_Read);
|
delay(TimeBtw_Read);
|
||||||
|
|
||||||
|
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
|
||||||
|
TIMERG0.wdt_feed=1;
|
||||||
|
TIMERG0.wdt_wprotect=0;
|
||||||
|
|
||||||
BLEDevice::init("");
|
BLEDevice::init("");
|
||||||
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
|
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
|
||||||
MyAdvertisedDeviceCallbacks myCallbacks;
|
MyAdvertisedDeviceCallbacks myCallbacks;
|
||||||
@@ -243,13 +242,13 @@ boolean BTtoMQTT() {
|
|||||||
JsonObject& BLEdata = jsonBuffer.createObject();
|
JsonObject& BLEdata = jsonBuffer.createObject();
|
||||||
#ifdef subjectHomePresence
|
#ifdef subjectHomePresence
|
||||||
String HomePresenceId;
|
String HomePresenceId;
|
||||||
for (int i = 0; i++; i<13){
|
for (int i = 0; i<12; i++){
|
||||||
String HomePresenceId = HomePresenceId + String(d[0].extract[i]);
|
HomePresenceId += String(d[0].extract[i]);
|
||||||
if(i % 2 == 0) HomePresenceId += ":";
|
if(((i-1) % 2 == 0) && (i!=11)) HomePresenceId += ":";
|
||||||
}
|
}
|
||||||
trc(F("HomePresenceId"));
|
trc(F("HomePresenceId"));
|
||||||
trc(HomePresenceId);
|
trc(HomePresenceId);
|
||||||
BLEdata.set("id", HomePresenceId);
|
BLEdata.set("id", (char *)HomePresenceId.c_str());
|
||||||
#endif
|
#endif
|
||||||
strupp(d[0].extract);
|
strupp(d[0].extract);
|
||||||
String topic = subjectBTtoMQTT + String(d[0].extract);
|
String topic = subjectBTtoMQTT + String(d[0].extract);
|
||||||
@@ -319,8 +318,7 @@ boolean BTtoMQTT() {
|
|||||||
String onedevice = discResult.substring(0,78);
|
String onedevice = discResult.substring(0,78);
|
||||||
onedevice.replace(STRING_MSG,"");
|
onedevice.replace(STRING_MSG,"");
|
||||||
String mac = onedevice.substring(53,65);
|
String mac = onedevice.substring(53,65);
|
||||||
trc(mactopic + " " + rssi);
|
pub(subjectBTtoMQTT + mac,onedevice.substring(66,70));
|
||||||
pub(subjectBTtoMQTT + mac + subjectBTtoMQTTrssi,onedevice.substring(66,70));
|
|
||||||
discResult = discResult.substring(78);
|
discResult = discResult.substring(78);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -439,14 +437,27 @@ boolean process_data(int offset, char * rest_data, char * mac_adress){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef subjectHomePresence
|
||||||
void haRoomPresence(JsonObject& HomePresence){
|
void haRoomPresence(JsonObject& HomePresence){
|
||||||
trc("BLE DISTANCE :");
|
int BLErssi = HomePresence["rssi"];
|
||||||
double BLErssi = HomePresence["rssi"];
|
trc(F("BLErssi"));
|
||||||
double ratio = BLErssi/-59;
|
trc(BLErssi);
|
||||||
double distance = (0.89)* pow(ratio,7.7095) + 0.11;
|
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;
|
HomePresence["distance"] = distance;
|
||||||
|
trc(F("BLE DISTANCE :"));
|
||||||
trc(distance);
|
trc(distance);
|
||||||
pub(subjectHomePresence,HomePresence);
|
pub(subjectHomePresence,HomePresence);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user