From 2606002e6e30e7a7bee9ec4975cde1e8d118b70c Mon Sep 17 00:00:00 2001 From: Staars Date: Thu, 15 Oct 2020 16:52:39 +0200 Subject: [PATCH] fix payload error, adaptions for HASS --- tasmota/xsns_61_MI_NRF24.ino | 387 ++++++++++++++++++++++++++--------- 1 file changed, 290 insertions(+), 97 deletions(-) diff --git a/tasmota/xsns_61_MI_NRF24.ino b/tasmota/xsns_61_MI_NRF24.ino index e9d2841eaa79..e76909a4ee66 100644 --- a/tasmota/xsns_61_MI_NRF24.ino +++ b/tasmota/xsns_61_MI_NRF24.ino @@ -20,6 +20,8 @@ -------------------------------------------------------------------------------------------- Version yyyymmdd Action Description -------------------------------------------------------------------------------------------- + 0.9.8.2 20201015 integrate - adaptions for HASS, fix some payload glitches + --- 0.9.8.1 20200918 integrate - add MHOC303, ATC-custom FW, allow lower case commands --- 0.9.8.0 20200705 integrate - add YEE-RC, NLIGHT and MJYD2S, add NRFUSE @@ -39,7 +41,7 @@ 0.9.3.0 20200222 integrate - use now the correct id-word instead of MAC-OUI, add CGG1 --- - 0.9.2.0 20200212 integrate - "backports" from MI-HM10, change reading pattern, + 0.9.2.0 20200212 integrate - "backports" from MI-MINRF, change reading pattern, add missing PDU-types, renaming driver --- 0.9.1.0 20200117 integrate - Added support for the LYWSD02 @@ -281,37 +283,91 @@ struct { uint32_t PDU[3]; bool active = false; } beacon; - bool activeScan = false; - bool stopScan = false; - bool triggeredTELE = false; + + struct { + uint32_t allwaysAggregate:1; // always show all known values of one sensor in brdigemode + // uint32_t ignoreBogusBattery:1; + uint32_t noSummary:1; // no sensor values at TELE-period + uint32_t minimalSummary:1; // DEPRECATED!! + uint32_t directBridgeMode:1; // send every received BLE-packet as a MQTT-message in real-time + } option; + + struct { + uint32_t shallTriggerTele:1; + uint32_t triggeredTele:1; + uint32_t activeScan:1; + uint32_t stopScan:1; + // uint32_t stopScan:1; + } mode; + + // bool activeScan = false; + // bool stopScan = false; + // bool triggeredTELE = false; #ifdef DEBUG_TASMOTA_SENSOR uint8_t streamBuffer[sizeof(buffer)]; // raw data stream bytes - uint8_t lsfrBuffer[sizeof(buffer)]; // correpsonding lfsr-bytes for the buffer, probably only useful for a BLE-packet + uint8_t lsfrBuffer[sizeof(buffer)]; // corresponding lfsr-bytes for the buffer, probably only useful for a BLE-packet #endif // DEBUG_TASMOTA_SENSOR - } MINRF; struct mi_sensor_t{ uint8_t type; //Flora = 1; MJ_HT_V1=2; LYWSD02=3; LYWSD03=4; CGG1=5; CGD1=6; YEERC=9 - uint8_t MAC[6]; + uint8_t lastCnt; //device generated counter of the packet + uint8_t shallSendMQTT; uint8_t showedUp; + uint8_t MAC[6]; + + union { + struct { + uint32_t temp:1; + uint32_t hum:1; + uint32_t tempHum:1; //every hum sensor has temp too, easier to use Tasmota dew point functions + uint32_t lux:1; + uint32_t moist:1; + uint32_t fert:1; + uint32_t bat:1; + uint32_t NMT:1; + uint32_t PIR:1; + uint32_t Btn:1; + }; + uint32_t raw; + } feature; + union { + struct { + uint32_t temp:1; + uint32_t hum:1; + uint32_t tempHum:1; //can be combined from the sensor + uint32_t lux:1; + uint32_t moist:1; + uint32_t fert:1; + uint32_t bat:1; + uint32_t NMT:1; + uint32_t motion:1; + uint32_t noMotion:1; + uint32_t Btn:1; + }; + uint32_t raw; + } eventType; + + uint32_t lastTime; + uint32_t lux; float temp; //Flora, MJ_HT_V1, LYWSD0x, CGx union { struct { - float moisture; - float fertility; - uint32_t lux; + uint8_t moisture; + uint16_t fertility; }; // Flora struct { float hum; - uint8_t bat; - }; // MJ_HT_V1, LYWSD0x, CGx + }; // MJ_HT_V1, LYWSD0x struct { - uint8_t btn; - uint8_t shallSendMQTT; - uint8_t lastCnt; - }; // yee-rc + uint16_t events; //"alarms" since boot + uint32_t NMT; // no motion time in seconds for the MJYD2S + }; + uint16_t Btn; + }; + union { + uint8_t bat; // many values seem to be hard-coded garbage (LYWSD0x, GCD1) }; }; @@ -329,7 +385,6 @@ struct mi_light_t{ uint32_t lastTime; uint8_t lux; //1 or 64 for the MJYD2S uint8_t eventType; //internal type of actual event for the MJYD2S - }; struct scan_entry_t { @@ -354,6 +409,18 @@ static union{ /********************************************************************************************/ +void MINRFinit(void){ + MINRFinitBLE(1); + + MINRF.option.allwaysAggregate = 1; + // MINRF.option.ignoreBogusBattery = 1; // from advertisements + MINRF.option.noSummary = 0; + MINRF.option.minimalSummary = 0; + MINRF.option.directBridgeMode = 0; +} + +/********************************************************************************************/ + /** * @brief * @@ -525,8 +592,8 @@ bool MINRFhandleBeacon(scan_entry_t * entry, uint32_t offset); * */ void MINRFhandleScan(void){ - if(MINRFscanResult.size()>20 || MINRF.stopScan) { - MINRF.activeScan=false; + if(MINRFscanResult.size()>20 || MINRF.mode.stopScan) { + MINRF.mode.activeScan=false; MINRFcomputefirstUsedPacketMode(); uint32_t i = 0; // pass counter as reference to lambda MINRFscanResult.erase(std::remove_if(MINRFscanResult.begin(), @@ -537,7 +604,7 @@ void MINRFhandleScan(void){ return ((e.showedUp < 3)); }), MINRFscanResult.end()); - MINRF.stopScan=false; + MINRF.mode.stopScan=false; return; } @@ -1013,19 +1080,43 @@ uint32_t MINRFgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type){ memcpy(_newSensor.MAC,_MAC, sizeof(_MAC)); _newSensor.type = _type; _newSensor.showedUp = 1; + _newSensor.eventType.raw = 0; + _newSensor.feature.raw = 0; _newSensor.temp =NAN; - switch (_type) - { - case 1: - _newSensor.moisture =NAN; - _newSensor.fertility =NAN; - _newSensor.lux = 0xffffffff; + _newSensor.bat=0x00; + _newSensor.lux = 0x00ffffff; + switch (_type){ + case FLORA: + _newSensor.moisture =0xff; + _newSensor.fertility =0xffff; + _newSensor.feature.temp=1; + _newSensor.feature.moist=1; + _newSensor.feature.fert=1; + _newSensor.feature.lux=1; + _newSensor.feature.bat=0; break; - case 2: case 3: case 4: case 5: case 6: case ATC: - _newSensor.hum=NAN; - _newSensor.bat=0x00; + case NLIGHT: + _newSensor.events=0x00; + _newSensor.feature.PIR=1; + _newSensor.feature.NMT=1; + break; + case MJYD2S: + _newSensor.NMT=0; + _newSensor.events=0x00; + _newSensor.feature.PIR=1; + _newSensor.feature.NMT=1; + _newSensor.feature.lux=1; + _newSensor.feature.bat=1; + break; + case YEERC: + _newSensor.feature.Btn=1; break; default: + _newSensor.hum=NAN; + _newSensor.feature.temp=1; + _newSensor.feature.hum=1; + _newSensor.feature.tempHum=1; + _newSensor.feature.bat=1; break; } MIBLEsensors.push_back(_newSensor); @@ -1067,7 +1158,7 @@ void MINRFconfirmSensors(void){ * */ void MINRFtriggerTele(void){ - MINRF.triggeredTELE = true; + MINRF.mode.triggeredTele= true; mqtt_data[0] = '\0'; if (MqttShowSensor()) { MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); @@ -1086,7 +1177,7 @@ void MINRFhandleMiBeaconPacket(void){ uint32_t _slot = MINRFgetSensorSlot(MINRF.buffer.miBeacon.MAC, MINRF.buffer.miBeacon.PID); if(_slot==0xff) return; DEBUG_SENSOR_LOG(PSTR("NRF: slot %u, size vector: %u %u"),_slot,MIBLEsensors.size()); - mi_sensor_t *_sensorVec = &MIBLEsensors.at(_slot); + mi_sensor_t *_sensorVec = &MIBLEsensors[_slot]; DEBUG_SENSOR_LOG(PSTR("NRF: %u %u %u"),_slot,_sensorVec->type,MINRF.buffer.miBeacon.type); float _tempFloat; int decryptRet; @@ -1110,14 +1201,14 @@ void MINRFhandleMiBeaconPacket(void){ if(MINRF.buffer.miBeacon.counter==_sensorVec->lastCnt) break; // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: YEE-RC button: %u Long: %u"), MINRF.buffer.miBeacon.Btn.num, MINRF.buffer.miBeacon.Btn.longPress); _sensorVec->lastCnt=MINRF.buffer.miBeacon.counter; - _sensorVec->btn=MINRF.buffer.miBeacon.Btn.num + (MINRF.buffer.miBeacon.Btn.longPress/2)*6; - _sensorVec->shallSendMQTT = 1; - MINRFtriggerTele(); + _sensorVec->Btn=MINRF.buffer.miBeacon.Btn.num + (MINRF.buffer.miBeacon.Btn.longPress/2)*6; + _sensorVec->eventType.Btn = 1; break; case 0x04: _tempFloat=(float)(MINRF.buffer.miBeacon.temp)/10.0f; if(_tempFloat<60){ _sensorVec->temp=_tempFloat; + _sensorVec->eventType.temp = 1; DEBUG_SENSOR_LOG(PSTR("Mode 4: temp updated")); } DEBUG_SENSOR_LOG(PSTR("Mode 4: U16: %u Temp"), MINRF.buffer.miBeacon.temp ); @@ -1126,18 +1217,21 @@ void MINRFhandleMiBeaconPacket(void){ _tempFloat=(float)(MINRF.buffer.miBeacon.hum)/10.0f; if(_tempFloat<101){ _sensorVec->hum=_tempFloat; + _sensorVec->eventType.hum = 1; DEBUG_SENSOR_LOG(PSTR("Mode 6: hum updated")); } DEBUG_SENSOR_LOG(PSTR("Mode 6: U16: %u Hum"), MINRF.buffer.miBeacon.hum); break; case 0x07: _sensorVec->lux=MINRF.buffer.miBeacon.lux & 0x00ffffff; + _sensorVec->eventType.lux = 1; DEBUG_SENSOR_LOG(PSTR("Mode 7: U24: %u Lux"), MINRF.buffer.miBeacon.lux & 0x00ffffff); break; case 0x08: _tempFloat =(float)MINRF.buffer.miBeacon.moist; if(_tempFloat<100){ _sensorVec->moisture=_tempFloat; + _sensorVec->eventType.moist = 1; DEBUG_SENSOR_LOG(PSTR("Mode 8: moisture updated")); } DEBUG_SENSOR_LOG(PSTR("Mode 8: U8: %u Moisture"), MINRF.buffer.miBeacon.moist); @@ -1146,6 +1240,7 @@ void MINRFhandleMiBeaconPacket(void){ _tempFloat=(float)(MINRF.buffer.miBeacon.fert); if(_tempFloat<65535){ // ??? _sensorVec->fertility=_tempFloat; + _sensorVec->eventType.fert = 1; DEBUG_SENSOR_LOG(PSTR("Mode 9: fertility updated")); } DEBUG_SENSOR_LOG(PSTR("Mode 9: U16: %u Fertility"), MINRF.buffer.miBeacon.fert); @@ -1153,6 +1248,7 @@ void MINRFhandleMiBeaconPacket(void){ case 0x0a: if(MINRF.buffer.miBeacon.bat<101){ _sensorVec->bat = MINRF.buffer.miBeacon.bat; + _sensorVec->eventType.bat = 1; DEBUG_SENSOR_LOG(PSTR("Mode a: bat updated")); } DEBUG_SENSOR_LOG(PSTR("Mode a: U8: %u %%"), MINRF.buffer.miBeacon.bat); @@ -1168,9 +1264,13 @@ void MINRFhandleMiBeaconPacket(void){ _sensorVec->hum = _tempFloat; DEBUG_SENSOR_LOG(PSTR("Mode d: hum updated")); } + _sensorVec->eventType.tempHum = 1; DEBUG_SENSOR_LOG(PSTR("Mode d: U16: %x Temp U16: %x Hum"), MINRF.buffer.miBeacon.HT.temp, MINRF.buffer.miBeacon.HT.hum); break; } + if(MIBLEsensors[_slot].eventType.raw == 0) return; + MIBLEsensors[_slot].shallSendMQTT = 1; + if (MINRF.option.directBridgeMode) MINRF.mode.shallTriggerTele = 1; } /** @@ -1188,26 +1288,31 @@ void MINRFhandleCGD1Packet(void){ // no MiBeacon float _tempFloat; _tempFloat=(float)(MINRF.buffer.CGDPacket.temp)/10.0f; if(_tempFloat<60){ - MIBLEsensors.at(_slot).temp = _tempFloat; + MIBLEsensors[_slot].temp = _tempFloat; DEBUG_SENSOR_LOG(PSTR("CGD1: temp updated")); } _tempFloat=(float)(MINRF.buffer.CGDPacket.hum)/10.0f; if(_tempFloat<100){ - MIBLEsensors.at(_slot).hum = _tempFloat; + MIBLEsensors[_slot].hum = _tempFloat; DEBUG_SENSOR_LOG(PSTR("CGD1: hum updated")); } DEBUG_SENSOR_LOG(PSTR("CGD1: U16: %x Temp U16: %x Hum"), MINRF.buffer.CGDPacket.temp, MINRF.buffer.CGDPacket.hum); + MIBLEsensors[_slot].eventType.tempHum = 1; break; case 0x0102: if(MINRF.buffer.CGDPacket.bat<101){ - MIBLEsensors.at(_slot).bat = MINRF.buffer.CGDPacket.bat; + MIBLEsensors[_slot].bat = MINRF.buffer.CGDPacket.bat; DEBUG_SENSOR_LOG(PSTR("Mode a: bat updated")); } + MIBLEsensors[_slot].eventType.bat = 1; break; default: DEBUG_SENSOR_LOG(PSTR("NRF: unexpected CGD1-packet")); MINRF_LOG_BUFFER(MINRF.buffer.raw); } + if(MIBLEsensors[_slot].eventType.raw == 0) return; + MIBLEsensors[_slot].shallSendMQTT = 1; + if (MINRF.option.directBridgeMode) MINRF.mode.shallTriggerTele = 1; } void MINRFhandleNlightPacket(void){ // no MiBeacon @@ -1335,11 +1440,13 @@ void MINRFhandleATCPacket(void){ // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: ATC: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"),MINRF.buffer.raw[0],MINRF.buffer.raw[1],MINRF.buffer.raw[2],MINRF.buffer.raw[3],MINRF.buffer.raw[4],MINRF.buffer.raw[5],MINRF.buffer.raw[6],MINRF.buffer.raw[7],MINRF.buffer.raw[8],MINRF.buffer.raw[9],MINRF.buffer.raw[10],MINRF.buffer.raw[11]); if(_slot==0xff) return; - MIBLEsensors.at(_slot).temp = (float)(__builtin_bswap16(_packet->temp))/10.0f; - MIBLEsensors.at(_slot).hum = (float)_packet->hum; - MIBLEsensors.at(_slot).bat = _packet->batPer; - + MIBLEsensors[_slot].temp = (float)(__builtin_bswap16(_packet->temp))/10.0f; + MIBLEsensors[_slot].hum = (float)_packet->hum; + MIBLEsensors[_slot].bat = _packet->batPer; + MIBLEsensors[_slot].eventType.tempHum = 1; + MIBLEsensors[_slot].eventType.bat = 1; MIBLEsensors[_slot].shallSendMQTT = 1; + if (MINRF.option.directBridgeMode) MINRF.mode.shallTriggerTele = 1; } /*********************************************************************************************\ @@ -1356,6 +1463,10 @@ void MINRF_EVERY_50_MSECOND() { // Every 50mseconds MINRF.timer++; if (!MINRFreceivePacket()){ + if(MINRF.mode.shallTriggerTele){ + MINRFtriggerTele(); + MINRF.mode.shallTriggerTele = 0; + } // DEBUG_SENSOR_LOG(PSTR("NRF: nothing received")); // if (MINRF.packetMode==ATC) AddLog_P2(LOG_LEVEL_INFO,PSTR("no ATC..")); } @@ -1384,7 +1495,7 @@ void MINRF_EVERY_50_MSECOND() { // Every 50mseconds break; } } - if (MINRF.beacon.active || MINRF.activeScan) { + if (MINRF.beacon.active || MINRF.mode.activeScan) { MINRF.firstUsedPacketMode=0; } @@ -1405,7 +1516,7 @@ void MINRF_EVERY_50_MSECOND() { // Every 50mseconds } } - if (MINRF.activeScan) MINRF.packetMode=0; + if (MINRF.mode.activeScan) MINRF.packetMode=0; MINRFinitBLE(MINRF.packetMode); @@ -1478,19 +1589,19 @@ bool NRFCmd(void) { MINRF.beacon.active = false; switch(XdrvMailbox.payload){ case 0: // new scan - MINRF.activeScan = true; - MINRF.stopScan = false; + MINRF.mode.activeScan = true; + MINRF.mode.stopScan = false; MINRFscanResult.erase(std::remove_if(MINRFscanResult.begin(), MINRFscanResult.end(), [](scan_entry_t&) { return true; }), MINRFscanResult.end()); break; case 1: // append scan - MINRF.activeScan = true; - MINRF.stopScan = false; + MINRF.mode.activeScan = true; + MINRF.mode.stopScan = false; break; case 2: // stop scan - MINRF.stopScan = true; + MINRF.mode.stopScan = true; break; } Response_P(S_JSON_NRF_COMMAND_NVALUE, command, XdrvMailbox.payload); @@ -1576,67 +1687,144 @@ const char HTTP_NRF24NEW[] PROGMEM = "{s}%sL01%c{m}%u%s / %u{e}"; void MINRFShow(bool json) { if (json) { +#ifdef USE_HOME_ASSISTANT + bool _noSummarySave = MINRF.option.noSummary; + bool _minimalSummarySave = MINRF.option.minimalSummary; + if(hass_mode==2){ + MINRF.option.noSummary = false; + MINRF.option.minimalSummary = false; + } +#endif //USE_HOME_ASSISTANT + if(!MINRF.mode.triggeredTele){ + if(MINRF.option.noSummary) return; // no message at TELEPERIOD + } + for (uint32_t i = 0; i < MIBLEsensors.size(); i++) { + if(MINRF.mode.triggeredTele && MIBLEsensors[i].eventType.raw == 0) continue; + if(MINRF.mode.triggeredTele && MIBLEsensors[i].shallSendMQTT==0) continue; + if(MIBLEsensors[i].showedUp < 3){ DEBUG_SENSOR_LOG(PSTR("NRF: sensor not fully registered yet")); if(MIBLEsensors[i].type != YEERC) break; // send every RC code, even if there is a potentially false MAC } - switch(MIBLEsensors[i].type){ - case YEERC: - if(MIBLEsensors[i].shallSendMQTT==0) continue; - break; - default: - if(MINRF.triggeredTELE) continue; - break; - } - ResponseAppend_P(PSTR(",\"%s-%02x%02x%02x\":{"),kMINRFDeviceType[MIBLEsensors[i].type-1],MIBLEsensors[i].MAC[3],MIBLEsensors[i].MAC[4],MIBLEsensors[i].MAC[5]); - switch(MIBLEsensors[i].type){ - case FLORA: - if(MINRF.triggeredTELE) { - ResponseJsonEnd(); - break; - } - char stemp[FLOATSZ]; - dtostrfd(MIBLEsensors[i].temp, Settings.flag2.temperature_resolution, stemp); - ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "\":%s"), stemp); - if(MIBLEsensors[i].lux!=0xffffffff){ // this is the error code -> no lux - ResponseAppend_P(PSTR(",\"" D_JSON_ILLUMINANCE "\":%u"), MIBLEsensors[i].lux); + ResponseAppend_P(PSTR(",\"%s-%02x%02x%02x\":"),kMINRFDeviceType[MIBLEsensors[i].type-1],MIBLEsensors[i].MAC[3],MIBLEsensors[i].MAC[4],MIBLEsensors[i].MAC[5]); + + uint32_t _positionCurlyBracket = strlen(mqtt_data); // ... this will be a ',' first, but later be replaced + + if((!MINRF.mode.triggeredTele && !MINRF.option.minimalSummary)||MINRF.mode.triggeredTele){ + bool tempHumSended = false; + if(MIBLEsensors[i].feature.tempHum){ + if(MIBLEsensors[i].eventType.tempHum || !MINRF.mode.triggeredTele || MINRF.option.allwaysAggregate){ + if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp) +#ifdef USE_HOME_ASSISTANT + ||(!hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { + ResponseAppend_P(PSTR(",")); + ResponseAppendTHD(MIBLEsensors[i].temp, MIBLEsensors[i].hum); + tempHumSended = true; + } } - if(!isnan(MIBLEsensors[i].moisture)){ - dtostrfd(MIBLEsensors[i].moisture, 0, stemp); - ResponseAppend_P(PSTR(",\"" D_JSON_MOISTURE "\":%s"), stemp); + } + if(MIBLEsensors[i].feature.temp && !tempHumSended){ + if(MIBLEsensors[i].eventType.temp || !MINRF.mode.triggeredTele || MINRF.option.allwaysAggregate) { + if (!isnan(MIBLEsensors[i].temp) +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { + char temperature[FLOATSZ]; + dtostrfd(MIBLEsensors[i].temp, Settings.flag2.temperature_resolution, temperature); + ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%s"), temperature); + } } - if(!isnan(MIBLEsensors[i].fertility)){ - dtostrfd(MIBLEsensors[i].fertility, 0, stemp); - ResponseAppend_P(PSTR(",\"Fertility\":%s"), stemp); + } + if(MIBLEsensors[i].feature.hum && !tempHumSended){ + if(MIBLEsensors[i].eventType.hum || !MINRF.mode.triggeredTele || MINRF.option.allwaysAggregate) { + if (!isnan(MIBLEsensors[i].hum)) { + char hum[FLOATSZ]; + dtostrfd(MIBLEsensors[i].hum, Settings.flag2.humidity_resolution, hum); + ResponseAppend_P(PSTR(",\"" D_JSON_HUMIDITY "\":%s"), hum); + } } - ResponseJsonEnd(); - break; - case YEERC: - if(MIBLEsensors[i].shallSendMQTT == 1){ - ResponseAppend_P(PSTR("\"Btn\":%u"), MIBLEsensors[i].btn); - MIBLEsensors[i].shallSendMQTT = 0; + } + if (MIBLEsensors[i].feature.lux){ + if(MIBLEsensors[i].eventType.lux || !MINRF.mode.triggeredTele || MINRF.option.allwaysAggregate){ + if (MIBLEsensors[i].lux!=0x0ffffff +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { // this is the error code -> no lux + ResponseAppend_P(PSTR(",\"" D_JSON_ILLUMINANCE "\":%u"), MIBLEsensors[i].lux); + } } - ResponseJsonEnd(); - break; - default: - if(MINRF.triggeredTELE) { - ResponseJsonEnd(); - break; + } + if (MIBLEsensors[i].feature.moist){ + if(MIBLEsensors[i].eventType.moist || !MINRF.mode.triggeredTele || MINRF.option.allwaysAggregate){ + if (MIBLEsensors[i].moisture!=0xff +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { + ResponseAppend_P(PSTR(",\"" D_JSON_MOISTURE "\":%u"), MIBLEsensors[i].moisture); + } } - if(!isnan(MIBLEsensors[i].temp) && !isnan(MIBLEsensors[i].hum)){ - ResponseAppendTHD(MIBLEsensors[i].temp,MIBLEsensors[i].hum); + } + if (MIBLEsensors[i].feature.fert){ + if(MIBLEsensors[i].eventType.fert || !MINRF.mode.triggeredTele || MINRF.option.allwaysAggregate){ + if (MIBLEsensors[i].fertility!=0xffff +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { + ResponseAppend_P(PSTR(",\"Fertility\":%u"), MIBLEsensors[i].fertility); + } } - if(MIBLEsensors[i].bat!=0x00){ // this is the error code -> no battery - ResponseAppend_P(PSTR(",\"Battery\":%u"), MIBLEsensors[i].bat); + } + if (MIBLEsensors[i].feature.Btn){ + if(MIBLEsensors[i].eventType.Btn){ + ResponseAppend_P(PSTR(",\"Btn\":%u"),MIBLEsensors[i].Btn); } - ResponseJsonEnd(); - break; + } + } // minimal summary + if (MIBLEsensors[i].feature.PIR){ + if(MIBLEsensors[i].eventType.motion || !MINRF.mode.triggeredTele){ + if(MINRF.mode.triggeredTele) ResponseAppend_P(PSTR(",\"PIR\":1")); // only real-time + ResponseAppend_P(PSTR(",\"Events\":%u"),MIBLEsensors[i].events); + } + else if(MIBLEsensors[i].eventType.noMotion && MINRF.mode.triggeredTele){ + ResponseAppend_P(PSTR(",\"PIR\":0")); + } + } + + if (MIBLEsensors[i].feature.NMT || !MINRF.mode.triggeredTele){ + if(MIBLEsensors[i].eventType.NMT){ + ResponseAppend_P(PSTR(",\"NMT\":%u"), MIBLEsensors[i].NMT); + } + } + if (MIBLEsensors[i].feature.bat){ + if(MIBLEsensors[i].eventType.bat || !MINRF.mode.triggeredTele || MINRF.option.allwaysAggregate){ + if (MIBLEsensors[i].bat != 0x00 // this is the error code -> no battery +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { + ResponseAppend_P(PSTR(",\"Battery\":%u"), MIBLEsensors[i].bat); + } + } + } + if(_positionCurlyBracket==strlen(mqtt_data)) ResponseAppend_P(PSTR(",")); // write some random char, to be overwritten in the next step + ResponseAppend_P(PSTR("}")); + mqtt_data[_positionCurlyBracket] = '{'; + MIBLEsensors[i].eventType.raw = 0; + if(MIBLEsensors[i].shallSendMQTT==1){ + MIBLEsensors[i].shallSendMQTT = 0; + continue; } } for(uint32_t i=0; i