diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index bccd057cf..51b89031c 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -167,7 +167,6 @@ bool i2c_flg = false; // I2C configured bool spi_flg = false; // SPI configured bool soft_spi_flg = false; // Software SPI configured bool ntp_force_sync = false; // Force NTP sync -bool reset_web_log_flag = false; // Reset web console log myio my_module; // Active copy of Module GPIOs (17 x 8 bits) gpio_flag my_module_flag; // Active copy of Module GPIO flags StateBitfield global_state; // Global states (currently Wifi and Mqtt) (8 bits) diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index d8ab7b56e..415d14e53 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -383,7 +383,7 @@ const char HTTP_END[] PROGMEM = ""; const char HTTP_DEVICE_CONTROL[] PROGMEM = ""; // ?o is related to WebGetArg("o", tmp, sizeof(tmp)); -const char HTTP_DEVICE_STATE[] PROGMEM = "%s%s"; // {c} = %'>
%s
"; // {c} = %'>
"), mqtt_data); + char stemp[strlen(mqtt_data) +1]; + memcpy(stemp, mqtt_data, sizeof(stemp)); + + WSContentBegin(200, CT_HTML); + WSContentSend_P(PSTR("{t}%s"), stemp); if (devices_present) { - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s{t}"), mqtt_data); + WSContentSend_P(PSTR("{t}")); uint8_t fsize = (devices_present < 5) ? 70 - (devices_present * 8) : 32; if (SONOFF_IFAN02 == my_module_type) { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_DEVICE_STATE, - mqtt_data, 36, (bitRead(power, 0)) ? "bold" : "normal", 54, GetStateText(bitRead(power, 0))); + WSContentSend_P(HTTP_DEVICE_STATE, 36, (bitRead(power, 0)) ? "bold" : "normal", 54, GetStateText(bitRead(power, 0))); uint8_t fanspeed = GetFanspeed(); snprintf_P(svalue, sizeof(svalue), PSTR("%d"), fanspeed); - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_DEVICE_STATE, - mqtt_data, 64, (fanspeed) ? "bold" : "normal", 54, (fanspeed) ? svalue : GetStateText(0)); + WSContentSend_P(HTTP_DEVICE_STATE, 64, (fanspeed) ? "bold" : "normal", 54, (fanspeed) ? svalue : GetStateText(0)); } else { for (uint8_t idx = 1; idx <= devices_present; idx++) { snprintf_P(svalue, sizeof(svalue), PSTR("%d"), bitRead(power, idx -1)); - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_DEVICE_STATE, - mqtt_data, 100 / devices_present, (bitRead(power, idx -1)) ? "bold" : "normal", fsize, (devices_present < 5) ? GetStateText(bitRead(power, idx -1)) : svalue); + WSContentSend_P(HTTP_DEVICE_STATE, 100 / devices_present, (bitRead(power, idx -1)) ? "bold" : "normal", fsize, (devices_present < 5) ? GetStateText(bitRead(power, idx -1)) : svalue); } } - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s"), mqtt_data); + WSContentSend_P(PSTR("")); } - WSHeaderSend(); - WSSend(200, CT_HTML, mqtt_data); + WSContentEnd(); + return true; } @@ -987,7 +1003,7 @@ void HandleConfiguration(void) WSContentButton(BUTTON_RESTORE); WSContentSpaceButton(BUTTON_MAIN); - WSContentEnd(); + WSContentStop(); } /*-------------------------------------------------------------------------------------------*/ @@ -1005,13 +1021,12 @@ void HandleTemplateConfiguration(void) char stemp[20]; // Template number and Sensor name if (WebServer->hasArg("m")) { - String page = ""; + WSContentBegin(200, CT_PLAIN); for (uint8_t i = 0; i < MAXMODULE; i++) { // "}2'%d'>%s (%d)}3" - "}2'0'>Sonoff Basic (1)}3" uint8_t midx = pgm_read_byte(kModuleNiceList + i); - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MODULE_TEMPLATE_REPLACE, midx, AnyModuleName(midx).c_str(), midx +1); - page += mqtt_data; + WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, AnyModuleName(midx).c_str(), midx +1); } - WSSend(200, CT_PLAIN, page); + WSContentEnd(); return; } @@ -1025,32 +1040,26 @@ void HandleTemplateConfiguration(void) gpio_flag flag = ModuleFlag(); Settings.module = module_save; - String page = AnyModuleName(module); // NAME: Generic - page += F("}1"); // Field separator - + WSContentBegin(200, CT_PLAIN); + WSContentSend_P(PSTR("%s}1"), AnyModuleName(module).c_str()); // NAME: Generic for (uint8_t i = 0; i < sizeof(kGpioNiceList); i++) { // GPIO: }2'0'>None (0)}3}2'17'>Button1 (17)}3... if (1 == i) { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MODULE_TEMPLATE_REPLACE, 255, D_SENSOR_USER, 255); // }2'255'>User (255)}3 - page += mqtt_data; + WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, 255, D_SENSOR_USER, 255); // }2'255'>User (255)}3 } uint8_t midx = pgm_read_byte(kGpioNiceList + i); - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MODULE_TEMPLATE_REPLACE, midx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames), midx); - page += mqtt_data; + WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames), midx); } - page += F("}1"); // Field separator - mqtt_data[0] = '\0'; + WSContentSend_P(PSTR("}1")); // Field separator for (uint8_t i = 0; i < sizeof(cmodule); i++) { // 17,148,29,149,7,255,255,255,138,255,139,255,255 if ((i < 6) || ((i > 8) && (i != 11))) { // Ignore flash pins GPIO06, 7, 8 and 11 - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s%s%d"), mqtt_data, (i>0)?",":"", cmodule.io[i]); + WSContentSend_P(PSTR("%s%d"), (i>0)?",":"", cmodule.io[i]); } } - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}1%d}1%d"), mqtt_data, flag, Settings.user_template_base); // FLAG: 1 BASE: 17 - page += mqtt_data; - - WSSend(200, CT_PLAIN, page); + WSContentSend_P(PSTR("}1%d}1%d"), flag, Settings.user_template_base); // FLAG: 1 BASE: 17 + WSContentEnd(); return; } @@ -1078,7 +1087,7 @@ void HandleTemplateConfiguration(void) WSContentSend_P(HTTP_FORM_TEMPLATE_FLAG); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } void TemplateSaveSettings(void) @@ -1132,7 +1141,7 @@ void HandleModuleConfiguration(void) ModuleGpios(&cmodule); if (WebServer->hasArg("m")) { - String page = ""; + WSContentBegin(200, CT_PLAIN); uint8_t vidx = 0; for (uint8_t i = 0; i <= MAXMODULE; i++) { // "}2'%d'>%s (%d)}3" - "}2'255'>UserTemplate (0)}3" - "}2'0'>Sonoff Basic (1)}3" if (0 == i) { @@ -1142,23 +1151,21 @@ void HandleModuleConfiguration(void) midx = pgm_read_byte(kModuleNiceList + i -1); vidx = midx +1; } - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MODULE_TEMPLATE_REPLACE, midx, AnyModuleName(midx).c_str(), vidx); - page += mqtt_data; + WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, AnyModuleName(midx).c_str(), vidx); } - WSSend(200, CT_PLAIN, page); + WSContentEnd(); return; } if (WebServer->hasArg("g")) { - String page = ""; + WSContentBegin(200, CT_PLAIN); for (uint8_t j = 0; j < sizeof(kGpioNiceList); j++) { midx = pgm_read_byte(kGpioNiceList + j); if (!GetUsedInModule(midx, cmodule.io)) { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MODULE_TEMPLATE_REPLACE, midx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames), midx); - page += mqtt_data; + WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames), midx); } } - WSSend(200, CT_PLAIN, page); + WSContentEnd(); return; } @@ -1185,7 +1192,7 @@ void HandleModuleConfiguration(void) WSContentSend_P(PSTR("")); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } void ModuleSaveSettings(void) @@ -1327,7 +1334,7 @@ void HandleWifiConfiguration(void) } else { WSContentSpaceButton(BUTTON_CONFIGURATION); } - WSContentEnd(); + WSContentStop(); } void WifiSaveSettings(void) @@ -1386,7 +1393,7 @@ void HandleLoggingConfiguration(void) WSContentSend_P(HTTP_FORM_LOG2, Settings.syslog_host, Settings.syslog_port, Settings.tele_period); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } void LoggingSaveSettings(void) @@ -1462,7 +1469,7 @@ void HandleOtherConfiguration(void) WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } void OtherSaveSettings(void) @@ -1558,7 +1565,7 @@ void HandleResetConfiguration(void) WSContentSend_P(PSTR("
" D_CONFIGURATION_RESET "
")); WSContentSend_P(HTTP_MSG_RSTRT); WSContentSpaceButton(BUTTON_MAIN); - WSContentEnd(); + WSContentStop(); char command[CMDSZ]; snprintf_P(command, sizeof(command), PSTR(D_CMND_RESET " 1")); @@ -1576,7 +1583,7 @@ void HandleRestoreConfiguration(void) WSContentSend_P(HTTP_FORM_RST); WSContentSend_P(HTTP_FORM_RST_UPG, D_RESTORE); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); upload_error = 0; upload_file_type = UPL_SETTINGS; @@ -1678,7 +1685,7 @@ void HandleInformation(void) "
")); // WSContentSend_P(PSTR("")); WSContentSpaceButton(BUTTON_MAIN); - WSContentEnd(); + WSContentStop(); } #endif // Not FIRMWARE_MINIMAL @@ -1695,7 +1702,7 @@ void HandleUpgradeFirmware(void) WSContentSend_P(HTTP_FORM_UPG, Settings.ota_url); WSContentSend_P(HTTP_FORM_RST_UPG, D_UPGRADE); WSContentSpaceButton(BUTTON_MAIN); - WSContentEnd(); + WSContentStop(); upload_error = 0; upload_file_type = UPL_TASMOTA; @@ -1723,7 +1730,7 @@ void HandleUpgradeFirmwareStart(void) WSContentSend_P(PSTR("
" D_UPGRADE_STARTED " ...
")); WSContentSend_P(HTTP_MSG_RSTRT); WSContentSpaceButton(BUTTON_MAIN); - WSContentEnd(); + WSContentStop(); snprintf_P(command, sizeof(command), PSTR(D_CMND_UPGRADE " 1")); ExecuteWebCommand(command, SRC_WEBGUI); @@ -1770,7 +1777,7 @@ void HandleUploadDone(void) SettingsBufferFree(); WSContentSend_P(PSTR("

")); WSContentSpaceButton(BUTTON_MAIN); - WSContentEnd(); + WSContentStop(); } void HandleUploadLoop(void) @@ -1990,7 +1997,7 @@ void HandleHttpCommand(void) if (!(!strcmp(tmp1, WEB_USERNAME) && !strcmp(tmp2, Settings.web_password))) { valid = 0; } } - String message = F("{\"" D_RSLT_WARNING "\":\""); + WSContentBegin(200, CT_JSON); if (valid) { uint8_t curridx = web_log_index; String svalue = WebServer->arg("cmnd"); @@ -1998,7 +2005,8 @@ void HandleHttpCommand(void) ExecuteWebCommand((char*)svalue.c_str(), SRC_WEBCOMMAND); if (web_log_index != curridx) { uint8_t counter = curridx; - message = F("{"); + WSContentSend_P(PSTR("{")); + bool cflg = false; do { char* tmp; size_t len; @@ -2007,28 +2015,28 @@ void HandleHttpCommand(void) // [14:49:36 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}] char* JSON = (char*)memchr(tmp, '{', len); if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O]) - if (message.length() > 1) { message += F(","); } size_t JSONlen = len - (JSON - tmp); if (JSONlen > sizeof(mqtt_data)) { JSONlen = sizeof(mqtt_data); } - strlcpy(mqtt_data, JSON +1, JSONlen -2); - message += mqtt_data; + char stemp[JSONlen]; + strlcpy(stemp, JSON +1, JSONlen -2); + WSContentSend_P(PSTR("%s%s"), (cflg) ? "," : "", stemp); + cflg = true; } } counter++; if (!counter) counter++; // Skip 0 as it is not allowed } while (counter != web_log_index); - message += F("}"); + WSContentSend_P(PSTR("}")); } else { - message += F(D_ENABLE_WEBLOG_FOR_RESPONSE "\"}"); + WSContentSend_P(PSTR("{\"" D_RSLT_WARNING "\":\"" D_ENABLE_WEBLOG_FOR_RESPONSE "\"}")); } } else { - message += F(D_ENTER_COMMAND " cmnd=\"}"); + WSContentSend_P(PSTR("{\"" D_RSLT_WARNING "\":\"" D_ENTER_COMMAND " cmnd=\"}")); } } else { - message += F(D_NEED_USER_AND_PASSWORD "\"}"); + WSContentSend_P(PSTR("{\"" D_RSLT_WARNING "\":\"" D_NEED_USER_AND_PASSWORD "\"}")); } - WSHeaderSend(); - WSSend(200, CT_JSON, message); + WSContentEnd(); } /*-------------------------------------------------------------------------------------------*/ @@ -2049,7 +2057,7 @@ void HandleConsole(void) WSContentSendStyle(); WSContentSend_P(HTTP_FORM_CMND); WSContentSpaceButton(BUTTON_MAIN); - WSContentEnd(); + WSContentStop(); } void HandleConsoleRefresh(void) @@ -2067,10 +2075,8 @@ void HandleConsoleRefresh(void) WebGetArg("c2", stmp, sizeof(stmp)); if (strlen(stmp)) { counter = atoi(stmp); } - bool last_reset_web_log_flag = reset_web_log_flag; - // mqtt_data used as scratch space - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%d}1%d}1"), web_log_index, last_reset_web_log_flag); - String message = mqtt_data; + WSContentBegin(200, CT_PLAIN); + WSContentSend_P(PSTR("%d}1%d}1"), web_log_index, reset_web_log_flag); if (!reset_web_log_flag) { counter = 0; reset_web_log_flag = true; @@ -2085,21 +2091,18 @@ void HandleConsoleRefresh(void) size_t len; GetLog(counter, &tmp, &len); if (len) { - if (cflg) { - message += F("\n"); - } else { - cflg = true; - } if (len > sizeof(mqtt_data) -2) { len = sizeof(mqtt_data); } - strlcpy(mqtt_data, tmp, len); - message += mqtt_data; // mqtt_data used as scratch space + char stemp[len +1]; + strlcpy(stemp, tmp, len); + WSContentSend_P(PSTR("%s%s"), (cflg) ? "\n" : "", stemp); + cflg = true; } counter++; - if (!counter) { counter++; } // Skip log index 0 as it is not allowed + if (!counter) { counter++; } // Skip log index 0 as it is not allowed } while (counter != web_log_index); } - message += F("}1"); - WSSend(200, CT_PLAIN, message); + WSContentSend_P(PSTR("}1")); + WSContentEnd(); } /********************************************************************************************/ diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 6330259fd..57086d46e 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -774,7 +774,7 @@ void HandleMqttConfiguration(void) Settings.mqtt_fulltopic); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } void MqttSaveSettings(void) diff --git a/sonoff/xdrv_07_domoticz.ino b/sonoff/xdrv_07_domoticz.ino index c2020176d..799b2d4b8 100644 --- a/sonoff/xdrv_07_domoticz.ino +++ b/sonoff/xdrv_07_domoticz.ino @@ -482,7 +482,7 @@ void HandleDomoticzConfiguration(void) WSContentSend_P(PSTR("")); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } void DomoticzSaveSettings(void) diff --git a/sonoff/xdrv_09_timers.ino b/sonoff/xdrv_09_timers.ino index 9a85ad3c4..a3ab51a1b 100644 --- a/sonoff/xdrv_09_timers.ino +++ b/sonoff/xdrv_09_timers.ino @@ -721,7 +721,7 @@ void HandleTimerConfiguration(void) WSContentSend_P(HTTP_FORM_TIMER4); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } void TimerSaveSettings(void) diff --git a/sonoff/xdrv_11_knx.ino b/sonoff/xdrv_11_knx.ino index 5ab0c7c88..45c9fe15f 100644 --- a/sonoff/xdrv_11_knx.ino +++ b/sonoff/xdrv_11_knx.ino @@ -934,7 +934,7 @@ void HandleKNXConfiguration(void) WSContentSend_P(PSTR("")); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } } diff --git a/sonoff/xsns_34_hx711.ino b/sonoff/xsns_34_hx711.ino index 2072dc496..c287ef3f4 100644 --- a/sonoff/xsns_34_hx711.ino +++ b/sonoff/xsns_34_hx711.ino @@ -434,7 +434,7 @@ void HandleHxAction(void) WSContentSend_P(HTTP_FORM_HX711, stemp1, stemp2); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); - WSContentEnd(); + WSContentStop(); } void HxSaveSettings(void)