From 01ad5633daa5242d969552fb41d051f4b2cef0c3 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Mon, 7 Feb 2022 20:39:56 +0100
Subject: [PATCH] Fixing lorawan config and refactor

---
 ampel-firmware/config.public.h |  2 +-
 ampel-firmware/lorawan.cpp     | 22 +++++++++++-----------
 ampel-firmware/lorawan.h       |  1 +
 ampel-firmware/web_server.cpp  |  2 +-
 4 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/ampel-firmware/config.public.h b/ampel-firmware/config.public.h
index 71df084..cf0325f 100644
--- a/ampel-firmware/config.public.h
+++ b/ampel-firmware/config.public.h
@@ -119,7 +119,7 @@
  */
 
 // 1) Requires "MCCI LoRaWAN LMIC library", which will be automatically used with PlatformIO but should be added in "Arduino IDE".
-// 2) If you really want, region and transceiver type can be specified in lorawan.cpp
+// 2) If you need to, region and transceiver type can be specified in lorawan.cpp. Default is "Europe 868"
 // 3) It has been tested with "TTGO ESP32 SX1276 LoRa 868" and will only work with an ESP32 + LoRa modem
 // 4) In order to use LoRaWAN, a gateway should be close to the co2ampel, and an account, an application and a device should be registered,
 //      e.g. on https://www.thethingsnetwork.org/docs/applications/
diff --git a/ampel-firmware/lorawan.cpp b/ampel-firmware/lorawan.cpp
index c29b312..7bc1e3d 100644
--- a/ampel-firmware/lorawan.cpp
+++ b/ampel-firmware/lorawan.cpp
@@ -20,24 +20,22 @@
 #include <hal/hal.h>
 #include <arduino_lmic_hal_boards.h>
 
-// Check that a Region was defined
+namespace config {
 #if defined(CFG_eu868)
-#  define LMIC_FREQUENCY_PLAN "Europe 868"
+  const char *lorawan_frequency_plan = "Europe 868";
 #elif defined(CFG_us915)
-#  define LMIC_FREQUENCY_PLAN "US 915"
+  const char *lorawan_frequency_plan = "US 915";
 #elif defined(CFG_au915)
-#  define LMIC_FREQUENCY_PLAN "Australia 915"
+  const char *lorawan_frequency_plan = "Australia 915";
 #elif defined(CFG_as923)
-#  define LMIC_FREQUENCY_PLAN "Asia 923"
+  const char *lorawan_frequency_plan = "Asia 923";
 #elif defined(CFG_kr920)
-#  define LMIC_FREQUENCY_PLAN "Korea 920"
+  const char *lorawan_frequency_plan = "Korea 920";
 #elif defined(CFG_in866)
-#  define LMIC_FREQUENCY_PLAN "India 866"
+  const char *lorawan_frequency_plan = "India 866";
 #else
 #  error "Region should be specified"
 #endif
-
-namespace config {
   // Values should be defined in config.h
   uint16_t lorawan_sending_interval = LORAWAN_SENDING_INTERVAL; // [s]
 
@@ -72,7 +70,9 @@ namespace lorawan {
   char last_transmission[23] = "";
 
   void initialize() {
-    Serial.println(F("Starting LoRaWAN. Frequency plan : " LMIC_FREQUENCY_PLAN " MHz."));
+    Serial.print(F("Starting LoRaWAN. Frequency plan : "));
+    Serial.print(config::lorawan_frequency_plan);
+    Serial.println(F(" MHz."));
 
     // More info about pin mapping : https://github.com/mcci-catena/arduino-lmic#pin-mapping
     // Has been tested successfully with ESP32 TTGO LoRa32 V1, and might work with other ESP32+LoRa boards.
@@ -87,7 +87,7 @@ namespace lorawan {
   }
 
   // Checks if OTAA is connected, or if payload should be sent.
-  // NOTE: while a transaction is in process (i.e. until the TXcomplete event has been received, no blocking code (e.g. delay loops etc.) are allowed, otherwise the LMIC/OS code might miss the event.
+  // NOTE: while a transaction is in process (i.e. until the TXcomplete event has been received), no blocking code (e.g. delay loops etc.) are allowed, otherwise the LMIC/OS code might miss the event.
   // If this rule is not followed, a typical symptom is that the first send is ok and all following ones end with the 'TX not complete' failure.
   void process() {
     os_runloop_once();
diff --git a/ampel-firmware/lorawan.h b/ampel-firmware/lorawan.h
index 2903683..ed005f5 100644
--- a/ampel-firmware/lorawan.h
+++ b/ampel-firmware/lorawan.h
@@ -9,6 +9,7 @@
 
 namespace config {
   extern uint16_t lorawan_sending_interval; // [s]
+  extern const char *lorawan_frequency_plan; // e.g. "Europe 868"
 }
 
 namespace lorawan {
diff --git a/ampel-firmware/web_server.cpp b/ampel-firmware/web_server.cpp
index 75897bc..bad5faf 100644
--- a/ampel-firmware/web_server.cpp
+++ b/ampel-firmware/web_server.cpp
@@ -236,7 +236,7 @@ namespace web_server {
         mqtt::connected ? "Yes" : "No", mqtt::last_successful_publish, config::mqtt_sending_interval,
 #endif
 #if defined(AMPEL_LORAWAN) && defined(ESP32)
-        lorawan::connected ? "Yes" : "No", LMIC_FREQUENCY_PLAN, lorawan::last_transmission,
+        lorawan::connected ? "Yes" : "No", config::lorawan_frequency_plan, lorawan::last_transmission,
         config::lorawan_sending_interval,
 #endif
         config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", ampel.sensorId, ampel.sensorId,
-- 
GitLab