From 965c2c6133d298c816b8ea0611325830657825b3 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 25 Apr 2019 16:28:00 +0200 Subject: [PATCH] prepare usage of boilerplate --- .gitmodules | 3 + ConfigGenerator/configGen.py | 40 --- ConfigGenerator/configuration_c.tmpl | 153 ---------- ConfigGenerator/configuration_h.tmpl | 17 -- productionMode.cpp => application.cpp | 0 productionMode.h => application.h | 0 configuration.cpp | 389 -------------------------- configuration.h | 24 -- configurationMode.cpp | 56 ---- configurationMode.h | 18 -- esp8266boilerplate | 1 + 11 files changed, 4 insertions(+), 697 deletions(-) create mode 100644 .gitmodules delete mode 100644 ConfigGenerator/configGen.py delete mode 100644 ConfigGenerator/configuration_c.tmpl delete mode 100644 ConfigGenerator/configuration_h.tmpl rename productionMode.cpp => application.cpp (100%) rename productionMode.h => application.h (100%) delete mode 100644 configuration.cpp delete mode 100644 configuration.h delete mode 100644 configurationMode.cpp delete mode 100644 configurationMode.h create mode 160000 esp8266boilerplate diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..26dbf14 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "esp8266boilerplate"] + path = esp8266boilerplate + url = ../esp8266boilerplate.git diff --git a/ConfigGenerator/configGen.py b/ConfigGenerator/configGen.py deleted file mode 100644 index 9266a21..0000000 --- a/ConfigGenerator/configGen.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/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":"RgbLed1"}, - {"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"}, - {"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"RgbLed1"}, - {"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883}, - {"label":"MQTT Topic Color Command", "key":"mqttTopicColorCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/ColorCommand"}, - {"label":"MQTT Topic Command", "key":"mqttTopicCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/Command"}, - {"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RgbLed1/Debug"}, - {"label":"DebugMode", "key":"debugMode", "type":"I", "default":0} -] - - -magic = 0xC0DE0005 -appName = "ESP8266 based TwoLedSignal" -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)) diff --git a/ConfigGenerator/configuration_c.tmpl b/ConfigGenerator/configuration_c.tmpl deleted file mode 100644 index 9bf61a9..0000000 --- a/ConfigGenerator/configuration_c.tmpl +++ /dev/null @@ -1,153 +0,0 @@ -#raw -#include - -#include -#include -#include - -#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 = - "" - " " - " $appName" - " " - " " - "

$appName - ESP8266 Configuration Page

"; - - if (configSaved) { - configSaved = false; - buffer += "

Configuration saved

"; - } - - buffer += - "
" - " " - #for $configItem in $configItems - #if $configItem.label != "_" - " " - " " - " " - #end if - #end for - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - "
" - "
" - " " - ""; - - 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("---"); -} \ No newline at end of file diff --git a/ConfigGenerator/configuration_h.tmpl b/ConfigGenerator/configuration_h.tmpl deleted file mode 100644 index e528f14..0000000 --- a/ConfigGenerator/configuration_h.tmpl +++ /dev/null @@ -1,17 +0,0 @@ -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(); \ No newline at end of file diff --git a/productionMode.cpp b/application.cpp similarity index 100% rename from productionMode.cpp rename to application.cpp diff --git a/productionMode.h b/application.h similarity index 100% rename from productionMode.h rename to application.h diff --git a/configuration.cpp b/configuration.cpp deleted file mode 100644 index b562d1a..0000000 --- a/configuration.cpp +++ /dev/null @@ -1,389 +0,0 @@ -#include - -#include -#include -#include - -#include "defines.h" -#include "configuration.h" - - -tConfigBlock configBlock; -const uint32_t MAGIC = 3235774471; -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, "RgbLed1"); - strcpy(configBlock.mqttPass, "geheim123"); - strcpy(configBlock.mqttClientId, "RgbLed1"); - configBlock.mqttPort = 8883; - strcpy(configBlock.mqttTopicColorCommand, "IoT/RgbLed1/ColorCommand"); - strcpy(configBlock.mqttTopicCommand, "IoT/RgbLed1/Command"); - strcpy(configBlock.mqttDebugTopic, "IoT/RgbLed1/Debug"); - configBlock.debugMode = 0; - } - - if (! checkAuthentication()) { - return webServer.requestAuthentication(); - } - - - String buffer = - "" - " " - " ESP8266 based TwoLedSignal" - " " - " " - "

ESP8266 based TwoLedSignal - ESP8266 Configuration Page

"; - - if (configSaved) { - configSaved = false; - buffer += "

Configuration saved

"; - } - - buffer += - "
" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - " " - " " - "
" - " " - "
" - "
" - " " - ""; - - 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("mqttTopicColorCommand"); - strcpy(configBlock.mqttTopicColorCommand, arg.c_str()); - arg = webServer.arg("mqttTopicCommand"); - strcpy(configBlock.mqttTopicCommand, arg.c_str()); - arg = webServer.arg("mqttDebugTopic"); - strcpy(configBlock.mqttDebugTopic, arg.c_str()); - arg = webServer.arg("debugMode"); - configBlock.debugMode = 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("mqttTopicColorCommand = <"); - Serial.print(configBlock.mqttTopicColorCommand); - Serial.println(">"); - - Serial.print("mqttTopicCommand = <"); - Serial.print(configBlock.mqttTopicCommand); - Serial.println(">"); - - Serial.print("mqttDebugTopic = <"); - Serial.print(configBlock.mqttDebugTopic); - Serial.println(">"); - - Serial.print("debugMode = <"); - Serial.print(configBlock.debugMode); - Serial.println(">"); - - - Serial.println("---"); -} diff --git a/configuration.h b/configuration.h deleted file mode 100644 index 2b21764..0000000 --- a/configuration.h +++ /dev/null @@ -1,24 +0,0 @@ -typedef struct { - uint32_t magic; - char confUser[16]; - char confPasswd[16]; - char wifiSsid[32]; - char wifiKey[64]; - char mqttBroker[32]; - char mqttUser[32]; - char mqttPass[32]; - char mqttClientId[32]; - uint32_t mqttPort; - char mqttTopicColorCommand[64]; - char mqttTopicCommand[64]; - char mqttDebugTopic[64]; - uint32_t debugMode; -} tConfigBlock; - -extern const uint32_t MAGIC; -extern tConfigBlock configBlock; -extern const char* CONFIG_SSID; - -void configServeIndex(); -void configServeGetConfiguration(); -void showConfiguration(); \ No newline at end of file diff --git a/configurationMode.cpp b/configurationMode.cpp deleted file mode 100644 index 66a461a..0000000 --- a/configurationMode.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * configurationMode.cpp - * - * Created on: Aug 20, 2017 - * Author: wn - */ - -#include "defines.h" - -#include -#include -#include -#include - - -#include "configurationMode.h" -#include "configuration.h" - - - - -ESP8266WebServer webServer(80); - -void configServeNotFound() { - webServer.send(404, "text/plain", "page not found"); -#ifdef DEBUG - Serial.println("page not found served"); -#endif -} - - - -void setupConfigurationNetwork() { - WiFi.mode(WIFI_AP); - WiFi.softAP(CONFIG_SSID); -#ifdef DEBUG - Serial.println("AP started"); -#endif -} - -void setupConfigurationServer() { - webServer.on("/", configServeIndex); - webServer.on("/config", configServeGetConfiguration); - webServer.onNotFound(configServeNotFound); - webServer.begin(); -#ifdef DEBUG - Serial.println("Webserver started"); -#endif -} - -void loopConfiguration() { - webServer.handleClient(); -} - - - diff --git a/configurationMode.h b/configurationMode.h deleted file mode 100644 index 59f5e19..0000000 --- a/configurationMode.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * configurationMode.h - * - * Created on: Aug 20, 2017 - * Author: wn - */ - -#ifndef CONFIGURATIONMODE_H_ -#define CONFIGURATIONMODE_H_ - - - -void setupConfigurationNetwork(); -void setupConfigurationServer(); -void loopConfiguration(); - - -#endif /* CONFIGURATIONMODE_H_ */ diff --git a/esp8266boilerplate b/esp8266boilerplate new file mode 160000 index 0000000..28b3265 --- /dev/null +++ b/esp8266boilerplate @@ -0,0 +1 @@ +Subproject commit 28b32656aa94ebe02b3e48db614cde552fa16957