diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.cpp index da9802e..a9ad3a3 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.cpp @@ -5,6 +5,7 @@ ESPMegaWebServer::ESPMegaWebServer(uint16_t port, ESPMegaIoT *iot) this->port = port; this->iot = iot; this->server = new AsyncWebServer(port); + this->saveConfigHandler = new AsyncCallbackJsonWebHandler("/save_config", std::bind(&ESPMegaWebServer::saveConfigJSONHandler, this, std::placeholders::_1, std::placeholders::_2)); } ESPMegaWebServer::~ESPMegaWebServer() @@ -20,8 +21,7 @@ void ESPMegaWebServer::begin() this->server->on("/", HTTP_GET, bindedDashboardHandler); auto bindedConfigHandler = std::bind(&ESPMegaWebServer::configHandler, this, std::placeholders::_1); this->server->on("/config", HTTP_GET, bindedConfigHandler); - auto bindedSaveConfigHandler = std::bind(&ESPMegaWebServer::saveConfigHandler, this, std::placeholders::_1); - this->server->on("/config", HTTP_POST, bindedSaveConfigHandler); + this->server->addHandler(saveConfigHandler); auto bindedOtaRequestHandler = std::bind(&ESPMegaWebServer::otaRequestHandler, this, std::placeholders::_1); auto bindedOtaUploadHandler = std::bind(&ESPMegaWebServer::otaUploadHandler, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6); this->server->on("/ota_update", HTTP_POST, bindedOtaRequestHandler, bindedOtaUploadHandler); @@ -277,7 +277,7 @@ void ESPMegaWebServer::otaUploadHandler(AsyncWebServerRequest *request, String f } } -void ESPMegaWebServer::saveConfigHandler(AsyncWebServerRequest *request) +void ESPMegaWebServer::saveConfigJSONHandler(AsyncWebServerRequest *request, JsonVariant &json) { /** * Request POST body should be a JSON object @@ -296,65 +296,84 @@ void ESPMegaWebServer::saveConfigHandler(AsyncWebServerRequest *request) * web_username: String, the username of the web server * web_password: String, the password of the web server */ - StaticJsonDocument<1024> doc; - DeserializationError error = deserializeJson(doc, request->getParam("plain")->value()); - if (error) - { - request->send(400, "text/plain", "Invalid JSON"); - return; - } - JsonObject root = doc.as(); + ESP_LOGD("ESPMegaWebServer", "Saving config"); + JsonObject root = json.as(); // Network Config NetworkConfig networkConfig; + networkConfig.useStaticIp = true; + networkConfig.useWifi = false; IPAddress ip; + ESP_LOGD("ESPMegaWebServer", "Checking IP Address"); if (!ip.fromString(root["ip_address"].as())) { + ESP_LOGE("ESPMegaWebServer", "Invalid Config IP Address"); request->send(400, "text/plain", "Invalid IP Address"); return; } networkConfig.ip = ip; + ESP_LOGD("ESPMegaWebServer", "Checking Netmask"); if (!ip.fromString(root["netmask"].as())) { + ESP_LOGE("ESPMegaWebServer", "Invalid Config Netmask"); request->send(400, "text/plain", "Invalid Netmask"); return; } networkConfig.subnet = ip; + ESP_LOGD("ESPMegaWebServer", "Checking Gateway"); if (!ip.fromString(root["gateway"].as())) { + ESP_LOGE("ESPMegaWebServer", "Invalid Config Gateway"); request->send(400, "text/plain", "Invalid Gateway"); return; } networkConfig.gateway = ip; + ESP_LOGD("ESPMegaWebServer", "Checking DNS"); if (!ip.fromString(root["dns"].as())) { + ESP_LOGE("ESPMegaWebServer", "Invalid Config DNS"); request->send(400, "text/plain", "Invalid DNS"); return; } networkConfig.dns1 = ip; + ESP_LOGD("ESPMegaWebServer", "Setting Hostname"); strcpy(networkConfig.hostname, root["hostname"].as().c_str()); // MQTT Config MqttConfig mqttConfig; + ESP_LOGD("ESPMegaWebServer", "Setting MQTT Server"); strcpy(mqttConfig.mqtt_server, root["bms_ip"].as().c_str()); + ESP_LOGD("ESPMegaWebServer", "Checking MQTT Port"); uint16_t mqttPort = root["bms_port"].as(); if (mqttConfig.mqtt_port <= 0 || mqttConfig.mqtt_port > 65535) { + ESP_LOGE("ESPMegaWebServer", "Invalid Config MQTT Port"); request->send(400, "text/plain", "Invalid MQTT Port"); return; } mqttConfig.mqtt_port = mqttPort; + ESP_LOGD("ESPMegaWebServer", "Checking MQTT Use Auth"); mqttConfig.mqtt_useauth = root["bms_useauth"].as(); + ESP_LOGD("ESPMegaWebServer", "Setting MQTT Username"); strcpy(mqttConfig.mqtt_user, root["bms_username"].as().c_str()); + ESP_LOGD("ESPMegaWebServer", "Setting MQTT Password"); strcpy(mqttConfig.mqtt_password, root["bms_password"].as().c_str()); + ESP_LOGD("ESPMegaWebServer", "Setting MQTT Base Topic"); strcpy(mqttConfig.base_topic, root["bms_endpoint"].as().c_str()); // Web Server Config + ESP_LOGD("ESPMegaWebServer", "Setting Web Username"); strcpy(this->webUsername, root["web_username"].as().c_str()); + ESP_LOGD("ESPMegaWebServer", "Setting Web Password"); strcpy(this->webPassword, root["web_password"].as().c_str()); // Commit changes to FRAM + ESP_LOGD("ESPMegaWebServer", "Committing Network Config to FRAM"); this->iot->setNetworkConfig(networkConfig); this->iot->saveNetworkConfig(); + ESP_LOGD("ESPMegaWebServer", "Committing MQTT Config to FRAM"); this->iot->setMqttConfig(mqttConfig); this->iot->saveMqttConfig(); + ESP_LOGD("ESPMegaWebServer", "Committing Web Server Config to FRAM"); this->saveCredentialsToFRAM(); + ESP_LOGD("ESPMegaWebServer", "Config saved"); // Send response request->send(200, "text/plain", "OK"); + ESP.restart(); } \ No newline at end of file diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.hpp index 7ecaa79..ed5de4d 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaWebServer.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -44,7 +45,8 @@ class ESPMegaWebServer String dashboardProcessor(const String& var); void configHandler(AsyncWebServerRequest *request); String configProcessor(const String& var); - void saveConfigHandler(AsyncWebServerRequest *request); + AsyncCallbackJsonWebHandler *saveConfigHandler; + void saveConfigJSONHandler(AsyncWebServerRequest *request, JsonVariant &json); void otaRequestHandler(AsyncWebServerRequest *request); void otaUploadHandler(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final); void restAPIHandler(AsyncWebServerRequest *request); diff --git a/ESPMegaPRO-firmware/platformio.ini b/ESPMegaPRO-firmware/platformio.ini index c726d5b..4e216a6 100644 --- a/ESPMegaPRO-firmware/platformio.ini +++ b/ESPMegaPRO-firmware/platformio.ini @@ -31,5 +31,5 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 https://github.com/me-no-dev/ESPAsyncWebServer.git bblanchon/ArduinoJson@^6.21.4 monitor_speed = 115200 -build_flags = -DCORE_DEBUG_LEVEL=1 +build_flags = -DCORE_DEBUG_LEVEL=5 extra_scripts = pre:helper_scripts/html2cpp.py \ No newline at end of file diff --git a/ESPMegaPRO-firmware/src/main.cpp b/ESPMegaPRO-firmware/src/main.cpp index 8bb9f87..fdf4c3a 100644 --- a/ESPMegaPRO-firmware/src/main.cpp +++ b/ESPMegaPRO-firmware/src/main.cpp @@ -8,7 +8,7 @@ // #define WRITE_DEFAULT_NETCONF #define CLIMATE_CARD_ENABLE #define MQTT_CARD_REGISTER -#define DISPLAY_ENABLE +//#define DISPLAY_ENABLE #define WEB_SERVER_ENABLE // Demo PLC firmware using the ESPMegaPRO OOP library