From 244eb10c2941180c90973552cdabecb031518972 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 24 Apr 2018 22:29:12 +0200 Subject: [PATCH 01/13] memory display & reduced task stack sizes & reduced bt loop pause --- src/blecsan.cpp | 8 ++++++-- src/globals.h | 3 ++- src/macsniff.cpp | 10 ++++++++-- src/main.cpp | 18 ++++++++++++------ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 1471f5f6..1d3c70da 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -242,6 +242,8 @@ void bt_loop(void * pvParameters) // Initialize BT controller to allocate task and other resource. ESP_LOGI(TAG, "Enabling Bluetooth Controller"); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + bt_cfg.controller_task_stack_size = 8192; // double BT stack size + if (esp_bt_controller_init(&bt_cfg) != ESP_OK) { ESP_LOGE(TAG, "Bluetooth controller initialize failed"); @@ -254,7 +256,9 @@ void bt_loop(void * pvParameters) ESP_LOGE(TAG, "Bluetooth controller enable failed"); goto end; } - + + esp_bt_controller_mem_release(ESP_BT_MODE_BTDM); // gives 30KB more RAM for heap + // Init and alloc the resource for bluetooth, must be prior to every bluetooth stuff ESP_LOGI(TAG, "Init Bluetooth stack"); status = esp_bluedroid_init(); @@ -282,7 +286,7 @@ void bt_loop(void * pvParameters) while(1) { - vTaskDelay(500/portTICK_PERIOD_MS); + vTaskDelay(10/portTICK_PERIOD_MS); yield(); } diff --git a/src/globals.h b/src/globals.h index eb238842..1b861002 100644 --- a/src/globals.h +++ b/src/globals.h @@ -48,11 +48,12 @@ extern uint8_t mydata[]; extern uint64_t uptimecounter; extern uint32_t currentMillis ; extern osjob_t sendjob; -extern char display_lora[], display_lmic[]; +extern char display_lora[], display_lmic[], display_mem[]; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters extern bool joinstate; extern std::set macs; +extern const uint32_t heapmem; #ifdef HAS_DISPLAY extern HAS_DISPLAY u8x8; diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 3e5db96b..a98f1cce 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -27,6 +27,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { bool added = false; uint32_t addr2int, vendor2int; // temporary buffer for MAC and Vendor OUI uint16_t hashedmac; // temporary buffer for generated hash value + uint16_t memlevel; // % of used heap mem // only last 3 MAC Address bytes are used for MAC address anonymization // but since it's uint32 we take 4 bytes to avoid 1st value to be 0 @@ -64,11 +65,16 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { #endif } + // Display heap memory left + memlevel = 1 - (ESP.getFreeHeap() / heapmem); + sprintf(display_mem, "%d.1%%", memlevel); + // Log scan result - ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s", + ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s -> %d Bytes left", sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", rssi, buff, hashedmac, macs_wifi, macs_ble, - added ? "new" : "known"); + added ? "new " : "known", + ESP.getFreeHeap()); #ifdef VENDORFILTER } else { diff --git a/src/main.cpp b/src/main.cpp index 4cd60af2..e97b84e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,9 +47,10 @@ uint8_t DisplayState = 0; // globals for state machine uint16_t LEDBlinkduration = 0, LEDInterval = 0, color = COLOR_NONE; // state machine variables uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // MAC counters globals for display uint8_t channel = 0; // wifi channel rotation counter global for display -char display_lora[16], display_lmic[16]; // display buffers +char display_lora[16], display_lmic[16], display_mem[16]; // display buffers enum states LEDState = LED_OFF, previousLEDState = LED_OFF; // LED state global for state machine bool joinstate = false; // LoRa network joined? global flag +const uint32_t heapmem = ESP.getFreeHeap(); // free heap memory after start (:= 100%) std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) @@ -310,7 +311,7 @@ uint64_t uptime() { esp_chip_info_t chip_info; esp_chip_info(&chip_info); u8x8.printf("ESP32 %d cores\nWiFi%s%s\n", - chip_info.cores, + chip_info.cores, (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : ""); u8x8.printf("ESP Rev.%d\n", chip_info.revision); @@ -344,6 +345,10 @@ uint64_t uptime() { u8x8.printf("%-16s", "BLTH:off"); #endif + // update free heap memory display (line 4) + u8x8.setCursor(11,4); + u8x8.printf("%-5s", display_mem); + // update RSSI limiter status & wifi channel display (line 5) u8x8.setCursor(0,5); u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%-4d", cfg.rssilimit); @@ -500,6 +505,7 @@ void setup() { #endif u8x8.setCursor(0,5); u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%d", cfg.rssilimit); + sprintf(display_lora, "Join wait"); #endif @@ -521,12 +527,12 @@ ESP_LOGI(TAG, "Starting Lora task on core 1"); xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 1); ESP_LOGI(TAG, "Starting Wifi task on core 0"); -xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 16384, ( void * ) 1, 1, NULL, 0); +xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 2048, ( void * ) 1, 1, NULL, 0); #ifdef BLECOUNTER if (cfg.blescan) { // start BLE task only if BLE function is enabled in NVRAM configuration ESP_LOGI(TAG, "Starting Bluetooth task on core 0"); - xTaskCreatePinnedToCore(bt_loop, "btscan", 16384, ( void * ) 1, 1, NULL, 0); + xTaskCreatePinnedToCore(bt_loop, "btscan", 2048, ( void * ) 1, 1, NULL, 0); } #endif @@ -547,8 +553,8 @@ void loop() { // simple state machine for controlling display, LED, button, etc. uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) - currentMillis = millis(); // timebase for state machine in milliseconds (32bit) - + currentMillis = millis(); // timebase for state machine in milliseconds (32bit) + #ifdef HAS_LED switchLEDstate(); switchLED(); From bedd90bedede4bc6242be1acb23c5e5a363db525 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 24 Apr 2018 22:37:20 +0200 Subject: [PATCH 02/13] v1.3.21 --- src/globals.h | 6 +++--- src/main.cpp | 2 +- src/main.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/globals.h b/src/globals.h index 1b861002..35bc74ff 100644 --- a/src/globals.h +++ b/src/globals.h @@ -3,11 +3,11 @@ // std::set for unified array functions #include -//#include -//#include +#include +#include +// OLED Display #ifdef HAS_DISPLAY - // OLED Display #include #endif diff --git a/src/main.cpp b/src/main.cpp index e97b84e9..5a78ca77 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -553,7 +553,7 @@ void loop() { // simple state machine for controlling display, LED, button, etc. uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) - currentMillis = millis(); // timebase for state machine in milliseconds (32bit) + currentMillis = millis(); // timebase for state machine in milliseconds (32bit) #ifdef HAS_LED switchLEDstate(); diff --git a/src/main.h b/src/main.h index d92ed7a2..438d70cd 100644 --- a/src/main.h +++ b/src/main.h @@ -1,6 +1,6 @@ // program version - note: increment version after modifications to configData_t struct!! -#define PROGVERSION "1.3.2" // use max 10 chars here! +#define PROGVERSION "1.3.21" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- From e0dc8cd4d003d29d9e566a50e50aca7d7302231c Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Wed, 25 Apr 2018 11:46:58 +0200 Subject: [PATCH 03/13] bugfixing LED blink routine (not fixed) --- src/macsniff.cpp | 10 +++++----- src/main.cpp | 27 ++++++++++++++++----------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/macsniff.cpp b/src/macsniff.cpp index a98f1cce..45747b27 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -27,7 +27,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { bool added = false; uint32_t addr2int, vendor2int; // temporary buffer for MAC and Vendor OUI uint16_t hashedmac; // temporary buffer for generated hash value - uint16_t memlevel; // % of used heap mem + float memlevel; // % of used heap mem // only last 3 MAC Address bytes are used for MAC address anonymization // but since it's uint32 we take 4 bytes to avoid 1st value to be 0 @@ -51,6 +51,10 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // Count only if MAC was not yet seen if (added) { + // Display heap memory left + memlevel = ESP.getFreeHeap() / heapmem * 100; + sprintf(display_mem, "%d%%", memlevel); + // increment counter and one blink led if (sniff_type == MAC_SNIFF_WIFI ) { macs_wifi++; // increment Wifi MACs counter if (joinstate) @@ -65,10 +69,6 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { #endif } - // Display heap memory left - memlevel = 1 - (ESP.getFreeHeap() / heapmem); - sprintf(display_mem, "%d.1%%", memlevel); - // Log scan result ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s -> %d Bytes left", sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", diff --git a/src/main.cpp b/src/main.cpp index 5a78ca77..1256b5a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,6 +50,7 @@ uint8_t channel = 0; // wifi channel rotation counter global for char display_lora[16], display_lmic[16], display_mem[16]; // display buffers enum states LEDState = LED_OFF, previousLEDState = LED_OFF; // LED state global for state machine bool joinstate = false; // LoRa network joined? global flag +bool oneblink = false, blinkdone = false; // flags for state machine for blinking LED once const uint32_t heapmem = ESP.getFreeHeap(); // free heap memory after start (:= 100%) std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) @@ -70,10 +71,11 @@ int redirect_log(const char * fmt, va_list args) { #endif void blink_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval) { + ESP_LOGI(TAG, "blink_LED color: %d, duration: %d, interval: %d", set_color, set_blinkduration, set_interval); color = set_color; // set color for RGB LED LEDBlinkduration = set_blinkduration; // duration on LEDInterval = set_interval; // duration off - on - off - LEDState = LED_ON; // start blink + oneblink = set_interval ? false : true; // set blinking mode: continuous or single blink } void reset_counters() { @@ -151,15 +153,15 @@ void lorawan_loop(void * pvParameters) { // LED indicators for viusalizing LoRaWAN state if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { - // 5 quick blinks 20ms on each 1/5 second while joining + // quick blink 20ms on each 1/5 second while joining blink_LED(COLOR_YELLOW, 20, 200); // TX data pending } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { - // 3 small blink 10ms on each 1/2sec (not when joining) + // small blink 10ms on each 1/2sec (not when joining) blink_LED(COLOR_BLUE, 10, 500); // This should not happen so indicate a problem } else if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) { - // 5 heartbeat long blink 200ms on each 2 seconds + // heartbeat long blink 200ms on each 2 seconds blink_LED(COLOR_RED, 200, 2000); } else { // led off @@ -414,14 +416,17 @@ uint64_t uptime() { void switchLEDstate() { - // LEDInterval != 0 -> LED is currently blinking -> toggle if interval cycle time elapsed - // LEDInterval = 0 -> do one blink then turn off LED - - if (LEDInterval) // LED is blinking, wait until time elapsed, then toggle + if (oneblink && !blinkdone) { // keep LED on until one blink is done + LEDState = (currentMillis % LEDBlinkduration) > 0 ? LED_ON : LED_OFF; + blinkdone = LEDState ? false : true; + } + + else + + if (LEDInterval) // LED is blinking, wait until time elapsed, then toggle LED LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? LED_ON : LED_OFF; - else // only one blink - LEDState = (currentMillis < LEDBlinkduration) ? LED_ON : LED_OFF; - + + } // switchLEDstate() #endif From 0ca1aa5fd0d8c872327ea4d1b23910d52bfeb6aa Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Wed, 25 Apr 2018 12:07:36 +0200 Subject: [PATCH 04/13] bugfixing LED blink (note yet fixed) --- src/main.cpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1256b5a5..5407dc15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,7 +50,7 @@ uint8_t channel = 0; // wifi channel rotation counter global for char display_lora[16], display_lmic[16], display_mem[16]; // display buffers enum states LEDState = LED_OFF, previousLEDState = LED_OFF; // LED state global for state machine bool joinstate = false; // LoRa network joined? global flag -bool oneblink = false, blinkdone = false; // flags for state machine for blinking LED once +bool blinkdone = false; // flag for state machine for blinking LED once const uint32_t heapmem = ESP.getFreeHeap(); // free heap memory after start (:= 100%) std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) @@ -71,11 +71,10 @@ int redirect_log(const char * fmt, va_list args) { #endif void blink_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval) { - ESP_LOGI(TAG, "blink_LED color: %d, duration: %d, interval: %d", set_color, set_blinkduration, set_interval); color = set_color; // set color for RGB LED LEDBlinkduration = set_blinkduration; // duration on LEDInterval = set_interval; // duration off - on - off - oneblink = set_interval ? false : true; // set blinking mode: continuous or single blink + blinkdone = false; } void reset_counters() { @@ -410,23 +409,17 @@ uint64_t uptime() { #endif previousLEDState = LEDState; + blinkdone = LEDState ? true : false; } }; // switchLED() void switchLEDstate() { - - if (oneblink && !blinkdone) { // keep LED on until one blink is done - LEDState = (currentMillis % LEDBlinkduration) > 0 ? LED_ON : LED_OFF; - blinkdone = LEDState ? false : true; - } - - else - if (LEDInterval) // LED is blinking, wait until time elapsed, then toggle LED LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? LED_ON : LED_OFF; - - + else // check if in oneblink mode + if (!blinkdone) { // keep LED on until one blink is done + LEDState = (currentMillis % LEDBlinkduration) > 0 ? LED_ON : LED_OFF; } // switchLEDstate() #endif From 391ad0fe90da305348a402417babe1eceb4774b9 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Wed, 25 Apr 2018 12:09:22 +0200 Subject: [PATCH 05/13] bugfixing LED routine (not yet fixed) --- src/main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5407dc15..b8235598 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -409,17 +409,21 @@ uint64_t uptime() { #endif previousLEDState = LEDState; - blinkdone = LEDState ? true : false; + blinkdone = LEDState ? true : false; // if LED was turned on, a blink was done } }; // switchLED() void switchLEDstate() { + if (LEDInterval) // LED is blinking, wait until time elapsed, then toggle LED LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? LED_ON : LED_OFF; + else // check if in oneblink mode - if (!blinkdone) { // keep LED on until one blink is done + + if (!blinkdone) // keep LED on until one blink is done LEDState = (currentMillis % LEDBlinkduration) > 0 ? LED_ON : LED_OFF; + } // switchLEDstate() #endif From dbf67eb78072ebc25d4520f7f15c2c58101370d5 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Wed, 25 Apr 2018 12:15:17 +0200 Subject: [PATCH 06/13] bugfixing LED routine (not yet fixed) --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index b8235598..86ac19b2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -409,7 +409,7 @@ uint64_t uptime() { #endif previousLEDState = LEDState; - blinkdone = LEDState ? true : false; // if LED was turned on, a blink was done + blinkdone = LEDState ? false : true; // if LED was turned off, a blink was done } }; // switchLED() From 6a89aef53afb278fe53fb59051eb3544540df45e Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Wed, 25 Apr 2018 12:24:01 +0200 Subject: [PATCH 07/13] bugfixing LED routine (not yet done) --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 86ac19b2..fe67a55e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,7 +50,7 @@ uint8_t channel = 0; // wifi channel rotation counter global for char display_lora[16], display_lmic[16], display_mem[16]; // display buffers enum states LEDState = LED_OFF, previousLEDState = LED_OFF; // LED state global for state machine bool joinstate = false; // LoRa network joined? global flag -bool blinkdone = false; // flag for state machine for blinking LED once +bool blinkdone = true; // flag for state machine for blinking LED once const uint32_t heapmem = ESP.getFreeHeap(); // free heap memory after start (:= 100%) std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) From 903183284e9b6b63c7234554c06bbf48228c0f13 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 26 Apr 2018 18:04:18 +0200 Subject: [PATCH 08/13] Check if LED at compile time --- src/macsniff.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 45747b27..da69ba22 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -57,15 +57,17 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // increment counter and one blink led if (sniff_type == MAC_SNIFF_WIFI ) { macs_wifi++; // increment Wifi MACs counter - if (joinstate) - blink_LED(COLOR_GREEN, 50, 0); - } + #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) + blink_LED(COLOR_GREEN, 50); + #endif + } #ifdef BLECOUNTER else if (sniff_type == MAC_SNIFF_BLE ) { macs_ble++; // increment BLE Macs counter - if (joinstate) - blink_LED(COLOR_MAGENTA, 50, 0); - } + #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) + blink_LED(COLOR_MAGENTA, 50); + #endif + } #endif } From 7d0d352924fee0c518c1f071e2379b19f3e16064 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 26 Apr 2018 18:04:46 +0200 Subject: [PATCH 09/13] Fixed millis related vars to unsigned long --- src/globals.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/globals.h b/src/globals.h index 35bc74ff..158fecf0 100644 --- a/src/globals.h +++ b/src/globals.h @@ -46,7 +46,7 @@ typedef struct { extern configData_t cfg; extern uint8_t mydata[]; extern uint64_t uptimecounter; -extern uint32_t currentMillis ; +extern unsigned long currentMillis ; extern osjob_t sendjob; extern char display_lora[], display_lmic[], display_mem[]; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; From 93debcbc6628b53947e3ae93941143c751097418 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 26 Apr 2018 18:07:48 +0200 Subject: [PATCH 10/13] Fixed Led Management --- src/main.cpp | 166 +++++++++++++++++++++++++++++++-------------------- src/main.h | 9 ++- 2 files changed, 106 insertions(+), 69 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fe67a55e..71b7ad81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,14 +41,18 @@ Refer to LICENSE.txt file in repository for more details. configData_t cfg; // struct holds current device configuration osjob_t sendjob, initjob; // LMIC jobs uint64_t uptimecounter = 0; // timer global for uptime counter -uint32_t currentMillis = millis(); // timer global for state machine -uint32_t previousDisplaymillis = currentMillis; // Display refresh for state machine +unsigned long currentMillis = millis(); // timer global for state machine +unsigned long previousDisplaymillis = currentMillis; // Display refresh for state machine uint8_t DisplayState = 0; // globals for state machine -uint16_t LEDBlinkduration = 0, LEDInterval = 0, color = COLOR_NONE; // state machine variables uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // MAC counters globals for display uint8_t channel = 0; // wifi channel rotation counter global for display char display_lora[16], display_lmic[16], display_mem[16]; // display buffers -enum states LEDState = LED_OFF, previousLEDState = LED_OFF; // LED state global for state machine +led_states LEDState = LED_OFF; // LED state global for state machine +led_states previousLEDState = LED_ON; // This will force LED to be off at boot since State is OFF +unsigned long LEDBlinkSarted = 0; // When (in millis() led blink started) +uint16_t LEDBlinkDuration = 0; // How long the blink need to be +//uint16_t LEDBlinkduration = 0, LEDInterval = 0, color = COLOR_NONE; // state machine variables +uint16_t LEDColor = COLOR_NONE; // state machine variables bool joinstate = false; // LoRa network joined? global flag bool blinkdone = true; // flag for state machine for blinking LED once const uint32_t heapmem = ESP.getFreeHeap(); // free heap memory after start (:= 100%) @@ -70,13 +74,6 @@ int redirect_log(const char * fmt, va_list args) { } #endif -void blink_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval) { - color = set_color; // set color for RGB LED - LEDBlinkduration = set_blinkduration; // duration on - LEDInterval = set_interval; // duration off - on - off - blinkdone = false; -} - void reset_counters() { macs.clear(); // clear all macs container macs_total = 0; // reset all counters @@ -150,22 +147,9 @@ void lorawan_loop(void * pvParameters) { os_runloop_once(); - // LED indicators for viusalizing LoRaWAN state - if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { - // quick blink 20ms on each 1/5 second while joining - blink_LED(COLOR_YELLOW, 20, 200); - // TX data pending - } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { - // small blink 10ms on each 1/2sec (not when joining) - blink_LED(COLOR_BLUE, 10, 500); - // This should not happen so indicate a problem - } else if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) { - // heartbeat long blink 200ms on each 2 seconds - blink_LED(COLOR_RED, 200, 2000); - } else { - // led off - blink_LED(COLOR_NONE, 0, 0); - } + #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) + led_loop(); + #endif vTaskDelay(10/portTICK_PERIOD_MS); yield(); @@ -393,44 +377,85 @@ uint64_t uptime() { } #endif -#ifdef HAS_LED - void switchLED() { - - // led need to change state? avoid digitalWrite() for nothing - if (LEDState != previousLEDState) { - #ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, !LEDState); - #else - digitalWrite(HAS_LED, LEDState); - #endif +#if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) + void blink_LED(uint16_t set_color, uint16_t set_blinkduration) { + LEDColor = set_color; // set color for RGB LED + LEDBlinkDuration = set_blinkduration; // duration + LEDBlinkSarted = millis(); // Time Start here + LEDState = LED_ON; // Let main set LED on + } - #ifdef HAS_RGB_LED - rgb_set_color(LEDState ? color : COLOR_NONE); - #endif + void led_loop() { + // Custom blink running always have priority other LoRaWAN led management + if ( LEDBlinkSarted && LEDBlinkDuration) { - previousLEDState = LEDState; - blinkdone = LEDState ? false : true; // if LED was turned off, a blink was done + //ESP_LOGI(TAG, "Start=%ld for %g",LEDBlinkSarted, LEDBlinkDuration ); + + // Custom blink is finished, let this order, avoid millis() overflow + if ( (millis() - LEDBlinkSarted) >= LEDBlinkDuration) { + // Led besomes off, and stop blink + LEDState = LED_OFF; + LEDBlinkSarted = 0; + LEDBlinkDuration = 0; + LEDColor = COLOR_NONE ; + } else { + // In case of LoRaWAN led management blinked off + LEDState = LED_ON; + } + + // No custom blink, check LoRaWAN state + } else { + + // LED indicators for viusalizing LoRaWAN state + if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { + LEDColor = COLOR_YELLOW; + // quick blink 20ms on each 1/5 second + LEDState = ((millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending + } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { + LEDColor = COLOR_BLUE; + // small blink 10ms on each 1/2sec (not when joining) + LEDState = ((millis() % 500) < 20) ? LED_ON : LED_OFF; + // This should not happen so indicate a problem + } else if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) { + LEDColor = COLOR_RED; + // heartbeat long blink 200ms on each 2 seconds + LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF; + } else { + // led off + LEDColor = COLOR_NONE; + LEDState = LED_OFF; + } } - }; // switchLED() + //ESP_LOGI(TAG, "state=%d previous=%d Color=%d",LEDState, previousLEDState, LEDColor ); + // led need to change state? avoid digitalWrite() for nothing + if (LEDState != previousLEDState) { + if (LEDState == LED_ON) { + rgb_set_color(LEDColor); + #ifdef LED_ACTIVE_LOW + digitalWrite(HAS_LED, LOW); + #else + digitalWrite(HAS_LED, HIGH); + #endif + } else { + rgb_set_color(COLOR_NONE); + #ifdef LED_ACTIVE_LOW + digitalWrite(HAS_LED, HIGH); + #else + digitalWrite(HAS_LED, LOW); + #endif + } + previousLEDState = LEDState; + } + }; // led_loop() - void switchLEDstate() { - - if (LEDInterval) // LED is blinking, wait until time elapsed, then toggle LED - LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? LED_ON : LED_OFF; - - else // check if in oneblink mode - - if (!blinkdone) // keep LED on until one blink is done - LEDState = (currentMillis % LEDBlinkduration) > 0 ? LED_ON : LED_OFF; - - } // switchLEDstate() -#endif + #endif /* begin Aruino SETUP ------------------------------------------------------------ */ void setup() { + char features[64] = ""; // disable brownout detection #ifdef DISABLE_BROWNOUT @@ -470,18 +495,28 @@ void setup() { loadConfig(); // includes initialize if necessary // initialize led if needed -#ifdef HAS_LED +#if (HAS_LED != NOT_A_PIN) pinMode(HAS_LED, OUTPUT); - blink_LED(COLOR_NONE, 0, 0); // LED off + strcat(features, " LED"); #endif +#ifdef HAS_RGB_LED + rgb_set_color(COLOR_PINK); + strcat(features, " RGB"); + delay(1000); +#endif + + // initialize button handling if needed #ifdef HAS_BUTTON + strcat(features, " BTN_"); #ifdef BUTTON_PULLUP + strcat(features, "PU"); // install button interrupt (pullup mode) pinMode(HAS_BUTTON, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), isr_button_pressed, RISING); #else + strcat(features, "PD"); // install button interrupt (pulldown mode) pinMode(HAS_BUTTON, INPUT_PULLDOWN); attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), isr_button_pressed, FALLING); @@ -490,11 +525,13 @@ void setup() { // initialize wifi antenna if needed #ifdef HAS_ANTENNA_SWITCH + strcat(features, " ANT"); antenna_init(); #endif #ifdef HAS_DISPLAY -// initialize display + strcat(features, " OLED"); + // initialize display init_display(PROGNAME, PROGVERSION); DisplayState = cfg.screenon; u8x8.setPowerSave(!cfg.screenon); // set display off if disabled @@ -511,6 +548,9 @@ void setup() { sprintf(display_lora, "Join wait"); #endif +// Display features compiled for +ESP_LOGI(TAG, "Features %s", features); + // output LoRaWAN keys to console #ifdef VERBOSE printKeys(); @@ -553,15 +593,13 @@ do_send(&sendjob); void loop() { // simple state machine for controlling display, LED, button, etc. - uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) currentMillis = millis(); // timebase for state machine in milliseconds (32bit) - - #ifdef HAS_LED - switchLEDstate(); - switchLED(); - #endif + #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) + led_loop(); + #endif + #ifdef HAS_BUTTON readButton(); #endif diff --git a/src/main.h b/src/main.h index 438d70cd..8e6a7445 100644 --- a/src/main.h +++ b/src/main.h @@ -5,7 +5,7 @@ //--- Declarations --- -enum states { +enum led_states { LED_OFF, LED_ON }; @@ -13,8 +13,10 @@ enum states { //--- Prototypes --- // defined in main.cpp -void blink_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval); void reset_counters(); +void reset_counters(void); +void blink_LED(uint16_t set_color, uint16_t set_blinkduration); +void led_loop(void); // defined in configmanager.cpp void eraseConfig(void); @@ -35,6 +37,3 @@ void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); // defined in blescan.cpp void bt_loop(void *ignore); - -// defined in main.cpp -void reset_counters(void); \ No newline at end of file From 3912322c9e4a98e748dfd1f947616afc95393520 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 26 Apr 2018 19:05:52 +0200 Subject: [PATCH 11/13] minor code sanitizations --- src/blecsan.cpp | 2 +- src/main.cpp | 27 +++++++++++++-------------- src/main.h | 3 +-- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 1d3c70da..8dd4bb15 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -257,7 +257,7 @@ void bt_loop(void * pvParameters) goto end; } - esp_bt_controller_mem_release(ESP_BT_MODE_BTDM); // gives 30KB more RAM for heap + //esp_bt_controller_mem_release(ESP_BT_MODE_BTDM); // gives 30KB more RAM for heap // Init and alloc the resource for bluetooth, must be prior to every bluetooth stuff ESP_LOGI(TAG, "Init Bluetooth stack"); diff --git a/src/main.cpp b/src/main.cpp index 71b7ad81..70b1003e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,14 +47,13 @@ uint8_t DisplayState = 0; // globals for state machine uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // MAC counters globals for display uint8_t channel = 0; // wifi channel rotation counter global for display char display_lora[16], display_lmic[16], display_mem[16]; // display buffers -led_states LEDState = LED_OFF; // LED state global for state machine -led_states previousLEDState = LED_ON; // This will force LED to be off at boot since State is OFF -unsigned long LEDBlinkSarted = 0; // When (in millis() led blink started) -uint16_t LEDBlinkDuration = 0; // How long the blink need to be -//uint16_t LEDBlinkduration = 0, LEDInterval = 0, color = COLOR_NONE; // state machine variables -uint16_t LEDColor = COLOR_NONE; // state machine variables +led_states LEDState = LED_OFF; // LED state global for state machine +led_states previousLEDState = LED_ON; // This will force LED to be off at boot since State is OFF +unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started) +uint16_t LEDBlinkDuration = 0; // How long the blink need to be +uint16_t LEDColor = COLOR_NONE; // state machine variable to set RGB LED color bool joinstate = false; // LoRa network joined? global flag -bool blinkdone = true; // flag for state machine for blinking LED once +bool blinkdone = true; // flag for state machine for blinking LED once const uint32_t heapmem = ESP.getFreeHeap(); // free heap memory after start (:= 100%) std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) @@ -381,21 +380,21 @@ uint64_t uptime() { void blink_LED(uint16_t set_color, uint16_t set_blinkduration) { LEDColor = set_color; // set color for RGB LED LEDBlinkDuration = set_blinkduration; // duration - LEDBlinkSarted = millis(); // Time Start here + LEDBlinkStarted = millis(); // Time Start here LEDState = LED_ON; // Let main set LED on } void led_loop() { // Custom blink running always have priority other LoRaWAN led management - if ( LEDBlinkSarted && LEDBlinkDuration) { + if ( LEDBlinkStarted && LEDBlinkDuration) { - //ESP_LOGI(TAG, "Start=%ld for %g",LEDBlinkSarted, LEDBlinkDuration ); + //ESP_LOGI(TAG, "Start=%ld for %g",LEDBlinkStarted, LEDBlinkDuration ); // Custom blink is finished, let this order, avoid millis() overflow - if ( (millis() - LEDBlinkSarted) >= LEDBlinkDuration) { - // Led besomes off, and stop blink + if ( (millis() - LEDBlinkStarted) >= LEDBlinkDuration) { + // Led becomes off, and stop blink LEDState = LED_OFF; - LEDBlinkSarted = 0; + LEDBlinkStarted = 0; LEDBlinkDuration = 0; LEDColor = COLOR_NONE ; } else { @@ -574,7 +573,7 @@ xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 2048, ( void * ) 1, 1, NULL #ifdef BLECOUNTER if (cfg.blescan) { // start BLE task only if BLE function is enabled in NVRAM configuration ESP_LOGI(TAG, "Starting Bluetooth task on core 0"); - xTaskCreatePinnedToCore(bt_loop, "btscan", 2048, ( void * ) 1, 1, NULL, 0); + xTaskCreatePinnedToCore(bt_loop, "btscan", 4096, ( void * ) 1, 1, NULL, 0); } #endif diff --git a/src/main.h b/src/main.h index 8e6a7445..99e27afe 100644 --- a/src/main.h +++ b/src/main.h @@ -1,6 +1,6 @@ // program version - note: increment version after modifications to configData_t struct!! -#define PROGVERSION "1.3.21" // use max 10 chars here! +#define PROGVERSION "1.3.22" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- @@ -13,7 +13,6 @@ enum led_states { //--- Prototypes --- // defined in main.cpp -void reset_counters(); void reset_counters(void); void blink_LED(uint16_t set_color, uint16_t set_blinkduration); void led_loop(void); From 1c7d9292015253ae260596d5d5a2cc7db2fb231c Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 26 Apr 2018 23:25:56 +0200 Subject: [PATCH 12/13] v1.3.22 --- platformio.ini | 4 ++-- src/paxcounter.conf | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index f780a017..5f249bba 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,9 +11,9 @@ ; ---> SELECT TARGET PLATFORM HERE! <--- [platformio] -env_default = heltec_wifi_lora_32 +;env_default = heltec_wifi_lora_32 ;env_default = ttgov1 -;env_default = ttgov2 +env_default = ttgov2 ;env_default = lopy ;env_default = lopy4 ;env_default = lolin32lite_lora diff --git a/src/paxcounter.conf b/src/paxcounter.conf index bfae6146..30189b7b 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -33,8 +33,8 @@ #define WIFI_CHANNEL_SWITCH_INTERVAL 50 // [seconds/100] -> 0,5 sec. // LoRa payload send cycle -//#define SEND_SECS 120 // [seconds/2] -> 240 sec. -#define SEND_SECS 30 // [seconds/2] -> 60 sec. +#define SEND_SECS 120 // [seconds/2] -> 240 sec. +//#define SEND_SECS 30 // [seconds/2] -> 60 sec. // Default LoRa Spreadfactor #define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs From bf14b061b734edf98533b6214dd8283c9f945462 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 26 Apr 2018 23:26:33 +0200 Subject: [PATCH 13/13] v1.3.22 --- platformio.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platformio.ini b/platformio.ini index 5f249bba..f780a017 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,9 +11,9 @@ ; ---> SELECT TARGET PLATFORM HERE! <--- [platformio] -;env_default = heltec_wifi_lora_32 +env_default = heltec_wifi_lora_32 ;env_default = ttgov1 -env_default = ttgov2 +;env_default = ttgov2 ;env_default = lopy ;env_default = lopy4 ;env_default = lolin32lite_lora