+
" + "
Redirecting"; + +String postSave_html; + +void (*userConfigLoop)() = NULL; + +void byte2buff(char* msg, byte* payload, unsigned int len) { + unsigned int i, j; + for (i=j=0; i < len ;) { + msg[j++] = payload[i++]; + } + msg[j] = '\0'; +} + +void save_config_json(){ + File f = LittleFS.open(cfgFile, "w"); + serializeJson(cfg, f); + f.close(); +} + +void reset_config() { + deserializeJson(cfg, "{meta:{}}"); + save_config_json(); +} + +void maskConfig(char* buff) { + DynamicJsonDocument temp_cfg = cfg; + if(cfg.containsKey("w_pw")) temp_cfg["w_pw"] = "********"; + if(cfg.containsKey("token")) temp_cfg["token"] = "********"; + serializeJson(temp_cfg, buff, JSON_CHAR_LENGTH); +} + +IRAM_ATTR void reboot() { + WiFi.disconnect(); + ESP.restart(); +} + +void loadConfig() { + // check Factory Reset Request and reset if requested or load the config + if(!LittleFS.begin()) { LittleFS.format(); } // before the reset_config and reading + + pinMode(RESET_PIN, INPUT_PULLUP); + if( digitalRead(RESET_PIN) == 0 ) { + unsigned long t1 = millis(); + while(digitalRead(RESET_PIN) == 0) { + delay(500); + Serial.print("."); + } + if (millis() - t1 > 5000) { + reset_config(); // Factory Reset + } + } + attachInterrupt(RESET_PIN, reboot, FALLING); + + if (LittleFS.exists(cfgFile)) { + String buff; + File f = LittleFS.open(cfgFile, "r"); + DeserializationError error = deserializeJson(cfg, f.readString()); + f.close(); + + if (error) { + deserializeJson(cfg, "{meta:{}}"); + } else { + Serial.println("CONFIG JSON Successfully loaded"); + char maskBuffer[JSON_CHAR_LENGTH]; + maskConfig(maskBuffer); + Serial.println(String(maskBuffer)); + } + } else { + deserializeJson(cfg, "{meta:{}}"); + } +} + +void saveEnv() { + int args = webServer.args(); + for (int i = 0; i < args ; i++){ + if (webServer.argName(i).indexOf(String("meta.")) == 0 ) { + String temp = webServer.arg(i); + temp.trim(); + cfg["meta"][webServer.argName(i).substring(5)] = temp; + } else { + String temp = webServer.arg(i); + temp.trim(); + cfg[webServer.argName(i)] = temp; + } + } + cfg["config"] = "done"; + save_config_json(); + // redirect uri augmentation here + // + webServer.send(200, "text/html", redirect_html); +} + +void pre_reboot() { + int args = webServer.args(); + for (int i = 0; i < args ; i++){ + Serial.printf("%s -> %s\n", webServer.argName(i).c_str(), webServer.arg(i).c_str()); + } + webServer.send(200, "text/html", postSave_html); +} + +bool getHTML(String* html, char* fname) { + if (LittleFS.exists(fname)) { + String buff; + File f = LittleFS.open(fname, "r"); + buff = f.readString(); + buff.trim(); + f.close(); + *html = buff; + return true; + } else { + return false; + } +} + +void configDevice() { + DNSServer dnsServer; + const byte DNS_PORT = 53; + IPAddress apIP(192, 168, 1, 1); + WiFi.mode(WIFI_AP); + WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); + char ap_name[100]; + sprintf(ap_name, "%s_%08X", ssid_pfix, (unsigned int)ESP.getEfuseMac()); + WiFi.softAP(ap_name); + dnsServer.start(DNS_PORT, "*", apIP); + + if (getHTML(&postSave_html, (char*)"/postSave.html")) { + // argument redirection + } else { + postSave_html = postSave_html_default; + } + + webServer.on("/save", saveEnv); + webServer.on("/reboot", reboot); + webServer.on("/pre_boot", pre_reboot); + + webServer.onNotFound([]() { + webServer.send(200, "text/html", html_begin + user_config_html + html_end); + }); + webServer.begin(); + Serial.println("starting the config"); + while(1) { + yield(); + dnsServer.processNextRequest(); + webServer.handleClient(); + if(userConfigLoop != NULL) { + (*userConfigLoop)(); + } + } +} \ No newline at end of file