web_config.cpp 7.68 KB
Newer Older
1
2
#include "web_config.h"

Eric Duminil's avatar
Eric Duminil committed
3
4
5
6
7
namespace config {
  iotwebconf::TextTParameter<STRING_LEN> stringParam = iotwebconf::Builder<iotwebconf::TextTParameter< STRING_LEN>>(
      "stringParam").label("String param").build();
}

Eric Duminil's avatar
Notes    
Eric Duminil committed
8
9
//TODO: Is there any conflict with SPIFFS/LittleFS?
//TODO: Actually use those parameters
Eric Duminil's avatar
TODO    
Eric Duminil committed
10
//TODO: Make sure values are *really* set to defaults during first run.
Eric Duminil's avatar
Notes    
Eric Duminil committed
11

12
13
14
15
16
17
18
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

Eric Duminil's avatar
Eric Duminil committed
19
  DNSServer dnsServer; //TODO: Check if needed
20
21
22
23

  IotWebConf *iotWebConf;

// -- Configuration specific key. The value should be modified if config structure was changed.
Eric Duminil's avatar
Eric Duminil committed
24
  const char config_version[] = "ampel_test_v6";
25

Eric Duminil's avatar
Eric Duminil committed
26
27
28
  /**
   * Services
   */
Eric Duminil's avatar
Eric Duminil committed
29
30
31
32
33
34
35
36
37
38
39
  iotwebconf::ParameterGroup serviceParams = iotwebconf::ParameterGroup("Services", "Services");

  iotwebconf::CheckboxTParameter ampelWifiParam = iotwebconf::Builder<iotwebconf::CheckboxTParameter>("WiFi").label(
      "WiFi").defaultValue(true).build();
  iotwebconf::CheckboxTParameter ampelCSVParam =
      iotwebconf::Builder<iotwebconf::CheckboxTParameter>("CSV").label("CSV").defaultValue(true).build();
  iotwebconf::CheckboxTParameter ampelMQTTParam = iotwebconf::Builder<iotwebconf::CheckboxTParameter>("MQTT").label(
      "MQTT").defaultValue(true).build();
  iotwebconf::CheckboxTParameter ampelLoRaWANParam =
      iotwebconf::Builder<iotwebconf::CheckboxTParameter>("LoRaWAN").label("LoRaWAN").defaultValue(false).build();

Eric Duminil's avatar
Eric Duminil committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  //TODO: Distribute to corresponding classes, possibly with callbacks?
  /**
   * CO2 sensor
   */
  iotwebconf::ParameterGroup co2Params = iotwebconf::ParameterGroup("co2", "CO2 Sensor"); //TODO: Conflict with labels?

  //TODO: UIntTParameter?
  iotwebconf::IntTParameter<uint16_t> timestepParam = iotwebconf::Builder<iotwebconf::IntTParameter<uint16_t>>(
      "timestep").label("Measurement timestep").defaultValue(MEASUREMENT_TIMESTEP).min(2).max(1800).step(1).placeholder(
      "2..1800").build();

  iotwebconf::FloatTParameter temperatureOffsetParam =
      iotwebconf::Builder<iotwebconf::FloatTParameter>("temp_offset").label("Temperature offset").defaultValue(
      TEMPERATURE_OFFSET).placeholder("-3").step(0.1).build();

  iotwebconf::IntTParameter<uint16_t> altitudeParam = iotwebconf::Builder<iotwebconf::IntTParameter<uint16_t>>(
      "altitude").label("Altitude").defaultValue(ALTITUDE_ABOVE_SEA_LEVEL).min(0).step(1).build();

  iotwebconf::IntTParameter<uint16_t> atmosphericCO2Param = iotwebconf::Builder<iotwebconf::IntTParameter<uint16_t>>(
      "atmospheric_co2").label("Atmospheric CO2 concentration").defaultValue(
  ATMOSPHERIC_CO2_CONCENTRATION).min(400).max(2000).step(1).placeholder("400..2000").build();

  iotwebconf::CheckboxTParameter autoCalibrateParam = iotwebconf::Builder<iotwebconf::CheckboxTParameter>("asc").label(
      "Auto-calibration").defaultValue(true).build();

  /**
   * CSV
   */
  iotwebconf::ParameterGroup csvParams = iotwebconf::ParameterGroup("csvs", "CSV");

  iotwebconf::IntTParameter<uint16_t> csvTimestepParam = iotwebconf::Builder<iotwebconf::IntTParameter<uint16_t>>(
      "csv_timestep").label("CSV timestep").defaultValue(CSV_INTERVAL).min(0).step(1).build();

  /**
   * MQTT
   */
  iotwebconf::ParameterGroup mqttParams = iotwebconf::ParameterGroup("mqtts", "MQTT");

  iotwebconf::IntTParameter<uint16_t> mqttTimestepParam = iotwebconf::Builder<iotwebconf::IntTParameter<uint16_t>>(
      "mqtt_timestep").label("MQTT timestep").defaultValue(
  MQTT_SENDING_INTERVAL).min(0).step(1).defaultValue(300).build();

  iotwebconf::CheckboxTParameter mqttCommandsParam = iotwebconf::Builder<iotwebconf::CheckboxTParameter>(
      "mqtt_commands").label("MQTT commands").defaultValue(false).build();

Eric Duminil's avatar
Eric Duminil committed
85
  iotwebconf::TextTParameter<STRING_LEN> mqttServerParam = iotwebconf::Builder<iotwebconf::TextTParameter<STRING_LEN>>(
Eric Duminil's avatar
Eric Duminil committed
86
87
88
89
90
      "mqtt_server").label("MQTT Server").defaultValue(MQTT_SERVER).build();

  iotwebconf::IntTParameter<uint16_t> mqttPortParam = iotwebconf::Builder<iotwebconf::IntTParameter<uint16_t>>(
      "mqtt_port").label("MQTT Port").defaultValue(MQTT_PORT).build();

Eric Duminil's avatar
Eric Duminil committed
91
92
  iotwebconf::TextTParameter<STRING_LEN> mqttUserParam = iotwebconf::Builder<iotwebconf::TextTParameter<STRING_LEN>>(
      "mqtt_user").label("MQTT User").defaultValue(MQTT_USER).build();
Eric Duminil's avatar
Eric Duminil committed
93

Eric Duminil's avatar
Eric Duminil committed
94
95
96
  iotwebconf::PasswordTParameter<STRING_LEN> mqttPasswordParam = iotwebconf::Builder<
      iotwebconf::PasswordTParameter< STRING_LEN>>("mqtt_password").label("MQTT password").defaultValue(
  MQTT_PASSWORD).build();
Eric Duminil's avatar
Eric Duminil committed
97

Eric Duminil's avatar
Eric Duminil committed
98
99
100
  /**
   * NTP Time
   */
Eric Duminil's avatar
Eric Duminil committed
101

Eric Duminil's avatar
Eric Duminil committed
102
103
104
  /**
   * LED
   */
Eric Duminil's avatar
Eric Duminil committed
105

Eric Duminil's avatar
Eric Duminil committed
106
107
108
  /**
   * LoRa & Stuff
   */
109
110
111
112
113

  void update() {
    iotWebConf->doLoop(); // Listen for HTTP requests from clients
  }

Eric Duminil's avatar
Eric Duminil committed
114
115
116
117
118
119
120
121
122
123
124
  void setWifiConnectionCallback(void (*function)()) {
    iotWebConf->setWifiConnectionCallback(function);
  }

  void setWifiConnectionFailedCallback(void (*function)()) {
    iotWebConf->setWifiConnectionFailedHandler([&function]() -> iotwebconf::WifiAuthInfo* {
      function();
      return NULL;
    });
  }

125
126
127
  void initialize() {
    iotWebConf = new IotWebConf(ampel.sensorId, &dnsServer, &http, HTTP_PASSWORD, config_version);

Eric Duminil's avatar
Eric Duminil committed
128
129
130
131
132
133
134
    serviceParams.addItem(&ampelWifiParam);
    serviceParams.addItem(&ampelMQTTParam);
    serviceParams.addItem(&ampelCSVParam);
    serviceParams.addItem(&ampelLoRaWANParam);

    co2Params.addItem(&timestepParam);
    co2Params.addItem(&temperatureOffsetParam);
Eric Duminil's avatar
Eric Duminil committed
135
136
137
    co2Params.addItem(&altitudeParam);
    co2Params.addItem(&atmosphericCO2Param);
    co2Params.addItem(&autoCalibrateParam);
Eric Duminil's avatar
Eric Duminil committed
138

Eric Duminil's avatar
Eric Duminil committed
139
140
141
142
143
144
145
146
147
    csvParams.addItem(&csvTimestepParam);

    mqttParams.addItem(&mqttTimestepParam);
    mqttParams.addItem(&mqttCommandsParam);
    mqttParams.addItem(&mqttServerParam);
    mqttParams.addItem(&mqttPortParam);
    mqttParams.addItem(&mqttUserParam);
    mqttParams.addItem(&mqttPasswordParam);

Eric Duminil's avatar
Eric Duminil committed
148
149
    iotWebConf->addParameterGroup(&serviceParams);
    iotWebConf->addParameterGroup(&co2Params);
Eric Duminil's avatar
Eric Duminil committed
150
151
    iotWebConf->addParameterGroup(&csvParams);
    iotWebConf->addParameterGroup(&mqttParams);
Eric Duminil's avatar
Eric Duminil committed
152
153
154

    sensor_console::defineCommand("reset_config", []() {
      Serial.println(F("Resetting config..."));
Eric Duminil's avatar
Notes    
Eric Duminil committed
155
      //TODO: Reset every group
Eric Duminil's avatar
Eric Duminil committed
156
157
158
159
160
      iotWebConf->getSystemParameterGroup()->applyDefaultValue();
      iotWebConf->saveConfig();
      Serial.println(F("Done!"));
    }, F("(resets the complete IotWeb config)"));

Eric Duminil's avatar
Eric Duminil committed
161
162
163
164
165
166
167
168
    sensor_console::defineStringCommand("conf", [](char *new_value) {
      Serial.print(F("Setting stringParam to "));
      Serial.println(new_value);
      strncpy(config::stringParam.value(), new_value, STRING_LEN);
      Serial.println(F("Done"));
      iotWebConf->saveConfig();
    }, F("some_text (config setter)"));

Eric Duminil's avatar
Eric Duminil committed
169
170
171
172
173
#if !defined(AMPEL_WIFI)
    iotWebConf->loadConfig();
    return;
#endif

174
175
176
177
178
179
180
181
182
    const int ONBOARD_LED_PIN = 2;
# ifdef ESP8266
    iotWebConf->setStatusPin(ONBOARD_LED_PIN, LOW);
# else
    iotWebConf->setStatusPin(ONBOARD_LED_PIN, HIGH);
# endif
    iotWebConf->setWifiConnectionTimeoutMs(1000UL * WIFI_TIMEOUT);

#if defined(ESP8266)
Eric Duminil's avatar
Eric Duminil committed
183
  WiFi.hostname(ampel.sensorId);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#elif defined(ESP32)
    WiFi.setHostname(ampel.sensorId);
#endif

    iotWebConf->skipApStartup();
    //TODO: Add callbacks
    //TODO: Add LED effects
    //TODO: Allow offline config loading
    //TODO: Add default values for SSID/password
    //TODO: Add other params
    //TODO: Use HTTP_USER / HTTP_PASSWORD for config
    //TODO: Move to own class
    //TODO: Remove AP Password config?
    //TODO: Save LoRaWAN key if possible?
    //FIXME: Why does MQTT fail? (on ESP32)

    iotWebConf->init();

Eric Duminil's avatar
Eric Duminil committed
202
203
    //TODO: Authenticate only if required?
    //TODO: / captive fast return?
204
205
206
207

    http.on("/config", [] {
      iotWebConf->handleConfig();
    });
Eric Duminil's avatar
Eric Duminil committed
208

209
210
211
212
213
    http.onNotFound([]() {
      iotWebConf->handleNotFound();
    });
  }
}