153 lines
3.4 KiB
Cheetah
153 lines
3.4 KiB
Cheetah
|
#raw
|
||
|
#include <Arduino.h>
|
||
|
|
||
|
#include <ESP8266WiFi.h>
|
||
|
#include <ESP8266WebServer.h>
|
||
|
#include <EEPROM.h>
|
||
|
|
||
|
#include "defines.h"
|
||
|
#include "configuration.h"
|
||
|
#end raw
|
||
|
|
||
|
|
||
|
tConfigBlock configBlock;
|
||
|
const uint32_t MAGIC = $magic;
|
||
|
const char* CONFIG_SSID = "$confWifiSsid";
|
||
|
extern ESP8266WebServer webServer;
|
||
|
|
||
|
bool configSaved = false;
|
||
|
|
||
|
|
||
|
static bool checkAuthentication() {
|
||
|
Serial.print("User: "); Serial.println(configBlock.confUser);
|
||
|
Serial.print("Pass: "); Serial.println(configBlock.confPasswd);
|
||
|
return webServer.authenticate(configBlock.confUser, configBlock.confPasswd);
|
||
|
}
|
||
|
|
||
|
void configServeIndex() {
|
||
|
bool configValid = (configBlock.magic == MAGIC);
|
||
|
|
||
|
if (! configValid) {
|
||
|
configBlock.magic = MAGIC;
|
||
|
#for $configItem in $configItems
|
||
|
#if $configItem.label != "_"
|
||
|
#if $configItem.type == "C"
|
||
|
strcpy(configBlock.$configItem.key, "$configItem.default");
|
||
|
#else if $configItem.type == "I"
|
||
|
configBlock.$configItem.key = $configItem.default;
|
||
|
#end if
|
||
|
#end if
|
||
|
#end for
|
||
|
}
|
||
|
|
||
|
if (! checkAuthentication()) {
|
||
|
return webServer.requestAuthentication();
|
||
|
}
|
||
|
|
||
|
|
||
|
String buffer =
|
||
|
"<!doctype html"
|
||
|
"<html lang=\"en\">"
|
||
|
" <head>"
|
||
|
" <title>$appName</title>"
|
||
|
" </head>"
|
||
|
" <body>"
|
||
|
" <h1>$appName - ESP8266 Configuration Page</h1>";
|
||
|
|
||
|
if (configSaved) {
|
||
|
configSaved = false;
|
||
|
buffer += "<h2>Configuration saved</h2>";
|
||
|
}
|
||
|
|
||
|
buffer +=
|
||
|
" <form action=\"/config\" method=\"GET\">"
|
||
|
" <table>"
|
||
|
#for $configItem in $configItems
|
||
|
#if $configItem.label != "_"
|
||
|
" <tr>"
|
||
|
" <td>"
|
||
|
" <label for\"$configItem.key\">$configItem.label</label>"
|
||
|
" </td><td>"
|
||
|
" <input type=\"text\" name=\"$configItem.key\" id=\"$configItem.key\" ";
|
||
|
|
||
|
#if $configItem.type == "C"
|
||
|
buffer += " size=\"$configItem.length\" ";
|
||
|
buffer += " value=\"";
|
||
|
buffer += configBlock.$configItem.key;
|
||
|
buffer += "\"";
|
||
|
#else if $configItem.type == "I"
|
||
|
buffer += " value=\"";
|
||
|
buffer += configBlock.$configItem.key;
|
||
|
buffer += "\"";
|
||
|
#end if
|
||
|
|
||
|
buffer +=
|
||
|
" />"
|
||
|
" </td>"
|
||
|
" </tr>"
|
||
|
#end if
|
||
|
#end for
|
||
|
" <tr>"
|
||
|
" <td colspan=\"2\">"
|
||
|
" <button type=\"submit\">Save</button>"
|
||
|
" </td>"
|
||
|
" </tr>"
|
||
|
" </table>"
|
||
|
" </form>"
|
||
|
" </body>"
|
||
|
"</html>";
|
||
|
|
||
|
webServer.send(200, "text/html", buffer);
|
||
|
|
||
|
|
||
|
#ifdef DEBUG
|
||
|
Serial.println("indexHtml request served");
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void configServeGetConfiguration() {
|
||
|
if (! checkAuthentication()) {
|
||
|
return webServer.requestAuthentication();
|
||
|
}
|
||
|
|
||
|
String arg;
|
||
|
|
||
|
#for $configItem in $configItems
|
||
|
#if $configItem.label != "_"
|
||
|
arg = webServer.arg("$configItem.key");
|
||
|
#if $configItem.type == "C"
|
||
|
strcpy(configBlock.$configItem.key, arg.c_str());
|
||
|
#else if $configItem.type == "I"
|
||
|
configBlock.$configItem.key = atoi(arg.c_str());
|
||
|
#end if
|
||
|
#end if
|
||
|
#end for
|
||
|
|
||
|
configBlock.magic = MAGIC;
|
||
|
|
||
|
showConfiguration();
|
||
|
|
||
|
EEPROM.begin(512);
|
||
|
EEPROM.put(EEPROM_ADDR, configBlock);
|
||
|
EEPROM.commit();
|
||
|
|
||
|
Serial.println("EEPROM saved");
|
||
|
|
||
|
configSaved = true;
|
||
|
webServer.sendHeader("Location", String("/"), true);
|
||
|
webServer.send(302, "text/plain", "");
|
||
|
//webServer.send(200, "text/html", "configuration saved");
|
||
|
}
|
||
|
|
||
|
void showConfiguration() {
|
||
|
Serial.println("Configuration is");
|
||
|
|
||
|
#for $configItem in $configItems
|
||
|
Serial.print("$configItem.key = <");
|
||
|
Serial.print(configBlock.$configItem.key);
|
||
|
Serial.println(">");
|
||
|
|
||
|
#end for
|
||
|
|
||
|
Serial.println("---");
|
||
|
}
|