From 63f78eead51b28dcad424e8bfe58848312af2ff8 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Sat, 15 May 2021 23:38:11 +0200
Subject: [PATCH] Trying to use a config

---
 ampel-firmware/ampel-firmware.ino |  3 ++
 ampel-firmware/web_config.cpp     | 57 +++++++++++++++----------------
 ampel-firmware/web_config.h       |  6 ++++
 3 files changed, 36 insertions(+), 30 deletions(-)

diff --git a/ampel-firmware/ampel-firmware.ino b/ampel-firmware/ampel-firmware.ino
index 370541a..522aa6d 100644
--- a/ampel-firmware/ampel-firmware.ino
+++ b/ampel-firmware/ampel-firmware.ino
@@ -112,6 +112,9 @@ void setup() {
 
   sensor::initialize();
 
+  Serial.print("JUST A CONFIG TEST : ");
+  Serial.println(config::stringParam.value());
+
 #ifdef AMPEL_CSV
   csv_writer::initialize(ampel.sensorId);
 #endif
diff --git a/ampel-firmware/web_config.cpp b/ampel-firmware/web_config.cpp
index 7c844ea..2373514 100644
--- a/ampel-firmware/web_config.cpp
+++ b/ampel-firmware/web_config.cpp
@@ -1,5 +1,10 @@
 #include "web_config.h"
 
+namespace config {
+  iotwebconf::TextTParameter<STRING_LEN> stringParam = iotwebconf::Builder<iotwebconf::TextTParameter< STRING_LEN>>(
+      "stringParam").label("String param").build();
+}
+
 namespace web_config {
 #if defined(ESP8266)
   ESP8266WebServer http(80); // Create a webserver object that listens for HTTP request on port 80
@@ -11,16 +16,12 @@ namespace web_config {
 
   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(
@@ -54,6 +55,27 @@ namespace web_config {
   void initialize() {
     iotWebConf = new IotWebConf(ampel.sensorId, &dnsServer, &http, HTTP_PASSWORD, config_version);
 
+    group1.addItem(&intParam);
+    group2.addItem(&floatParam);
+    group2.addItem(&checkboxParam);
+    group2.addItem(&chooserParam);
+
+    iotWebConf->addSystemParameter(&config::stringParam);
+    iotWebConf->addParameterGroup(&group1);
+    iotWebConf->addParameterGroup(&group2);
+
+    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)"));
+
+#if !defined(AMPEL_WIFI)
+    iotWebConf->loadConfig();
+    return;
+#endif
+
     const int ONBOARD_LED_PIN = 2;
 # ifdef ESP8266
     iotWebConf->setStatusPin(ONBOARD_LED_PIN, LOW);
@@ -63,20 +85,11 @@ namespace web_config {
     iotWebConf->setWifiConnectionTimeoutMs(1000UL * WIFI_TIMEOUT);
 
 #if defined(ESP8266)
-    WiFi.hostname(ampel.sensorId);
+  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
@@ -89,23 +102,7 @@ namespace web_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)"));
 
     //TODO: Authenticate only if required?
     //TODO: / captive fast return?
diff --git a/ampel-firmware/web_config.h b/ampel-firmware/web_config.h
index 03a56d3..91e4077 100644
--- a/ampel-firmware/web_config.h
+++ b/ampel-firmware/web_config.h
@@ -7,6 +7,8 @@
 #  include <WebServer.h>
 #endif
 
+#define STRING_LEN 64
+
 //#include "config.h"
 
 #include <IotWebConf.h>
@@ -15,6 +17,10 @@
 #include "util.h"
 //#include "sensor_console.h"
 
+namespace config {
+  extern iotwebconf::TextTParameter<STRING_LEN> stringParam;
+}
+
 namespace web_config {
   void initialize();
   void setWifiConnectionCallback(void (*function)());
-- 
GitLab