From da1be5d7b3eb95d9db0c913f3af50fc9c92b3288 Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan@faked.org>
Date: Sun, 2 Aug 2020 02:28:07 +0200
Subject: [PATCH] more cleanup LoRa retry causes "CORRUPT HEAP"

---
 include/config.h.sample |  1 +
 include/power.h         |  1 -
 platformio.ini          |  2 +-
 src/gps.cpp             |  3 ++-
 src/lora.cpp            | 18 ++++++++++++++++--
 src/main.cpp            |  3 ++-
 src/power.cpp           | 11 +++--------
 7 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/include/config.h.sample b/include/config.h.sample
index d342c1d..299492a 100644
--- a/include/config.h.sample
+++ b/include/config.h.sample
@@ -1,6 +1,7 @@
 #define SLEEP_SECONDS       900     // 15 minutes
 #define GPS_INT_SECONDS     1
 #define GPS_WAIT_SECONDS    120
+#define LORA_RETRY          3
 
 #define TTN_DEVEUI { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };  // LSB
 #define TTN_APPEUI { 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0xD5, 0xB3, 0x70 };  // LSB
diff --git a/include/power.h b/include/power.h
index bad4546..1e4943c 100644
--- a/include/power.h
+++ b/include/power.h
@@ -2,7 +2,6 @@
 
 void powerLed(axp_chgled_mode_t);
 float powerGetBattVoltage();
-void powerSetOutput(uint8_t channel, bool enabled);
 void powerStatus();
 void powerSetup();
 void powerSleep();
diff --git a/platformio.ini b/platformio.ini
index 274a24b..a75b147 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -20,7 +20,7 @@ build_flags =
   -D ARDUINO_LMIC_CFG_NETWORK_TTN
   -D CFG_sx1276_radio
   -D CFG_eu868
-  -D LMIC_DEBUG_LEVEL=0
+  -D LMIC_DEBUG_LEVEL=2
 lib_deps =
   AXP202X_Library@1.1.2
   TinyGPSPlus@1.0.2
diff --git a/src/gps.cpp b/src/gps.cpp
index 8d59ca0..1c41ca9 100644
--- a/src/gps.cpp
+++ b/src/gps.cpp
@@ -4,7 +4,7 @@
 #include <TinyGPS++.h>
 
 HardwareSerial GPS(1);
-TinyGPSPlus gps;
+extern TinyGPSPlus gps;
 extern int state;
 unsigned long gpsTimer;
 unsigned long gpsCount;
@@ -29,6 +29,7 @@ void gpsStatus()
     Serial.printf("Time       : %02d:%02d:%02d\n", gps.time.hour(), gps.time.minute(), gps.time.second());
     Serial.printf("Speed      : %g\n", gps.speed.kmph());
     Serial.println("#########################\n");
+    gpsCount = 0;
 }
 
 void gpsSetup()
diff --git a/src/lora.cpp b/src/lora.cpp
index 0ea3cb0..2530b9f 100644
--- a/src/lora.cpp
+++ b/src/lora.cpp
@@ -7,6 +7,7 @@
 #include <CayenneLPP.h>
 
 extern int state;
+int retry;
 
 class cLoRaWAN : public Arduino_LoRaWAN_ttn
 {
@@ -105,6 +106,7 @@ void onEvent(ev_t ev)
         break;
     case EV_JOINING:
         Serial.println(F("EV_JOINING"));
+        retry = 0;
         break;
     case EV_JOINED:
         Serial.println(F("EV_JOINED"));
@@ -159,8 +161,20 @@ void onEvent(ev_t ev)
         break;
     case EV_JOIN_TXCOMPLETE:
         Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
-        // restart sending
-        state = STATE_READY;
+        if (retry < LORA_RETRY)
+        {
+            //Serial.printf("\nLoRa Join failed, %d retries left.\n\n", LORA_RETRY - retry);
+            // TODO: this will cause "CORRUPT HEAP: Bad head at / assertion "head != NULL" failed / abort() was called"
+            //state = STATE_INIT;
+            
+            Serial.println("\nLoRa Join failed.");
+            state = STATE_DONE;
+        }
+        else
+        {
+            Serial.println("\nLoRa Join failed, no retries left.");
+            state = STATE_DONE;
+        }
         break;
     default:
         Serial.println(F("Unknown event " + (unsigned)ev));
diff --git a/src/main.cpp b/src/main.cpp
index 1c67022..ad5d3a9 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -8,11 +8,12 @@
 
 int state;
 CayenneLPP payload(55);
+TinyGPSPlus gps;
 
 void createPayload()
 {
     payload.reset();
-    TinyGPSPlus gps = getGps();
+    gps = getGps();
     payload.addGPS(1, gps.location.lat(), gps.location.lng(), gps.altitude.meters());
     payload.addAnalogInput(2, powerGetBattVoltage() / 1000);
 }
diff --git a/src/power.cpp b/src/power.cpp
index 8215256..bbf29c4 100644
--- a/src/power.cpp
+++ b/src/power.cpp
@@ -15,11 +15,6 @@ float powerGetBattVoltage()
     return axp.getBattVoltage();
 }
 
-void powerSetOutput(uint8_t channel, bool enabled)
-{
-    axp.setPowerOutPut(channel, enabled);
-}
-
 void powerStatus()
 {
     Serial.println("\n########## Power ##########");
@@ -78,9 +73,9 @@ void powerSleep()
 {
     Serial.printf("\nSystem has been up for %llu seconds.\n", esp_timer_get_time()/1000000);
     Serial.printf("Going to sleep for %d seconds.\n", SLEEP_SECONDS);
-    powerSetOutput(AXP192_LDO2, AXP202_OFF); // LORA : off
-    powerSetOutput(AXP192_LDO3, AXP202_OFF); // GPS  : off
-    powerLed(AXP20X_LED_OFF);
+    axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // LORA : off
+    axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // GPS  : off
+    axp.setChgLEDMode(AXP20X_LED_OFF);
     esp_sleep_enable_ext0_wakeup(GPIO_NUM_38, LOW); // wake up with "user" button (middle)
     esp_sleep_enable_timer_wakeup(1000000 * SLEEP_SECONDS);
     Serial.println("\n### END ###");
-- 
GitLab