From 64521b42b6b46fc3c47d3623aa069eb48ad0a0b0 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 11 Jul 2019 14:09:42 +0200 Subject: [PATCH] Add Epoch and UptimeSec to JSON messages Add Epoch and UptimeSec to JSON messages (#6068) --- sonoff/_changelog.ino | 5 ++++- sonoff/sonoff.ino | 14 ++++---------- sonoff/support.ino | 11 +++++++++++ sonoff/xdrv_03_energy.ino | 2 +- sonoff/xdrv_15_pca9685.ino | 4 ++-- sonoff/xsns_29_mcp230xx.ino | 14 +++++++++----- sonoff/xsns_40_pn532.ino | 8 +++++--- sonoff/xsns_44_sps30.ino | 2 +- 8 files changed, 37 insertions(+), 23 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 0616140bd..5e041384e 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,9 +1,12 @@ /*********************************************************************************************\ * 6.6.0.1 20190708 + * Fix Domoticz battery level set to 100 if define USE_ADC_VCC is not used (#6033) + * Change defines USE_TX20_WIND_SENSOR and USE_RC_SWITCH in my_user_config.h to disable to lower iram usage enabling latest core compilation (#6060, #6062) * Add blend RGB leds with White leds for better whites (#5895, #5704) * Add command SetOption41 0..8 to control number of Tuya switches (#6039) + * Add AZ7798 automatic setting of clock display (#6034) * Add command SetOption42 0..255 to set overtemperature (Celsius only) threshold resulting in power off all on energy monitoring devices. Default setting is 90 (#6036) - * Fix Domoticz battery level set to 100 if define USE_ADC_VCC is not used (#6033) + * Add Epoch and UptimeSec to JSON messages (#6068) * * 6.6.0 20190707 * Remove support of TLS on core 2.3.0 and extent support on core 2.4.2 and up diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index ccfaaccef..719ff812d 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -1940,7 +1940,8 @@ void MqttShowState(void) { char stemp1[33]; - ResponseAppend_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\""), GetDateAndTime(DT_LOCAL).c_str(), GetUptime().c_str()); + ResponseAppendTime(); + ResponseAppend_P(PSTR(",\"" D_JSON_UPTIME "\":\"%s\",\"UptimeSec\":%u"), GetUptime().c_str(), UpTime()); #ifdef USE_ADC_VCC dtostrfd((double)ESP.getVcc()/1000, 3, stemp1); @@ -1987,7 +1988,8 @@ void MqttPublishTeleState(void) bool MqttShowSensor(void) { - ResponseAppend_P(PSTR("{\"" D_JSON_TIME "\":\"%s\""), GetDateAndTime(DT_LOCAL).c_str()); + ResponseAppendTime(); + int json_data_start = strlen(mqtt_data); for (uint32_t i = 0; i < MAX_SWITCHES; i++) { #ifdef USE_TM1638 @@ -2083,14 +2085,6 @@ void PerformEverySecond(void) XdrvCall(FUNC_EVERY_SECOND); XsnsCall(FUNC_EVERY_SECOND); -/* - if ((2 == RtcTime.minute) && latest_uptime_flag) { - latest_uptime_flag = false; - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"" D_JSON_UPTIME "\":\"%s\"}"), GetDateAndTime(DT_LOCAL).c_str(), GetUptime().c_str()); - MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_UPTIME)); - } - if ((3 == RtcTime.minute) && !latest_uptime_flag) latest_uptime_flag = true; -*/ } /*********************************************************************************************\ diff --git a/sonoff/support.ino b/sonoff/support.ino index 41e5ffd3b..3bdddcb9d 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -880,6 +880,17 @@ int ResponseAppend_P(const char* format, ...) // Content send snprintf_P char d return len + mlen; } +int ResponseAppendTime(void) +{ + return ResponseAppend_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"Epoch\":%u"), GetDateAndTime(DT_LOCAL).c_str(), UtcTime()); +} + +int ResponseBeginTime(void) +{ + mqtt_data[0] = '\0'; + return ResponseAppendTime(); +} + int ResponseJsonEnd(void) { return ResponseAppend_P(PSTR("}")); diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index b9c58e127..ca6c900c4 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -311,7 +311,7 @@ void EnergyMarginCheck(void) void EnergyMqttShow(void) { // {"Time":"2017-12-16T11:48:55","ENERGY":{"Total":0.212,"Yesterday":0.000,"Today":0.014,"Period":2.0,"Power":22.0,"Factor":1.00,"Voltage":213.6,"Current":0.100}} - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\""), GetDateAndTime(DT_LOCAL).c_str()); + ResponseBeginTime(); int tele_period_save = tele_period; tele_period = 2; EnergyShow(true); diff --git a/sonoff/xdrv_15_pca9685.ino b/sonoff/xdrv_15_pca9685.ino index 3e8d26e42..906207851 100644 --- a/sonoff/xdrv_15_pca9685.ino +++ b/sonoff/xdrv_15_pca9685.ino @@ -166,8 +166,8 @@ bool PCA9685_Command(void) void PCA9685_OutputTelemetry(bool telemetry) { if (0 == pca9685_detected) { return; } // We do not do this if the PCA9685 has not been detected - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"PCA9685\": {"), GetDateAndTime(DT_LOCAL).c_str()); - ResponseAppend_P(PSTR("\"PWM_FREQ\":%i,"),pca9685_freq); + ResponseBeginTime(); + ResponseAppend_P(PSTR(",\"PCA9685\":{\"PWM_FREQ\":%i,"),pca9685_freq); for (uint32_t pin=0;pin<16;pin++) { ResponseAppend_P(PSTR("\"PWM%i\":%i,"),pin,pca9685_pin_pwm_value[pin]); } diff --git a/sonoff/xsns_29_mcp230xx.ino b/sonoff/xsns_29_mcp230xx.ino index 6d4384edb..2c6ae71ef 100644 --- a/sonoff/xsns_29_mcp230xx.ino +++ b/sonoff/xsns_29_mcp230xx.ino @@ -303,8 +303,9 @@ void MCP230xx_CheckForInterrupt(void) { break; } if (int_tele) { - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"MCP230XX_INT\":{\"D%i\":%i,\"MS\":%lu}}"), - GetDateAndTime(DT_LOCAL).c_str(), intp+(mcp230xx_port*8), ((mcp230xx_intcap >> intp) & 0x01),millis_since_last_int); + ResponseBeginTime(); + ResponseAppend_P(PSTR(",\"MCP230XX_INT\":{\"D%i\":%i,\"MS\":%lu}}"), + intp+(mcp230xx_port*8), ((mcp230xx_intcap >> intp) & 0x01),millis_since_last_int); MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR("MCP230XX_INT")); } if (int_event) { @@ -729,7 +730,8 @@ void MCP230xx_OutputTelemetry(void) { } if (outputcount) { char stt[7]; - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"MCP230_OUT\": {"), GetDateAndTime(DT_LOCAL).c_str()); + ResponseBeginTime(); + ResponseAppend_P(PSTR(",\"MCP230_OUT\":{")); for (uint32_t pinx = 0;pinx < mcp230xx_pincount;pinx++) { if (Settings.mcp230xx_config[pinx].pinmode >= 5) { sprintf(stt,ConvertNumTxt(((gpiototal>>pinx)&1),Settings.mcp230xx_config[pinx].pinmode)); @@ -744,7 +746,8 @@ void MCP230xx_OutputTelemetry(void) { #endif // USE_MCP230xx_OUTPUT void MCP230xx_Interrupt_Counter_Report(void) { - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"MCP230_INTTIMER\": {"), GetDateAndTime(DT_LOCAL).c_str()); + ResponseBeginTime(); + ResponseAppend_P(PSTR(",\"MCP230_INTTIMER\":{")); for (uint32_t pinx = 0;pinx < mcp230xx_pincount;pinx++) { if (Settings.mcp230xx_config[pinx].int_count_en) { // Counting is enabled for this pin so we add to report ResponseAppend_P(PSTR("\"INTCNT_D%i\":%i,"),pinx,mcp230xx_int_counter[pinx]); @@ -758,7 +761,8 @@ void MCP230xx_Interrupt_Counter_Report(void) { void MCP230xx_Interrupt_Retain_Report(void) { uint16_t retainresult = 0; - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"MCP_INTRETAIN\": {"), GetDateAndTime(DT_LOCAL).c_str()); + ResponseBeginTime(); + ResponseAppend_P(PSTR(",\"MCP_INTRETAIN\":{")); for (uint32_t pinx = 0;pinx < mcp230xx_pincount;pinx++) { if (Settings.mcp230xx_config[pinx].int_retain_flag) { ResponseAppend_P(PSTR("\"D%i\":%i,"),pinx,mcp230xx_int_retainer[pinx]); diff --git a/sonoff/xsns_40_pn532.ino b/sonoff/xsns_40_pn532.ino index 210607fa8..f7aaffc71 100644 --- a/sonoff/xsns_40_pn532.ino +++ b/sonoff/xsns_40_pn532.ino @@ -497,7 +497,7 @@ void PN532_ScanForTag(void) pn532_function = 0; #endif // USE_PN532_DATA_FUNCTION - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\""), GetDateAndTime(DT_LOCAL).c_str()); + ResponseBeginTime(); #ifdef USE_PN532_DATA_FUNCTION ResponseAppend_P(PSTR(",\"PN532\":{\"UID\":\"%s\", \"DATA\":\"%s\"}}"), uids, card_datas); @@ -544,7 +544,8 @@ bool PN532_Command(void) if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"E")) { pn532_function = 1; // Block 1 of next card/tag will be reset to 0x00... AddLog_P(LOG_LEVEL_INFO, PSTR("NFC: PN532 NFC - Next scanned tag data block 1 will be erased")); - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"PN532\":{\"COMMAND\":\"E\"}}"), GetDateAndTime(DT_LOCAL).c_str()); + ResponseBeginTime(); + ResponseAppend_P(PSTR(",\"PN532\":{\"COMMAND\":\"E\"}}")); return serviced; } if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"S")) { @@ -560,7 +561,8 @@ bool PN532_Command(void) pn532_newdata[pn532_newdata_len] = 0x00; // Null terminate the string pn532_function = 2; AddLog_P2(LOG_LEVEL_INFO, PSTR("NFC: PN532 NFC - Next scanned tag data block 1 will be set to '%s'"), pn532_newdata); - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\",\"PN532\":{\"COMMAND\":\"S\"}}"), GetDateAndTime(DT_LOCAL).c_str()); + ResponseBeginTime(); + ResponseAppend_P(PSTR(",\"PN532\":{\"COMMAND\":\"S\"}}")); return serviced; } } diff --git a/sonoff/xsns_44_sps30.ino b/sonoff/xsns_44_sps30.ino index 0d7b2e133..1b1434044 100644 --- a/sonoff/xsns_44_sps30.ino +++ b/sonoff/xsns_44_sps30.ino @@ -253,7 +253,7 @@ void SPS30_Show(bool json) { void CmdClean(void) { sps30_cmd(SPS_CMD_CLEAN); - Response_P(PSTR("{\"" D_JSON_TIME "\":\"%s\""), GetDateAndTime(DT_LOCAL).c_str()); + ResponseBeginTime(); ResponseAppend_P(PSTR(",\"SPS30\":{\"CFAN\":\"true\"}}")); MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); }