From 55653f0c3346acac9f6e75e82dfbf1990b1fd56c Mon Sep 17 00:00:00 2001 From: d0m1n1qu3 Date: Fri, 10 Jul 2020 18:03:07 +0200 Subject: [PATCH 1/7] read the rssi from the sensor sometimes it is hard to find out why a sensor sends no data with the rssi you got a value of the BLE signal strenght and can optimize the location of the sensor or the ESP32 if the rssi got worse over time the battery could be the problem rssi monitoring is a must have feature for radio signals :-) --- tasmota/xsns_62_MI_ESP32.ino | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/tasmota/xsns_62_MI_ESP32.ino b/tasmota/xsns_62_MI_ESP32.ino index 9fa6a71d8..9cea6b513 100644 --- a/tasmota/xsns_62_MI_ESP32.ino +++ b/tasmota/xsns_62_MI_ESP32.ino @@ -125,6 +125,7 @@ struct mi_sensor_t{ uint8_t type; //Flora = 1; MI-HT_V1=2; LYWSD02=3; LYWSD03=4; CGG1=5; CGD1=6 uint8_t serial[6]; uint8_t showedUp; + int rssi; float temp; //Flora, MJ_HT_V1, LYWSD0x, CGx union { struct { @@ -242,11 +243,11 @@ class MI32AdvCallbacks: public NimBLEAdvertisedDeviceCallbacks { memcpy(addr,advertisedDevice->getAddress().getNative(),6); MI32_ReverseMAC(addr); if(uuid==0xfe95) { - MI32ParseResponse((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr); + MI32ParseResponse((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr, advertisedDevice->getRSSI()); MI32Scan->erase(advertisedDevice->getAddress()); } else if(uuid==0xfdcd) { - MI32parseCGD1Packet((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr); + MI32parseCGD1Packet((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr, advertisedDevice->getRSSI()); MI32Scan->erase(advertisedDevice->getAddress()); } else { @@ -925,12 +926,13 @@ void MI32parseMiBeacon(char * _buf, uint32_t _slot){ } } -void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6]){ // no MiBeacon +void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6], int rssi){ // no MiBeacon uint8_t _addr[6]; memcpy(_addr,addr,6); uint32_t _slot = MIBLEgetSensorSlot(_addr, 0x0576); // This must be hard-coded, no object-id in Cleargrass-packet DEBUG_SENSOR_LOG(PSTR("MI32: Sensor slot: %u"), _slot); if(_slot==0xff) return; + MIBLEsensors[_slot].rssi=rssi; cg_packet_t _packet; memcpy((char*)&_packet,_buf,sizeof(_packet)); switch (_packet.mode){ @@ -959,7 +961,7 @@ void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6]){ // no M } } -void MI32ParseResponse(char *buf, uint16_t bufsize, uint8_t addr[6]) { +void MI32ParseResponse(char *buf, uint16_t bufsize, uint8_t addr[6], int rssi) { if(bufsize<10) { return; } @@ -969,7 +971,10 @@ void MI32ParseResponse(char *buf, uint16_t bufsize, uint8_t addr[6]) { uint8_t _addr[6]; memcpy(_addr,addr,6); uint16_t _slot = MIBLEgetSensorSlot(_addr, _type); - if(_slot!=0xff) MI32parseMiBeacon(_pos,_slot); + if(_slot!=0xff) { + MIBLEsensors[_slot].rssi=rssi; + MI32parseMiBeacon(_pos,_slot); + } } /***********************************************************************\ @@ -1209,6 +1214,7 @@ bool MI32Cmd(void) { const char HTTP_MI32[] PROGMEM = "{s}MI ESP32 {m}%u%s / %u{e}"; const char HTTP_MI32_SERIAL[] PROGMEM = "{s}%s %s{m}%02x:%02x:%02x:%02x:%02x:%02x%{e}"; +const char HTTP_RSSI[] PROGMEM = "{s}%s" " RSSI" "{m}%d dBm{e}"; const char HTTP_BATTERY[] PROGMEM = "{s}%s" " Battery" "{m}%u %%{e}"; const char HTTP_VOLTAGE[] PROGMEM = "{s}%s " D_VOLTAGE "{m}%s V{e}"; const char HTTP_MI32_FLORA_DATA[] PROGMEM = "{s}%s" " Fertility" "{m}%u us/cm{e}"; @@ -1229,14 +1235,13 @@ void MI32Show(bool json) kMI32SlaveType[MIBLEsensors[i].type-1], MIBLEsensors[i].serial[3], MIBLEsensors[i].serial[4], MIBLEsensors[i].serial[5]); + ResponseAppend_P(PSTR("\"RSSI\":%d"), MIBLEsensors[i].rssi); // all sensors have rssi + if (MIBLEsensors[i].type == FLORA) { if (!isnan(MIBLEsensors[i].temp)) { char temperature[FLOATSZ]; // all sensors have temperature dtostrfd(MIBLEsensors[i].temp, Settings.flag2.temperature_resolution, temperature); - ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "\":%s"), temperature); - } else { - ResponseAppend_P(PSTR("}")); - continue; + ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%s"), temperature); } if (MIBLEsensors[i].lux!=0x0ffffff) { // this is the error code -> no lux ResponseAppend_P(PSTR(",\"" D_JSON_ILLUMINANCE "\":%u"), MIBLEsensors[i].lux); @@ -1250,6 +1255,7 @@ void MI32Show(bool json) } if (MIBLEsensors[i].type > FLORA){ if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp)) { + ResponseAppend_P(PSTR(",")); ResponseAppendTHD(MIBLEsensors[i].temp, MIBLEsensors[i].hum); } } @@ -1286,6 +1292,7 @@ void MI32Show(bool json) for (i; i Date: Sat, 11 Jul 2020 22:09:26 +0200 Subject: [PATCH 2/7] add some checks is rssi is available and init variables at object creation --- tasmota/xsns_62_MI_ESP32.ino | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tasmota/xsns_62_MI_ESP32.ino b/tasmota/xsns_62_MI_ESP32.ino index 9cea6b513..8441b8c86 100644 --- a/tasmota/xsns_62_MI_ESP32.ino +++ b/tasmota/xsns_62_MI_ESP32.ino @@ -242,12 +242,17 @@ class MI32AdvCallbacks: public NimBLEAdvertisedDeviceCallbacks { uint8_t addr[6]; memcpy(addr,advertisedDevice->getAddress().getNative(),6); MI32_ReverseMAC(addr); + int rssi = 0xffff; + if(advertisedDevice->haveRSSI()) { + rssi = advertisedDevice->getRSSI(); + } + // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("RSSI: %d"),rssi); // actually i never got a 0xffff if(uuid==0xfe95) { - MI32ParseResponse((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr, advertisedDevice->getRSSI()); + MI32ParseResponse((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr, rssi); MI32Scan->erase(advertisedDevice->getAddress()); } else if(uuid==0xfdcd) { - MI32parseCGD1Packet((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr, advertisedDevice->getRSSI()); + MI32parseCGD1Packet((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr, rssi); MI32Scan->erase(advertisedDevice->getAddress()); } else { @@ -341,6 +346,8 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_serial)[6], uint16_t _type){ _newSensor.showedUp = 255; _newSensor.temp =NAN; _newSensor.bat=0x00; + _newSensor.rssi=0xffff; + _newSensor.firmware[0]='\0'; switch (_type) { case 1: @@ -1235,8 +1242,11 @@ void MI32Show(bool json) kMI32SlaveType[MIBLEsensors[i].type-1], MIBLEsensors[i].serial[3], MIBLEsensors[i].serial[4], MIBLEsensors[i].serial[5]); - ResponseAppend_P(PSTR("\"RSSI\":%d"), MIBLEsensors[i].rssi); // all sensors have rssi - + if (MIBLEsensors[i].rssi!=0xffff) { // this is the error code -> no valid value + ResponseAppend_P(PSTR("\"RSSI\":%d"), MIBLEsensors[i].rssi); // all sensors have rssi + } else { + ResponseAppend_P(PSTR("\"RSSI\":null")); // to know that it is sometimes out of range + } if (MIBLEsensors[i].type == FLORA) { if (!isnan(MIBLEsensors[i].temp)) { char temperature[FLOATSZ]; // all sensors have temperature @@ -1292,7 +1302,9 @@ void MI32Show(bool json) for (i; i no valid value + WSContentSend_PD(HTTP_RSSI, kMI32SlaveType[MIBLEsensors[i].type-1], MIBLEsensors[i].rssi); + } if (MIBLEsensors[i].type==FLORA) { if (!isnan(MIBLEsensors[i].temp)) { char temperature[FLOATSZ]; From 4f10a15a6530e0368881bb2f6f93ad246a970004 Mon Sep 17 00:00:00 2001 From: d0m1n1qu3 Date: Sun, 12 Jul 2020 21:19:40 +0200 Subject: [PATCH 3/7] shift the init or the variable firmware to FLORA only because of changes from Staars --- tasmota/xsns_62_MI_ESP32.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/xsns_62_MI_ESP32.ino b/tasmota/xsns_62_MI_ESP32.ino index e86f93fff..b110cb57f 100644 --- a/tasmota/xsns_62_MI_ESP32.ino +++ b/tasmota/xsns_62_MI_ESP32.ino @@ -520,13 +520,13 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter) _newSensor.temp =NAN; _newSensor.bat=0x00; _newSensor.rssi=0xffff; - _newSensor.firmware[0]='\0'; _newSensor.lux = 0x00ffffff; switch (_type) { case FLORA: _newSensor.moisture =NAN; _newSensor.fertility =NAN; + _newSensor.firmware[0]='\0'; break; case 2: case 3: case 4: case 5: case 6: _newSensor.hum=NAN; From e468f941dbaa708bc60893151ba2c16f1281f802 Mon Sep 17 00:00:00 2001 From: d0m1n1qu3 Date: Mon, 13 Jul 2020 17:57:30 +0200 Subject: [PATCH 4/7] using D_RSSI instead of new String --- tasmota/xsns_62_MI_ESP32.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/xsns_62_MI_ESP32.ino b/tasmota/xsns_62_MI_ESP32.ino index b110cb57f..a46f516d3 100644 --- a/tasmota/xsns_62_MI_ESP32.ino +++ b/tasmota/xsns_62_MI_ESP32.ino @@ -1454,7 +1454,7 @@ bool MI32Cmd(void) { const char HTTP_MI32[] PROGMEM = "{s}MI ESP32 {m}%u%s / %u{e}"; const char HTTP_MI32_SERIAL[] PROGMEM = "{s}%s %s{m}%02x:%02x:%02x:%02x:%02x:%02x%{e}"; -const char HTTP_RSSI[] PROGMEM = "{s}%s" " RSSI" "{m}%d dBm{e}"; +const char HTTP_RSSI[] PROGMEM = "{s}%s " D_RSSI "{m}%d dBm{e}"; const char HTTP_BATTERY[] PROGMEM = "{s}%s" " Battery" "{m}%u %%{e}"; const char HTTP_VOLTAGE[] PROGMEM = "{s}%s " D_VOLTAGE "{m}%s V{e}"; const char HTTP_LASTBUTTON[] PROGMEM = "{s}%s Last Button{m}%u {e}"; From e0f075afb1eb086accae2966bbd09eba74550a03 Mon Sep 17 00:00:00 2001 From: Javier Arigita Date: Mon, 13 Jul 2020 20:22:37 +0200 Subject: [PATCH 5/7] Bugfix local temperature sensor in fahrenheit --- tasmota/xdrv_39_thermostat.ino | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tasmota/xdrv_39_thermostat.ino b/tasmota/xdrv_39_thermostat.ino index 768088117..8e863822d 100644 --- a/tasmota/xdrv_39_thermostat.ino +++ b/tasmota/xdrv_39_thermostat.ino @@ -1333,7 +1333,13 @@ void ThermostatGetLocalSensor(uint8_t ctr_output) { if (root.success()) { const char* value_c = root[THERMOSTAT_SENSOR_NAME]["Temperature"]; if (value_c != NULL && strlen(value_c) > 0 && (isdigit(value_c[0]) || (value_c[0] == '-' && isdigit(value_c[1])) ) ) { - int16_t value = (int16_t)(CharToFloat(value_c) * 10); + int16_t value; + if (Thermostat[ctr_output].status.temp_format == TEMP_FAHRENHEIT) { + value = (int16_t)ThermostatFahrenheitToCelsius((int32_t)(CharToFloat(value_c) * 10), TEMP_CONV_ABSOLUTE); + } + else { + value = (int16_t)(CharToFloat(value_c) * 10); + } if ( (value >= -1000) && (value <= 1000) && (Thermostat[ctr_output].status.sensor_type == SENSOR_LOCAL)) { From 3640372fd08740a1fb03dab2c877d9c2a2172781 Mon Sep 17 00:00:00 2001 From: d0m1n1qu3 Date: Mon, 13 Jul 2020 21:08:10 +0200 Subject: [PATCH 6/7] fix that firmware is send everytime since commit 39705a2e059cf616199c1e18eb04a87d00482b7c --- tasmota/xsns_62_MI_ESP32.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tasmota/xsns_62_MI_ESP32.ino b/tasmota/xsns_62_MI_ESP32.ino index a46f516d3..efbf1a78a 100644 --- a/tasmota/xsns_62_MI_ESP32.ino +++ b/tasmota/xsns_62_MI_ESP32.ino @@ -1508,7 +1508,9 @@ void MI32Show(bool json) if (!isnan(MIBLEsensors[i].fertility)) { ResponseAppend_P(PSTR(",\"Fertility\":%f"), MIBLEsensors[i].fertility); } - ResponseAppend_P(PSTR(",\"Firmware\":\"%s\""), MIBLEsensors[i].firmware); + if (MIBLEsensors[i].firmware != 0x00) { // this is the error code -> no firmware + ResponseAppend_P(PSTR(",\"Firmware\":\"%s\""), MIBLEsensors[i].firmware); + } } if (MIBLEsensors[i].type > FLORA){ if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp)) { From 032c87c7a965644935dfba622381a6e1e633d7eb Mon Sep 17 00:00:00 2001 From: d0m1n1qu3 Date: Mon, 13 Jul 2020 21:24:38 +0200 Subject: [PATCH 7/7] fix that firmware is send everytime since commit 39705a2e059cf616199c1e18eb04a87d00482b7c --- tasmota/xsns_62_MI_ESP32.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/xsns_62_MI_ESP32.ino b/tasmota/xsns_62_MI_ESP32.ino index efbf1a78a..0211d276d 100644 --- a/tasmota/xsns_62_MI_ESP32.ino +++ b/tasmota/xsns_62_MI_ESP32.ino @@ -1508,7 +1508,7 @@ void MI32Show(bool json) if (!isnan(MIBLEsensors[i].fertility)) { ResponseAppend_P(PSTR(",\"Fertility\":%f"), MIBLEsensors[i].fertility); } - if (MIBLEsensors[i].firmware != 0x00) { // this is the error code -> no firmware + if (MIBLEsensors[i].firmware[0] != '\0') { // this is the error code -> no firmware ResponseAppend_P(PSTR(",\"Firmware\":\"%s\""), MIBLEsensors[i].firmware); } }