add MQTT publishing of service data from BLE devices (#264)
* add MQTT publishing of service data from BLE devices * add service data publishing to HM1X setups
This commit is contained in:
1 parent
0b282fc553
commit
e0fe3b721b
2 files changed
+24
-13
No files matched your search
+23
-13
@@ -82,6 +82,8 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
|||||||
client.publish((char *)(mactopic + "/tx").c_str(),cTXPower);
|
client.publish((char *)(mactopic + "/tx").c_str(),cTXPower);
|
||||||
}
|
}
|
||||||
if (advertisedDevice.haveServiceData()){
|
if (advertisedDevice.haveServiceData()){
|
||||||
|
char mac[mac_adress.length()+1];
|
||||||
|
mac_adress.toCharArray(mac,mac_adress.length()+1);
|
||||||
trc(F("Get service data "));
|
trc(F("Get service data "));
|
||||||
std::string serviceData = advertisedDevice.getServiceData();
|
std::string serviceData = advertisedDevice.getServiceData();
|
||||||
int serviceDataLength = serviceData.length();
|
int serviceDataLength = serviceData.length();
|
||||||
@@ -94,7 +96,15 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
|||||||
}
|
}
|
||||||
returnedString = returnedString + String(a,HEX);
|
returnedString = returnedString + String(a,HEX);
|
||||||
}
|
}
|
||||||
trc(returnedString);
|
|
||||||
|
char service_data[returnedString.length()+1];
|
||||||
|
returnedString.toCharArray(service_data,returnedString.length()+1);
|
||||||
|
service_data[returnedString.length()] = '\0';
|
||||||
|
trc(F("service_data"));
|
||||||
|
trc(service_data);
|
||||||
|
String mactopic(mac);
|
||||||
|
mactopic = subjectBTtoMQTT + mactopic + subjectBTtoMQTTservicedata;
|
||||||
|
client.publish((char *)mactopic.c_str(),service_data);
|
||||||
|
|
||||||
trc(F("Get service data UUID"));
|
trc(F("Get service data UUID"));
|
||||||
BLEUUID serviceDataUUID = advertisedDevice.getServiceDataUUID();
|
BLEUUID serviceDataUUID = advertisedDevice.getServiceDataUUID();
|
||||||
@@ -102,11 +112,6 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
|||||||
|
|
||||||
if (strstr(serviceDataUUID.toString().c_str(),"fe95") != NULL){
|
if (strstr(serviceDataUUID.toString().c_str(),"fe95") != NULL){
|
||||||
trc("Processing BLE device data");
|
trc("Processing BLE device data");
|
||||||
char service_data[returnedString.length()+1];
|
|
||||||
returnedString.toCharArray(service_data,returnedString.length()+1);
|
|
||||||
service_data[returnedString.length()] = '\0';
|
|
||||||
char mac[mac_adress.length()+1];
|
|
||||||
mac_adress.toCharArray(mac,mac_adress.length()+1);
|
|
||||||
if (strstr(service_data,"209800") != NULL) {
|
if (strstr(service_data,"209800") != NULL) {
|
||||||
trc("mi flora data reading");
|
trc("mi flora data reading");
|
||||||
boolean result = process_data(-22,service_data,mac);
|
boolean result = process_data(-22,service_data,mac);
|
||||||
@@ -157,7 +162,7 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
|
|||||||
#else
|
#else
|
||||||
boolean BTtoMQTT(){
|
boolean BTtoMQTT(){
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
if (now > (timeBLE + TimeBtw_Read)) {//retriving value of temperature and humidity of the box from DHT every xUL
|
if (now > (timeBLE + TimeBtw_Read)) {
|
||||||
timeBLE = now;
|
timeBLE = now;
|
||||||
BLEDevice::init("");
|
BLEDevice::init("");
|
||||||
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
|
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
|
||||||
@@ -238,13 +243,20 @@ boolean BTtoMQTT() {
|
|||||||
if((strlen(d[0].extract)) == 12) // if a mac adress is detected we publish it
|
if((strlen(d[0].extract)) == 12) // if a mac adress is detected we publish it
|
||||||
{
|
{
|
||||||
strupp(d[0].extract);
|
strupp(d[0].extract);
|
||||||
String mactopic(d[0].extract);
|
String mactopic;
|
||||||
trc(mactopic);
|
trc(d[0].extract);
|
||||||
mactopic = subjectBTtoMQTT + mactopic + subjectBTtoMQTTrssi;
|
trc(d[0].extract);
|
||||||
|
mactopic = subjectBTtoMQTT + String(d[0].extract) + subjectBTtoMQTTrssi;
|
||||||
int rssi = (int)strtol(d[2].extract, NULL, 16) - 256;
|
int rssi = (int)strtol(d[2].extract, NULL, 16) - 256;
|
||||||
char val[12];
|
char val[12];
|
||||||
sprintf(val, "%d", rssi);
|
sprintf(val, "%d", rssi);
|
||||||
client.publish((char *)mactopic.c_str(),val);
|
client.publish((char *)mactopic.c_str(),val);
|
||||||
|
trc(F("service_data"));
|
||||||
|
String Service_data(d[5].extract);
|
||||||
|
Service_data = Service_data.substring(14);
|
||||||
|
trc(Service_data);
|
||||||
|
mactopic = subjectBTtoMQTT + String(d[0].extract) + subjectBTtoMQTTservicedata;
|
||||||
|
client.publish((char *)mactopic.c_str(),(char *)(Service_data).c_str());
|
||||||
if (strcmp(d[4].extract, "fe95") == 0)
|
if (strcmp(d[4].extract, "fe95") == 0)
|
||||||
if (strstr(d[5].extract,"209800") != NULL) {
|
if (strstr(d[5].extract,"209800") != NULL) {
|
||||||
trc("mi flora data reading");
|
trc("mi flora data reading");
|
||||||
@@ -325,8 +337,6 @@ boolean BTtoMQTT() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
boolean process_data(int offset, char * rest_data, char * mac_adress){
|
boolean process_data(int offset, char * rest_data, char * mac_adress){
|
||||||
trc(F("rest_data"));
|
|
||||||
trc(rest_data);
|
|
||||||
int data_length = 0;
|
int data_length = 0;
|
||||||
switch (rest_data[51 + offset]) {
|
switch (rest_data[51 + offset]) {
|
||||||
case '1' :
|
case '1' :
|
||||||
@@ -385,7 +395,7 @@ boolean process_data(int offset, char * rest_data, char * mac_adress){
|
|||||||
trc("can't read values");
|
trc("can't read values");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
client.publish((char *)mactopic.c_str(),val);;
|
client.publish((char *)mactopic.c_str(),val);
|
||||||
trc(val);
|
trc(val);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
#define subjectBTtoMQTTmoi "/moi"
|
#define subjectBTtoMQTTmoi "/moi"
|
||||||
#define subjectBTtoMQTTfer "/fer"
|
#define subjectBTtoMQTTfer "/fer"
|
||||||
#define subjectBTtoMQTTlux "/lux"
|
#define subjectBTtoMQTTlux "/lux"
|
||||||
|
#define subjectBTtoMQTTservicedata "/servicedata"
|
||||||
#define TimeBtw_Read 55555 //define the time between 2 scans
|
#define TimeBtw_Read 55555 //define the 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
|
||||||
|
|||||||
Reference in new issue
Block a user