From 15feb027f59ab54896dd74c416b2751a92cdb02a Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Tue, 22 Feb 2022 13:12:00 +0100
Subject: [PATCH] Do not stay in Access Point mode indefinitely

---
 ampel-firmware/ampel-firmware.ino | 1 +
 ampel-firmware/config.public.h    | 5 +++++
 ampel-firmware/web_config.cpp     | 7 ++++++-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/ampel-firmware/ampel-firmware.ino b/ampel-firmware/ampel-firmware.ino
index 6ad9154..da1f42b 100644
--- a/ampel-firmware/ampel-firmware.ino
+++ b/ampel-firmware/ampel-firmware.ino
@@ -181,6 +181,7 @@ void wifiConnected() {
 }
 
 void wifiFailed() {
+  // Ampel will go back to Access Point mode for AP_TIMEOUT seconds, and try connection again after
   Serial.print(F("WiFi - Could not connect to "));
   Serial.println(config::selected_ssid());
   led_effects::showKITTWheel(color::red);
diff --git a/ampel-firmware/config.public.h b/ampel-firmware/config.public.h
index 8839e26..6285ba6 100644
--- a/ampel-firmware/config.public.h
+++ b/ampel-firmware/config.public.h
@@ -44,7 +44,12 @@
 // SSID and PASSWORD need to be defined, but can be empty.
 #  define WIFI_SSID     ""
 #  define WIFI_PASSWORD ""
+// How long should the Ampel try to connect to WIFI_SSID?
 #  define WIFI_TIMEOUT  30 // [s]
+// If the Ampel cannot connect to WIFI_SSID, it will start an Access Point for ACCESS_POINT_TIMEOUT seconds.
+// If someone connects to this Access Point, the Ampel will stay in this mode until everybody logs out.
+// If nobody connects to the Access Point before ACCESS_POINT_TIMEOUT seconds, the Ampel will try to connect WIFI_SSID again.
+#  define ACCESS_POINT_TIMEOUT  30 // [s]
 
 /**
  * Sensor
diff --git a/ampel-firmware/web_config.cpp b/ampel-firmware/web_config.cpp
index f144355..f8aac2d 100644
--- a/ampel-firmware/web_config.cpp
+++ b/ampel-firmware/web_config.cpp
@@ -2,6 +2,9 @@
 
 #define STRING_LEN 33 // Should be enough for ip, addresses, passwords...
 
+#define STRING(x) #x
+#define STRINGIFY(x) STRING(x) // Allows to call STRINGIFY(SOME_MACRO) and convert the value of SOME_MACRO to string
+
 #include "config.h"
 #ifndef AMPEL_PASSWORD
 #  error Missing config.h file. Please copy config.public.h to config.h.
@@ -158,7 +161,6 @@ namespace web_config {
   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);
@@ -172,10 +174,13 @@ namespace web_config {
 
     iotWebConf->getThingNameParameter()->label = "Ampel name";
     iotWebConf->getApPasswordParameter()->label = "Ampel password (user : 'admin')";
+    iotWebConf->getApTimeoutParameter()->label = "Access Point timeout";
+    iotWebConf->getApTimeoutParameter()->visible = true;
 
     iotWebConf->getApPasswordParameter()->defaultValue = AMPEL_PASSWORD;
     iotWebConf->getWifiSsidParameter()->defaultValue = WIFI_SSID;
     iotWebConf->getWifiPasswordParameter()->defaultValue = WIFI_PASSWORD;
+    iotWebConf->getApTimeoutParameter()->defaultValue = STRINGIFY(ACCESS_POINT_TIMEOUT); // Defined as number in config.h but stored as string in webconf.
 
     co2Params.addItem(&timestepParam);
     co2Params.addItem(&temperatureOffsetParam);
-- 
GitLab