From 1e9fedcbd7f9065773e151f521f9619882ea9ef4 Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 17 Jan 2020 15:15:00 -0800 Subject: [PATCH 01/32] minor changes needed to build with platformio I prefer platformio over the arduino IDE because it automatically does library dependencies and has lots of other goodness. To build and install to a connected device just do: pio run --- .gitignore | 2 +- main/main.ino | 2 ++ platformio.ini | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 platformio.ini diff --git a/.gitignore b/.gitignore index 5dc1407..63dbb54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ - +.pio main/configuration.h main/credentials.h diff --git a/main/main.ino b/main/main.ino index b848b23..18d973b 100644 --- a/main/main.ino +++ b/main/main.ino @@ -51,6 +51,8 @@ RTC_DATA_ATTR uint32_t count = 0; // Application // ----------------------------------------------------------------------------- +void buildPacket(uint8_t txBuffer[]); // needed for platformio + void send() { char buffer[40]; snprintf(buffer, sizeof(buffer), "Latitude: %10.6f\n", gps_latitude()); diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..1bb135b --- /dev/null +++ b/platformio.ini @@ -0,0 +1,34 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[platformio] +src_dir = main + +[env:esp32] +platform = espressif32 +board = ttgo-t-beam +framework = arduino + +; note: we add src to our include search path so that lmic_project_config can override +build_flags = -Wall -Wextra -O3 -Wl,-Map,.pio/build/esp32/output.map -D CFG_us915 -D CFG_sx1276_radio +; -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG + +lib_deps = + arduino-lmic + TinyGPSPlus + ESP8266_SSD1306 + AXP202X_Library + +; Adafruit ILI9341@1.2.0 +; AutoConnect +; ArduinoJson@5.13.4 +; Micro-RTSP@>=0.1.6 +; Adafruit-GFX-Library=https://github.com/geeksville/Adafruit-GFX-Library.git + From e36d706c9559b3d2a1b7cee5cdbe6d4addfbf5e0 Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 17 Jan 2020 15:16:42 -0800 Subject: [PATCH 02/32] add vscode workspace definitions --- .gitignore | 8 ++++++++ .vscode/extensions.json | 7 +++++++ .vscode/launch.json | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 .vscode/extensions.json create mode 100644 .vscode/launch.json diff --git a/.gitignore b/.gitignore index 63dbb54..171f0f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,11 @@ .pio main/configuration.h main/credentials.h + +# ignore vscode IDE settings files +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..8281e64 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..522266b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,32 @@ +// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY + +// PIO Unified Debugger +// +// Documentation: https://docs.platformio.org/page/plus/debugging.html +// Configuration: https://docs.platformio.org/page/projectconf/section_env_debug.html + +{ + "version": "0.2.0", + "configurations": [ + { + "type": "platformio-debug", + "request": "launch", + "name": "PIO Debug", + "executable": "/home/kevinh/development/lora/ttgo-tbeam-ttn-tracker/.pio/build/esp32/firmware.elf", + "toolchainBinDir": "/home/kevinh/.platformio/packages/toolchain-xtensa32/bin", + "preLaunchTask": { + "type": "PlatformIO", + "task": "Pre-Debug" + }, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "type": "platformio-debug", + "request": "launch", + "name": "PIO Debug (skip Pre-Debug)", + "executable": "/home/kevinh/development/lora/ttgo-tbeam-ttn-tracker/.pio/build/esp32/firmware.elf", + "toolchainBinDir": "/home/kevinh/.platformio/packages/toolchain-xtensa32/bin", + "internalConsoleOptions": "openOnSessionStart" + } + ] +} \ No newline at end of file From a1166364a72bf035519664b615c0df3f92b5319f Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 17 Jan 2020 15:19:18 -0800 Subject: [PATCH 03/32] altitudeGps is an integer, fix the format string. /home/kevinh/development/lora/ttgo-tbeam-ttn-tracker/main/gps.ino: In function 'void buildPacket(uint8_t*)': /home/kevinh/development/lora/ttgo-tbeam-ttn-tracker/main/gps.ino:83:42: warning: format '%f' expects argument of type 'double', but argument 3 has type 'int' [-Wformat=] sprintf(t, "Alt: %f", altitudeGps); --- main/gps.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/gps.ino b/main/gps.ino index ca40a8f..69e4d6a 100644 --- a/main/gps.ino +++ b/main/gps.ino @@ -80,7 +80,7 @@ static void gps_loop() { Serial.println(t); sprintf(t, "Lng: %f", _gps.location.lng()); Serial.println(t); - sprintf(t, "Alt: %f", altitudeGps); + sprintf(t, "Alt: %d", altitudeGps); Serial.println(t); sprintf(t, "Hdop: %d", hdopGps); Serial.println(t); From bb01201e57d7ed4367f002cb795755d67d16392f Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 17 Jan 2020 15:22:03 -0800 Subject: [PATCH 04/32] we don't want warnings about missing initalizers --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 1bb135b..1244ecb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,7 +17,7 @@ board = ttgo-t-beam framework = arduino ; note: we add src to our include search path so that lmic_project_config can override -build_flags = -Wall -Wextra -O3 -Wl,-Map,.pio/build/esp32/output.map -D CFG_us915 -D CFG_sx1276_radio +build_flags = -Wall -Wextra -Wno-missing-field-initializers -O3 -Wl,-Map,.pio/build/esp32/output.map -D CFG_us915 -D CFG_sx1276_radio ; -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG lib_deps = From 6f2fd78ae2e5e8db5b271e9abfc1b51c160e4366 Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 17 Jan 2020 15:37:37 -0800 Subject: [PATCH 05/32] switch to V10 BEAM, fix incorrect reset pin binding --- main/configuration.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index e02a60f..6bc406f 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -40,8 +40,8 @@ void ttn_register(void (*callback)(uint8_t message)); // ----------------------------------------------------------------------------- // Select which T-Beam board is being used. Only uncomment one. -#define T_BEAM_V07 // AKA Rev0 (first board released) -// #define T_BEAM_V10 // AKA Rev1 (second board released) +// #define T_BEAM_V07 // AKA Rev0 (first board released) +#define T_BEAM_V10 // AKA Rev1 (second board released) // Select the payload format. Change on TTN as well. Only uncomment one. #define PAYLOAD_USE_FULL @@ -129,10 +129,14 @@ void ttn_register(void (*callback)(uint8_t message)); #define MISO_GPIO 19 #define MOSI_GPIO 27 #define NSS_GPIO 18 +#if defined(T_BEAM_V10) +#define RESET_GPIO 14 +#else #define RESET_GPIO 23 +#endif #define DIO0_GPIO 26 -#define DIO1_GPIO 33 -#define DIO2_GPIO 32 +#define DIO1_GPIO 33 // Note: not really used on this board +#define DIO2_GPIO 32 // Note: not really used on this board // ----------------------------------------------------------------------------- // AXP192 (Rev1-specific options) From 6a0382cba56282e645abb45107bde624522c2c4f Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 17 Jan 2020 15:39:01 -0800 Subject: [PATCH 06/32] fix warning about nop redefinition of axp192 address --- main/configuration.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/configuration.h b/main/configuration.h index 6bc406f..feb29df 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -142,7 +142,7 @@ void ttn_register(void (*callback)(uint8_t message)); // AXP192 (Rev1-specific options) // ----------------------------------------------------------------------------- -#define AXP192_SLAVE_ADDRESS 0x34 +// #define AXP192_SLAVE_ADDRESS 0x34 // Now defined in axp20x.h #define GPS_POWER_CTRL_CH 3 #define LORA_POWER_CTRL_CH 2 #define PMU_IRQ 35 From 33483b3d5389dba09937e2735f4d4574eb4f86a3 Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 17 Jan 2020 20:49:26 -0800 Subject: [PATCH 07/32] share common init and make OTAA work correctly with a non single-ch gway --- main/main.ino | 38 +++++++++++++++++++++++++++++++++++++- main/ttn.ino | 39 ++++++++++++++++++++++----------------- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/main/main.ino b/main/main.ino index 18d973b..837640f 100644 --- a/main/main.ino +++ b/main/main.ino @@ -101,9 +101,45 @@ void sleep() { #endif } +void printHex2(unsigned v) { + v &= 0xff; + if (v < 16) + Serial.print('0'); + Serial.print(v, HEX); +} + + void callback(uint8_t message) { if (EV_JOINING == message) screen_print("Joining TTN...\n"); - if (EV_JOINED == message) screen_print("TTN joined!\n"); + if (EV_JOINED == message) { + screen_print("TTN joined!\n"); + + Serial.println(F("EV_JOINED")); + + u4_t netid = 0; + devaddr_t devaddr = 0; + u1_t nwkKey[16]; + u1_t artKey[16]; + LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey); + Serial.print("netid: "); + Serial.println(netid, DEC); + Serial.print("devaddr: "); + Serial.println(devaddr, HEX); + Serial.print("AppSKey: "); + for (size_t i=0; i Date: Fri, 17 Jan 2020 21:29:38 -0800 Subject: [PATCH 08/32] remove old settings --- platformio.ini | 5 ----- 1 file changed, 5 deletions(-) diff --git a/platformio.ini b/platformio.ini index 1244ecb..adf824b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -26,9 +26,4 @@ lib_deps = ESP8266_SSD1306 AXP202X_Library -; Adafruit ILI9341@1.2.0 -; AutoConnect -; ArduinoJson@5.13.4 -; Micro-RTSP@>=0.1.6 -; Adafruit-GFX-Library=https://github.com/geeksville/Adafruit-GFX-Library.git From 73bac3be457c6e9a66375a3f6a967b9a882bb65c Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 17 Jan 2020 21:30:41 -0800 Subject: [PATCH 09/32] we are supposed to use 0xff to mark unused gpios --- main/configuration.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index feb29df..4c9621d 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -135,8 +135,8 @@ void ttn_register(void (*callback)(uint8_t message)); #define RESET_GPIO 23 #endif #define DIO0_GPIO 26 -#define DIO1_GPIO 33 // Note: not really used on this board -#define DIO2_GPIO 32 // Note: not really used on this board +#define DIO1_GPIO 0xff // Note: not really used on this board +#define DIO2_GPIO 0xff // Note: not really used on this board // ----------------------------------------------------------------------------- // AXP192 (Rev1-specific options) From fdc0edee88b09940890122928a650ab8a69d4441 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 18 Jan 2020 12:37:39 -0800 Subject: [PATCH 10/32] make lora packet count persist in flash (also remove some unneeded init) --- main/main.ino | 18 +++--------------- main/screen.ino | 2 +- main/ttn.ino | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/main/main.ino b/main/main.ino index 837640f..360c241 100644 --- a/main/main.ino +++ b/main/main.ino @@ -36,9 +36,6 @@ String baChStatus = "No charging"; bool ssd1306_found = false; bool axp192_found = false; -// Message counter, stored in RTC memory, survives deep sleep -RTC_DATA_ATTR uint32_t count = 0; - #if defined(PAYLOAD_USE_FULL) // includes number of satellites and accuracy uint8_t txBuffer[10]; @@ -70,10 +67,7 @@ void send() { bool confirmed = false; #endif - ttn_cnt(count); ttn_send(txBuffer, sizeof(txBuffer), LORAWAN_PORT, confirmed); - - count++; } void sleep() { @@ -170,9 +164,7 @@ void callback(uint8_t message) { } } -uint32_t get_count() { - return count; -} + void scanI2Cdevice(void) { @@ -281,8 +273,8 @@ void setup() { // Init GPS gps_setup(); - // Show logo on first boot - if (0 == count) { + // Show logo on first boot after removing battery + if (ttn_get_count() == 0) { screen_print(APP_NAME " " APP_VERSION, 0, 0); screen_show_logo(); screen_update(); @@ -299,11 +291,7 @@ void setup() { ttn_register(callback); ttn_join(); - ttn_sf(LORAWAN_SF); ttn_adr(LORAWAN_ADR); - if(!LORAWAN_ADR){ - LMIC_setLinkCheckMode(0); // Link check problematic if not using ADR. Must be set after join - } } void loop() { diff --git a/main/screen.ino b/main/screen.ino index 5cd6422..d7cf520 100644 --- a/main/screen.ino +++ b/main/screen.ino @@ -35,7 +35,7 @@ void _screen_header() { char buffer[20]; // Message count - snprintf(buffer, sizeof(buffer), "#%03d", get_count() % 1000); + snprintf(buffer, sizeof(buffer), "#%03d", ttn_get_count() % 1000); display->setTextAlignment(TEXT_ALIGN_LEFT); display->drawString(0, 2, buffer); diff --git a/main/ttn.ino b/main/ttn.ino index c666f33..e059d30 100644 --- a/main/ttn.ino +++ b/main/ttn.ino @@ -27,6 +27,7 @@ along with this program. If not, see . #include #include #include +#include #include "configuration.h" #include "credentials.h" @@ -42,6 +43,10 @@ const lmic_pinmap lmic_pins = { .dio = {DIO0_GPIO, DIO1_GPIO, DIO2_GPIO}, }; + +// Message counter, stored in RTC memory, survives deep sleep. +static RTC_DATA_ATTR uint32_t count = 0; + #ifdef USE_ABP // These callbacks are only used in over-the-air activation, so they are // left empty here (we cannot leave them out completely unless @@ -96,7 +101,9 @@ void onEvent(ev_t event) { // Disable link check validation (automatically enabled // during join, but because slow data rates change max TX // size, we don't use it in this example. - LMIC_setLinkCheckMode(0); + if(!LORAWAN_ADR){ + LMIC_setLinkCheckMode(0); // Link check problematic if not using ADR. Must be set after join + } break; case EV_TXCOMPLETE: Serial.println(F("EV_TXCOMPLETE (inc. RX win. wait)")); @@ -137,7 +144,20 @@ void ttn_response(uint8_t * buffer, size_t len) { } } +// If the value for LORA packet counts is unknown, restore from flash +static void initCount() { + if(count == 0) { + Preferences p; + p.begin("lora", true); + count = p.getUInt("count", 0); + p.end(); + } +} + + bool ttn_setup() { + initCount(); + // SPI interface SPI.begin(SCK_GPIO, MISO_GPIO, MOSI_GPIO, NSS_GPIO); @@ -238,11 +258,31 @@ void ttn_adr(bool enabled) { LMIC_setLinkCheckMode(!enabled); } -void ttn_cnt(uint32_t num) { - LMIC_setSeqnoUp(num); +uint32_t ttn_get_count() { + return count; +} + +static void ttn_set_cnt() { + LMIC_setSeqnoUp(count); + + // We occasionally mirror our count to flash, to ensure that if we lose power we will at least start with a count that is almost correct + // (otherwise the TNN network will discard packets until count once again reaches the value they've seen). We limit these writes to a max rate + // of one write every 5 minutes. Which should let the FLASH last for 300 years (given the ESP32 NVS algoritm) + static uint32_t lastWriteMsec = UINT32_MAX; // Ensure we write at least once + uint32_t now = millis(); + if(now < lastWriteMsec || (now - lastWriteMsec) > 5 * 60 * 1000L) { // write if we roll over (50 days) or 5 mins + lastWriteMsec = now; + + Preferences p; + p.begin("lora", false); + p.putUInt("count", count); + p.end(); + } } void ttn_send(uint8_t * data, uint8_t data_size, uint8_t port, bool confirmed){ + ttn_set_cnt(); // we are about to send using the current packet count + // Check if there is not a current TX/RX job running if (LMIC.opmode & OP_TXRXPEND) { _ttn_callback(EV_PENDING); @@ -254,6 +294,7 @@ void ttn_send(uint8_t * data, uint8_t data_size, uint8_t port, bool confirmed){ LMIC_setTxData2(port, data, data_size, confirmed ? 1 : 0); _ttn_callback(EV_QUEUED); + count++; } void ttn_loop() { From c263355adad601582d8a049a8c8a27ccdf66be44 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 18 Jan 2020 13:21:00 -0800 Subject: [PATCH 11/32] Store joinned network info in flash, so that OTAA will work nicely --- main/main.ino | 33 +------------------- main/ttn.ino | 82 ++++++++++++++++++++++++++++++++++++++++++++------ platformio.ini | 2 +- 3 files changed, 74 insertions(+), 43 deletions(-) diff --git a/main/main.ino b/main/main.ino index 360c241..6e39b7c 100644 --- a/main/main.ino +++ b/main/main.ino @@ -95,44 +95,13 @@ void sleep() { #endif } -void printHex2(unsigned v) { - v &= 0xff; - if (v < 16) - Serial.print('0'); - Serial.print(v, HEX); -} + void callback(uint8_t message) { if (EV_JOINING == message) screen_print("Joining TTN...\n"); if (EV_JOINED == message) { screen_print("TTN joined!\n"); - - Serial.println(F("EV_JOINED")); - - u4_t netid = 0; - devaddr_t devaddr = 0; - u1_t nwkKey[16]; - u1_t artKey[16]; - LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey); - Serial.print("netid: "); - Serial.println(netid, DEC); - Serial.print("devaddr: "); - Serial.println(devaddr, HEX); - Serial.print("AppSKey: "); - for (size_t i=0; i Date: Sat, 18 Jan 2020 13:28:12 -0800 Subject: [PATCH 12/32] oops - though these pins are no connects, we need to lie to the lmic library because it has an assertion check that will fail. --- main/configuration.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index 4c9621d..feb29df 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -135,8 +135,8 @@ void ttn_register(void (*callback)(uint8_t message)); #define RESET_GPIO 23 #endif #define DIO0_GPIO 26 -#define DIO1_GPIO 0xff // Note: not really used on this board -#define DIO2_GPIO 0xff // Note: not really used on this board +#define DIO1_GPIO 33 // Note: not really used on this board +#define DIO2_GPIO 32 // Note: not really used on this board // ----------------------------------------------------------------------------- // AXP192 (Rev1-specific options) From 4018141a45aa5a7606da13985b07348ff2f9e375 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 18 Jan 2020 14:11:00 -0800 Subject: [PATCH 13/32] generate DevEUI based on macaddr if not defined --- main/credentials.h | 7 +++--- main/ttn.ino | 55 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/main/credentials.h b/main/credentials.h index b9e746d..6d91159 100644 --- a/main/credentials.h +++ b/main/credentials.h @@ -7,8 +7,8 @@ Credentials file #pragma once // Only one of these settings must be defined -#define USE_ABP -//#define USE_OTAA +//#define USE_ABP +#define USE_OTAA #ifdef USE_ABP @@ -31,7 +31,8 @@ Credentials file static const u1_t PROGMEM APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // This should also be in little endian format, see above. - static const u1_t PROGMEM DEVEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + // Note: You do not need to set this field, if unset it will be generated automatically based on the device macaddr + static u1_t DEVEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // This key should be in big endian format (or, since it is not really a // number but a block of memory, endianness does not really apply). In diff --git a/main/ttn.ino b/main/ttn.ino index 5c8f809..b6c7717 100644 --- a/main/ttn.ino +++ b/main/ttn.ino @@ -58,7 +58,7 @@ void os_getDevKey (u1_t* buf) { } #ifdef USE_OTAA void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); } -void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8); } +void os_getDevEui (u1_t* buf) { memcpy(buf, DEVEUI, 8); } void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); } #endif @@ -91,6 +91,24 @@ void forceTxSingleChannelDr() { } +// DevEUI generator using devices's MAC address - from https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/lorawan.cpp +void gen_lora_deveui(uint8_t *pdeveui) { + uint8_t *p = pdeveui, dmac[6]; + int i = 0; + esp_efuse_mac_get_default(dmac); + // deveui is LSB, we reverse it so TTN DEVEUI display + // will remain the same as MAC address + // MAC is 6 bytes, devEUI 8, set first 2 ones + // with an arbitrary value + *p++ = 0xFF; + *p++ = 0xFE; + // Then next 6 bytes are mac address reversed + for (i = 0; i < 6; i++) { + *p++ = dmac[5 - i]; + } +} + + static void printHex2(unsigned v) { v &= 0xff; if (v < 16) @@ -98,6 +116,24 @@ static void printHex2(unsigned v) { Serial.print(v, HEX); } +// generate DevEUI from macaddr if needed +void initDevEUI() { + bool needInit = true; + for(int i = 0; i < sizeof(DEVEUI); i++) + if(DEVEUI[i]) needInit = false; + + if(needInit) + gen_lora_deveui(DEVEUI); + + Serial.print("DevEUI: "); + for(int i = 0; i < sizeof(DEVEUI); i++) { + if (i != 0) + Serial.print("-"); + printHex2(DEVEUI[i]); + } + Serial.println(); +} + // LMIC library will call this method when an event is fired void onEvent(ev_t event) { switch(event) { @@ -126,16 +162,16 @@ void onEvent(ev_t event) { Serial.println(devaddr, HEX); Serial.print("AppSKey: "); for (size_t i=0; i Date: Sat, 18 Jan 2020 17:41:17 -0800 Subject: [PATCH 14/32] move axpinit and document LDO & LED usage on V1 hardware --- main/configuration.h | 2 +- main/main.ino | 107 +++++++++++++++++++++++++------------------ 2 files changed, 64 insertions(+), 45 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index feb29df..b7c2789 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -91,9 +91,9 @@ void ttn_register(void (*callback)(uint8_t message)); #define I2C_SDA 21 #define I2C_SCL 22 -#define LED_PIN 14 #if defined(T_BEAM_V07) +#define LED_PIN 14 #define BUTTON_PIN 39 #elif defined(T_BEAM_V10) #define BUTTON_PIN 38 diff --git a/main/main.ino b/main/main.ino index 6e39b7c..73eac14 100644 --- a/main/main.ino +++ b/main/main.ino @@ -171,6 +171,66 @@ void scanI2Cdevice(void) Serial.println("done\n"); } +/** + * Init the power manager chip + * + * axp192 power + DCDC1 0.7-3.5V @ 1200mA max -> OLED // If you turn this off you'll lose comms to the axp192 because the OLED and the axp192 share the same i2c bus, instead use ssd1306 sleep mode + DCDC2 -> unused + DCDC3 0.7-3.5V @ 700mA max -> ESP32 (keep this on!) + LDO1 30mA -> charges GPS backup battery // turn this off during deep sleep to force the tiny J13 battery by the GPS to power the GPS ram (for a couple of days) + LDO2 200mA -> LORA + LDO3 200mA -> GPS + */ +void axp192Init() { + if (axp192_found) { + if (!axp.begin(Wire, AXP192_SLAVE_ADDRESS)) { + Serial.println("AXP192 Begin PASS"); + } else { + Serial.println("AXP192 Begin FAIL"); + } + // axp.setChgLEDMode(LED_BLINK_4HZ); + Serial.printf("DCDC1: %s\n", axp.isDCDC1Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("DCDC2: %s\n", axp.isDCDC2Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("LDO2: %s\n", axp.isLDO2Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("LDO3: %s\n", axp.isLDO3Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("DCDC3: %s\n", axp.isDCDC3Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("Exten: %s\n", axp.isExtenEnable() ? "ENABLE" : "DISABLE"); + Serial.println("----------------------------------------"); + + axp.setPowerOutPut(AXP192_LDO2, AXP202_ON); + axp.setPowerOutPut(AXP192_LDO3, AXP202_ON); + axp.setPowerOutPut(AXP192_DCDC2, AXP202_ON); + axp.setPowerOutPut(AXP192_EXTEN, AXP202_ON); + axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON); + axp.setDCDC1Voltage(3300); // for the OLED power + + Serial.printf("DCDC1: %s\n", axp.isDCDC1Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("DCDC2: %s\n", axp.isDCDC2Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("LDO2: %s\n", axp.isLDO2Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("LDO3: %s\n", axp.isLDO3Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("DCDC3: %s\n", axp.isDCDC3Enable() ? "ENABLE" : "DISABLE"); + Serial.printf("Exten: %s\n", axp.isExtenEnable() ? "ENABLE" : "DISABLE"); + + pinMode(PMU_IRQ, INPUT_PULLUP); + attachInterrupt(PMU_IRQ, [] { + pmu_irq = true; + }, FALLING); + + axp.adc1Enable(AXP202_BATT_CUR_ADC1, 1); + axp.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ, 1); + axp.clearIRQ(); + + if (axp.isChargeing()) { + baChStatus = "Charging"; + } + } else { + Serial.println("AXP192 not found"); + } +} + + + void setup() { // Debug #ifdef DEBUG_PORT @@ -183,55 +243,14 @@ void setup() { Wire.begin(I2C_SDA, I2C_SCL); scanI2Cdevice(); axp192_found = true; - if (axp192_found) { - if (!axp.begin(Wire, AXP192_SLAVE_ADDRESS)) { - Serial.println("AXP192 Begin PASS"); - } else { - Serial.println("AXP192 Begin FAIL"); - } - // axp.setChgLEDMode(LED_BLINK_4HZ); - Serial.printf("DCDC1: %s\n", axp.isDCDC1Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("DCDC2: %s\n", axp.isDCDC2Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("LDO2: %s\n", axp.isLDO2Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("LDO3: %s\n", axp.isLDO3Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("DCDC3: %s\n", axp.isDCDC3Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("Exten: %s\n", axp.isExtenEnable() ? "ENABLE" : "DISABLE"); - Serial.println("----------------------------------------"); - - axp.setPowerOutPut(AXP192_LDO2, AXP202_ON); - axp.setPowerOutPut(AXP192_LDO3, AXP202_ON); - axp.setPowerOutPut(AXP192_DCDC2, AXP202_ON); - axp.setPowerOutPut(AXP192_EXTEN, AXP202_ON); - axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON); - axp.setDCDC1Voltage(3300); - - Serial.printf("DCDC1: %s\n", axp.isDCDC1Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("DCDC2: %s\n", axp.isDCDC2Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("LDO2: %s\n", axp.isLDO2Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("LDO3: %s\n", axp.isLDO3Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("DCDC3: %s\n", axp.isDCDC3Enable() ? "ENABLE" : "DISABLE"); - Serial.printf("Exten: %s\n", axp.isExtenEnable() ? "ENABLE" : "DISABLE"); - - pinMode(PMU_IRQ, INPUT_PULLUP); - attachInterrupt(PMU_IRQ, [] { - pmu_irq = true; - }, FALLING); - - axp.adc1Enable(AXP202_BATT_CUR_ADC1, 1); - axp.enableIRQ(AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_BATT_REMOVED_IRQ | AXP202_BATT_CONNECT_IRQ, 1); - axp.clearIRQ(); - - if (axp.isChargeing()) { - baChStatus = "Charging"; - } - } else { - Serial.println("AXP192 not found"); - } + axp192Init(); #endif // Buttons & LED pinMode(BUTTON_PIN, INPUT_PULLUP); +#ifdef LED_PIN pinMode(LED_PIN, OUTPUT); +#endif // Hello DEBUG_MSG(APP_NAME " " APP_VERSION "\n"); From ddf47aa1342b1ab8987a4ba11b7c31f1c3e37b12 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 18 Jan 2020 18:00:00 -0800 Subject: [PATCH 15/32] begin implementing deep sleep (and powering down gps/lora/display) --- main/configuration.h | 3 +++ main/main.ino | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index b7c2789..56b432f 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -66,6 +66,9 @@ void ttn_register(void (*callback)(uint8_t message)); #define LORAWAN_ADR 0 // Enable ADR #define GPS_WAIT_FOR_LOCK 5000 // Wait 5s after every boot for GPS lock +// If defined, we will enter deep sleep after sending our packet. We will sleep until this interval expires or the user presses the button +#define DEEPSLEEP_INTERVAL (60 * 60 * 1000ULL) // sleep after we've received one message from the server (or we ran out of time), sleep for this many msecs + // ----------------------------------------------------------------------------- // DEBUG // ----------------------------------------------------------------------------- diff --git a/main/main.ino b/main/main.ino index 73eac14..f2aad3e 100644 --- a/main/main.ino +++ b/main/main.ino @@ -178,7 +178,7 @@ void scanI2Cdevice(void) DCDC1 0.7-3.5V @ 1200mA max -> OLED // If you turn this off you'll lose comms to the axp192 because the OLED and the axp192 share the same i2c bus, instead use ssd1306 sleep mode DCDC2 -> unused DCDC3 0.7-3.5V @ 700mA max -> ESP32 (keep this on!) - LDO1 30mA -> charges GPS backup battery // turn this off during deep sleep to force the tiny J13 battery by the GPS to power the GPS ram (for a couple of days) + LDO1 30mA -> charges GPS backup battery // charges the tiny J13 battery by the GPS to power the GPS ram (for a couple of days), can not be turned off LDO2 200mA -> LORA LDO3 200mA -> GPS */ @@ -198,8 +198,8 @@ void axp192Init() { Serial.printf("Exten: %s\n", axp.isExtenEnable() ? "ENABLE" : "DISABLE"); Serial.println("----------------------------------------"); - axp.setPowerOutPut(AXP192_LDO2, AXP202_ON); - axp.setPowerOutPut(AXP192_LDO3, AXP202_ON); + axp.setPowerOutPut(AXP192_LDO2, AXP202_ON); // LORA radio + axp.setPowerOutPut(AXP192_LDO3, AXP202_ON); // GPS main power axp.setPowerOutPut(AXP192_DCDC2, AXP202_ON); axp.setPowerOutPut(AXP192_EXTEN, AXP202_ON); axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON); @@ -230,6 +230,37 @@ void axp192Init() { } +void doDeepSleep() +{ +#if DEEPSLEEP_INTERVAL + uint64_t msecToWake = DEEPSLEEP_INTERVAL; + Serial.printf("Entering deep sleep %llu\n", msecToWake); + + // not using wifi yet, but once we are this is needed to shutoff the radio hw + // esp_wifi_stop(); + + screen_off(); // datasheet says this will draw only 10ua + if(axp192_found) { + axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // LORA radio + axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // GPS main power + } + + // FIXME - use an external 10k pulldown so we can leave the RTC peripherals powered off + // until then we need the following lines + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); + + // Only GPIOs which are have RTC functionality can be used in this bit map: 0,2,4,12-15,25-27,32-39. + uint64_t gpioMask = (1ULL << BUTTON_PIN); + + // FIXME change polarity so we can wake on ANY_HIGH instead - that would allow us to use all three buttons (instead of just the first) + gpio_pullup_en((gpio_num_t) BUTTON_PIN); + + esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ALL_LOW); + + esp_sleep_enable_timer_wakeup(msecToWake * 1000ULL); // call expects usecs + esp_deep_sleep_start(); // 0.25 mA sleep current (battery) +#endif +} void setup() { // Debug From 2da1f623ff8acf2c379f9d7c3304e7c6809918d2 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 19 Jan 2020 10:01:05 -0800 Subject: [PATCH 16/32] read wake reason at boot --- main/main.ino | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/main/main.ino b/main/main.ino index f2aad3e..150a85f 100644 --- a/main/main.ino +++ b/main/main.ino @@ -38,12 +38,16 @@ bool axp192_found = false; #if defined(PAYLOAD_USE_FULL) // includes number of satellites and accuracy - uint8_t txBuffer[10]; + static uint8_t txBuffer[10]; #elif defined(PAYLOAD_USE_CAYENNE) // CAYENNE DF static uint8_t txBuffer[11] = {0x03, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; #endif +// deep sleep support +RTC_DATA_ATTR int bootCount = 0; +esp_sleep_source_t wakeCause; // the reason we booted this time + // ----------------------------------------------------------------------------- // Application // ----------------------------------------------------------------------------- @@ -240,9 +244,12 @@ void doDeepSleep() // esp_wifi_stop(); screen_off(); // datasheet says this will draw only 10ua + LMIC_shutdown(); // cleanly shutdown the radio + if(axp192_found) { - axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // LORA radio - axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // GPS main power + // turn on after initial testing with real hardware + // axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // LORA radio + // axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // GPS main power } // FIXME - use an external 10k pulldown so we can leave the RTC peripherals powered off @@ -258,17 +265,33 @@ void doDeepSleep() esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ALL_LOW); esp_sleep_enable_timer_wakeup(msecToWake * 1000ULL); // call expects usecs - esp_deep_sleep_start(); // 0.25 mA sleep current (battery) + esp_deep_sleep_start(); // TBD mA sleep current (battery) #endif } +// Perform power on init that we do on each wake from deep sleep +void initDeepSleep() { + bootCount++; + wakeCause = esp_sleep_get_wakeup_cause(); + /* + Not using yet because we are using wake on all buttons being low + + wakeButtons = esp_sleep_get_ext1_wakeup_status(); // If one of these buttons is set it was the reason we woke + if (wakeCause == ESP_SLEEP_WAKEUP_EXT1 && !wakeButtons) // we must have been using the 'all buttons rule for waking' to support busted boards, assume button one was pressed + wakeButtons = ((uint64_t)1) << buttons.gpios[0]; + */ + + Serial.printf("booted, wake cause %d (boot count %d)\n", wakeCause, bootCount); +} + void setup() { // Debug #ifdef DEBUG_PORT DEBUG_PORT.begin(SERIAL_BAUD); #endif - delay(1000); + initDeepSleep(); + // delay(1000); FIXME - remove #ifdef T_BEAM_V10 Wire.begin(I2C_SDA, I2C_SCL); @@ -293,7 +316,7 @@ void setup() { gps_setup(); // Show logo on first boot after removing battery - if (ttn_get_count() == 0) { + if (bootCount == 0) { screen_print(APP_NAME " " APP_VERSION, 0, 0); screen_show_logo(); screen_update(); From a35c6dcbe96178e722faee4ec953bb292656e78f Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 19 Jan 2020 10:12:48 -0800 Subject: [PATCH 17/32] dynamically probe for screen and don't even use it if... waking from deep sleep timer --- main/main.ino | 14 +++++++++----- main/screen.ino | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/main/main.ino b/main/main.ino index 150a85f..a927291 100644 --- a/main/main.ino +++ b/main/main.ino @@ -272,7 +272,7 @@ void doDeepSleep() // Perform power on init that we do on each wake from deep sleep void initDeepSleep() { bootCount++; - wakeCause = esp_sleep_get_wakeup_cause(); + wakeCause = esp_sleep_get_wakeup_cause(); /* Not using yet because we are using wake on all buttons being low @@ -293,12 +293,15 @@ void setup() { initDeepSleep(); // delay(1000); FIXME - remove - #ifdef T_BEAM_V10 Wire.begin(I2C_SDA, I2C_SCL); scanI2Cdevice(); + + // FIXME - remove once we know dynamic probing is working + #ifdef T_BEAM_V10 axp192_found = true; - axp192Init(); + ssd1306_found = true; #endif + axp192Init(); // Buttons & LED pinMode(BUTTON_PIN, INPUT_PULLUP); @@ -309,8 +312,9 @@ void setup() { // Hello DEBUG_MSG(APP_NAME " " APP_VERSION "\n"); - // Display - screen_setup(); + // Don't init display if we don't have one or we are waking headless due to a timer event + if(ssd1306_found && wakeCause != ESP_SLEEP_WAKEUP_TIMER) + screen_setup(); // Init GPS gps_setup(); diff --git a/main/screen.ino b/main/screen.ino index d7cf520..dc11833 100644 --- a/main/screen.ino +++ b/main/screen.ino @@ -32,6 +32,8 @@ SSD1306Wire * display; uint8_t _screen_line = SCREEN_HEADER_HEIGHT - 1; void _screen_header() { + if(!display) return; + char buffer[20]; // Message count @@ -51,25 +53,36 @@ void _screen_header() { } void screen_show_logo() { + if(!display) return; + uint8_t x = (display->getWidth() - TTN_IMAGE_WIDTH) / 2; uint8_t y = SCREEN_HEADER_HEIGHT + (display->getHeight() - SCREEN_HEADER_HEIGHT - TTN_IMAGE_HEIGHT) / 2 + 1; display->drawXbm(x, y, TTN_IMAGE_WIDTH, TTN_IMAGE_HEIGHT, TTN_IMAGE); } void screen_off() { + if(!display) return; + display->displayOff(); } void screen_on() { + if(!display) return; + display->displayOn(); } void screen_clear() { + if(!display) return; + display->clear(); } void screen_print(const char * text, uint8_t x, uint8_t y, uint8_t alignment) { DEBUG_MSG(text); + + if(!display) return; + display->setTextAlignment((OLEDDISPLAY_TEXT_ALIGNMENT) alignment); display->drawString(x, y, text); } @@ -79,6 +92,8 @@ void screen_print(const char * text, uint8_t x, uint8_t y) { } void screen_print(const char * text) { + if(!display) return; + display->print(text); if (_screen_line + 8 > display->getHeight()) { // scroll @@ -88,7 +103,8 @@ void screen_print(const char * text) { } void screen_update() { - display->display(); + if(display) + display->display(); } void screen_setup() { @@ -103,6 +119,8 @@ void screen_setup() { } void screen_loop() { + if(!display) return; + #ifdef T_BEAM_V10 if (axp192_found && pmu_irq) { pmu_irq = false; From 8863cb7d5c4703800de7bd09e7bb1611a2c8bf2b Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 19 Jan 2020 10:59:30 -0800 Subject: [PATCH 18/32] enter deep sleep as soon as we send one packet --- main/main.ino | 186 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 129 insertions(+), 57 deletions(-) diff --git a/main/main.ino b/main/main.ino index a927291..01a3b3a 100644 --- a/main/main.ino +++ b/main/main.ino @@ -36,6 +36,8 @@ String baChStatus = "No charging"; bool ssd1306_found = false; bool axp192_found = false; +bool packetSent, packetQueued; + #if defined(PAYLOAD_USE_FULL) // includes number of satellites and accuracy static uint8_t txBuffer[10]; @@ -54,24 +56,37 @@ esp_sleep_source_t wakeCause; // the reason we booted this time void buildPacket(uint8_t txBuffer[]); // needed for platformio -void send() { - char buffer[40]; - snprintf(buffer, sizeof(buffer), "Latitude: %10.6f\n", gps_latitude()); - screen_print(buffer); - snprintf(buffer, sizeof(buffer), "Longitude: %10.6f\n", gps_longitude()); - screen_print(buffer); - snprintf(buffer, sizeof(buffer), "Error: %4.2fm\n", gps_hdop()); - screen_print(buffer); +/** + * If we have a valid position send it to the server. + * @return true if we decided to send. + */ +bool trySend() { + packetSent = false; + if (0 < gps_hdop() && gps_hdop() < 50 && gps_latitude() != 0 && gps_longitude() != 0) + { + char buffer[40]; + snprintf(buffer, sizeof(buffer), "Latitude: %10.6f\n", gps_latitude()); + screen_print(buffer); + snprintf(buffer, sizeof(buffer), "Longitude: %10.6f\n", gps_longitude()); + screen_print(buffer); + snprintf(buffer, sizeof(buffer), "Error: %4.2fm\n", gps_hdop()); + screen_print(buffer); - buildPacket(txBuffer); + buildPacket(txBuffer); #if LORAWAN_CONFIRMED_EVERY > 0 - bool confirmed = (count % LORAWAN_CONFIRMED_EVERY == 0); + bool confirmed = (count % LORAWAN_CONFIRMED_EVERY == 0); #else - bool confirmed = false; + bool confirmed = false; #endif - ttn_send(txBuffer, sizeof(txBuffer), LORAWAN_PORT, confirmed); + packetQueued = true; + ttn_send(txBuffer, sizeof(txBuffer), LORAWAN_PORT, confirmed); + return true; + } + else { + return false; + } } void sleep() { @@ -115,9 +130,11 @@ void callback(uint8_t message) { if (EV_PENDING == message) screen_print("Message discarded\n"); if (EV_QUEUED == message) screen_print("Message queued\n"); - if (EV_TXCOMPLETE == message) { + // We only want to say 'packetSent' for our packets (not packets needed for joining) + if (EV_TXCOMPLETE == message && packetQueued) { screen_print("Message sent\n"); - sleep(); + packetQueued = false; + packetSent = true; } if (EV_RESPONSE == message) { @@ -234,41 +251,6 @@ void axp192Init() { } -void doDeepSleep() -{ -#if DEEPSLEEP_INTERVAL - uint64_t msecToWake = DEEPSLEEP_INTERVAL; - Serial.printf("Entering deep sleep %llu\n", msecToWake); - - // not using wifi yet, but once we are this is needed to shutoff the radio hw - // esp_wifi_stop(); - - screen_off(); // datasheet says this will draw only 10ua - LMIC_shutdown(); // cleanly shutdown the radio - - if(axp192_found) { - // turn on after initial testing with real hardware - // axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // LORA radio - // axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // GPS main power - } - - // FIXME - use an external 10k pulldown so we can leave the RTC peripherals powered off - // until then we need the following lines - esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); - - // Only GPIOs which are have RTC functionality can be used in this bit map: 0,2,4,12-15,25-27,32-39. - uint64_t gpioMask = (1ULL << BUTTON_PIN); - - // FIXME change polarity so we can wake on ANY_HIGH instead - that would allow us to use all three buttons (instead of just the first) - gpio_pullup_en((gpio_num_t) BUTTON_PIN); - - esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ALL_LOW); - - esp_sleep_enable_timer_wakeup(msecToWake * 1000ULL); // call expects usecs - esp_deep_sleep_start(); // TBD mA sleep current (battery) -#endif -} - // Perform power on init that we do on each wake from deep sleep void initDeepSleep() { bootCount++; @@ -313,7 +295,10 @@ void setup() { DEBUG_MSG(APP_NAME " " APP_VERSION "\n"); // Don't init display if we don't have one or we are waking headless due to a timer event - if(ssd1306_found && wakeCause != ESP_SLEEP_WAKEUP_TIMER) + if(wakeCause == ESP_SLEEP_WAKEUP_TIMER) + ssd1306_found = false; // forget we even have the hardware + + if(ssd1306_found) screen_setup(); // Init GPS @@ -340,20 +325,92 @@ void setup() { ttn_adr(LORAWAN_ADR); } -void loop() { - gps_loop(); - ttn_loop(); - screen_loop(); +#ifdef DEEPSLEEP_INTERVAL + +void doDeepSleep() +{ + uint64_t msecToWake = DEEPSLEEP_INTERVAL; + Serial.printf("Entering deep sleep %llu\n", msecToWake); + + // not using wifi yet, but once we are this is needed to shutoff the radio hw + // esp_wifi_stop(); + + screen_off(); // datasheet says this will draw only 10ua + LMIC_shutdown(); // cleanly shutdown the radio + + if(axp192_found) { + // turn on after initial testing with real hardware + // axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // LORA radio + // axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // GPS main power + } + + // FIXME - use an external 10k pulldown so we can leave the RTC peripherals powered off + // until then we need the following lines + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); + + // Only GPIOs which are have RTC functionality can be used in this bit map: 0,2,4,12-15,25-27,32-39. + uint64_t gpioMask = (1ULL << BUTTON_PIN); + + // FIXME change polarity so we can wake on ANY_HIGH instead - that would allow us to use all three buttons (instead of just the first) + gpio_pullup_en((gpio_num_t) BUTTON_PIN); + + esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ALL_LOW); + + esp_sleep_enable_timer_wakeup(msecToWake * 1000ULL); // call expects usecs + esp_deep_sleep_start(); // TBD mA sleep current (battery) +} + + +/// send ONE packet (or timeout waiting for GPS/LORA) and then enter deep sleep. If screen is on let the user have a few seconds to +/// see the results +void deepSleepLoop() { + // Send every SEND_INTERVAL millis + static uint32_t last = 0, timeToSleep = 0; + static bool first = true; + + uint32_t now = millis(); + + if(packetSent) { // we've sent at least one packet to the server + timeToSleep = ssd1306_found ? now + 5000 : now; // If the display is on allow the user a few seconds to see it, otherwise go to sleep ASAP + } + + if(timeToSleep && now >= timeToSleep) // if a sleep was queued, possibly do it + doDeepSleep(); + + if (0 == last || now - last > SEND_INTERVAL) { + if (trySend()) { + last = millis(); + first = false; + Serial.println("TRANSMITTED"); + } else { + if (first) { + screen_print("Waiting GPS lock\n"); + first = false; + } + if (millis() > GPS_WAIT_FOR_LOCK) { // we never found a GPS lock, just go back to sleep + doDeepSleep(); + } + } + } +} + +#else + +/// loop endlessly, sending a packet every SEND_INTERVAL +void nonDeepSleepLoop() { + if(packetSent) { + packetSent = false; + sleep(); + } // Send every SEND_INTERVAL millis static uint32_t last = 0; static bool first = true; if (0 == last || millis() - last > SEND_INTERVAL) { - if (0 < gps_hdop() && gps_hdop() < 50 && gps_latitude() != 0 && gps_longitude() != 0) { + if (trySend()) { last = millis(); first = false; - Serial.println("TRANSMITTING"); - send(); + Serial.println("TRANSMITTED"); } else { if (first) { screen_print("Waiting GPS lock\n"); @@ -365,3 +422,18 @@ void loop() { } } } + +#endif + + +void loop() { + gps_loop(); + ttn_loop(); + screen_loop(); + +#ifdef DEEPSLEEP_INTERVAL + deepSleepLoop(); +#else + nonDeepSleepLoop(); +#endif +} From 5a11a7a8808b478493ecc8fd4e590b6829e9e726 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 20 Jan 2020 10:51:35 -0800 Subject: [PATCH 19/32] to facilitate development make radio optional for now --- main/configuration.h | 7 +++++-- main/main.ino | 26 +++++++++++++++++--------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index 56b432f..500cf06 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -64,10 +64,13 @@ void ttn_register(void (*callback)(uint8_t message)); #define LORAWAN_CONFIRMED_EVERY 0 // Send confirmed message every these many messages (0 means never) #define LORAWAN_SF DR_SF7 // Spreading factor #define LORAWAN_ADR 0 // Enable ADR -#define GPS_WAIT_FOR_LOCK 5000 // Wait 5s after every boot for GPS lock +#define REQUIRE_RADIO false // If true, we will fail to start if the radio is not found + +// If not defined, we will wait for lock forever +// #define GPS_WAIT_FOR_LOCK 5000 // Wait 5s after every boot for GPS lock // If defined, we will enter deep sleep after sending our packet. We will sleep until this interval expires or the user presses the button -#define DEEPSLEEP_INTERVAL (60 * 60 * 1000ULL) // sleep after we've received one message from the server (or we ran out of time), sleep for this many msecs +// #define DEEPSLEEP_INTERVAL (60 * 60 * 1000ULL) // sleep after we've received one message from the server (or we ran out of time), sleep for this many msecs // ----------------------------------------------------------------------------- // DEBUG diff --git a/main/main.ino b/main/main.ino index 01a3b3a..64f5176 100644 --- a/main/main.ino +++ b/main/main.ino @@ -280,8 +280,8 @@ void setup() { // FIXME - remove once we know dynamic probing is working #ifdef T_BEAM_V10 - axp192_found = true; - ssd1306_found = true; + // axp192_found = true; + // ssd1306_found = true; #endif axp192Init(); @@ -315,14 +315,18 @@ void setup() { // TTN setup if (!ttn_setup()) { screen_print("[ERR] Radio module not found!\n"); - delay(MESSAGE_TO_SLEEP_DELAY); - screen_off(); - sleep_forever(); - } - ttn_register(callback); - ttn_join(); - ttn_adr(LORAWAN_ADR); + if(REQUIRE_RADIO) { + delay(MESSAGE_TO_SLEEP_DELAY); + screen_off(); + sleep_forever(); + } + } + else { + ttn_register(callback); + ttn_join(); + ttn_adr(LORAWAN_ADR); + } } #ifdef DEEPSLEEP_INTERVAL @@ -387,9 +391,11 @@ void deepSleepLoop() { screen_print("Waiting GPS lock\n"); first = false; } +#ifdef GPS_WAIT_FOR_LOCK if (millis() > GPS_WAIT_FOR_LOCK) { // we never found a GPS lock, just go back to sleep doDeepSleep(); } +#endif } } } @@ -416,9 +422,11 @@ void nonDeepSleepLoop() { screen_print("Waiting GPS lock\n"); first = false; } +#ifdef GPS_WAIT_FOR_LOCK if (millis() > GPS_WAIT_FOR_LOCK) { sleep(); } +#endif } } } From e84a840190245519d28d47b111a0ef972627609e Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 20 Jan 2020 10:51:58 -0800 Subject: [PATCH 20/32] if prefs file open fails, don't try other operations --- main/ttn.ino | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/main/ttn.ino b/main/ttn.ino index b6c7717..b047df8 100644 --- a/main/ttn.ino +++ b/main/ttn.ino @@ -176,12 +176,13 @@ void onEvent(ev_t event) { Serial.println(); Preferences p; - p.begin("lora", false); - p.putUInt("netId", netid); - p.putUInt("devAddr", devaddr); - p.putBytes("nwkKey", nwkKey, sizeof(nwkKey)); - p.putBytes("artKey", artKey, sizeof(artKey)); - p.end(); + if(p.begin("lora", false)) { + p.putUInt("netId", netid); + p.putUInt("devAddr", devaddr); + p.putBytes("nwkKey", nwkKey, sizeof(nwkKey)); + p.putBytes("artKey", artKey, sizeof(artKey)); + p.end(); + } break; } case EV_TXCOMPLETE: Serial.println(F("EV_TXCOMPLETE (inc. RX win. wait)")); @@ -226,9 +227,10 @@ void ttn_response(uint8_t * buffer, size_t len) { static void initCount() { if(count == 0) { Preferences p; - p.begin("lora", true); - count = p.getUInt("count", 0); - p.end(); + if(p.begin("lora", true)) { + count = p.getUInt("count", 0); + p.end(); + } } } @@ -326,7 +328,7 @@ void ttn_join() { #endif Preferences p; - p.begin("lora", true); + p.begin("lora", true); // we intentionally ignore failure here uint32_t netId = p.getUInt("netId", UINT32_MAX); uint32_t devAddr = p.getUInt("devAddr", UINT32_MAX); uint8_t nwkKey[16], artKey[16]; @@ -377,9 +379,10 @@ static void ttn_set_cnt() { lastWriteMsec = now; Preferences p; - p.begin("lora", false); - p.putUInt("count", count); - p.end(); + if(p.begin("lora", false)) { + p.putUInt("count", count); + p.end(); + } } } From 1f01b71d4a8a263db4bf235a43cf5bb94f0d689d Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 05:01:22 -0800 Subject: [PATCH 21/32] require radio now that I've tested with real hardware --- main/configuration.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index 500cf06..a596bce 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -57,14 +57,14 @@ void ttn_register(void (*callback)(uint8_t message)); #define DEBUG_PORT Serial // Serial debug port #define SERIAL_BAUD 115200 // Serial debug baud rate #define SLEEP_BETWEEN_MESSAGES 0 // Do sleep between messages -#define SEND_INTERVAL 10000 // Sleep for these many millis +#define SEND_INTERVAL 60000 // Sleep for these many millis #define MESSAGE_TO_SLEEP_DELAY 5000 // Time after message before going to sleep #define LOGO_DELAY 5000 // Time to show logo on first boot #define LORAWAN_PORT 10 // Port the messages will be sent to #define LORAWAN_CONFIRMED_EVERY 0 // Send confirmed message every these many messages (0 means never) #define LORAWAN_SF DR_SF7 // Spreading factor #define LORAWAN_ADR 0 // Enable ADR -#define REQUIRE_RADIO false // If true, we will fail to start if the radio is not found +#define REQUIRE_RADIO true // If true, we will fail to start if the radio is not found // If not defined, we will wait for lock forever // #define GPS_WAIT_FOR_LOCK 5000 // Wait 5s after every boot for GPS lock From e1189839fecdffa2fb3cbf1c6d69886be61e86fe Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 05:02:12 -0800 Subject: [PATCH 22/32] on initial powerup GPS generates a fake zero alt report (before lock) --- main/main.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main/main.ino b/main/main.ino index 64f5176..6ed1511 100644 --- a/main/main.ino +++ b/main/main.ino @@ -62,7 +62,8 @@ void buildPacket(uint8_t txBuffer[]); // needed for platformio */ bool trySend() { packetSent = false; - if (0 < gps_hdop() && gps_hdop() < 50 && gps_latitude() != 0 && gps_longitude() != 0) + // We also wait for altitude being not exactly zero, because the GPS chip generates a bogus 0 alt report when first powered on + if (0 < gps_hdop() && gps_hdop() < 50 && gps_latitude() != 0 && gps_longitude() != 0 && gps_altitude() != 0) { char buffer[40]; snprintf(buffer, sizeof(buffer), "Latitude: %10.6f\n", gps_latitude()); From fb662abe6d130481027f50d983719246c5a12fe3 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 05:02:27 -0800 Subject: [PATCH 23/32] debug console is 115200 bps --- platformio.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/platformio.ini b/platformio.ini index 127c2af..e932be2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,6 +20,8 @@ framework = arduino build_flags = -Wall -Wextra -Wno-missing-field-initializers -O3 -Wl,-Map,.pio/build/esp32/output.map -D CFG_us915 -D CFG_sx1276_radio ; -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG +monitor_speed = 115200 + lib_deps = https://github.com/mcci-catena/arduino-lmic TinyGPSPlus From 2a2e8172e333b2583cb50bb6ee2999630343ac56 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 05:02:45 -0800 Subject: [PATCH 24/32] show screen messages out serial port (for screenless devices) --- main/screen.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/main/screen.ino b/main/screen.ino index dc11833..1c7502f 100644 --- a/main/screen.ino +++ b/main/screen.ino @@ -92,6 +92,7 @@ void screen_print(const char * text, uint8_t x, uint8_t y) { } void screen_print(const char * text) { + Serial.printf("Screen: %s\n", text); if(!display) return; display->print(text); From e890e7a80acf01a4cb9bede36a86ed8f86518c9c Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 05:30:08 -0800 Subject: [PATCH 25/32] deep sleep now powers down GPS and lora radio --- main/configuration.h | 9 +-- main/main.ino | 129 +++++++++++++------------------------------ 2 files changed, 42 insertions(+), 96 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index a596bce..4aee938 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -56,8 +56,8 @@ void ttn_register(void (*callback)(uint8_t message)); #define DEBUG_PORT Serial // Serial debug port #define SERIAL_BAUD 115200 // Serial debug baud rate -#define SLEEP_BETWEEN_MESSAGES 0 // Do sleep between messages -#define SEND_INTERVAL 60000 // Sleep for these many millis +#define SLEEP_BETWEEN_MESSAGES true // Do sleep between messages +#define SEND_INTERVAL (5 * 60 * 1000) // Sleep for these many millis #define MESSAGE_TO_SLEEP_DELAY 5000 // Time after message before going to sleep #define LOGO_DELAY 5000 // Time to show logo on first boot #define LORAWAN_PORT 10 // Port the messages will be sent to @@ -67,10 +67,7 @@ void ttn_register(void (*callback)(uint8_t message)); #define REQUIRE_RADIO true // If true, we will fail to start if the radio is not found // If not defined, we will wait for lock forever -// #define GPS_WAIT_FOR_LOCK 5000 // Wait 5s after every boot for GPS lock - -// If defined, we will enter deep sleep after sending our packet. We will sleep until this interval expires or the user presses the button -// #define DEEPSLEEP_INTERVAL (60 * 60 * 1000ULL) // sleep after we've received one message from the server (or we ran out of time), sleep for this many msecs +#define GPS_WAIT_FOR_LOCK (30 * 1000) // Wait 30s after every boot for GPS lock (may need longer than 5s because we turned the gps off during deep sleep) // ----------------------------------------------------------------------------- // DEBUG diff --git a/main/main.ino b/main/main.ino index 6ed1511..d9b6dd4 100644 --- a/main/main.ino +++ b/main/main.ino @@ -90,6 +90,40 @@ bool trySend() { } } + +void doDeepSleep(uint64_t msecToWake) +{ + Serial.printf("Entering deep sleep for %llu seconds\n", msecToWake / 1000); + + // not using wifi yet, but once we are this is needed to shutoff the radio hw + // esp_wifi_stop(); + + screen_off(); // datasheet says this will draw only 10ua + LMIC_shutdown(); // cleanly shutdown the radio + + if(axp192_found) { + // turn on after initial testing with real hardware + axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // LORA radio + axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // GPS main power + } + + // FIXME - use an external 10k pulldown so we can leave the RTC peripherals powered off + // until then we need the following lines + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); + + // Only GPIOs which are have RTC functionality can be used in this bit map: 0,2,4,12-15,25-27,32-39. + uint64_t gpioMask = (1ULL << BUTTON_PIN); + + // FIXME change polarity so we can wake on ANY_HIGH instead - that would allow us to use all three buttons (instead of just the first) + gpio_pullup_en((gpio_num_t) BUTTON_PIN); + + esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ALL_LOW); + + esp_sleep_enable_timer_wakeup(msecToWake * 1000ULL); // call expects usecs + esp_deep_sleep_start(); // TBD mA sleep current (battery) +} + + void sleep() { #if SLEEP_BETWEEN_MESSAGES @@ -110,7 +144,7 @@ void sleep() { // We sleep for the interval between messages minus the current millis // this way we distribute the messages evenly every SEND_INTERVAL millis uint32_t sleep_for = (millis() < SEND_INTERVAL) ? SEND_INTERVAL - millis() : SEND_INTERVAL; - sleep_millis(sleep_for); + doDeepSleep(sleep_for); #endif } @@ -330,81 +364,11 @@ void setup() { } } -#ifdef DEEPSLEEP_INTERVAL +void loop() { + gps_loop(); + ttn_loop(); + screen_loop(); -void doDeepSleep() -{ - uint64_t msecToWake = DEEPSLEEP_INTERVAL; - Serial.printf("Entering deep sleep %llu\n", msecToWake); - - // not using wifi yet, but once we are this is needed to shutoff the radio hw - // esp_wifi_stop(); - - screen_off(); // datasheet says this will draw only 10ua - LMIC_shutdown(); // cleanly shutdown the radio - - if(axp192_found) { - // turn on after initial testing with real hardware - // axp.setPowerOutPut(AXP192_LDO2, AXP202_OFF); // LORA radio - // axp.setPowerOutPut(AXP192_LDO3, AXP202_OFF); // GPS main power - } - - // FIXME - use an external 10k pulldown so we can leave the RTC peripherals powered off - // until then we need the following lines - esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); - - // Only GPIOs which are have RTC functionality can be used in this bit map: 0,2,4,12-15,25-27,32-39. - uint64_t gpioMask = (1ULL << BUTTON_PIN); - - // FIXME change polarity so we can wake on ANY_HIGH instead - that would allow us to use all three buttons (instead of just the first) - gpio_pullup_en((gpio_num_t) BUTTON_PIN); - - esp_sleep_enable_ext1_wakeup(gpioMask, ESP_EXT1_WAKEUP_ALL_LOW); - - esp_sleep_enable_timer_wakeup(msecToWake * 1000ULL); // call expects usecs - esp_deep_sleep_start(); // TBD mA sleep current (battery) -} - - -/// send ONE packet (or timeout waiting for GPS/LORA) and then enter deep sleep. If screen is on let the user have a few seconds to -/// see the results -void deepSleepLoop() { - // Send every SEND_INTERVAL millis - static uint32_t last = 0, timeToSleep = 0; - static bool first = true; - - uint32_t now = millis(); - - if(packetSent) { // we've sent at least one packet to the server - timeToSleep = ssd1306_found ? now + 5000 : now; // If the display is on allow the user a few seconds to see it, otherwise go to sleep ASAP - } - - if(timeToSleep && now >= timeToSleep) // if a sleep was queued, possibly do it - doDeepSleep(); - - if (0 == last || now - last > SEND_INTERVAL) { - if (trySend()) { - last = millis(); - first = false; - Serial.println("TRANSMITTED"); - } else { - if (first) { - screen_print("Waiting GPS lock\n"); - first = false; - } -#ifdef GPS_WAIT_FOR_LOCK - if (millis() > GPS_WAIT_FOR_LOCK) { // we never found a GPS lock, just go back to sleep - doDeepSleep(); - } -#endif - } - } -} - -#else - -/// loop endlessly, sending a packet every SEND_INTERVAL -void nonDeepSleepLoop() { if(packetSent) { packetSent = false; sleep(); @@ -431,18 +395,3 @@ void nonDeepSleepLoop() { } } } - -#endif - - -void loop() { - gps_loop(); - ttn_loop(); - screen_loop(); - -#ifdef DEEPSLEEP_INTERVAL - deepSleepLoop(); -#else - nonDeepSleepLoop(); -#endif -} From b1da652d41453c93bcfca5a297cef1ab26458170 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 05:39:35 -0800 Subject: [PATCH 26/32] let the main cpu mostly sleep until we have gps lock --- main/main.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main/main.ino b/main/main.ino index d9b6dd4..61a9821 100644 --- a/main/main.ino +++ b/main/main.ino @@ -392,6 +392,10 @@ void loop() { sleep(); } #endif + + // No GPS lock yet, let the OS put the main CPU in low power mode for 100ms (or until another interrupt comes in) + // i.e. don't just keep spinning in loop as fast as we can. + delay(100); } } } From f655882cfa4a94f48521545936752f0597c4ba5f Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 05:40:08 -0800 Subject: [PATCH 27/32] dynamic probing for devices works --- main/main.ino | 6 ------ 1 file changed, 6 deletions(-) diff --git a/main/main.ino b/main/main.ino index 61a9821..40b54a9 100644 --- a/main/main.ino +++ b/main/main.ino @@ -308,16 +308,10 @@ void setup() { #endif initDeepSleep(); - // delay(1000); FIXME - remove Wire.begin(I2C_SDA, I2C_SCL); scanI2Cdevice(); - // FIXME - remove once we know dynamic probing is working - #ifdef T_BEAM_V10 - // axp192_found = true; - // ssd1306_found = true; - #endif axp192Init(); // Buttons & LED From 5e18cd11719c6c3a535c446a1df3ddd0127ed8b9 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 06:14:49 -0800 Subject: [PATCH 28/32] If user presses button for 3 secs, discard all lora settings --- main/main.ino | 40 ++++++++++++++++++++++++++++++++-------- main/ttn.ino | 9 +++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/main/main.ino b/main/main.ino index 40b54a9..63654a2 100644 --- a/main/main.ino +++ b/main/main.ino @@ -127,16 +127,19 @@ void doDeepSleep(uint64_t msecToWake) void sleep() { #if SLEEP_BETWEEN_MESSAGES - // Show the going to sleep message on the screen - char buffer[20]; - snprintf(buffer, sizeof(buffer), "Sleeping in %3.1fs\n", (MESSAGE_TO_SLEEP_DELAY / 1000.0)); - screen_print(buffer); + // If the user has a screen, tell them we are about to sleep + if(ssd1306_found) { + // Show the going to sleep message on the screen + char buffer[20]; + snprintf(buffer, sizeof(buffer), "Sleeping in %3.1fs\n", (MESSAGE_TO_SLEEP_DELAY / 1000.0)); + screen_print(buffer); - // Wait for MESSAGE_TO_SLEEP_DELAY millis to sleep - delay(MESSAGE_TO_SLEEP_DELAY); + // Wait for MESSAGE_TO_SLEEP_DELAY millis to sleep + delay(MESSAGE_TO_SLEEP_DELAY); - // Turn off screen - screen_off(); + // Turn off screen + screen_off(); + } // Set the user button to wake the board sleep_interrupt(BUTTON_PIN, LOW); @@ -368,6 +371,27 @@ void loop() { sleep(); } + // if user presses button for more than 3 secs, discard our network prefs and reboot (FIXME, use a debounce lib instead of this boilerplate) + static bool wasPressed = false; + static uint32_t minPressMs; // what tick should we call this press long enough + if(!digitalRead(BUTTON_PIN)) { + if(!wasPressed) { // just started a new press + Serial.println("pressing"); + wasPressed = true; + minPressMs = millis() + 3000; + } + } else if(wasPressed) { + // we just did a release + wasPressed = false; + if(millis() > minPressMs) { + // held long enough + screen_print("Erasing prefs"); + ttn_erase_prefs(); + delay(5000); // Give some time to read the screen + ESP.restart(); + } + } + // Send every SEND_INTERVAL millis static uint32_t last = 0; static bool first = true; diff --git a/main/ttn.ino b/main/ttn.ino index b047df8..b760e1d 100644 --- a/main/ttn.ino +++ b/main/ttn.ino @@ -386,6 +386,15 @@ static void ttn_set_cnt() { } } +/// Blow away our prefs (i.e. to rejoin from scratch) +void ttn_erase_prefs() { + Preferences p; + if(p.begin("lora", false)) { + p.clear(); + p.end(); + } +} + void ttn_send(uint8_t * data, uint8_t data_size, uint8_t port, bool confirmed){ ttn_set_cnt(); // we are about to send using the current packet count From afc05492068827a482cbaf8da961d2fb3c5df6e9 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 06:15:01 -0800 Subject: [PATCH 29/32] vscode data file --- .vscode/settings.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..22da32b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,15 @@ +{ + "files.associations": { + "*.tcc": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "fstream": "cpp", + "iomanip": "cpp", + "limits": "cpp", + "sstream": "cpp", + "system_error": "cpp" + } +} \ No newline at end of file From ccc35bb1f96d240fd1039ab6027eb572f91d796d Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 06:34:28 -0800 Subject: [PATCH 30/32] use a larger spreading factor --- main/configuration.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/configuration.h b/main/configuration.h index 4aee938..958d936 100644 --- a/main/configuration.h +++ b/main/configuration.h @@ -62,12 +62,12 @@ void ttn_register(void (*callback)(uint8_t message)); #define LOGO_DELAY 5000 // Time to show logo on first boot #define LORAWAN_PORT 10 // Port the messages will be sent to #define LORAWAN_CONFIRMED_EVERY 0 // Send confirmed message every these many messages (0 means never) -#define LORAWAN_SF DR_SF7 // Spreading factor +#define LORAWAN_SF DR_SF10 // Spreading factor (recommended DR_SF7 for ttn network map purposes, DR_SF10 works for slow moving trackers) #define LORAWAN_ADR 0 // Enable ADR #define REQUIRE_RADIO true // If true, we will fail to start if the radio is not found // If not defined, we will wait for lock forever -#define GPS_WAIT_FOR_LOCK (30 * 1000) // Wait 30s after every boot for GPS lock (may need longer than 5s because we turned the gps off during deep sleep) +#define GPS_WAIT_FOR_LOCK (60 * 1000) // Wait after every boot for GPS lock (may need longer than 5s because we turned the gps off during deep sleep) // ----------------------------------------------------------------------------- // DEBUG From 128c10ca88bb9b479a1af83cffb2cf293901a8d0 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 08:06:30 -0800 Subject: [PATCH 31/32] make sure user doesn't accidentally put their credentials in git --- .gitignore | 3 ++- main/credentials.h | 4 ++++ main/ttn.ino | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 171f0f3..8a96b58 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .pio main/configuration.h main/credentials.h +credentials-private.h # ignore vscode IDE settings files .vscode/* @@ -8,4 +9,4 @@ main/credentials.h !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json -*.code-workspace \ No newline at end of file +*.code-workspace diff --git a/main/credentials.h b/main/credentials.h index 6d91159..656ff73 100644 --- a/main/credentials.h +++ b/main/credentials.h @@ -24,6 +24,9 @@ Credentials file #ifdef USE_OTAA +#if 0 + // copy these variables to ../credentials-private.h and customize with your secret appkey. + // This EUI must be in little-endian format, so least-significant-byte // first. When copying an EUI from ttnctl output, this means to reverse // the bytes. For TTN issued EUIs the last bytes should be 0x00, 0x00, @@ -39,5 +42,6 @@ Credentials file // practice, a key taken from ttnctl can be copied as-is. // The key shown here is the semtech default key. static const u1_t PROGMEM APPKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +#endif #endif diff --git a/main/ttn.ino b/main/ttn.ino index b760e1d..ad0aacc 100644 --- a/main/ttn.ino +++ b/main/ttn.ino @@ -31,6 +31,9 @@ along with this program. If not, see . #include "configuration.h" #include "credentials.h" +// This file should not be in any publig git repos, it contains your secret APPKEY +#include "../credentials-private.h" + // ----------------------------------------------------------------------------- // Globals // ----------------------------------------------------------------------------- From be4983b0562638378572973cb756300d38187d1d Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 08:28:18 -0800 Subject: [PATCH 32/32] add explicit dependency on SPI lib --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index e932be2..217f9bc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -27,5 +27,5 @@ lib_deps = TinyGPSPlus ESP8266_SSD1306 AXP202X_Library - + SPI