From 69e042808471e93440e71c1cad118cfd01d1db24 Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Sat, 13 Oct 2018 21:44:23 +0200 Subject: [PATCH] correct impossibility to read value with HM10 #281 and make the code more flexible (deduct an offset from a key value instead of fixed offset) --- OpenMQTTGateway.ino | 8 ++++++++ ZgatewayBT.ino | 34 ++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/OpenMQTTGateway.ino b/OpenMQTTGateway.ino index 3004fcd..5a2453a 100644 --- a/OpenMQTTGateway.ino +++ b/OpenMQTTGateway.ino @@ -881,6 +881,14 @@ void revert_hex_data(char * in, char * out, int l){ out[l-1] = '\0'; } +int strpos(char *haystack, char *needle) //from @miere https://stackoverflow.com/users/548685/miere +{ + char *p = strstr(haystack, needle); + if (p) + return p - haystack; + return -1; +} + bool to_bool(String const& s) { // thanks Chris Jester-Young from stackoverflow return s != "0"; } diff --git a/ZgatewayBT.ino b/ZgatewayBT.ino index 1dc953f..13d499e 100644 --- a/ZgatewayBT.ino +++ b/ZgatewayBT.ino @@ -112,13 +112,18 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht if (strstr(serviceDataUUID.toString().c_str(),"fe95") != NULL){ trc("Processing BLE device data"); + int pos = -1; + pos = strpos(service_data,"209800"); if (strstr(service_data,"209800") != NULL) { trc("mi flora data reading"); - boolean result = process_data(-22,service_data,mac); + + boolean result = process_data(pos - 24,service_data,mac); } + pos = -1; + pos = strpos(service_data,"20aa01"); if (strstr(service_data,"20aa01") != NULL){ trc("mi jia data reading"); - boolean result = process_data(-24,service_data,mac); + boolean result = process_data(pos - 26,service_data,mac); } } } @@ -257,16 +262,21 @@ boolean BTtoMQTT() { 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 (strstr(d[5].extract,"209800") != NULL) { - trc("mi flora data reading"); - boolean result = process_data(0,d[5].extract,d[0].extract); - } - if (strstr(d[5].extract,"20aa01") != NULL){ - trc("mi jia data reading"); - boolean result = process_data(-2,d[5].extract,d[0].extract); - } - return true; + if (strcmp(d[4].extract, "fe95") == 0) { + int pos = -1; + pos = strpos(d[5].extract,"209800"); + if (pos != -1) { + trc("mi flora data reading"); + boolean result = process_data(pos - 38,(char *)Service_data.c_str(),d[0].extract); + } + pos = -1; + pos = strpos(d[5].extract,"20aa01"); + if (pos != -1){ + trc("mi jia data reading"); + boolean result = process_data(pos - 40,(char *)Service_data.c_str(),d[0].extract); + } + return true; + } } } }