-
Eric Duminil authored6ea33a0e
#include "web_config.h"
#define STRING_LEN 33 // Should be enough for ip, addresses, passwords...
#include "config.h"
#ifndef AMPEL_PASSWORD
# error Missing config.h file. Please copy config.public.h to config.h.
# warning config.h has a new structure (e.g. AMPEL_WIFI should be set to true or false)
#endif
#include "util.h"
#include "sensor_console.h"
#include "src/lib/IotWebConf/src/IotWebConf.h"
#include "src/lib/IotWebConf/src/IotWebConfTParameter.h"
#include "src/lib/IotWebConf/src/IotWebConfOptionalGroup.h"
//TODO: Convert all strings to F-strings
namespace web_config {
#if defined(ESP8266)
ESP8266WebServer http(80); // Create a webserver object that listens for HTTP request on port 80
#elif defined(ESP32)
WebServer http(80);
#endif
DNSServer dnsServer; //TODO: Check if needed
IotWebConf *iotWebConf;
const char config_version[IOTWEBCONF_CONFIG_VERSION_LENGTH] = "a10"; // -- Configuration specific key. The value should be modified if config structure was changed.
using namespace iotwebconf;
/**
* WiFi params
*/
CheckboxTParameter ampelWifiParam =
Builder<CheckboxTParameter>("WiFi").label("WiFi?").defaultValue(AMPEL_WIFI).build();
IntTParameter<uint16_t> wifiTimeoutParam =
Builder<IntTParameter<uint16_t>>("wifi_timeout").label("WiFi timeout").defaultValue(WIFI_TIMEOUT).min(0).placeholder(
"[s]").build();
//TODO: Distribute to corresponding classes, possibly with callbacks?
//TODO: Chainedparameters?
/**
* CO2 sensor
*/
ParameterGroup co2Params = ParameterGroup("co2", "CO2 Sensor");
IntTParameter<uint16_t> timestepParam =
Builder<IntTParameter<uint16_t>>("timestep").label("Measurement timestep").defaultValue(MEASUREMENT_TIMESTEP).min(
2).max(1800).placeholder("[s]").build();
FloatTParameter temperatureOffsetParam =
Builder<FloatTParameter>("temp_offset").label("Temperature offset").defaultValue(TEMPERATURE_OFFSET).placeholder(
"[K]").step(0.1).build();
IntTParameter<uint16_t> altitudeParam = Builder<IntTParameter<uint16_t>>("altitude").label("Altitude").defaultValue(
ALTITUDE_ABOVE_SEA_LEVEL).min(0).step(1).placeholder("[m]").build();
IntTParameter<uint16_t> atmosphericCO2Param = Builder<IntTParameter<uint16_t>>("atmospheric_co2").label(
"Atmospheric CO2 concentration").defaultValue(
ATMOSPHERIC_CO2_CONCENTRATION).min(400).max(2000).step(1).placeholder("ppm").build();
CheckboxTParameter autoCalibrateParam = Builder<CheckboxTParameter>("asc").label("Auto-calibration?").defaultValue(
AUTO_CALIBRATE_SENSOR).build();
/**
* LED
*/
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//NOTE: Could also be optional : for LED 0 / 1
ParameterGroup ledParams = ParameterGroup("LED", "LED");
IntTParameter<uint8_t> maxBrightnessParam =
Builder<IntTParameter<uint8_t>>("max_brightness").label("Max Brightness").defaultValue(MAX_BRIGHTNESS).min(0).max(
255).build();
IntTParameter<uint8_t> minBrightnessParam =
Builder<IntTParameter<uint8_t>>("min_brightness").label("Min Brightness").defaultValue(MIN_BRIGHTNESS).min(0).max(
255).build();
IntTParameter<uint16_t> ledCountParam = Builder<IntTParameter<uint16_t>>("led_count").label("LED ring").defaultValue(
LED_COUNT).min(12).max(16).step(4).build();
/**
* CSV
*/
OptionalParameterGroup csvParams = OptionalParameterGroup("CSV", "CSV", AMPEL_CSV);
IntTParameter<uint16_t> csvIntervalParam =
Builder<IntTParameter<uint16_t>>("csv_interval").label("CSV interval").defaultValue(CSV_INTERVAL).min(0).step(1).placeholder(
"[s]").build();
/**
* MQTT
*/
OptionalParameterGroup mqttParams = OptionalParameterGroup("MQTT", "MQTT", AMPEL_MQTT);
IntTParameter<uint16_t> mqttIntervalParam =
Builder<IntTParameter<uint16_t>>("mqtt_interval").label("MQTT interval").defaultValue(MQTT_SENDING_INTERVAL).min(
0).step(1).defaultValue(300).placeholder("[s]").build();
CheckboxTParameter mqttEncryptionParam =
Builder<CheckboxTParameter>("mqtt_encryption").label("Encrypt MQTT?").defaultValue(MQTT_ENCRYPTED).build();
CheckboxTParameter mqttCommandsParam =
Builder<CheckboxTParameter>("mqtt_commands").label("Allow MQTT commands?").defaultValue(ALLOW_MQTT_COMMANDS).build();
TextTParameter<STRING_LEN> mqttServerParam =
Builder<TextTParameter<STRING_LEN>>("mqtt_server").label("MQTT Server").defaultValue(MQTT_SERVER).build();
IntTParameter<uint16_t> mqttPortParam = Builder<IntTParameter<uint16_t>>("mqtt_port").label("MQTT Port").defaultValue(
MQTT_PORT).build();
TextTParameter<STRING_LEN> mqttUserParam =
Builder<TextTParameter<STRING_LEN>>("mqtt_user").label("MQTT User").defaultValue(MQTT_USER).build();
PasswordTParameter<STRING_LEN> mqttPasswordParam = Builder<PasswordTParameter<STRING_LEN>>("mqtt_password").label(
"MQTT password").defaultValue(MQTT_PASSWORD).build();
/**
* NTP Time
*/
ParameterGroup timeParams = ParameterGroup("NTP", "Time server");
TextTParameter<STRING_LEN> ntpServerParam =
Builder<TextTParameter<STRING_LEN>>("ntp_server").label("NTP Server").defaultValue(NTP_SERVER).build();
IntTParameter<int16_t> timeOffsetParam = Builder<IntTParameter<int16_t>>("timezone").label("Timezone").defaultValue(
UTC_OFFSET).min(-23).max(23).step(1).placeholder("[h]").build();
CheckboxTParameter dstParam = Builder<CheckboxTParameter>("dst").label("Daylight Saving Time?").defaultValue(
DAYLIGHT_SAVING_TIME).build();
/**
* LoRaWAN
*/
#if defined(ESP32)
OptionalParameterGroup loraParams = OptionalParameterGroup("LoRaWan", "LoRaWan", AMPEL_LORAWAN);
IntTParameter<uint16_t> loraIntervalParam =
Builder<IntTParameter<uint16_t>>("lora_interval").label("LoRa interval").defaultValue(LORAWAN_SENDING_INTERVAL).min(
0).step(1).defaultValue(300).placeholder("[s]").build();
TextTParameter<17> deviceEUIParam = Builder<TextTParameter<17>>("device_eui").label("Device EUI (MSB)").defaultValue(
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
LORAWAN_DEVICE_EUI).build();
TextTParameter<17> appEUIParam = Builder<TextTParameter<17>>("app_eui").label("App EUI (MSB)").defaultValue(
LORAWAN_APPLICATION_EUI).build();
PasswordTParameter<33> appKeyParam = Builder<PasswordTParameter<33>>("app_key").label("App key (MSB)").defaultValue(
LORAWAN_APPLICATION_KEY).build();
#endif
OptionalGroupHtmlFormatProvider optionalGroupHtmlFormatProvider;
void update() {
iotWebConf->doLoop(); // Listen for HTTP requests from clients
}
void setWifiConnectionCallback(void (*success_function)()) {
iotWebConf->setWifiConnectionCallback(success_function);
}
void setWifiFailCallback(void (*fail_function)()) {
std::function<WifiAuthInfo* ()> fail_and_return_null = [fail_function]() {
fail_function();
iotWebConf->forceApMode(true); // Will stay in AP.
return nullptr;
};
iotWebConf->setWifiConnectionFailedHandler(fail_and_return_null);
}
void defineStructure() {
iotWebConf->setHtmlFormatProvider(&optionalGroupHtmlFormatProvider);
iotWebConf->addSystemParameter(&elWifiParam);
iotWebConf->addSystemParameter(&wifiTimeoutParam);
iotWebConf->getThingNameParameter()->label = "Ampel name";
iotWebConf->getApPasswordParameter()->label = "Ampel password (user : 'admin')";
iotWebConf->getApPasswordParameter()->defaultValue = AMPEL_PASSWORD;
iotWebConf->getWifiSsidParameter()->defaultValue = WIFI_SSID;
iotWebConf->getWifiPasswordParameter()->defaultValue = WIFI_PASSWORD;
co2Params.addItem(×tepParam);
co2Params.addItem(&temperatureOffsetParam);
co2Params.addItem(&altitudeParam);
co2Params.addItem(&atmosphericCO2Param);
co2Params.addItem(&autoCalibrateParam);
ledParams.addItem(&minBrightnessParam);
ledParams.addItem(&maxBrightnessParam);
ledParams.addItem(&ledCountParam);
timeParams.addItem(&ntpServerParam);
timeParams.addItem(&timeOffsetParam);
timeParams.addItem(&dstParam);
csvParams.addItem(&csvIntervalParam);
mqttParams.addItem(&mqttIntervalParam);
mqttParams.addItem(&mqttServerParam);
mqttParams.addItem(&mqttPortParam);
mqttParams.addItem(&mqttUserParam);
mqttParams.addItem(&mqttPasswordParam);
mqttParams.addItem(&mqttEncryptionParam);
mqttParams.addItem(&mqttCommandsParam);
#if defined(ESP32)
loraParams.addItem(&loraIntervalParam);
loraParams.addItem(&deviceEUIParam);
loraParams.addItem(&appEUIParam);
loraParams.addItem(&appKeyParam);
#endif
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
iotWebConf->addParameterGroup(&co2Params);
iotWebConf->addParameterGroup(&ledParams);
iotWebConf->addParameterGroup(&timeParams);
iotWebConf->addParameterGroup(&csvParams);
iotWebConf->addParameterGroup(&mqttParams);
#if defined(ESP32)
iotWebConf->addParameterGroup(&loraParams);
#endif
}
void defineCommands() {
sensor_console::defineCommand("save_config", config::save, F("(Saves the config to EEPROM)"));
sensor_console::defineCommand("reset_config", []() {
Serial.println(F("Resetting config..."));
iotWebConf->getRootParameterGroup()->applyDefaultValue();
iotWebConf->saveConfig();
Serial.println(F("Done!"));
}, F("(Resets the complete IotWeb config)"));
sensor_console::defineStringCommand("ssid", [](char *ssid) {
Serial.print(F("Setting WiFi ssid to "));
Serial.println(ssid);
strlcpy(iotWebConf->getWifiSsidParameter()->valueBuffer, ssid, iotWebConf->getWifiSsidParameter()->getLength());
iotWebConf->saveConfig();
}, F("name (Sets SSID to name)"));
sensor_console::defineStringCommand("pwd", [](char *ssid) {
Serial.print(F("Setting WiFi password to "));
Serial.println(ssid);
strlcpy(iotWebConf->getWifiPasswordParameter()->valueBuffer, ssid, iotWebConf->getWifiPasswordParameter()->getLength());
iotWebConf->saveConfig();
}, F("abc (Sets WiFi password to abc)"));
sensor_console::defineIntCommand("wifi", [](int32_t onOff) {
config::is_wifi_on = onOff;
iotWebConf->saveConfig();
Serial.print(F("WiFi - "));
Serial.println(onOff ? F("On!") : F("Off!"));
}, F("0/1 (Turns Wifi on/off)"));
}
void initialize() {
iotWebConf = new IotWebConf(strlen(AMPEL_NAME) == 0 ? ampel.sensorId : AMPEL_NAME, &dnsServer, &http, "",
config_version);
defineStructure();
defineCommands();
iotWebConf->loadConfig();
if (!config::is_wifi_on) {
Serial.println(F("Wifi is off : no access point or connection. Use 'wifi 1' to turn on again!"));
return;
}
//NOTE: Can only work if wifi is on.
sensor_console::defineIntCommand("ap", [](int onOff) {
//TODO: Add On-board LED effects to show the changes?
if (onOff) {
Serial.print(F("Enable "));
} else {
Serial.print(F("Disable "));
}
Serial.println(F("AP mode!"));
iotWebConf->forceApMode(onOff);
}, F("0/1 (Enables/disables access point)"));
iotWebConf->setWifiConnectionTimeoutMs(1000UL * config::wifi_timeout);
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
iotWebConf->skipApStartup();
iotWebConf->init();
http.on("/config", [] {
iotWebConf->handleConfig();
});
http.onNotFound([]() {
iotWebConf->handleNotFound();
});
}
}
/***
* Define all the corresponding config values as reference, so that they can be updated.
*/
namespace config {
char* ampel_name() {
return web_config::iotWebConf->getThingName();
}
char* selected_ssid() {
return web_config::iotWebConf->getWifiSsidParameter()->valueBuffer;
}
char* ampel_password() {
return web_config::iotWebConf->getApPasswordParameter()->valueBuffer;
}
// Sensor
uint16_t &measurement_timestep = web_config::timestepParam.value(); // [s] Value between 2 and 1800 (range for SCD30 sensor).
uint16_t &altitude_above_sea_level = web_config::altitudeParam.value(); // [m]
uint16_t &co2_calibration_level = web_config::atmosphericCO2Param.value(); // [ppm]
bool &auto_calibrate_sensor = web_config::autoCalibrateParam.value(); // [true / false]
float &temperature_offset = web_config::temperatureOffsetParam.value(); // [K] Sign isn't relevant.
bool &is_wifi_on = web_config::ampelWifiParam.value();
uint16_t &wifi_timeout = web_config::wifiTimeoutParam.value();
void save() {
web_config::iotWebConf->saveConfig();
}
// LEDs
uint8_t &max_brightness = web_config::maxBrightnessParam.value();
uint8_t &min_brightness = web_config::minBrightnessParam.value();
uint16_t &led_count = web_config::ledCountParam.value();
// Time server
char *ntp_server = web_config::ntpServerParam.value();
int16_t &time_zone = web_config::timeOffsetParam.value();
bool &daylight_saving_time = web_config::dstParam.value();
// CSV
bool is_csv_active() {
return web_config::csvParams.isActive();
}
uint16_t &csv_interval = web_config::csvIntervalParam.value();
// MQTT
bool is_mqtt_active() {
return web_config::mqttParams.isActive();
}
char *mqtt_server = web_config::mqttServerParam.value();
char *mqtt_user = web_config::mqttUserParam.value();
char *mqtt_password = web_config::mqttPasswordParam.value();
uint16_t &mqtt_port = web_config::mqttPortParam.value();
uint16_t &mqtt_sending_interval = web_config::mqttIntervalParam.value();
bool &mqtt_encryption = web_config::mqttEncryptionParam.value();
bool &allow_mqtt_commands = web_config::mqttCommandsParam.value();
// LORAWAN
#if defined(ESP32)
bool is_lorawan_active() {
return web_config::loraParams.isActive();
}
uint16_t &lorawan_sending_interval = web_config::loraIntervalParam.value();
char *lorawan_device_eui = web_config::deviceEUIParam.value();
char *lorawan_app_key = web_config::appKeyParam.value();
char *lorawan_app_eui = web_config::appEUIParam.value();
#endif
}