web_config.cpp 5.24 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();
}

8
9
10
11
12
13
14
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
15
  DNSServer dnsServer; //TODO: Check if needed
16
17
18
19

  IotWebConf *iotWebConf;

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

  static const char chooserValues[][STRING_LEN] = { "red", "blue", "darkYellow" };
  static const char chooserNames[][STRING_LEN] = { "Red", "Blue", "Dark yellow" };

Eric Duminil's avatar
Eric Duminil committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  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();

  iotwebconf::ParameterGroup co2Params = iotwebconf::ParameterGroup("co2", "CO2 Sensor");

  //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").build();

47
48
49
50
51
52
53
54
55
56
57
58
  iotwebconf::ParameterGroup group2 = iotwebconf::ParameterGroup("c_factor", "Calibration factor");
  iotwebconf::CheckboxTParameter checkboxParam =
      iotwebconf::Builder<iotwebconf::CheckboxTParameter>("checkParam").label("Check param").defaultValue(true).build();
  iotwebconf::SelectTParameter<STRING_LEN> chooserParam =
      iotwebconf::Builder<iotwebconf::SelectTParameter< STRING_LEN>>("chooseParam").label("Choose param").optionValues(
          (const char*) chooserValues).optionNames((const char*) chooserNames).optionCount(
          sizeof(chooserValues) / STRING_LEN).nameLength(STRING_LEN).build();

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

Eric Duminil's avatar
Eric Duminil committed
59
60
61
62
63
64
65
66
67
68
69
  void setWifiConnectionCallback(void (*function)()) {
    iotWebConf->setWifiConnectionCallback(function);
  }

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

70
71
72
  void initialize() {
    iotWebConf = new IotWebConf(ampel.sensorId, &dnsServer, &http, HTTP_PASSWORD, config_version);

Eric Duminil's avatar
Eric Duminil committed
73
74
75
76
77
78
79
    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
80

Eric Duminil's avatar
Eric Duminil committed
81
82
    iotWebConf->addParameterGroup(&serviceParams);
    iotWebConf->addParameterGroup(&co2Params);
Eric Duminil's avatar
Eric Duminil committed
83
84
85
86
87
88
89
90

    sensor_console::defineCommand("reset_config", []() {
      Serial.println(F("Resetting config..."));
      iotWebConf->getSystemParameterGroup()->applyDefaultValue();
      iotWebConf->saveConfig();
      Serial.println(F("Done!"));
    }, F("(resets the complete IotWeb config)"));

Eric Duminil's avatar
Eric Duminil committed
91
92
93
94
95
96
97
98
    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
99
100
101
102
103
#if !defined(AMPEL_WIFI)
    iotWebConf->loadConfig();
    return;
#endif

104
105
106
107
108
109
110
111
112
    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
113
  WiFi.hostname(ampel.sensorId);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#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
132
133
    //TODO: Authenticate only if required?
    //TODO: / captive fast return?
134
135
136
137

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

139
140
141
142
143
    http.onNotFound([]() {
      iotWebConf->handleNotFound();
    });
  }
}