2017-08-20 22:29:01 +02:00
|
|
|
#raw
|
|
|
|
#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"
|
|
|
|
#end raw
|
|
|
|
|
|
|
|
|
|
|
|
tConfigBlock configBlock;
|
|
|
|
const uint32_t MAGIC = 0xC0DE0001;
|
|
|
|
extern ESP8266WebServer webServer;
|
|
|
|
|
|
|
|
void configServeIndex() {
|
2017-08-21 10:47:48 +02:00
|
|
|
bool configValid = (configBlock.magic == MAGIC);
|
|
|
|
|
|
|
|
if (! configValid) {
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
String buffer =
|
2017-08-20 22:29:01 +02:00
|
|
|
"<!doctype html"
|
|
|
|
"<html lang=\"en\">"
|
|
|
|
" <head>"
|
|
|
|
" <title>ESP8266 Thermometer Configuration Page</title>"
|
|
|
|
" </head>"
|
|
|
|
" <body>"
|
|
|
|
" <h1>ESP8266 Configuration Page</h1>"
|
|
|
|
" <form action=\"/config\" method=\"GET\">"
|
|
|
|
" <table>"
|
|
|
|
#for $configItem in $configItems
|
|
|
|
#if $configItem.label != "_"
|
|
|
|
" <tr>"
|
|
|
|
" <td>"
|
|
|
|
" <label for\"$configItem.key\">$configItem.label</label>"
|
|
|
|
" </td><td>"
|
2017-08-21 10:47:48 +02:00
|
|
|
" <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 +=
|
2017-08-20 23:29:14 +02:00
|
|
|
" />"
|
2017-08-20 22:29:01 +02:00
|
|
|
" </td>"
|
|
|
|
" </tr>"
|
|
|
|
#end if
|
|
|
|
#end for
|
|
|
|
" <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;
|
|
|
|
|
|
|
|
#for $configItem in $configItems
|
|
|
|
#if $configItem.label != "_"
|
|
|
|
arg = webServer.arg("$configItem.key");
|
|
|
|
Serial.print("$configItem.key");
|
|
|
|
Serial.println(arg);
|
2017-08-21 10:47:48 +02:00
|
|
|
#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
|
2017-08-20 22:29:01 +02:00
|
|
|
#end if
|
|
|
|
#end for
|
|
|
|
|
2017-08-21 10:47:48 +02:00
|
|
|
configBlock.magic = MAGIC;
|
|
|
|
|
|
|
|
showConfiguration();
|
|
|
|
|
|
|
|
EEPROM.begin(512);
|
|
|
|
for (uint16_t i = 0; i < sizeof(configBlock); i++) {
|
|
|
|
char c = *(((char*)(&configBlock))+i);
|
|
|
|
Serial.print("write to eeprom: ");
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.println(c);
|
|
|
|
EEPROM.write(EEPROM_ADDR + i, c);
|
|
|
|
}
|
|
|
|
EEPROM.commit();
|
|
|
|
|
|
|
|
Serial.println("EEPROM saved");
|
|
|
|
|
2017-08-20 22:29:01 +02:00
|
|
|
webServer.send(200, "text/html", "configuration saved");
|
|
|
|
}
|
2017-08-20 23:29:14 +02:00
|
|
|
|
|
|
|
void showConfiguration() {
|
|
|
|
Serial.println("Configuration is");
|
|
|
|
|
|
|
|
#for $configItem in $configItems
|
|
|
|
Serial.print("$configItem.key = ");
|
|
|
|
Serial.println(configBlock.$configItem.key);
|
|
|
|
|
|
|
|
#end for
|
|
|
|
|
|
|
|
Serial.println("---");
|
|
|
|
}
|