57 lines
3.9 KiB
Plaintext

<!-- { "title": "Configuration-Webserver for ESP8266 Projects" } -->
<h1>#title#</h1>
<p>
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.
</p>
<p>
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.
</p>
<p>
For that reason I wrote an approach with a small template-based generator script:
</p>
<p>
<pre>
<code "language"="python">
#!/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))
</code>
</pre>
</p>
<p>
Using the both templates for a <a href="https://gitlab.com/wolutator/EspThermometer2/blob/master/ConfigGenerator/configuration_c.tmpl" target="_blank">C-</a> and a <a href="https://gitlab.com/wolutator/EspThermometer2/blob/master/ConfigGenerator/configuration_h.tmpl" target="_blank">H-</a>file all the code for the configuration web page, the EEPROM handling code and the code to access the configuration variables is generated.
</p>
<p>
The functions provided using these templates in turn a call from a rather simple <a href="https://gitlab.com/wolutator/EspThermometer2/blob/master/configurationMode.cpp" target="_blank">configuration mode</a>. In pure configuration mode (when the device is unconfigured or the configuration mode is requested using a pulled-down pin) a new WLAN will be opened using the accesspoint functionality of the ESP8266. In production mode, the configuration accesspoint is not started, however, the configuration mode webserver is started nevertheless.
</p>
<p>
This code can be also found embedded in several of my projects. Find them at <a href="https://gitlab.com/wolutator/EspThermometer2" target="_blank">https://gitlab.com/wolutator/EspThermometer2</a>, <a href="https://gitlab.com/wolutator/TouchSwitch" target="_blank">https://gitlab.com/wolutator/TouchSwitch</a>, <a href="https://gitlab.com/wolutator/MySwitch" target="_blank">https://gitlab.com/wolutator/MySwitch</a>, <a href="https://gitlab.com/wolutator/RainSensor" target="_blank">https://gitlab.com/wolutator/RainSensor</a> and <a href="https://gitlab.com/wolutator/TwoLedSignal" target="_blank">https://gitlab.com/wolutator/TwoLedSignal</a>.
</p>