77#include " MqttPublisher.h"
88#include " EEPROM.h"
99#include < ESP8266WiFi.h>
10+ #include < ESP8266WebServer.h>
11+ #include < ESP8266HTTPUpdateServer.h>
1012
11- std::list<Sensor*> *sensors = new std::list<Sensor*>();
13+ std::list<Sensor *> *sensors = new std::list<Sensor *>();
1214
1315void wifiConnected ();
1416void configSaved ();
1517
1618DNSServer dnsServer;
1719WebServer server (80 );
18- HTTPUpdateServer httpUpdater;
20+ ESP8266HTTPUpdateServer httpUpdater;
1921WiFiClient net;
2022
2123MqttConfig mqttConfig;
2224MqttPublisher publisher;
2325
2426IotWebConf iotWebConf (WIFI_AP_SSID, &dnsServer, &server, WIFI_AP_DEFAULT_PASSWORD, CONFIG_VERSION);
25- IotWebConfParameter params[] = {
26- IotWebConfParameter (" MQTT server" , " mqttServer" , mqttConfig.server , sizeof (mqttConfig.server ), " text" , NULL , mqttConfig.server , NULL , true ),
27- IotWebConfParameter (" MQTT port" , " mqttPort" , mqttConfig.port , sizeof (mqttConfig.port ), " text" , NULL , mqttConfig.port , NULL , true ),
28- IotWebConfParameter (" MQTT username" , " mqttUsername" , mqttConfig.username , sizeof (mqttConfig.username ), " text" , NULL , mqttConfig.username , NULL , true ),
29- IotWebConfParameter (" MQTT password" , " mqttPassword" , mqttConfig.password , sizeof (mqttConfig.password ), " password" , NULL , mqttConfig.password , NULL , true ),
30- IotWebConfParameter (" MQTT topic" , " mqttTopic" , mqttConfig.topic , sizeof (mqttConfig.topic ), " text" , NULL , mqttConfig.topic , NULL , true )};
3127
32- boolean needReset = false ;
33- boolean connected = false ;
28+ iotwebconf::TextParameter mqttServerParam = iotwebconf::TextParameter(" MQTT server" , " mqttServer" , mqttConfig.server, sizeof (mqttConfig.server), nullptr , mqttConfig.server);
29+ iotwebconf::NumberParameter mqttPortParam = iotwebconf::NumberParameter(" MQTT port" , " mqttPort" , mqttConfig.port, sizeof (mqttConfig.port), nullptr , mqttConfig.port);
30+ iotwebconf::TextParameter mqttUsernameParam = iotwebconf::TextParameter(" MQTT username" , " mqttUsername" , mqttConfig.username, sizeof (mqttConfig.username), nullptr , mqttConfig.username);
31+ iotwebconf::PasswordParameter mqttPasswordParam = iotwebconf::PasswordParameter(" MQTT password" , " mqttPassword" , mqttConfig.password, sizeof (mqttConfig.password), nullptr , mqttConfig.password);
32+ iotwebconf::TextParameter mqttTopicParam = iotwebconf::TextParameter(" MQTT topic" , " mqttTopic" , mqttConfig.topic, sizeof (mqttConfig.topic), nullptr , mqttConfig.topic);
33+ iotwebconf::ParameterGroup paramGroup = iotwebconf::ParameterGroup(" MQTT Settings" , " " );
3434
35+ boolean needReset = false ;
3536
3637void process_message (byte *buffer, size_t len, Sensor *sensor)
3738{
@@ -58,7 +59,7 @@ void setup()
5859
5960 // Setup reading heads
6061 DEBUG (" Setting up %d configured sensors..." , NUM_OF_SENSORS);
61- const SensorConfig *config = SENSOR_CONFIGS;
62+ const SensorConfig *config = SENSOR_CONFIGS;
6263 for (uint8_t i = 0 ; i < NUM_OF_SENSORS; i++, config++)
6364 {
6465 Sensor *sensor = new Sensor (config, process_message);
@@ -70,34 +71,42 @@ void setup()
7071 // Setup WiFi and config stuff
7172 DEBUG (" Setting up WiFi and config stuff." );
7273
73- for (uint8_t i = 0 ; i < sizeof (params) / sizeof (params[0 ]); i++)
74- {
75- DEBUG (" Adding parameter %s." , params[i].label );
76- iotWebConf.addParameter (¶ms[i]);
77- }
74+ paramGroup.addItem (&mqttServerParam);
75+ paramGroup.addItem (&mqttPortParam);
76+ paramGroup.addItem (&mqttUsernameParam);
77+ paramGroup.addItem (&mqttPasswordParam);
78+ paramGroup.addItem (&mqttTopicParam);
79+
80+ iotWebConf.addParameterGroup (¶mGroup);
81+
7882 iotWebConf.setConfigSavedCallback (&configSaved);
7983 iotWebConf.setWifiConnectionCallback (&wifiConnected);
80- iotWebConf.setupUpdateServer (&httpUpdater);
84+
85+
86+ WiFi.onStationModeDisconnected ([](const WiFiEventStationModeDisconnected& event) {
87+ publisher.disconnect ();
88+ });
89+
90+ // -- Define how to handle updateServer calls.
91+ iotWebConf.setupUpdateServer (
92+ [](const char *updatePath)
93+ { httpUpdater.setup (&server, updatePath); },
94+ [](const char *userName, char *password)
95+ { httpUpdater.updateCredentials (userName, password); });
8196
8297 boolean validConfig = iotWebConf.init ();
8398 if (!validConfig)
8499 {
85100 DEBUG (" Missing or invalid config. MQTT publisher disabled." );
86- MqttConfig defaults;
87- // Resetting to default values
88- strcpy (mqttConfig.server , defaults.server );
89- strcpy (mqttConfig.port , defaults.port );
90- strcpy (mqttConfig.username , defaults.username );
91- strcpy (mqttConfig.password , defaults.password );
92- strcpy (mqttConfig.topic , defaults.topic );
93101 }
94102 else
95103 {
96104 // Setup MQTT publisher
97105 publisher.setup (mqttConfig);
98106 }
99107
100- server.on (" /" , [] { iotWebConf.handleConfig (); });
108+ server.on (" /" , []() { iotWebConf.handleConfig (); });
109+ server.on (" /reset" , []() { needReset = true ; });
101110 server.onNotFound ([]() { iotWebConf.handleNotFound (); });
102111
103112 DEBUG (" Setup done." );
@@ -130,6 +139,5 @@ void configSaved()
130139void wifiConnected ()
131140{
132141 DEBUG (" WiFi connection established." );
133- connected = true ;
134142 publisher.connect ();
135143}
0 commit comments