From fadda5d24173e350d6ca8086c108cd07f022dd2c Mon Sep 17 00:00:00 2001 From: Jan Grewe <jan@faked.org> Date: Fri, 31 Jul 2020 22:32:00 +0200 Subject: [PATCH] use button to go to sleep use state switch instead of if/else disable LMIC debug --- platformio.ini | 2 +- src/config.h.sample | 2 +- src/main.cpp | 66 ++++++++++++++++++++++++++------------------- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/platformio.ini b/platformio.ini index a75b147..274a24b 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=2 + -D LMIC_DEBUG_LEVEL=0 lib_deps = AXP202X_Library@1.1.2 TinyGPSPlus@1.0.2 diff --git a/src/config.h.sample b/src/config.h.sample index a457d04..2606b70 100644 --- a/src/config.h.sample +++ b/src/config.h.sample @@ -1,4 +1,4 @@ -#define TIME_TO_SLEEP 900 // 15 minutes +#define TIME_SLEEP 900 // 15 minutes #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/src/main.cpp b/src/main.cpp index b7e847c..f84574a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,11 @@ #include <CayenneLPP.h> #include <OneButton.h> +#define STATE_WAIT 1 +#define STATE_READY 2 +#define STATE_SENDING 3 +#define STATE_DONE 4 + class cLoRaWAN : public Arduino_LoRaWAN_ttn { public: @@ -62,18 +67,16 @@ AXP20X_Class axp; HardwareSerial GPS(1); TinyGPSPlus gps; cLoRaWAN LoRaWAN{}; +OneButton btn = OneButton(GPIO_NUM_38, true, true); CayenneLPP lpp(55); DynamicJsonDocument jsonBuffer(1024); -bool gpsFix = false; -bool statusSending = false; -bool statusSent = false; +int state; unsigned long timerGpsFix; RTC_DATA_ATTR int bootCount = 0; void onEvent(ev_t ev) { - Serial.print(os_getTime()); - Serial.print(": "); + Serial.printf("%d: ", os_getTime()); switch (ev) { case EV_SCAN_TIMEOUT: @@ -115,9 +118,8 @@ void onEvent(ev_t ev) Serial.println(LMIC.dataLen); Serial.println(F(" bytes of payload")); } - // this allows going to deep sleep - statusSending = false; - statusSent = true; + // go to sleep + state = STATE_DONE; break; case EV_LOST_TSYNC: Serial.println(F("EV_LOST_TSYNC")); @@ -145,9 +147,8 @@ void onEvent(ev_t ev) break; case EV_JOIN_TXCOMPLETE: Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept")); - // this allows restarting - statusSending = false; - statusSent = false; + // restart sending + state = STATE_READY; break; default: Serial.println(F("Unknown event " + (unsigned)ev)); @@ -194,7 +195,6 @@ void printGpsStatus() } Serial.println("##############################"); Serial.println("--- GPS ---"); - Serial.printf("Has Fix : %s\n", gps.satellites.isValid() ? "true" : "false"); Serial.printf("Latitude : %f\n", gps.location.lat()); Serial.printf("Longitude : %f\n", gps.location.lng()); Serial.printf("Satellites : %d\n", gps.satellites.value()); @@ -233,7 +233,7 @@ void setupGps() GPS.begin(9600, SERIAL_8N1, 34, 12); // IO34: RX, IO12: TX } -bool checkGpsFix() +void checkGpsFix() { if (millis() - timerGpsFix >= 1000 * 1) // 1 second { @@ -242,16 +242,13 @@ bool checkGpsFix() { gps.encode(GPS.read()); } - if (gps.satellites.value() >= 3 && - gps.satellites.isValid() && - gps.location.isValid()) + if (gps.location.isValid()) { printGpsStatus(); - return true; + state = STATE_READY; } timerGpsFix = millis(); } - return false; } void createPayload() @@ -275,12 +272,12 @@ void sendPayload() if (LMIC.opmode & OP_TXRXPEND) { Serial.println(F("OP_TXRXPEND, not sending")); + state = STATE_READY; } else { LMIC_setTxData2(1, lpp.getBuffer(), lpp.getSize(), 0); Serial.println(F("Packet queued")); - Serial.println(); } } @@ -290,7 +287,7 @@ void sendStatus() axp.setChgLEDMode(AXP20X_LED_BLINK_4HZ); createPayload(); sendPayload(); - statusSending = true; + state = STATE_SENDING; } void doSleep() @@ -304,6 +301,11 @@ void doSleep() esp_deep_sleep_start(); } +static void handleClick() +{ + doSleep(); +} + void print_wakeup_reason() { esp_sleep_wakeup_cause_t wakeup_reason; @@ -343,24 +345,32 @@ void setup() setupPower(); setupGps(); LoRaWAN.begin(pinMap); + btn.attachClick(handleClick); timerGpsFix = millis(); axp.setChgLEDMode(AXP20X_LED_BLINK_1HZ); + state = STATE_WAIT; } void loop() { + btn.tick(); LoRaWAN.loop(); - if (!gpsFix) - { - gpsFix = checkGpsFix(); - } - else if (gpsFix && !statusSending && !statusSent) + switch (state) { + case STATE_WAIT: + checkGpsFix(); + break; + case STATE_READY: sendStatus(); - } - else if (statusSent) - { + break; + case STATE_SENDING: + // idle + break; + case STATE_DONE: doSleep(); + break; + default: + break; } } -- GitLab