This commit is contained in:
2018-04-22 22:18:13 +02:00
commit 91c2841ef5
21 changed files with 2500 additions and 0 deletions

View File

@ -0,0 +1,40 @@
#!/usr/bin/python
from Cheetah.Template import Template
configItems = [
{"label":"_", "key":"magic", "type":"I", "default": ""},
{"label":"Config Username", "key":"confUser", "type":"C", "length":16, "default":"admin"},
{"label":"Config Password", "key":"confPasswd", "type":"C", "length":16, "default":"geheim123"},
{"label":"Wifi SSID", "key":"wifiSsid", "type":"C", "length":32, "default":"test"},
{"label":"Wifi Key", "key":"wifiKey", "type":"C", "length":64, "default":"geheim"},
{"label":"MQTT Broker", "key":"mqttBroker", "type":"C", "length":32, "default":"broker.hottis.de"},
{"label":"MQTT Username", "key":"mqttUser", "type":"C", "length":32, "default":"RainSensor1"},
{"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"},
{"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"RainSensor1"},
{"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883},
{"label":"MQTT Topic", "key":"mqttTopic", "type":"C", "length":64, "default":"IoT/RainSensor1/Value"},
{"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RainSensor1/Debug"},
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0},
{"label":"Period", "key":"period", "type":"I", "default":300}
]
magic = 0xC0DE0003
appName = "ESP8266 based RainSensor"
confWifiSsid = "espconfig"
params = {
"magic":magic,
"appName":appName,
"confWifiSsid":confWifiSsid,
"configItems":configItems
}
h_file = Template(file="configuration_h.tmpl", searchList=[params])
open('configuration.h','w').write(str(h_file))
c_file = Template(file="configuration_c.tmpl", searchList=[params])
open('configuration.cpp','w').write(str(c_file))

View File

@ -0,0 +1,153 @@
#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("---");
}

View File

@ -0,0 +1,17 @@
typedef struct {
#for $configItem in $configItems
#if $configItem.type == 'C'
char ${configItem.key}[$configItem.length];
#else if $configItem.type == 'I'
uint32_t $configItem.key;
#end if
#end for
} tConfigBlock;
extern const uint32_t MAGIC;
extern tConfigBlock configBlock;
extern const char* CONFIG_SSID;
void configServeIndex();
void configServeGetConfiguration();
void showConfiguration();