web_config.cpp 4.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
#include "web_config.h"

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
10
  DNSServer dnsServer; //TODO: Check if needed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

  IotWebConf *iotWebConf;

#define STRING_LEN 64

// -- Configuration specific key. The value should be modified if config structure was changed.
  const char config_version[] = "ampel_test_v3";

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

  iotwebconf::TextTParameter<STRING_LEN> stringParam = iotwebconf::Builder<iotwebconf::TextTParameter< STRING_LEN>>(
      "stringParam").label("String param").build();
  iotwebconf::ParameterGroup group1 = iotwebconf::ParameterGroup("group1", "");
  iotwebconf::IntTParameter<int16_t> intParam =
      iotwebconf::Builder<iotwebconf::IntTParameter<int16_t>>("intParam").label("Int param").defaultValue(30).min(1).max(
          100).step(1).placeholder("1..100").build();
// -- We can add a legend to the separator
  iotwebconf::ParameterGroup group2 = iotwebconf::ParameterGroup("c_factor", "Calibration factor");
  iotwebconf::FloatTParameter floatParam = iotwebconf::Builder<iotwebconf::FloatTParameter>("floatParam").label(
      "Float param").defaultValue(0.0).step(0.1).placeholder("e.g. 23.4").build();
  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
43
44
45
46
47
48
49
50
51
52
53
  void setWifiConnectionCallback(void (*function)()) {
    iotWebConf->setWifiConnectionCallback(function);
  }

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

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
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
  void initialize() {
    iotWebConf = new IotWebConf(ampel.sensorId, &dnsServer, &http, HTTP_PASSWORD, config_version);

    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)
    WiFi.hostname(ampel.sensorId);
#elif defined(ESP32)
    WiFi.setHostname(ampel.sensorId);
#endif

    group1.addItem(&intParam);
    group2.addItem(&floatParam);
    group2.addItem(&checkboxParam);
    group2.addItem(&chooserParam);

    iotWebConf->addSystemParameter(&stringParam);
    iotWebConf->addParameterGroup(&group1);
    iotWebConf->addParameterGroup(&group2);

    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->loadConfig();
    Serial.println("<<<<<<<<<<<<<<<");
    Serial.println(stringParam.value());
    Serial.println(intParam.value());
    Serial.println(floatParam.value());
    iotWebConf->init();
    Serial.println(stringParam.value());
    Serial.println(intParam.value());
    Serial.println(floatParam.value());
    Serial.println(">>>>>>>>>>>>>>>");

    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
110
111
    //TODO: Authenticate only if required?
    //TODO: / captive fast return?
112
113
114
115

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

117
118
119
120
121
    http.onNotFound([]() {
      iotWebConf->handleNotFound();
    });
  }
}