452 lines
12 KiB
C++
452 lines
12 KiB
C++
#include <Arduino.h>
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <EEPROM.h>
|
|
|
|
#include "defines.h"
|
|
#include "configuration.h"
|
|
|
|
|
|
tConfigBlock configBlock;
|
|
const uint32_t MAGIC = 3235774468;
|
|
const char* CONFIG_SSID = "espconfig";
|
|
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;
|
|
strcpy(configBlock.confUser, "admin");
|
|
strcpy(configBlock.confPasswd, "geheim123");
|
|
strcpy(configBlock.wifiSsid, "test");
|
|
strcpy(configBlock.wifiKey, "geheim");
|
|
strcpy(configBlock.mqttBroker, "broker.hottis.de");
|
|
strcpy(configBlock.mqttUser, "MySwitch1");
|
|
strcpy(configBlock.mqttPass, "geheim123");
|
|
strcpy(configBlock.mqttClientId, "MySwitch1");
|
|
configBlock.mqttPort = 8883;
|
|
strcpy(configBlock.mqttSwitchTopic, "IoT/MySwitch1/Value");
|
|
strcpy(configBlock.mqttLedTopic, "IoT/MySwitch1/Led");
|
|
strcpy(configBlock.mqttDebugTopic, "IoT/MySwitch1/Debug");
|
|
configBlock.debugMode = 0;
|
|
configBlock.debounce = 300;
|
|
configBlock.longPress = 1500;
|
|
configBlock.longPressRepeat = 1000;
|
|
}
|
|
|
|
if (! checkAuthentication()) {
|
|
return webServer.requestAuthentication();
|
|
}
|
|
|
|
|
|
String buffer =
|
|
"<!doctype html"
|
|
"<html lang=\"en\">"
|
|
" <head>"
|
|
" <title>ESP8266 based MySwitch</title>"
|
|
" </head>"
|
|
" <body>"
|
|
" <h1>ESP8266 based MySwitch - ESP8266 Configuration Page</h1>";
|
|
|
|
if (configSaved) {
|
|
configSaved = false;
|
|
buffer += "<h2>Configuration saved</h2>";
|
|
}
|
|
|
|
buffer +=
|
|
" <form action=\"/config\" method=\"GET\">"
|
|
" <table>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"confUser\">Config Username</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"confUser\" id=\"confUser\" ";
|
|
|
|
buffer += " size=\"16\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.confUser;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"confPasswd\">Config Password</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"confPasswd\" id=\"confPasswd\" ";
|
|
|
|
buffer += " size=\"16\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.confPasswd;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"wifiSsid\">Wifi SSID</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"wifiSsid\" id=\"wifiSsid\" ";
|
|
|
|
buffer += " size=\"32\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.wifiSsid;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"wifiKey\">Wifi Key</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"wifiKey\" id=\"wifiKey\" ";
|
|
|
|
buffer += " size=\"64\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.wifiKey;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"mqttBroker\">MQTT Broker</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"mqttBroker\" id=\"mqttBroker\" ";
|
|
|
|
buffer += " size=\"32\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.mqttBroker;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"mqttUser\">MQTT Username</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"mqttUser\" id=\"mqttUser\" ";
|
|
|
|
buffer += " size=\"32\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.mqttUser;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"mqttPass\">MQTT Password</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"mqttPass\" id=\"mqttPass\" ";
|
|
|
|
buffer += " size=\"32\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.mqttPass;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"mqttClientId\">MQTT ClientId</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"mqttClientId\" id=\"mqttClientId\" ";
|
|
|
|
buffer += " size=\"32\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.mqttClientId;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"mqttPort\">MQTT Port</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"mqttPort\" id=\"mqttPort\" ";
|
|
|
|
buffer += " value=\"";
|
|
buffer += configBlock.mqttPort;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"mqttSwitchTopic\">MQTT Switch Topic</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"mqttSwitchTopic\" id=\"mqttSwitchTopic\" ";
|
|
|
|
buffer += " size=\"64\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.mqttSwitchTopic;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"mqttLedTopic\">MQTT LED Topic</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"mqttLedTopic\" id=\"mqttLedTopic\" ";
|
|
|
|
buffer += " size=\"64\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.mqttLedTopic;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"mqttDebugTopic\">MQTT DebugTopic</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"mqttDebugTopic\" id=\"mqttDebugTopic\" ";
|
|
|
|
buffer += " size=\"64\" ";
|
|
buffer += " value=\"";
|
|
buffer += configBlock.mqttDebugTopic;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"debugMode\">DebugMode</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"debugMode\" id=\"debugMode\" ";
|
|
|
|
buffer += " value=\"";
|
|
buffer += configBlock.debugMode;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"debounce\">Debounce (us)</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"debounce\" id=\"debounce\" ";
|
|
|
|
buffer += " value=\"";
|
|
buffer += configBlock.debounce;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"longPress\">Long press (ms)</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"longPress\" id=\"longPress\" ";
|
|
|
|
buffer += " value=\"";
|
|
buffer += configBlock.longPress;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <tr>"
|
|
" <td>"
|
|
" <label for\"longPressRepeat\">Long press repeat (ms)</label>"
|
|
" </td><td>"
|
|
" <input type=\"text\" name=\"longPressRepeat\" id=\"longPressRepeat\" ";
|
|
|
|
buffer += " value=\"";
|
|
buffer += configBlock.longPressRepeat;
|
|
buffer += "\"";
|
|
|
|
buffer +=
|
|
" />"
|
|
" </td>"
|
|
" </tr>"
|
|
" <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;
|
|
|
|
arg = webServer.arg("confUser");
|
|
strcpy(configBlock.confUser, arg.c_str());
|
|
arg = webServer.arg("confPasswd");
|
|
strcpy(configBlock.confPasswd, arg.c_str());
|
|
arg = webServer.arg("wifiSsid");
|
|
strcpy(configBlock.wifiSsid, arg.c_str());
|
|
arg = webServer.arg("wifiKey");
|
|
strcpy(configBlock.wifiKey, arg.c_str());
|
|
arg = webServer.arg("mqttBroker");
|
|
strcpy(configBlock.mqttBroker, arg.c_str());
|
|
arg = webServer.arg("mqttUser");
|
|
strcpy(configBlock.mqttUser, arg.c_str());
|
|
arg = webServer.arg("mqttPass");
|
|
strcpy(configBlock.mqttPass, arg.c_str());
|
|
arg = webServer.arg("mqttClientId");
|
|
strcpy(configBlock.mqttClientId, arg.c_str());
|
|
arg = webServer.arg("mqttPort");
|
|
configBlock.mqttPort = atoi(arg.c_str());
|
|
arg = webServer.arg("mqttSwitchTopic");
|
|
strcpy(configBlock.mqttSwitchTopic, arg.c_str());
|
|
arg = webServer.arg("mqttLedTopic");
|
|
strcpy(configBlock.mqttLedTopic, arg.c_str());
|
|
arg = webServer.arg("mqttDebugTopic");
|
|
strcpy(configBlock.mqttDebugTopic, arg.c_str());
|
|
arg = webServer.arg("debugMode");
|
|
configBlock.debugMode = atoi(arg.c_str());
|
|
arg = webServer.arg("debounce");
|
|
configBlock.debounce = atoi(arg.c_str());
|
|
arg = webServer.arg("longPress");
|
|
configBlock.longPress = atoi(arg.c_str());
|
|
arg = webServer.arg("longPressRepeat");
|
|
configBlock.longPressRepeat = atoi(arg.c_str());
|
|
|
|
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");
|
|
|
|
Serial.print("magic = <");
|
|
Serial.print(configBlock.magic);
|
|
Serial.println(">");
|
|
|
|
Serial.print("confUser = <");
|
|
Serial.print(configBlock.confUser);
|
|
Serial.println(">");
|
|
|
|
Serial.print("confPasswd = <");
|
|
Serial.print(configBlock.confPasswd);
|
|
Serial.println(">");
|
|
|
|
Serial.print("wifiSsid = <");
|
|
Serial.print(configBlock.wifiSsid);
|
|
Serial.println(">");
|
|
|
|
Serial.print("wifiKey = <");
|
|
Serial.print(configBlock.wifiKey);
|
|
Serial.println(">");
|
|
|
|
Serial.print("mqttBroker = <");
|
|
Serial.print(configBlock.mqttBroker);
|
|
Serial.println(">");
|
|
|
|
Serial.print("mqttUser = <");
|
|
Serial.print(configBlock.mqttUser);
|
|
Serial.println(">");
|
|
|
|
Serial.print("mqttPass = <");
|
|
Serial.print(configBlock.mqttPass);
|
|
Serial.println(">");
|
|
|
|
Serial.print("mqttClientId = <");
|
|
Serial.print(configBlock.mqttClientId);
|
|
Serial.println(">");
|
|
|
|
Serial.print("mqttPort = <");
|
|
Serial.print(configBlock.mqttPort);
|
|
Serial.println(">");
|
|
|
|
Serial.print("mqttSwitchTopic = <");
|
|
Serial.print(configBlock.mqttSwitchTopic);
|
|
Serial.println(">");
|
|
|
|
Serial.print("mqttLedTopic = <");
|
|
Serial.print(configBlock.mqttLedTopic);
|
|
Serial.println(">");
|
|
|
|
Serial.print("mqttDebugTopic = <");
|
|
Serial.print(configBlock.mqttDebugTopic);
|
|
Serial.println(">");
|
|
|
|
Serial.print("debugMode = <");
|
|
Serial.print(configBlock.debugMode);
|
|
Serial.println(">");
|
|
|
|
Serial.print("debounce = <");
|
|
Serial.print(configBlock.debounce);
|
|
Serial.println(">");
|
|
|
|
Serial.print("longPress = <");
|
|
Serial.print(configBlock.longPress);
|
|
Serial.println(">");
|
|
|
|
Serial.print("longPressRepeat = <");
|
|
Serial.print(configBlock.longPressRepeat);
|
|
Serial.println(">");
|
|
|
|
|
|
Serial.println("---");
|
|
} |