add of Xiaomi Mijia BT4.0 clock temperature and humidity sensor (LYWSD02)
and enable multiple services data readings for Mijia standard device. Indeed the new version seems to publish several service data compared to old ones. Please note that I'have update the library with the work of @fguiet see: https://github.com/nkolban/ESP32_BLE_Arduino/pull/24
This commit is contained in:
1 parent
a11e336215
commit
4ba27481b5
3 files changed
+145
-82
No files matched your search
+114
-66
@@ -55,75 +55,86 @@ vector<BLEdevice> devices;
|
|||||||
static int taskCore = 0;
|
static int taskCore = 0;
|
||||||
|
|
||||||
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
||||||
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
||||||
trc(F("Creating BLE buffer"));
|
trc(F("Creating BLE buffer"));
|
||||||
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", (char *)mac_adress.c_str());
|
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()) BLEdata.set("name", (char *)advertisedDevice.getName().c_str());
|
if (advertisedDevice.haveName()) BLEdata.set("name", (char *)advertisedDevice.getName().c_str());
|
||||||
if (advertisedDevice.haveManufacturerData()) BLEdata.set("manufacturerdata", (char *)advertisedDevice.getManufacturerData().c_str());
|
if (advertisedDevice.haveManufacturerData()) BLEdata.set("manufacturerdata", (char *)advertisedDevice.getManufacturerData().c_str());
|
||||||
if (advertisedDevice.haveRSSI()) BLEdata.set("rssi", (int) advertisedDevice.getRSSI());
|
if (advertisedDevice.haveRSSI()) BLEdata.set("rssi", (int) advertisedDevice.getRSSI());
|
||||||
if (advertisedDevice.haveTXPower()) BLEdata.set("txpower", (int8_t) advertisedDevice.getTXPower());
|
if (advertisedDevice.haveTXPower()) BLEdata.set("txpower", (int8_t) advertisedDevice.getTXPower());
|
||||||
#ifdef subjectHomePresence
|
#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()) haRoomPresence(BLEdata);// this device has an rssi in consequence we can use it for home assistant room presence component
|
||||||
#endif
|
#endif
|
||||||
if (advertisedDevice.haveServiceData()){
|
if (advertisedDevice.haveServiceData()){
|
||||||
|
char mac[mac_adress.length()+1];
|
||||||
char mac[mac_adress.length()+1];
|
mac_adress.toCharArray(mac,mac_adress.length()+1);
|
||||||
mac_adress.toCharArray(mac,mac_adress.length()+1);
|
trc(F("Get services data :"));
|
||||||
|
int serviceDataCount = advertisedDevice.getServiceDataCount();
|
||||||
trc(F("Get service data "));
|
trc(serviceDataCount);
|
||||||
|
for (int j = 0; j < serviceDataCount;j++){
|
||||||
std::string serviceData = advertisedDevice.getServiceData();
|
std::string serviceData = advertisedDevice.getServiceData(j);
|
||||||
int serviceDataLength = serviceData.length();
|
int serviceDataLength = serviceData.length();
|
||||||
String returnedString = "";
|
String returnedString = "";
|
||||||
for (int i=0; i<serviceDataLength; i++)
|
for (int i=0; i<serviceDataLength; i++)
|
||||||
{
|
{
|
||||||
int a = serviceData[i];
|
int a = serviceData[i];
|
||||||
if (a < 16) {
|
if (a < 16) {
|
||||||
returnedString = returnedString + "0";
|
returnedString = returnedString + "0";
|
||||||
}
|
}
|
||||||
returnedString = returnedString + String(a,HEX);
|
returnedString = returnedString + String(a,HEX);
|
||||||
}
|
}
|
||||||
char service_data[returnedString.length()+1];
|
char service_data[returnedString.length()+1];
|
||||||
returnedString.toCharArray(service_data,returnedString.length()+1);
|
returnedString.toCharArray(service_data,returnedString.length()+1);
|
||||||
service_data[returnedString.length()] = '\0';
|
service_data[returnedString.length()] = '\0';
|
||||||
#ifdef pubBLEServiceData
|
#ifdef pubBLEServiceData
|
||||||
BLEdata.set("servicedata", service_data);
|
BLEdata.set("servicedata", service_data);
|
||||||
BLEdata.set("servicedatauuid", (char *)advertisedDevice.getServiceDataUUID().toString().c_str());
|
BLEdata.set("servicedatauuid", (char *)advertisedDevice.getServiceDataUUID(j).toString().c_str());
|
||||||
#endif
|
#endif
|
||||||
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((!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
|
||||||
pub((char *)mactopic.c_str(),BLEdata);
|
pub((char *)mactopic.c_str(),BLEdata);
|
||||||
if (strstr(BLEdata["servicedatauuid"].as<char*>(),"fe95") != NULL){
|
if (strstr(BLEdata["servicedatauuid"].as<char*>(),"fe95") != NULL){
|
||||||
trc("Processing BLE device data");
|
trc("Processing BLE device data");
|
||||||
int pos = -1;
|
int pos = -1;
|
||||||
pos = strpos(service_data,"209800");
|
pos = strpos(service_data,"209800");
|
||||||
if (pos != -1){
|
if (pos != -1){
|
||||||
trc(F("mi flora data reading"));
|
trc(F("mi flora data reading"));
|
||||||
#ifdef ZmqttDiscovery
|
#ifdef ZmqttDiscovery
|
||||||
if(!isDiscovered(mac)) MiFloraDiscovery(mac);
|
if(!isDiscovered(mac)) MiFloraDiscovery(mac);
|
||||||
#endif
|
#endif
|
||||||
process_data(pos - 24,service_data,mac);
|
process_data(pos - 24,service_data,mac);
|
||||||
}
|
}
|
||||||
pos = -1;
|
pos = -1;
|
||||||
pos = strpos(service_data,"20aa01");
|
pos = strpos(service_data,"20aa01");
|
||||||
if (pos != -1){
|
if (pos != -1){
|
||||||
trc(F("mi jia data reading"));
|
trc(F("mi jia data reading"));
|
||||||
#ifdef ZmqttDiscovery
|
#ifdef ZmqttDiscovery
|
||||||
if(!isDiscovered(mac)) MiJiaDiscovery(mac);
|
if(!isDiscovered(mac)) MiJiaDiscovery(mac);
|
||||||
#endif
|
#endif
|
||||||
process_data(pos - 26,service_data,mac);
|
process_data(pos - 26,service_data,mac);
|
||||||
}
|
}
|
||||||
|
pos = -1;
|
||||||
|
pos = strpos(service_data,"205b04");
|
||||||
|
if (pos != -1){
|
||||||
|
trc(F("LYWSD02 data reading"));
|
||||||
|
//example 70205b04b96ab883c8593f09041002e000
|
||||||
|
#ifdef ZmqttDiscovery
|
||||||
|
if(!isDiscovered(mac)) LYWSD02Discovery(mac);
|
||||||
|
#endif
|
||||||
|
process_data(pos - 24,service_data,mac);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void setupBT(){
|
void setupBT(){
|
||||||
BLEinterval = TimeBtw_Read;
|
BLEinterval = TimeBtw_Read;
|
||||||
trc(F("BLEinterval btw scans"));
|
trc(F("BLEinterval btw scans"));
|
||||||
@@ -302,6 +313,15 @@ vector<BLEdevice> devices;
|
|||||||
#endif
|
#endif
|
||||||
boolean result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract);
|
boolean result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract);
|
||||||
}
|
}
|
||||||
|
pos = -1;
|
||||||
|
pos = strpos(d[5].extract,"205b04");
|
||||||
|
if (pos != -1){
|
||||||
|
trc("LYWSD02 data reading");
|
||||||
|
#ifdef ZmqttDiscovery
|
||||||
|
if(!isDiscovered(d[0].extract)) LYWSD02Discovery(d[0].extract);
|
||||||
|
#endif
|
||||||
|
boolean result = process_data(pos - 38,(char *)Service_data.c_str(),d[0].extract);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -380,6 +400,34 @@ void MiJiaDiscovery(char * mac){
|
|||||||
device.isDisc = true;
|
device.isDisc = true;
|
||||||
devices.push_back(device);
|
devices.push_back(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LYWSD02Discovery(char * mac){
|
||||||
|
#define LYWSD02parametersCount 3
|
||||||
|
trc(F("LYWSD02Discovery"));
|
||||||
|
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,"");
|
||||||
|
}
|
||||||
|
BLEdevice device;
|
||||||
|
strcpy( device.macAdr, mac );
|
||||||
|
device.isDisc = true;
|
||||||
|
devices.push_back(device);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
boolean process_data(int offset, char * rest_data, char * mac_adress){
|
boolean process_data(int offset, char * rest_data, char * mac_adress){
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
|
|||||||
m_manufacturerData = "";
|
m_manufacturerData = "";
|
||||||
m_name = "";
|
m_name = "";
|
||||||
m_rssi = -9999;
|
m_rssi = -9999;
|
||||||
m_serviceData = "";
|
|
||||||
m_txPower = 0;
|
m_txPower = 0;
|
||||||
m_pScan = nullptr;
|
m_pScan = nullptr;
|
||||||
|
|
||||||
@@ -106,12 +105,24 @@ BLEScan* BLEAdvertisedDevice::getScan() {
|
|||||||
} // getScan
|
} // getScan
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the number of service data.
|
||||||
|
* @return Number of service data discovered.
|
||||||
|
*/
|
||||||
|
int BLEAdvertisedDevice::getServiceDataCount() {
|
||||||
|
if (m_haveServiceData)
|
||||||
|
return m_serviceDataVector.size();
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
} //getServiceDataCount
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the service data.
|
* @brief Get the service data.
|
||||||
* @return The ServiceData of the advertised device.
|
* @return The ServiceData of the advertised device.
|
||||||
*/
|
*/
|
||||||
std::string BLEAdvertisedDevice::getServiceData() {
|
std::string BLEAdvertisedDevice::getServiceData(int i) {
|
||||||
return m_serviceData;
|
return m_serviceDataVector[i];
|
||||||
} //getServiceData
|
} //getServiceData
|
||||||
|
|
||||||
|
|
||||||
@@ -119,8 +130,8 @@ std::string BLEAdvertisedDevice::getServiceData() {
|
|||||||
* @brief Get the service data UUID.
|
* @brief Get the service data UUID.
|
||||||
* @return The service data UUID.
|
* @return The service data UUID.
|
||||||
*/
|
*/
|
||||||
BLEUUID BLEAdvertisedDevice::getServiceDataUUID() {
|
BLEUUID BLEAdvertisedDevice::getServiceDataUUID(int i) {
|
||||||
return m_serviceDataUUID;
|
return m_serviceDataUUIDs[i];
|
||||||
} // getServiceDataUUID
|
} // getServiceDataUUID
|
||||||
|
|
||||||
|
|
||||||
@@ -128,8 +139,8 @@ BLEUUID BLEAdvertisedDevice::getServiceDataUUID() {
|
|||||||
* @brief Get the Service UUID.
|
* @brief Get the Service UUID.
|
||||||
* @return The Service UUID of the advertised device.
|
* @return The Service UUID of the advertised device.
|
||||||
*/
|
*/
|
||||||
BLEUUID BLEAdvertisedDevice::getServiceUUID() { //TODO Remove it eventually, is no longer useful
|
BLEUUID BLEAdvertisedDevice::getServiceUUID(int i) {
|
||||||
return m_serviceUUIDs[0];
|
return m_serviceUUIDs[i];
|
||||||
} // getServiceUUID
|
} // getServiceUUID
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -458,7 +469,7 @@ void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
|
|||||||
*/
|
*/
|
||||||
void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
|
void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
|
||||||
m_haveServiceData = true; // Set the flag that indicates we have service data.
|
m_haveServiceData = true; // Set the flag that indicates we have service data.
|
||||||
m_serviceData = serviceData; // Save the service data that we received.
|
m_serviceDataVector.push_back(serviceData); // Save the service data that we received.
|
||||||
} //setServiceData
|
} //setServiceData
|
||||||
|
|
||||||
|
|
||||||
@@ -468,7 +479,8 @@ void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
|
|||||||
*/
|
*/
|
||||||
void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
|
void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
|
||||||
m_haveServiceData = true; // Set the flag that indicates we have service data.
|
m_haveServiceData = true; // Set the flag that indicates we have service data.
|
||||||
m_serviceDataUUID = uuid;
|
m_serviceDataUUIDs.push_back(uuid);
|
||||||
|
ESP_LOGD(LOG_TAG, "- addServiceDataUUID(): serviceDataUUID: %s", uuid.toString().c_str());
|
||||||
} // setServiceDataUUID
|
} // setServiceDataUUID
|
||||||
|
|
||||||
|
|
||||||
@@ -499,7 +511,9 @@ std::string BLEAdvertisedDevice::toString() {
|
|||||||
free(pHex);
|
free(pHex);
|
||||||
}
|
}
|
||||||
if (haveServiceUUID()) {
|
if (haveServiceUUID()) {
|
||||||
ss << ", serviceUUID: " << getServiceUUID().toString();
|
for (int i; i < m_serviceUUIDs.size(); i++) {
|
||||||
|
ss << ", serviceUUID: " << getServiceUUID(i).toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (haveTXPower()) {
|
if (haveTXPower()) {
|
||||||
ss << ", txPower: " << (int)getTXPower();
|
ss << ", txPower: " << (int)getTXPower();
|
||||||
|
|||||||
@@ -35,9 +35,10 @@ public:
|
|||||||
std::string getName();
|
std::string getName();
|
||||||
int getRSSI();
|
int getRSSI();
|
||||||
BLEScan* getScan();
|
BLEScan* getScan();
|
||||||
std::string getServiceData();
|
std::string getServiceData(int i);
|
||||||
BLEUUID getServiceDataUUID();
|
BLEUUID getServiceDataUUID(int i);
|
||||||
BLEUUID getServiceUUID();
|
BLEUUID getServiceUUID(int i);
|
||||||
|
int getServiceDataCount();
|
||||||
int8_t getTXPower();
|
int8_t getTXPower();
|
||||||
uint8_t* getPayload();
|
uint8_t* getPayload();
|
||||||
|
|
||||||
@@ -91,9 +92,9 @@ private:
|
|||||||
BLEScan* m_pScan;
|
BLEScan* m_pScan;
|
||||||
int m_rssi;
|
int m_rssi;
|
||||||
std::vector<BLEUUID> m_serviceUUIDs;
|
std::vector<BLEUUID> m_serviceUUIDs;
|
||||||
int8_t m_txPower;
|
std::vector<BLEUUID> m_serviceDataUUIDs;
|
||||||
std::string m_serviceData;
|
std::vector<std::string> m_serviceDataVector;
|
||||||
BLEUUID m_serviceDataUUID;
|
int8_t m_txPower;
|
||||||
uint8_t* m_payload;
|
uint8_t* m_payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user