EspThermometer2/configuration.cpp

273 lines
6.9 KiB
C++
Raw Normal View History

2017-08-20 22:29:01 +02:00
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
2017-08-21 10:47:48 +02:00
#include <EEPROM.h>
2017-08-20 22:29:01 +02:00
2017-08-21 10:47:48 +02:00
#include "defines.h"
2017-08-20 22:29:01 +02:00
#include "configuration.h"
tConfigBlock configBlock;
const uint32_t MAGIC = 0xC0DE0001;
extern ESP8266WebServer webServer;
2017-08-22 14:15:56 +02:00
bool configSaved = false;
2017-08-20 22:29:01 +02:00
void configServeIndex() {
2017-08-21 10:47:48 +02:00
bool configValid = (configBlock.magic == MAGIC);
if (! configValid) {
strcpy(configBlock.wifiSsid, "EG-WLAN");
strcpy(configBlock.wifiKey, "shae3sheuthai2oluNgiqueiyahyumeiphughi8jequeil6taethooyeik1joh5");
strcpy(configBlock.mqttBroker, "broker.hottis.de");
strcpy(configBlock.mqttUser, "esp1");
strcpy(configBlock.mqttPass, "geheim123");
strcpy(configBlock.mqttClientId, "espClient1");
strcpy(configBlock.mqttTopic, "IoT/espThermometer2/espTherm1/measurement");
configBlock.mqttPort = 8883;
configBlock.measurePeriod = 300;
}
String buffer =
2017-08-20 22:29:01 +02:00
"<!doctype html"
"<html lang=\"en\">"
" <head>"
" <title>ESP8266 Thermometer Configuration Page</title>"
" </head>"
" <body>"
2017-08-22 14:15:56 +02:00
" <h1>ESP8266 Configuration Page</h1>";
if (configSaved) {
configSaved = false;
buffer += "<h2>Configuration saved</h2>";
}
buffer +=
2017-08-20 22:29:01 +02:00
" <form action=\"/config\" method=\"GET\">"
" <table>"
" <tr>"
" <td>"
" <label for\"wifiSsid\">Wifi SSID</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"wifiSsid\" id=\"wifiSsid\" ";
buffer += " size=\"32\" ";
buffer += " value=\"";
buffer += configBlock.wifiSsid;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"wifiKey\">Wifi Key</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"wifiKey\" id=\"wifiKey\" ";
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.wifiKey;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttBroker\">MQTT Broker</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"mqttBroker\" id=\"mqttBroker\" ";
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.mqttBroker;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttUser\">MQTT Username</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"mqttUser\" id=\"mqttUser\" ";
buffer += " size=\"32\" ";
buffer += " value=\"";
buffer += configBlock.mqttUser;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttPass\">MQTT Password</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"mqttPass\" id=\"mqttPass\" ";
buffer += " size=\"32\" ";
buffer += " value=\"";
buffer += configBlock.mqttPass;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttClientId\">MQTT ClientId</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"mqttClientId\" id=\"mqttClientId\" ";
buffer += " size=\"32\" ";
buffer += " value=\"";
buffer += configBlock.mqttClientId;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttTopic\">MQTT Topic</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"mqttTopic\" id=\"mqttTopic\" ";
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.mqttTopic;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttPort\">MQTT Port</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"mqttPort\" id=\"mqttPort\" ";
buffer += " value=\"";
buffer += configBlock.mqttPort;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"measurePeriod\">Measure Period</label>"
" </td><td>"
2017-08-21 10:47:48 +02:00
" <input type=\"text\" name=\"measurePeriod\" id=\"measurePeriod\" ";
buffer += " value=\"";
buffer += configBlock.measurePeriod;
buffer += "\"";
buffer +=
2017-08-20 23:29:14 +02:00
" />"
2017-08-20 22:29:01 +02:00
" </td>"
" </tr>"
" <tr>"
" <td colspan=\"2\">"
" <button type=\"submit\">Save</button>"
" </td>"
" </tr>"
" </table>"
" </form>"
" </body>"
2017-08-21 10:47:48 +02:00
"</html>";
webServer.send(200, "text/html", buffer);
2017-08-20 23:29:14 +02:00
2017-08-20 22:29:01 +02:00
#ifdef DEBUG
Serial.println("indexHtml request served");
#endif
}
void configServeGetConfiguration() {
String arg;
arg = webServer.arg("wifiSsid");
2017-08-21 10:47:48 +02:00
strcpy(configBlock.wifiSsid, arg.c_str());
2017-08-20 22:29:01 +02:00
arg = webServer.arg("wifiKey");
2017-08-21 10:47:48 +02:00
strcpy(configBlock.wifiKey, arg.c_str());
2017-08-20 22:29:01 +02:00
arg = webServer.arg("mqttBroker");
2017-08-21 10:47:48 +02:00
strcpy(configBlock.mqttBroker, arg.c_str());
2017-08-20 22:29:01 +02:00
arg = webServer.arg("mqttUser");
2017-08-21 10:47:48 +02:00
strcpy(configBlock.mqttUser, arg.c_str());
2017-08-20 22:29:01 +02:00
arg = webServer.arg("mqttPass");
2017-08-21 10:47:48 +02:00
strcpy(configBlock.mqttPass, arg.c_str());
2017-08-20 22:29:01 +02:00
arg = webServer.arg("mqttClientId");
2017-08-21 10:47:48 +02:00
strcpy(configBlock.mqttClientId, arg.c_str());
2017-08-20 22:29:01 +02:00
arg = webServer.arg("mqttTopic");
2017-08-21 10:47:48 +02:00
strcpy(configBlock.mqttTopic, arg.c_str());
2017-08-20 22:29:01 +02:00
arg = webServer.arg("mqttPort");
2017-08-21 10:47:48 +02:00
configBlock.mqttPort = atoi(arg.c_str());
2017-08-20 22:29:01 +02:00
arg = webServer.arg("measurePeriod");
2017-08-21 10:47:48 +02:00
configBlock.measurePeriod = atoi(arg.c_str());
configBlock.magic = MAGIC;
showConfiguration();
EEPROM.begin(512);
2017-08-22 14:15:56 +02:00
EEPROM.put(EEPROM_ADDR, configBlock);
2017-08-21 10:47:48 +02:00
EEPROM.commit();
Serial.println("EEPROM saved");
2017-08-22 14:15:56 +02:00
configSaved = true;
webServer.sendHeader("Location", String("/"), true);
webServer.send(302, "text/plain", "");
//webServer.send(200, "text/html", "configuration saved");
2017-08-20 22:29:01 +02:00
}
2017-08-20 23:29:14 +02:00
void showConfiguration() {
Serial.println("Configuration is");
2017-08-21 10:47:48 +02:00
Serial.print("magic = ");
Serial.println(configBlock.magic);
2017-08-20 23:29:14 +02:00
Serial.print("wifiSsid = ");
Serial.println(configBlock.wifiSsid);
Serial.print("wifiKey = ");
Serial.println(configBlock.wifiKey);
Serial.print("mqttBroker = ");
Serial.println(configBlock.mqttBroker);
Serial.print("mqttUser = ");
Serial.println(configBlock.mqttUser);
Serial.print("mqttPass = ");
Serial.println(configBlock.mqttPass);
Serial.print("mqttClientId = ");
Serial.println(configBlock.mqttClientId);
Serial.print("mqttTopic = ");
Serial.println(configBlock.mqttTopic);
Serial.print("mqttPort = ");
Serial.println(configBlock.mqttPort);
Serial.print("measurePeriod = ");
Serial.println(configBlock.measurePeriod);
Serial.println("---");
}