From ecfc55705ac91cd80f22ed12f545da667ddcf44f Mon Sep 17 00:00:00 2001
From: Wolfgang Hottgenroth
+In my previous ESP8266 based weekend projects I always hardcoded configuration data. For the ESP8266 these are at least the WiFi credentials SSID and WPA key. Moving it into a different WiFi network requires re-flashing it. There must be a better way I thought and found that's a single line of code to run the ESP8266 as an accesspoint, which opens its own WiFi network.
+
+So, I first hardcoded a web page with a form to enter configuration data, a data structure to hold it and some code to store it into the EEPROM or load it there.
+It appears that this was an error-prone process with a lot of redudancy in the code.
+
+For that reason I wrote an approach with a small template-based generator script:
+
+
+
+
+#!/usr/bin/python
+
+from Cheetah.Template import Template
+
+configItems = [
+ { "label" : "_" , "key" : "magic" , "type" : "I" , "default" : 0 },
+ { "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" : 64, "default" : "broker.hottis.de" },
+ { "label" : "MQTT Username" , "key" : "mqttUser" , "type" : "C" , "length" : 32, "default" : "esp1" },
+ { "label" : "MQTT Password" , "key" : "mqttPass" , "type" : "C" , "length" : 32, "default" : "geheim" },
+ { "label" : "MQTT ClientId" , "key" : "mqttClientId" , "type" : "C" , "length" : 32, "default" : "changeThis" },
+ { "label" : "MQTT Topic" , "key" : "mqttTopic" , "type" : "C" , "length" : 64, "default" : "IoT/espThermometer2/location/measurement" },
+ { "label" : "MQTT Port" , "key" : "mqttPort" , "type" : "I" , "default" :8883},
+ { "label" : "Measure Period" , "key" : "measurePeriod" , "type" : "I" , "default" :300}
+]
+
+h_file = Template(file= configuration_h.tmpl , searchList=[{ configItems :configItems}])
+open('configuration.h','w').write(str(h_file))
+c_file = Template(file= configuration_c.tmpl , searchList=[{ configItems :configItems}])
+open('configuration.cpp','w').write(str(c_file))
+
+
+The two templates are: +
+ ++
+
+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;
+
+void configServeIndex();
+void configServeGetConfiguration();
+void showConfiguration();
+
+
+
+
+
++And: +
+ ++
+
+#raw
+#include
+
+#include
+#include
+#include
+
+#include "defines.h"
+#include "configuration.h"
+#end raw
+
+
+tConfigBlock configBlock;
+const uint32_t MAGIC = 0xC0DE0001;
+extern ESP8266WebServer webServer;
+
+bool configSaved = false;
+
+void configServeIndex() {
+ 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 =
+ ""
+ " "
+ " ESP8266 Thermometer Configuration Page "
+ " "
+ " "
+ " ESP8266 Configuration Page
";
+
+ if (configSaved) {
+ configSaved = false;
+ buffer += "Configuration saved
";
+ }
+
+ buffer +=
+ " "
+ " "
+ "