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..2db39f36a 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -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 f8dfb7cc8..05d4ed7ef 100755 --- a/sonoff/xdrv_wemohue.ino +++ b/sonoff/xdrv_wemohue.ino @@ -242,18 +242,18 @@ void PollUdp() const char WEMO_EVENTSERVICE_XML[] PROGMEM = "" - "" - "" - "SetBinaryState" - "" - "" - "" - "BinaryState" - "BinaryState" - "in" - "" - "" - "" + "" + "" + "SetBinaryState" + "" + "" + "" + "BinaryState" + "BinaryState" + "in" + "" + "" + "" "" "BinaryState" "Boolean" @@ -263,34 +263,14 @@ const char WEMO_EVENTSERVICE_XML[] PROGMEM = "level" "string" "0" - "" - "" - "" - "" - "GetBinaryState" - "" - "" - "" - "BinaryState" - "BinaryState" - "out" - "" - "" + "" + "" "" "\r\n" "\r\n"; -const char WEMO_RESPONSE_STATE_XML[] PROGMEM = -"" - "" - "" - "{x2" - "" - "" -"\r\n" -"\r\n"; const char WEMO_SETUP_XML[] PROGMEM = "" - "" + "" "" "urn:Belkin:device:controllee:1" "{x1" @@ -319,32 +299,17 @@ void HandleUpnpEvent() { AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_BASIC_EVENT)); String request = WebServer->arg(0); - String state_xml = FPSTR(WEMO_RESPONSE_STATE_XML); - //differentiate get and set state if (request.indexOf(F("SetBinaryState")) > 0) { if (request.indexOf(F("State>1 0) { - AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_BASIC_EVENT " SET STATE 1")); + // ExecuteCommandPower(1, 1); ExecuteCommandPower(devices_present, 1); - state_xml.replace("{x1", "Set"); - state_xml.replace("{x2", "1"); } - else if (request.indexOf(F("State>0 0) { - AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_BASIC_EVENT " SET STATE 0")); + if (request.indexOf(F("State>0 0) { + // ExecuteCommandPower(1, 0); ExecuteCommandPower(devices_present, 0); - - state_xml.replace("{x1", "Set"); - state_xml.replace("{x2", "0"); } - WebServer->send(200, FPSTR(HDR_CTYPE_XML), state_xml.c_str()); - } - else if(request.indexOf(F("GetBinaryState")) > 0){ - char svalue[80]; - snprintf_P(svalue, sizeof(svalue), PSTR("%d"), bitRead(power, 1 -1)); - AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_BASIC_EVENT " GET STATE")); - state_xml.replace("{x1", "Get"); - state_xml.replace("{x2", svalue); - WebServer->send(200, FPSTR(HDR_CTYPE_XML), state_xml.c_str()); } + WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), ""); } void HandleUpnpService() @@ -757,3 +722,4 @@ void HandleHueApi(String *path) } #endif // USE_WEBSERVER #endif // USE_EMULATION +