From 2e7248190ca0c25c6369f8e8c5b09c6dc8081f77 Mon Sep 17 00:00:00 2001 From: arendst Date: Sun, 17 Dec 2017 16:01:30 +0100 Subject: [PATCH 1/2] Changes in Wemo and HTTP response to JSON * Change Wemo SetBinaryState to distinguish from GetBinaryState (#1357) * Change output of HTTP command to valid JSON only (#1363) --- sonoff/_releasenotes.ino | 6 ++++-- sonoff/sonoff.ino | 7 +++++-- sonoff/webserver.ino | 26 ++++++++++++-------------- sonoff/xdrv_wemohue.ino | 16 +++++++++------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index 2d9dd0b27..826c4011b 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,6 +1,8 @@ /* 5.10.0b - * Change Sonoff Pow Energy MQTT data JSON message and consolidate Status 8 into Status 10 - * Add optional support for PZEM004T energy sensor to enable with define USE_PZEM004T in user_config.h (#614) + * Add optional support for PZEM004T energy sensor + * Change Sonoff Pow Enenrgy MQTT data message and consolidate Status 8 into Status 10 + * Change Wemo SetBinaryState to distinguish from GetBinaryState (#1357) + * Change output of HTTP command to valid JSON only (#1363) * * 5.10.0a * Add (experimental) support for sensor SHT3x diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 11e642d78..467745b98 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -177,7 +177,7 @@ power_t rel_inverted = 0; // Relay inverted flag (1 = (0 = On, uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1 = Off)) uint8_t pwm_inverted = 0; // PWM inverted flag (1 = inverted) uint8_t dht_flg = 0; // DHT configured -uint8_t energy_flg = 1; // Energy monitor configured +bool energy_flg = true; // Energy monitor configured uint8_t i2c_flg = 0; // I2C configured uint8_t spi_flg = 0; // SPI configured uint8_t light_type = 0; // Light types @@ -189,7 +189,7 @@ boolean (*xsns_func_ptr[XSNS_MAX])(byte); // External Sensor Function Pointers char my_hostname[33]; // Composed Wifi hostname char mqtt_client[33]; // Composed MQTT Clientname char serial_in_buffer[INPUT_BUFFER_SIZE + 2]; // Receive buffer -char mqtt_data[TOPSZ + MESSZ]; // MQTT publish buffer +char mqtt_data[MESSZ + TOPSZ]; // MQTT publish buffer (MESSZ) and web page ajax buffer (MESSZ + TOPSZ) char log_data[TOPSZ + MESSZ]; // Logging String web_log[MAX_LOG_LINES]; // Web log buffer String backlog[MAX_BACKLOG]; // Command backlog @@ -349,6 +349,7 @@ void MqttSubscribe(char *topic) void MqttPublishDirect(const char* topic, boolean retained) { if (Settings.flag.mqtt_enabled) { + mqtt_data[MESSZ -1] = '\0'; if (MqttClient.publish(topic, mqtt_data, retained)) { snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MQTT "%s = %s%s"), topic, mqtt_data, (retained) ? " (" D_RETAINED ")" : ""); // MqttClient.loop(); // Do not use here! Will block previous publishes @@ -2622,6 +2623,8 @@ void GpioInit() } #endif // USE_IR_RECEIVE #endif // USE_IR_REMOTE + +// energy_flg = (((pin[GPIO_HLW_SEL] < 99) && (pin[GPIO_HLW_CF1] < 99) && (pin[GPIO_HLW_CF] < 99)) || ((pin[GPIO_PZEM_RX] < 99) && (pin[GPIO_PZEM_TX]))); } extern "C" { diff --git a/sonoff/webserver.ino b/sonoff/webserver.ino index c7d688ebd..0f08635d0 100644 --- a/sonoff/webserver.ino +++ b/sonoff/webserver.ino @@ -1337,7 +1337,7 @@ void HandleHttpCommand() } } - String message = ""; + String message = F("{\"" D_RSLT_WARNING "\":\""); if (valid) { byte curridx = web_log_index; if (strlen(WebServer->arg("cmnd").c_str())) { @@ -1351,18 +1351,15 @@ void HandleHttpCommand() if (web_log_index != curridx) { byte counter = curridx; + message = F("{"); do { if (web_log[counter].length()) { - if (message.length()) { - message += F("\n"); - } - if (Settings.flag.mqtt_enabled) { - // [14:49:36 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [RESULT = {"POWER":"OFF"}] -// message += web_log[counter].substring(17 + strlen(PUB_PREFIX) + strlen(Settings.mqtt_topic)); - message += web_log[counter].substring(web_log[counter].lastIndexOf("/",web_log[counter].indexOf("="))+1); - } else { - // [14:49:36 RSLT: RESULT = {"POWER":"OFF"}] > [RESULT = {"POWER":"OFF"}] - message += web_log[counter].substring(web_log[counter].indexOf(": ")+2); + // [14:49:36 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}] + if (web_log[counter].indexOf("{") > 0) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O]) + if (message.length() > 1) { + message += F(","); + } + message += web_log[counter].substring(web_log[counter].indexOf("{")+1,web_log[counter].length()-1); } } counter++; @@ -1370,13 +1367,14 @@ void HandleHttpCommand() counter = 0; } } while (counter != web_log_index); + message += F("}"); } else { - message = F(D_ENABLE_WEBLOG_FOR_RESPONSE "\n"); + message += F(D_ENABLE_WEBLOG_FOR_RESPONSE "\"}"); } } else { - message = F(D_NEED_USER_AND_PASSWORD "\n"); + message += F(D_NEED_USER_AND_PASSWORD "\"}"); } - WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), message); + WebServer->send(200, FPSTR(HDR_CTYPE_JSON), message); } void HandleConsole() diff --git a/sonoff/xdrv_wemohue.ino b/sonoff/xdrv_wemohue.ino index 9fbb3cd35..05d4ed7ef 100755 --- a/sonoff/xdrv_wemohue.ino +++ b/sonoff/xdrv_wemohue.ino @@ -299,13 +299,15 @@ void HandleUpnpEvent() { AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_BASIC_EVENT)); String request = WebServer->arg(0); - if (request.indexOf(F("State>1 0) { -// ExecuteCommandPower(1, 1); - ExecuteCommandPower(devices_present, 1); - } - if (request.indexOf(F("State>0 0) { -// ExecuteCommandPower(1, 0); - ExecuteCommandPower(devices_present, 0); + if (request.indexOf(F("SetBinaryState")) > 0) { + if (request.indexOf(F("State>1 0) { + // ExecuteCommandPower(1, 1); + ExecuteCommandPower(devices_present, 1); + } + if (request.indexOf(F("State>0 0) { + // ExecuteCommandPower(1, 0); + ExecuteCommandPower(devices_present, 0); + } } WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), ""); } From df397e9addbb25bc187c6e60cf35ed62ca4ca1a9 Mon Sep 17 00:00:00 2001 From: arendst Date: Sun, 17 Dec 2017 16:08:39 +0100 Subject: [PATCH 2/2] Missed update --- sonoff/sonoff.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 467745b98..2db39f36a 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -177,7 +177,7 @@ power_t rel_inverted = 0; // Relay inverted flag (1 = (0 = On, uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1 = Off)) uint8_t pwm_inverted = 0; // PWM inverted flag (1 = inverted) uint8_t dht_flg = 0; // DHT configured -bool energy_flg = true; // Energy monitor configured +uint8_t energy_flg = 1; // Energy monitor configured uint8_t i2c_flg = 0; // I2C configured uint8_t spi_flg = 0; // SPI configured uint8_t light_type = 0; // Light types