From 688d0993afe514c59bd33310679d31b9fcd62529 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 17 Apr 2018 18:08:47 +0200 Subject: [PATCH 01/46] state machine (part 1) --- src/globals.h | 2 +- src/lorawan.cpp | 3 - src/main.cpp | 361 ++++++++++++++++++++++++++--------------------- src/rcommand.cpp | 4 +- 4 files changed, 204 insertions(+), 166 deletions(-) diff --git a/src/globals.h b/src/globals.h index 72598669..a484a192 100644 --- a/src/globals.h +++ b/src/globals.h @@ -44,7 +44,7 @@ typedef struct { extern configData_t cfg; extern uint8_t mydata[]; -extern uint64_t uptimecounter; +extern uint64_t currentMillis ; extern osjob_t sendjob; extern char display_lora[], display_lmic[]; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; diff --git a/src/lorawan.cpp b/src/lorawan.cpp index fd8693e3..86e4fff8 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -13,9 +13,6 @@ // Local logging Tag static const char *TAG = "lorawan"; -// function defined in main.cpp -void set_onboard_led(int state); - // functions defined in rcommand.cpp void rcommand(int cmd, int arg); void switch_lora(int sf, int tx); diff --git a/src/main.cpp b/src/main.cpp index 2bfadabd..ec3fa91b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,8 +47,10 @@ osjob_t sendjob, initjob; // LMIC char display_lora[16], display_lmic[16]; uint8_t channel = 0; int macnum = 0; -uint64_t uptimecounter = 0; +uint64_t currentMillis = 0, previousLEDmillis = 0, previousDisplaymillis = 0; bool joinstate = false; +uint8_t DisplayState, LEDState; +uint16_t LEDBlinkduration = 500, LEDInterval = 1000, color = COLOR_NONE; std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) std::set wifis; // associative container holds unique Wifi MAC adress hashes @@ -77,10 +79,6 @@ void eraseConfig(void); void saveConfig(void); void loadConfig(void); -#ifdef HAS_LED - void set_onboard_led(int st); -#endif - /* begin LMIC specific parts ------------------------------------------------------------ */ // defined in lorawan.cpp @@ -151,48 +149,37 @@ static void lora_init (osjob_t* j) { void lorawan_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check - static bool led_state; - bool new_led_state; - while(1) { - uint16_t color; + os_runloop_once(); - // All follow is Led management - // Let join at the begining of if sequence, - // is prior to send because joining state send data + // LED management for viusalizing LoRaWAN state if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { - color = COLOR_YELLOW; // quick blink 20ms on each 1/5 second - new_led_state = ((millis() % 200) < 20) ? HIGH : LOW; - + color = COLOR_YELLOW; + LEDBlinkduration = 20; + LEDInterval = 200; + LEDState = 1; // TX data pending } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { - color = COLOR_BLUE; // small blink 10ms on each 1/2sec (not when joining) - new_led_state = ((millis() % 500) < 20) ? HIGH : LOW; - + color = COLOR_BLUE; + LEDBlinkduration = 10; + LEDInterval = 500; + LEDState =1; // This should not happen so indicate a problem } else if ( LMIC.opmode & (OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0 ) { - color = COLOR_RED; // heartbeat long blink 200ms on each 2 seconds - new_led_state = ((millis() % 2000) < 200) ? HIGH : LOW; + color = COLOR_RED; + LEDBlinkduration = 200; + LEDInterval = 2000; + LEDState = 1; } else { // led off - rgb_set_color(COLOR_NONE); + color = COLOR_NONE; + LEDState = 0; } - // led need to change state? avoid digitalWrite() for nothing - if (led_state != new_led_state) { - if (new_led_state == HIGH) { - set_onboard_led(1); - rgb_set_color(color); - } else { - set_onboard_led(0); - rgb_set_color(COLOR_NONE); - } - led_state = new_led_state; - } - + vTaskDelay(10/portTICK_PERIOD_MS); yield(); } @@ -218,21 +205,6 @@ void lorawan_loop(void * pvParameters) { bool btstop = btStop(); #endif -void set_onboard_led(int st){ -#ifdef HAS_LED - switch (st) { - #ifdef LED_ACTIVE_LOW - case 1: digitalWrite(HAS_LED, LOW); break; - case 0: digitalWrite(HAS_LED, HIGH); break; - #else - case 1: digitalWrite(HAS_LED, HIGH); break; - case 0: digitalWrite(HAS_LED, LOW); break; - #endif - } -#endif -}; - - #ifdef HAS_BUTTON // Button Handling, board dependent -> perhaps to be moved to hal/<$board.h> // IRAM_ATTR necessary here, see https://github.com/espressif/arduino-esp32/issues/855 @@ -303,15 +275,6 @@ void sniffer_loop(void * pvParameters) { yield(); } sprintf(display_lora, " "); // clear LoRa wait message fromd display - - /* - // TBD: need to check if long 2000ms pause causes stack problems while scanning continues - if (cfg.screenon && cfg.screensaver) { - vTaskDelay(2000/portTICK_PERIOD_MS); // pause for displaying results - yield(); - u8x8.setPowerSave(1 && cfg.screensaver); // set display off if screensaver is enabled - } - */ } // end of send data cycle @@ -330,68 +293,165 @@ uint64_t uptime() { } #ifdef HAS_DISPLAY -// Print a key on display -void DisplayKey(const uint8_t * key, uint8_t len, bool lsb) { - uint8_t start=lsb?len:0; - uint8_t end = lsb?0:len; - const uint8_t * p ; - for (uint8_t i=0; i= ( 1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS))) { + refreshDisplay(); + previousDisplaymillis += ( 1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS)); + } + // set display on/off according to current device configuration + if (DisplayState != cfg.screenon) { + DisplayState = cfg.screenon; + u8x8.setPowerSave(!cfg.screenon); + } + } // updateDisplay() - // Display chip information - #ifdef VERBOSE - 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.features & CHIP_FEATURE_BT) ? "/BT" : "", - (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : ""); - u8x8.printf("ESP Rev.%d\n", chip_info.revision); - u8x8.printf("%dMB %s Flash\n", spi_flash_get_chip_size() / (1024 * 1024), - (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "int." : "ext."); - #endif // VERBOSE - u8x8.print(Productname); - u8x8.print(" v"); - u8x8.println(PROGVERSION); - u8x8.println("DEVEUI:"); - os_getDevEui((u1_t*) buf); - DisplayKey(buf, 8, true); - delay(5000); - u8x8.clear(); -} #endif // HAS_DISPLAY +#ifdef HAS_BUTTON + void readButton() { + if (ButtonTriggered) { + ButtonTriggered = false; + ESP_LOGI(TAG, "Button pressed, resetting device to factory defaults"); + eraseConfig(); + esp_restart(); + } + } +#endif + +#ifdef HAS_LED + void updateLEDstatus() { + + if (LEDState == LOW) { + if (currentMillis - previousLEDmillis >= LEDInterval) { + LEDState = HIGH; + previousLEDmillis += LEDInterval; + } + } + else { + if (currentMillis - previousLEDmillis >= LEDBlinkduration) { + LEDState = LOW; + previousLEDmillis += LEDBlinkduration; + } + } + + }; // updateLEDstatus() + + void refreshLED() { + static bool previousLEDState; + // 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 + + #ifdef HAS_RGB_LED + rgb_set_color(color); + #endif + previousLEDState = LEDState; + } + }; // refreshLED() +#endif + + /* begin Aruino SETUP ------------------------------------------------------------ */ void setup() { @@ -436,8 +496,17 @@ void setup() { // initialize led if needed #ifdef HAS_LED + LEDState = 0; + color = COLOR_NONE; pinMode(HAS_LED, OUTPUT); - digitalWrite(HAS_LED, LOW); + #ifdef LED_ACTIVE_LOW + digitalWrite(HAS_LED, LEDState); + #else + digitalWrite(HAS_LED, !LEDState); + #endif + #ifdef HAS_RBG_LED + rgb_set_color (color); + #endif #endif // initialize button handling if needed @@ -460,7 +529,8 @@ void setup() { #ifdef HAS_DISPLAY // initialize display - init_display(PROGNAME, PROGVERSION); + init_display(PROGNAME, PROGVERSION); + DisplayState = cfg.screenon; u8x8.setPowerSave(!cfg.screenon); // set display off if disabled u8x8.draw2x2String(0, 0, "PAX:0"); u8x8.setCursor(0,4); @@ -513,55 +583,26 @@ do_send(&sendjob); // https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/ void loop() { - uptimecounter = uptime() / 1000; // count uptime seconds + // state machine for central control of all timimg based features + + currentMillis = uptime() / 1000; // count uptime seconds - #ifdef HAS_BUTTON // ...then check if pressed - if (ButtonTriggered) { - ButtonTriggered = false; - ESP_LOGI(TAG, "Button pressed, resetting device to factory defaults"); - eraseConfig(); - esp_restart(); - } - #endif - - #ifdef HAS_DISPLAY // ...then update mask - - // set display on/off according to current device configuration - u8x8.setPowerSave(!cfg.screenon); - - // update counter display (lines 0-4) - char buff[16]; - snprintf(buff, sizeof(buff), "PAX:%-4d", (int) macs.size()); // convert 16-bit MAC counter to decimal counter value - u8x8.draw2x2String(0, 0, buff); // display number on unique macs total Wifi + BLE - u8x8.setCursor(0,4); - u8x8.printf("WIFI: %-4d", (int) wifis.size()); - #ifdef BLECOUNTER - u8x8.setCursor(0,3); - if (cfg.blescan) - u8x8.printf("BLTH: %-4d", (int) bles.size()); - else - u8x8.printf("%-16s", "BLTH: off"); - #endif - - // update wifi channel display (line 4) - u8x8.setCursor(11,4); - u8x8.printf("ch:%02i", channel); - - // update RSSI limiter status display (line 5) - u8x8.setCursor(0,5); - u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %-4d", cfg.rssilimit); - - // update LoRa status display (line 6) - u8x8.setCursor(0,6); - u8x8.printf("%-16s", display_lora); - - // update LMiC event display (line 7) - u8x8.setCursor(0,7); - u8x8.printf("%-16s", display_lmic); - + #ifdef HAS_BUTTON + readButton(); #endif - vTaskDelay(1000/DISPLAYFPS/portTICK_PERIOD_MS); + #ifdef HAS_DISPLAY + updateDisplay(); + #endif + + #ifdef HAS_LED + updateLEDstatus(); + refreshLED(); + #endif + + //sendPayload(); + + } diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 29ed2f1c..4119b0b0 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -210,9 +210,9 @@ void get_config (int val) { void get_uptime (int val) { ESP_LOGI(TAG, "Remote command: get uptime"); - int size = sizeof(uptimecounter); + int size = sizeof(currentMillis ); unsigned char *sendData = new unsigned char[size]; - memcpy(sendData, (unsigned char*)&uptimecounter, size); + memcpy(sendData, (unsigned char*)¤tMillis , size); LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port delete sendData; // free memory ESP_LOGI(TAG, "%i bytes queued in send queue", size-1); From eefac5903082888230d1f46f869d34f7421d9cbd Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 17 Apr 2018 18:46:01 +0200 Subject: [PATCH 02/46] state machine (part 2) --- platformio.ini | 8 ++++---- src/main.cpp | 46 +++++++++++++++++++++------------------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/platformio.ini b/platformio.ini index fff50de0..2d11dae3 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 @@ -31,8 +31,8 @@ build_flags = ; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- ; otherwise device may crash in dense environments due to serial buffer overflow ; - -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE -; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO +; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE + -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG ; diff --git a/src/main.cpp b/src/main.cpp index ec3fa91b..05324c57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,13 +44,13 @@ configData_t cfg; // struct holds current device configuration osjob_t sendjob, initjob; // LMIC // Initialize global variables -char display_lora[16], display_lmic[16]; -uint8_t channel = 0; -int macnum = 0; +#define DISPLAYREFRESH_MS (1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS)) // calculate ms from fps +char display_lora[16], display_lmic[16]; // display buffers uint64_t currentMillis = 0, previousLEDmillis = 0, previousDisplaymillis = 0; -bool joinstate = false; uint8_t DisplayState, LEDState; uint16_t LEDBlinkduration = 500, LEDInterval = 1000, color = COLOR_NONE; +uint8_t channel = 0; // wifi channel counter +bool joinstate = false; std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) std::set wifis; // associative container holds unique Wifi MAC adress hashes @@ -86,7 +86,6 @@ void gen_lora_deveui(uint8_t * pdeveui); void RevBytes(unsigned char* b, size_t c); void get_hard_deveui(uint8_t *pdeveui); - #ifdef VERBOSE void printKeys(void); #endif // VERBOSE @@ -145,6 +144,13 @@ static void lora_init (osjob_t* j) { LMIC_startJoining(); } +void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_state) { + color = set_color; + LEDBlinkduration = set_blinkduration; + LEDInterval = set_interval; + LEDState = set_state; +} + // LMIC FreeRTos Task void lorawan_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check @@ -153,31 +159,21 @@ void lorawan_loop(void * pvParameters) { os_runloop_once(); - // LED management for viusalizing LoRaWAN state + // LED indicators for viusalizing LoRaWAN state if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { // quick blink 20ms on each 1/5 second - color = COLOR_YELLOW; - LEDBlinkduration = 20; - LEDInterval = 200; - LEDState = 1; + set_LED(COLOR_YELLOW, 20, 200, 1); // TX data pending } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { // small blink 10ms on each 1/2sec (not when joining) - color = COLOR_BLUE; - LEDBlinkduration = 10; - LEDInterval = 500; - LEDState =1; + set_LED(COLOR_BLUE, 10, 500, 1); // 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 - color = COLOR_RED; - LEDBlinkduration = 200; - LEDInterval = 2000; - LEDState = 1; + set_LED(COLOR_RED, 200, 2000, 1); } else { // led off - color = COLOR_NONE; - LEDState = 0; + set_LED(COLOR_NONE, 0, 0, 0); } vTaskDelay(10/portTICK_PERIOD_MS); @@ -390,9 +386,9 @@ uint64_t uptime() { void updateDisplay() { // timed display refresh according to frames per second setting - if (currentMillis - previousDisplaymillis >= ( 1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS))) { + if (currentMillis - previousDisplaymillis >= DISPLAYREFRESH_MS) { refreshDisplay(); - previousDisplaymillis += ( 1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS)); + previousDisplaymillis += DISPLAYREFRESH_MS; } // set display on/off according to current device configuration if (DisplayState != cfg.screenon) { @@ -418,15 +414,15 @@ uint64_t uptime() { #ifdef HAS_LED void updateLEDstatus() { - if (LEDState == LOW) { + if (LEDState == 0) { if (currentMillis - previousLEDmillis >= LEDInterval) { - LEDState = HIGH; + LEDState = 1; previousLEDmillis += LEDInterval; } } else { if (currentMillis - previousLEDmillis >= LEDBlinkduration) { - LEDState = LOW; + LEDState = 0; previousLEDmillis += LEDBlinkduration; } } From cbca740792580ce6c838d3c6e580957af036d1e2 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 17 Apr 2018 19:20:54 +0200 Subject: [PATCH 03/46] state machine (part 3) --- src/globals.h | 3 ++- src/main.cpp | 8 +++++--- src/main.h | 4 ++-- src/rcommand.cpp | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/globals.h b/src/globals.h index a484a192..c695a388 100644 --- a/src/globals.h +++ b/src/globals.h @@ -44,7 +44,8 @@ typedef struct { extern configData_t cfg; extern uint8_t mydata[]; -extern uint64_t currentMillis ; +extern uint64_t uptimecounter; +extern uint32_t currentMillis ; extern osjob_t sendjob; extern char display_lora[], display_lmic[]; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; diff --git a/src/main.cpp b/src/main.cpp index 05324c57..788ee74b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,9 +44,9 @@ configData_t cfg; // struct holds current device configuration osjob_t sendjob, initjob; // LMIC // Initialize global variables -#define DISPLAYREFRESH_MS (1 / DISPLAYFPS * (1000/portTICK_PERIOD_MS)) // calculate ms from fps char display_lora[16], display_lmic[16]; // display buffers -uint64_t currentMillis = 0, previousLEDmillis = 0, previousDisplaymillis = 0; +uint64_t uptimecounter = 0; +uint32_t currentMillis = 0, previousLEDmillis = 0, previousDisplaymillis = 0; uint8_t DisplayState, LEDState; uint16_t LEDBlinkduration = 500, LEDInterval = 1000, color = COLOR_NONE; uint8_t channel = 0; // wifi channel counter @@ -579,9 +579,11 @@ do_send(&sendjob); // https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/ void loop() { + uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) + // state machine for central control of all timimg based features - currentMillis = uptime() / 1000; // count uptime seconds + currentMillis = millis(); // timebase for state machine in milliseconds (32bit) #ifdef HAS_BUTTON readButton(); diff --git a/src/main.h b/src/main.h index 490fea31..f077f9e0 100644 --- a/src/main.h +++ b/src/main.h @@ -1,7 +1,7 @@ #pragma once // program version - note: increment version after modifications to configData_t struct!! -#define PROGVERSION "1.3.01" // use max 10 chars here! +#define PROGVERSION "1.3.1" // use max 10 chars here! #define PROGNAME "PAXCNT" // Verbose enables serial output @@ -47,7 +47,7 @@ #define RGBLUMINOSITY 30 // 30% // OLED Display refresh cycle (in Milliseconds) -#define DISPLAYFPS 5 // [fps] -> 5 Frames per second ps = 200ms refreseh cycle +#define DISPLAYREFRESH_MS 40 // e.g. 40ms -> 1000/40 = 25 frames per second // LMIC settings // define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 4119b0b0..0ef9f2ef 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -210,9 +210,9 @@ void get_config (int val) { void get_uptime (int val) { ESP_LOGI(TAG, "Remote command: get uptime"); - int size = sizeof(currentMillis ); + int size = sizeof(uptimecounter); unsigned char *sendData = new unsigned char[size]; - memcpy(sendData, (unsigned char*)¤tMillis , size); + memcpy(sendData, (unsigned char*)&uptimecounter , size); LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port delete sendData; // free memory ESP_LOGI(TAG, "%i bytes queued in send queue", size-1); From f6864b19731e240e83c8532323293e46f5e1a1e6 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 17 Apr 2018 19:28:58 +0200 Subject: [PATCH 04/46] state machine (part 4) --- src/main.cpp | 11 ++++------- src/main.h | 4 ++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 788ee74b..1da702c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -385,7 +385,7 @@ uint64_t uptime() { } void updateDisplay() { - // timed display refresh according to frames per second setting + // timed display refresh according to refresh cycle setting if (currentMillis - previousDisplaymillis >= DISPLAYREFRESH_MS) { refreshDisplay(); previousDisplaymillis += DISPLAYREFRESH_MS; @@ -580,10 +580,9 @@ do_send(&sendjob); void loop() { uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) - - // state machine for central control of all timimg based features - currentMillis = millis(); // timebase for state machine in milliseconds (32bit) + + // simple state machine for controlling display, LED, button, etc. #ifdef HAS_BUTTON readButton(); @@ -598,9 +597,7 @@ void loop() { refreshLED(); #endif - //sendPayload(); - - + //sendPayload(); } diff --git a/src/main.h b/src/main.h index f077f9e0..cd606adb 100644 --- a/src/main.h +++ b/src/main.h @@ -35,8 +35,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 758a960371cfdca76580d9985cedfeee958fc3a9 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 17 Apr 2018 22:42:06 +0200 Subject: [PATCH 05/46] new led management (part 1) --- platformio.ini | 4 +-- src/lorawan.cpp | 14 ++++---- src/macsniff.cpp | 12 +++---- src/main.cpp | 84 +++++++++++++++++++----------------------------- 4 files changed, 47 insertions(+), 67 deletions(-) diff --git a/platformio.ini b/platformio.ini index 2d11dae3..8be3a272 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/lorawan.cpp b/src/lorawan.cpp index 86e4fff8..1832d2f5 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -116,13 +116,13 @@ void do_send(osjob_t* j){ mydata[1] = data & 0xff; #ifdef BLECOUNTER - // Sum of unique BLE MACs seen - data = (uint16_t) bles.size(); - mydata[2] = (data & 0xff00) >> 8; - mydata[3] = data & 0xff; + // Sum of unique BLE MACs seen + data = (uint16_t) bles.size(); + mydata[2] = (data & 0xff00) >> 8; + mydata[3] = data & 0xff; #else - mydata[2] = 0; - mydata[3] = 0; + mydata[2] = 0; + mydata[3] = 0; #endif // Total BLE+WIFI unique MACs seen @@ -164,7 +164,7 @@ void onEvent (ev_t ev) { case EV_JOINED: strcpy_P(buff, PSTR("JOINED")); - sprintf(display_lora, " "); // erase "Join Wait" message from display + sprintf(display_lora, ""); // erase "Join Wait" message from display // Disable link check validation (automatically enabled // during join, but not supported by TTN at this time). LMIC_setLinkCheckMode(0); diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 6084d6be..e57526b1 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -11,6 +11,9 @@ // Local logging tag static const char *TAG = "macsniff"; +// defined in main.cpp +void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count); + static wifi_country_t wifi_country = {.cc=WIFI_MY_COUNTRY, .schan=WIFI_CHANNEL_MIN, .nchan=WIFI_CHANNEL_MAX, .policy=WIFI_COUNTRY_POLICY_MANUAL}; uint16_t salt; @@ -49,21 +52,16 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // Insert only if it was not found on global count if (added) { - if (sniff_type == MAC_SNIFF_WIFI ) { - rgb_set_color(COLOR_GREEN); + set_LED(COLOR_GREEN, 20, 0, 1); wifis.insert(hashedmac); // add hashed MAC to wifi container } #ifdef BLECOUNTER else if (sniff_type == MAC_SNIFF_BLE ) { - rgb_set_color(COLOR_MAGENTA); + set_LED(COLOR_MAGENTA, 20, 0, 1); bles.insert(hashedmac); // add hashed MAC to BLE container } #endif - - // Not sure user will have time to see the LED - // TBD do light off further in the code - rgb_set_color(COLOR_NONE); } ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s", diff --git a/src/main.cpp b/src/main.cpp index 1da702c7..5c0e608b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,9 +46,9 @@ osjob_t sendjob, initjob; // LMIC // Initialize global variables char display_lora[16], display_lmic[16]; // display buffers uint64_t uptimecounter = 0; -uint32_t currentMillis = 0, previousLEDmillis = 0, previousDisplaymillis = 0; -uint8_t DisplayState, LEDState; -uint16_t LEDBlinkduration = 500, LEDInterval = 1000, color = COLOR_NONE; +uint32_t currentMillis = 0, previousDisplaymillis = 0; +uint8_t DisplayState, LEDState = 0, LEDcount = 0; +uint16_t LEDBlinkduration = 0, LEDInterval = 0, color=COLOR_NONE; uint8_t channel = 0; // wifi channel counter bool joinstate = false; @@ -79,6 +79,14 @@ void eraseConfig(void); void saveConfig(void); void loadConfig(void); +void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count) { + color = set_color; // set color for RGB LED + LEDBlinkduration = set_blinkduration; // duration on + LEDInterval = set_interval; // duration off - on - off + LEDcount = set_count * 2; // number of blinks before LED off + LEDState = set_count ? 1 : 0; // sets LED to off if 0 blinks +} + /* begin LMIC specific parts ------------------------------------------------------------ */ // defined in lorawan.cpp @@ -144,13 +152,6 @@ static void lora_init (osjob_t* j) { LMIC_startJoining(); } -void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_state) { - color = set_color; - LEDBlinkduration = set_blinkduration; - LEDInterval = set_interval; - LEDState = set_state; -} - // LMIC FreeRTos Task void lorawan_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check @@ -161,16 +162,16 @@ void lorawan_loop(void * pvParameters) { // LED indicators for viusalizing LoRaWAN state if ( LMIC.opmode & (OP_JOINING | OP_REJOIN) ) { - // quick blink 20ms on each 1/5 second - set_LED(COLOR_YELLOW, 20, 200, 1); + // 5 quick blinks 20ms on each 1/5 second while joining + set_LED(COLOR_YELLOW, 20, 200, 5); // TX data pending } else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) { - // small blink 10ms on each 1/2sec (not when joining) - set_LED(COLOR_BLUE, 10, 500, 1); + // 3 small blink 10ms on each 1/2sec (not when joining) + set_LED(COLOR_BLUE, 10, 500, 3); // 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 - set_LED(COLOR_RED, 200, 2000, 1); + // 5 heartbeat long blink 200ms on each 2 seconds + set_LED(COLOR_RED, 200, 2000, 5); } else { // led off set_LED(COLOR_NONE, 0, 0, 0); @@ -270,7 +271,7 @@ void sniffer_loop(void * pvParameters) { vTaskDelay(1000/portTICK_PERIOD_MS); yield(); } - sprintf(display_lora, " "); // clear LoRa wait message fromd display + sprintf(display_lora, ""); // clear LoRa wait message fromd display } // end of send data cycle @@ -412,39 +413,30 @@ uint64_t uptime() { #endif #ifdef HAS_LED - void updateLEDstatus() { - - if (LEDState == 0) { - if (currentMillis - previousLEDmillis >= LEDInterval) { - LEDState = 1; - previousLEDmillis += LEDInterval; - } - } - else { - if (currentMillis - previousLEDmillis >= LEDBlinkduration) { - LEDState = 0; - previousLEDmillis += LEDBlinkduration; - } - } - - }; // updateLEDstatus() - - void refreshLED() { + void switchLED() { static bool previousLEDState; // 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); + #else + digitalWrite(HAS_LED, LEDState); #endif #ifdef HAS_RGB_LED rgb_set_color(color); #endif previousLEDState = LEDState; + LEDcount--; // decrement blink counter } - }; // refreshLED() + }; // switchLED() + + void switchLEDstate() { + if (!LEDcount) // no more blinks? -> switch off LED + LEDState = 0; + else if (LEDInterval) // blinks left? -> toggle LED and decrement blinks + LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? 1 : 0; + } // switchLEDstate() #endif @@ -469,7 +461,6 @@ void setup() { #endif ESP_LOGI(TAG, "Starting %s %s", PROGNAME, PROGVERSION); - rgb_set_color(COLOR_NONE); // initialize system event handler for wifi task, needed for wifi_sniffer_init() esp_event_loop_init(NULL, NULL); @@ -492,17 +483,8 @@ void setup() { // initialize led if needed #ifdef HAS_LED - LEDState = 0; - color = COLOR_NONE; pinMode(HAS_LED, OUTPUT); - #ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, LEDState); - #else - digitalWrite(HAS_LED, !LEDState); - #endif - #ifdef HAS_RBG_LED - rgb_set_color (color); - #endif + set_LED(COLOR_NONE, 0, 0, 0); // LED off #endif // initialize button handling if needed @@ -593,8 +575,8 @@ void loop() { #endif #ifdef HAS_LED - updateLEDstatus(); - refreshLED(); + switchLEDstate(); + switchLED(); #endif //sendPayload(); From bf6a21f44f79f4803549c72dcdcff833178db679 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 17 Apr 2018 23:05:12 +0200 Subject: [PATCH 06/46] new led management (part 2) --- platformio.ini | 4 ++-- src/macsniff.cpp | 4 ++-- src/main.cpp | 5 ++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/platformio.ini b/platformio.ini index 8be3a272..e3217138 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,10 +11,10 @@ ; ---> 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 = lopy +env_default = lopy ;env_default = lopy4 ;env_default = lolin32lite_lora ;env_default = lolin32_lora diff --git a/src/macsniff.cpp b/src/macsniff.cpp index e57526b1..6762d662 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -53,12 +53,12 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // Insert only if it was not found on global count if (added) { if (sniff_type == MAC_SNIFF_WIFI ) { - set_LED(COLOR_GREEN, 20, 0, 1); + set_LED(COLOR_GREEN, 50, 0, 1); wifis.insert(hashedmac); // add hashed MAC to wifi container } #ifdef BLECOUNTER else if (sniff_type == MAC_SNIFF_BLE ) { - set_LED(COLOR_MAGENTA, 20, 0, 1); + set_LED(COLOR_MAGENTA, 50, 0, 1); bles.insert(hashedmac); // add hashed MAC to BLE container } #endif diff --git a/src/main.cpp b/src/main.cpp index 5c0e608b..f45403e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -424,7 +424,10 @@ uint64_t uptime() { #endif #ifdef HAS_RGB_LED - rgb_set_color(color); + if (LEDState) + rgb_set_color(color); // LED on + else + rgb_set_color(COLOR_NONE); // LED off #endif previousLEDState = LEDState; LEDcount--; // decrement blink counter From e41aaa45a0b797c2337c3ebcf70782b5cd61cc53 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 17 Apr 2018 23:14:01 +0200 Subject: [PATCH 07/46] cleanups --- platformio.ini | 4 ++-- src/main.cpp | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index e3217138..8be3a272 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,10 +11,10 @@ ; ---> 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 = lopy +;env_default = lopy ;env_default = lopy4 ;env_default = lolin32lite_lora ;env_default = lolin32_lora diff --git a/src/main.cpp b/src/main.cpp index f45403e9..73b6b343 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -424,11 +424,9 @@ uint64_t uptime() { #endif #ifdef HAS_RGB_LED - if (LEDState) - rgb_set_color(color); // LED on - else - rgb_set_color(COLOR_NONE); // LED off + rgb_set_color(LEDState ? color : COLOR_NONE); #endif + previousLEDState = LEDState; LEDcount--; // decrement blink counter } From 7f7dfaeec8815036543e9be8b340cde448285f1b Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 19 Apr 2018 10:55:59 +0200 Subject: [PATCH 08/46] removed 2 of 3 set arrays to save RAM --- src/globals.h | 11 +++-------- src/lorawan.cpp | 14 ++++++------- src/macsniff.cpp | 13 ++++-------- src/main.cpp | 51 ++++++++++++++++++++++++------------------------ src/rcommand.cpp | 13 +++++------- 5 files changed, 43 insertions(+), 59 deletions(-) diff --git a/src/globals.h b/src/globals.h index c695a388..ae9440f9 100644 --- a/src/globals.h +++ b/src/globals.h @@ -3,8 +3,8 @@ // std::set for unified array functions #include -#include -#include +//#include +//#include #ifdef HAS_DISPLAY // OLED Display @@ -49,15 +49,10 @@ extern uint32_t currentMillis ; extern osjob_t sendjob; extern char display_lora[], display_lmic[]; 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 wifis; extern std::set macs; #ifdef HAS_DISPLAY extern HAS_DISPLAY u8x8; #endif - -#ifdef BLECOUNTER - extern int scanTime; - extern std::set bles; -#endif diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 1832d2f5..395c77e8 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -111,15 +111,13 @@ void do_send(osjob_t* j){ uint8_t mydata[4]; uint16_t data; // Sum of unique WIFI MACs seen - data = (uint16_t) wifis.size(); - mydata[0] = (data & 0xff00) >> 8; - mydata[1] = data & 0xff; + mydata[0] = (macs_wifi & 0xff00) >> 8; + mydata[1] = macs_wifi & 0xff; #ifdef BLECOUNTER // Sum of unique BLE MACs seen - data = (uint16_t) bles.size(); - mydata[2] = (data & 0xff00) >> 8; - mydata[3] = data & 0xff; + mydata[2] = (macs_ble & 0xff00) >> 8; + mydata[3] = macs_ble & 0xff; #else mydata[2] = 0; mydata[3] = 0; @@ -128,8 +126,8 @@ void do_send(osjob_t* j){ // Total BLE+WIFI unique MACs seen // TBD ? //data = (uint16_t) macs.size(); - //mydata[4] = (data & 0xff00) >> 8; - //mydata[5] = data & 0xff; + //mydata[4] = (macs_total & 0xff00) >> 8; + //mydata[5] = macs_total & 0xff; // Check if there is not a current TX/RX job running if (LMIC.opmode & OP_TXRXPEND) { diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 6762d662..6fb5e9a8 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -53,26 +53,21 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // Insert only if it was not found on global count if (added) { if (sniff_type == MAC_SNIFF_WIFI ) { + macs_wifi++; // increment Wifi MACs counter set_LED(COLOR_GREEN, 50, 0, 1); - wifis.insert(hashedmac); // add hashed MAC to wifi container } #ifdef BLECOUNTER else if (sniff_type == MAC_SNIFF_BLE ) { + macs_ble++; // increment BLE Macs counter set_LED(COLOR_MAGENTA, 50, 0, 1); - bles.insert(hashedmac); // add hashed MAC to BLE container } #endif } ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s", sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", - rssi, buff, hashedmac, - (int) wifis.size(), - #ifdef BLECOUNTER - (int) bles.size(), - #else - 0, - #endif + rssi, buff, hashedmac, + macs_wifi, added ? "new" : "known"); #ifdef VENDORFILTER diff --git a/src/main.cpp b/src/main.cpp index 73b6b343..11a59ec6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,9 +24,6 @@ Refer to LICENSE.txt file in repository for more details. // Basic Config #include "globals.h" -// std::set for unified array functions -#include - // Does nothing and avoid any compilation error with I2C #include @@ -43,22 +40,23 @@ Refer to LICENSE.txt file in repository for more details. configData_t cfg; // struct holds current device configuration osjob_t sendjob, initjob; // LMIC +enum states { + LED_ON, + LED_OFF +}; + // Initialize global variables char display_lora[16], display_lmic[16]; // display buffers -uint64_t uptimecounter = 0; -uint32_t currentMillis = 0, previousDisplaymillis = 0; -uint8_t DisplayState, LEDState = 0, LEDcount = 0; -uint16_t LEDBlinkduration = 0, LEDInterval = 0, color=COLOR_NONE; -uint8_t channel = 0; // wifi channel counter -bool joinstate = false; +uint64_t uptimecounter = 0; // timer global for uptime counter +uint32_t currentMillis = 0; // timer global for state machine +uint8_t DisplayState, LEDcount = 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 +enum states LEDState = LED_OFF; // LED state global for state machine +bool joinstate = false; // LoRa network joined? global flag std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) -std::set wifis; // associative container holds unique Wifi MAC adress hashes - -#ifdef BLECOUNTER - std::set bles; // associative container holds unique BLE MAC adresses hashes - int scanTime; -#endif // this variable will be changed in the ISR, and read in main loop static volatile bool ButtonTriggered = false; @@ -83,8 +81,8 @@ void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_inter color = set_color; // set color for RGB LED LEDBlinkduration = set_blinkduration; // duration on LEDInterval = set_interval; // duration off - on - off - LEDcount = set_count * 2; // number of blinks before LED off - LEDState = set_count ? 1 : 0; // sets LED to off if 0 blinks + LEDcount = set_count * 2; // number of on/off cycles before LED off + LEDState = set_count ? LED_ON : LED_OFF; // sets LED to off if 0 blinks } /* begin LMIC specific parts ------------------------------------------------------------ */ @@ -250,10 +248,9 @@ void sniffer_loop(void * pvParameters) { // clear counter if not in cumulative counter mode if (cfg.countermode != 1) { macs.clear(); // clear all macs container - wifis.clear(); // clear Wifi macs couner - #ifdef BLECOUNTER - bles.clear(); // clear BLE macs counter - #endif + macs_total = 0; // reset all counters + macs_wifi = 0; + macs_ble = 0; salt_reset(); // get new salt for salting hashes } @@ -358,12 +355,12 @@ uint64_t uptime() { snprintf(buff, sizeof(buff), "PAX:%-4d", (int) macs.size()); // convert 16-bit MAC counter to decimal counter value u8x8.draw2x2String(0, 0, buff); // display number on unique macs total Wifi + BLE u8x8.setCursor(0,4); - u8x8.printf("WIFI: %-4d", (int) wifis.size()); + u8x8.printf("WIFI: %-4d", macs_wifi); #ifdef BLECOUNTER u8x8.setCursor(0,3); if (cfg.blescan) - u8x8.printf("BLTH: %-4d", (int) bles.size()); + u8x8.printf("BLTH: %-4d", macs_ble); else u8x8.printf("%-16s", "BLTH: off"); #endif @@ -387,6 +384,8 @@ uint64_t uptime() { void updateDisplay() { // timed display refresh according to refresh cycle setting + uint32_t previousDisplaymillis = currentMillis; + if (currentMillis - previousDisplaymillis >= DISPLAYREFRESH_MS) { refreshDisplay(); previousDisplaymillis += DISPLAYREFRESH_MS; @@ -414,7 +413,7 @@ uint64_t uptime() { #ifdef HAS_LED void switchLED() { - static bool previousLEDState; + enum states previousLEDState; // led need to change state? avoid digitalWrite() for nothing if (LEDState != previousLEDState) { #ifdef LED_ACTIVE_LOW @@ -434,9 +433,9 @@ uint64_t uptime() { void switchLEDstate() { if (!LEDcount) // no more blinks? -> switch off LED - LEDState = 0; + LEDState = LED_OFF; else if (LEDInterval) // blinks left? -> toggle LED and decrement blinks - LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? 1 : 0; + LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? LED_ON : LED_OFF; } // switchLEDstate() #endif diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 0ef9f2ef..6875ad8e 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -65,10 +65,9 @@ void set_reset(int val) { case 1: // reset MAC counter ESP_LOGI(TAG, "Remote command: reset MAC counter"); macs.clear(); // clear all macs container - wifis.clear(); // clear Wifi macs container - #ifdef BLECOUNTER - bles.clear(); // clear BLE macs container - #endif + macs_total = 0; // reset all counters + macs_wifi = 0; + macs_ble = 0; salt_reset(); // get new 16bit salt sprintf(display_lora, "Reset counter"); break; @@ -151,10 +150,8 @@ void set_blescan(int val) { ESP_LOGI(TAG, "Remote command: set BLE scan mode to %s", val ? "on" : "off"); switch (val) { case 0: - cfg.blescan = 0; - #ifdef BLECOUNTER - bles.clear(); // clear BLE macs container - #endif + cfg.blescan = 0; + macs_ble = 0; // clear BLE counter break; default: cfg.blescan = 1; From f922b90242c01ca4a83e59f2e348eb2088eb69a1 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 19 Apr 2018 11:07:18 +0200 Subject: [PATCH 09/46] main.cpp: code sanitization --- src/main.cpp | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 11a59ec6..8468872d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -72,11 +72,32 @@ int redirect_log(const char * fmt, va_list args) { } #endif +//--- Prototypes --- + +// LMIC functions, defined in this main.cpp +void onEvent(ev_t ev); +void do_send(osjob_t* j); + // defined in configmanager.cpp void eraseConfig(void); void saveConfig(void); void loadConfig(void); +// defined in lorawan.cpp +void gen_lora_deveui(uint8_t * pdeveui); +void RevBytes(unsigned char* b, size_t c); +void get_hard_deveui(uint8_t *pdeveui); + +// defined in wifisniffer.cpp +void wifi_sniffer_init(void); +void wifi_sniffer_set_channel(uint8_t channel); +void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); + +// defined in blescan.cpp +void bt_loop(void *ignore); + +//--- + void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count) { color = set_color; // set color for RGB LED LEDBlinkduration = set_blinkduration; // duration on @@ -87,10 +108,6 @@ void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_inter /* begin LMIC specific parts ------------------------------------------------------------ */ -// defined in lorawan.cpp -void gen_lora_deveui(uint8_t * pdeveui); -void RevBytes(unsigned char* b, size_t c); -void get_hard_deveui(uint8_t *pdeveui); #ifdef VERBOSE void printKeys(void); @@ -136,10 +153,6 @@ const lmic_pinmap lmic_pins = { .dio = {DIO0, DIO1, DIO2} }; -// LMIC functions -void onEvent(ev_t ev); -void do_send(osjob_t* j); - // LoRaWAN Initjob static void lora_init (osjob_t* j) { // reset MAC state @@ -212,14 +225,6 @@ void lorawan_loop(void * pvParameters) { /* begin wifi specific parts ---------------------------------------------------------- */ -// defined in wifisniffer.cpp -void wifi_sniffer_init(void); -void wifi_sniffer_set_channel(uint8_t channel); -void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); - -// defined in blescan.cpp -void bt_loop(void *ignore); - // Sniffer Task void sniffer_loop(void * pvParameters) { From 54945f173ecc5fa68f63a235ae393e5896eb43fa Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 19 Apr 2018 11:09:17 +0200 Subject: [PATCH 10/46] main.cpp: code sanitization --- src/main.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8468872d..2d8374b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,16 +74,14 @@ int redirect_log(const char * fmt, va_list args) { //--- Prototypes --- -// LMIC functions, defined in this main.cpp -void onEvent(ev_t ev); -void do_send(osjob_t* j); - // defined in configmanager.cpp void eraseConfig(void); void saveConfig(void); void loadConfig(void); // defined in lorawan.cpp +void onEvent(ev_t ev); +void do_send(osjob_t* j); void gen_lora_deveui(uint8_t * pdeveui); void RevBytes(unsigned char* b, size_t c); void get_hard_deveui(uint8_t *pdeveui); From fab0e4f0ddca1a58ea1f7db27a0cacc9a7a9330d Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 19 Apr 2018 15:17:23 +0200 Subject: [PATCH 11/46] code sanitization --- platformio.ini | 2 +- src/blecsan.cpp | 4 +- src/configmanager.cpp | 2 +- src/globals.h | 1 + src/macsniff.cpp | 36 +++++------ src/macsniff.h | 2 +- src/main.cpp | 74 ++++++++------------- src/main.h | 146 ++++++++---------------------------------- src/paxcounter.conf | 129 +++++++++++++++++++++++++++++++++++++ src/rcommand.cpp | 11 +--- 10 files changed, 210 insertions(+), 197 deletions(-) create mode 100644 src/paxcounter.conf diff --git a/platformio.ini b/platformio.ini index 8be3a272..f780a017 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,7 +38,7 @@ build_flags = ; ; override lora settings from LMiC library in lmic/config.h and use main.h instead -D_lmic_config_h_ - -include "src/main.h" + -include "src/paxcounter.conf" [env:heltec_wifi_lora_32] platform = espressif32 diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 4e028aaf..1471f5f6 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -233,8 +233,10 @@ esp_err_t register_ble_functionality(void) // Main start code running in its own Xtask -void bt_loop(void *ignore) +void bt_loop(void * pvParameters) { + configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check + esp_err_t status; // Initialize BT controller to allocate task and other resource. diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 49341163..fecd37a1 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -18,7 +18,7 @@ esp_err_t err; // populate cfg vars with factory settings void defaultConfig() { - cfg.lorasf = LORASFDEFAULT; // 7-12, initial lora spreadfactor defined in main.h + cfg.lorasf = LORASFDEFAULT; // 7-12, initial lora spreadfactor defined in paxcounter.conf cfg.txpower = 15; // 2-15, lora tx power cfg.adrmode = 1; // 0=disabled, 1=enabled cfg.screensaver = 0; // 0=disabled, 1=enabled diff --git a/src/globals.h b/src/globals.h index ae9440f9..eb238842 100644 --- a/src/globals.h +++ b/src/globals.h @@ -22,6 +22,7 @@ #include "rgb_led.h" #include "macsniff.h" +#include "main.h" // Struct holding devices's runtime configuration typedef struct { diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 6fb5e9a8..30ada2b5 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -11,46 +11,44 @@ // Local logging tag static const char *TAG = "macsniff"; -// defined in main.cpp -void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count); - static wifi_country_t wifi_country = {.cc=WIFI_MY_COUNTRY, .schan=WIFI_CHANNEL_MIN, .nchan=WIFI_CHANNEL_MAX, .policy=WIFI_COUNTRY_POLICY_MANUAL}; +// globals uint16_t salt; -uint16_t salt_reset(void) { - salt = random(65536); // get new 16bit random for salting hashes +uint16_t reset_salt(void) { + salt = random(65536); // get new 16bit random for salting hashes and set global salt var return salt; } bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { - char buff[16]; // temporary buffer for printf + char buff[16]; // temporary buffer for printf bool added = false; - uint32_t addr2int; - uint32_t vendor2int; - uint16_t hashedmac; + uint32_t addr2int, vendor2int; // temporary buffer for MAC and Vendor OUI + uint16_t hashedmac; // temporary buffer for generated hash value - // only last 3 MAC Address bytes are used for MAC Address Anonymization + // 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 addr2int = ( (uint32_t)paddr[2] ) | ( (uint32_t)paddr[3] << 8 ) | ( (uint32_t)paddr[4] << 16 ) | ( (uint32_t)paddr[5] << 24 ); #ifdef VENDORFILTER vendor2int = ( (uint32_t)paddr[2] ) | ( (uint32_t)paddr[1] << 8 ) | ( (uint32_t)paddr[0] << 16 ); // use OUI vendor filter list only on Wifi, not on BLE - if ( (sniff_type==MAC_SNIFF_BLE) || std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() ) { + if ( (sniff_type==MAC_SNIFF_BLE) || std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() ) + { #endif // salt and hash MAC, and if new unique one, store identifier in container and increment counter on display // https://en.wikipedia.org/wiki/MAC_Address_Anonymization - addr2int += (uint32_t) salt; // add 16-bit salt to pseudo MAC + addr2int += (uint32_t)salt; // add 16-bit salt to pseudo MAC snprintf(buff, sizeof(buff), "%08X", addr2int); // convert unsigned 32-bit salted MAC to 8 digit hex string hashedmac = rokkit(&buff[3], 5); // hash MAC last string value, use 5 chars to fit hash in uint16_t container auto newmac = macs.insert(hashedmac); // add hashed MAC to total container if new unique added = newmac.second ? true:false; // true if hashed MAC is unique in container - // Insert only if it was not found on global count + // Count only if MAC was not yet seen if (added) { if (sniff_type == MAC_SNIFF_WIFI ) { macs_wifi++; // increment Wifi MACs counter @@ -63,12 +61,12 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { } #endif } - - ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s", - sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", - rssi, buff, hashedmac, - macs_wifi, - added ? "new" : "known"); + + // Log scan result + ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s", + sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", + rssi, buff, hashedmac, macs_wifi, + added ? "new" : "known"); #ifdef VENDORFILTER } else { diff --git a/src/macsniff.h b/src/macsniff.h index 441ab157..a92046df 100644 --- a/src/macsniff.h +++ b/src/macsniff.h @@ -19,7 +19,7 @@ typedef struct { uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */ } wifi_ieee80211_packet_t; -uint16_t salt_reset(void); +uint16_t reset_salt(void); void wifi_sniffer_init(void); void wifi_sniffer_set_channel(uint8_t channel); void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); diff --git a/src/main.cpp b/src/main.cpp index 2d8374b1..4f4c0d31 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,18 +32,13 @@ Refer to LICENSE.txt file in repository for more details. #include #include -// ESP32 Functions -#include // needed for Wifi event handler -#include // needed for reading ESP32 chip attributes -#include // needed for ESP_LOGx on arduino framework +// ESP32 lib Functions +#include // needed for Wifi event handler +#include // needed for reading ESP32 chip attributes +#include // needed for ESP_LOGx on arduino framework -configData_t cfg; // struct holds current device configuration -osjob_t sendjob, initjob; // LMIC - -enum states { - LED_ON, - LED_OFF -}; +configData_t cfg; // struct holds current device configuration +osjob_t sendjob, initjob; // LMIC jobs // Initialize global variables char display_lora[16], display_lmic[16]; // display buffers @@ -65,6 +60,7 @@ static volatile bool ButtonTriggered = false; static const char *TAG = "paxcnt"; // Note: Log level control seems not working during runtime, // so we need to switch loglevel by compiler build option in platformio.ini + #ifndef VERBOSE int redirect_log(const char * fmt, va_list args) { //do nothing @@ -72,30 +68,6 @@ int redirect_log(const char * fmt, va_list args) { } #endif -//--- Prototypes --- - -// defined in configmanager.cpp -void eraseConfig(void); -void saveConfig(void); -void loadConfig(void); - -// defined in lorawan.cpp -void onEvent(ev_t ev); -void do_send(osjob_t* j); -void gen_lora_deveui(uint8_t * pdeveui); -void RevBytes(unsigned char* b, size_t c); -void get_hard_deveui(uint8_t *pdeveui); - -// defined in wifisniffer.cpp -void wifi_sniffer_init(void); -void wifi_sniffer_set_channel(uint8_t channel); -void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); - -// defined in blescan.cpp -void bt_loop(void *ignore); - -//--- - void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count) { color = set_color; // set color for RGB LED LEDBlinkduration = set_blinkduration; // duration on @@ -104,6 +76,13 @@ void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_inter LEDState = set_count ? LED_ON : LED_OFF; // sets LED to off if 0 blinks } +void reset_counters() { + macs.clear(); // clear all macs container + macs_total = 0; // reset all counters + macs_wifi = 0; + macs_ble = 0; +} + /* begin LMIC specific parts ------------------------------------------------------------ */ @@ -163,6 +142,7 @@ static void lora_init (osjob_t* j) { // LMIC FreeRTos Task void lorawan_loop(void * pvParameters) { + configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check while(1) { @@ -227,34 +207,31 @@ void lorawan_loop(void * pvParameters) { void sniffer_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check - channel=0; + char buff[16]; int nloop=0, lorawait=0; - while (true) { + while (1) { nloop++; // actual number of wifi loops, controls cycle when data is sent - vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS); - yield(); channel = (channel % WIFI_CHANNEL_MAX) + 1; // rotates variable channel 1..WIFI_CHANNEL_MAX wifi_sniffer_set_channel(channel); ESP_LOGD(TAG, "Wifi set channel %d", channel); // duration of one wifi scan loop reached? then send data and begin new scan cycle if ( nloop >= ( (100 / cfg.wifichancycle) * (cfg.wifiscancycle * 2)) +1 ) { + nloop=0; channel=0; // reset wifi scan + channel loop counter do_send(&sendjob); // Prepare and execute LoRaWAN data upload - vTaskDelay(500/portTICK_PERIOD_MS); - yield(); + + //vTaskDelay(500/portTICK_PERIOD_MS); // tbd - is this delay really needed here? + //yield(); // clear counter if not in cumulative counter mode if (cfg.countermode != 1) { - macs.clear(); // clear all macs container - macs_total = 0; // reset all counters - macs_wifi = 0; - macs_ble = 0; - salt_reset(); // get new salt for salting hashes + reset_counters(); // clear macs container and reset all counters + reset_salt(); // get new salt for salting hashes } // check if payload is sent @@ -275,6 +252,9 @@ void sniffer_loop(void * pvParameters) { } // end of send data cycle + vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS); + yield(); + } // end of infinite wifi channel rotation loop } @@ -536,7 +516,7 @@ wifi_sniffer_init(); // setup wifi in monitor mode and start MAC counting // initialize salt value using esp_random() called by random() in arduino-esp32 core // note: do this *after* wifi has started, since gets it's seed from RF noise -salt_reset(); // get new 16bit for salting hashes +reset_salt(); // get new 16bit for salting hashes // run wifi task on core 0 and lora task on core 1 and bt task on core 0 ESP_LOGI(TAG, "Starting Lora task on core 1"); diff --git a/src/main.h b/src/main.h index cd606adb..919550a2 100644 --- a/src/main.h +++ b/src/main.h @@ -1,131 +1,41 @@ #pragma once // program version - note: increment version after modifications to configData_t struct!! -#define PROGVERSION "1.3.1" // use max 10 chars here! +#define PROGVERSION "1.3.2" // use max 10 chars here! #define PROGNAME "PAXCNT" -// Verbose enables serial output -#define VERBOSE 1 // comment out to silence the device, for mute use build option +//--- Declarations --- -// set this to include BLE counting and vendor filter functions -#define VENDORFILTER 1 // comment out if you want to count things, not people -#define BLECOUNTER 1 // comment out if you don't want BLE count +enum states { + LED_ON, + LED_OFF +}; -// BLE scan parameters -#define BLESCANTIME 11 // [seconds] scan duration, see note below -#define BLESCANWINDOW 10 // [milliseconds] scan window, see below, 3 .. 10240, default 10 -#define BLESCANINTERVAL 10 // [milliseconds] how long to wait between scans, 3 .. 10240, default 10 +//--- Prototypes --- -/* Note: guide for setting bluetooth parameters -* -* |< Scan Window > |< Scan Window > |< Scan Window > | -* |< Scan Interval >|< Scan Interval >|< Scan Interval >| -* |< Scan duration >| -* -* Scan duration sets how long scanning should be going on, interrupting a wifi scan cycle. -* Scan window sets how much of the interval should be occupied by scanning. -* Scan interval is how long scanning should be done on each channel. BLE uses 3 channels for advertising. -* -> Adjust these values with power consumption in mind if power is limited. -*/ +// defined in main.cpp +void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count); +void reset_counters(); -// WiFi scan parameters -#define WIFI_CHANNEL_MIN 1 // start channel number where scan begings -#define WIFI_CHANNEL_MAX 13 // total channel number to scan -#define WIFI_MY_COUNTRY "EU" // select locale for Wifi RF settings -#define WIFI_CHANNEL_SWITCH_INTERVAL 50 // [seconds/100] -> 0,5 sec. +// defined in configmanager.cpp +void eraseConfig(void); +void saveConfig(void); +void loadConfig(void); -// LoRa payload send cycle -//#define SEND_SECS 120 // [seconds/2] -> 240 sec. -#define SEND_SECS 30 // [seconds/2] -> 60 sec. +// defined in lorawan.cpp +void onEvent(ev_t ev); +void do_send(osjob_t* j); +void gen_lora_deveui(uint8_t * pdeveui); +void RevBytes(unsigned char* b, size_t c); +void get_hard_deveui(uint8_t *pdeveui); -// Default LoRa Spreadfactor -#define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs -#define MAXLORARETRY 500 // maximum count of TX retries if LoRa busy -#define RCMDPORT 2 // LoRaWAN Port on which device listenes for remote commands +// defined in wifisniffer.cpp +void wifi_sniffer_init(void); +void wifi_sniffer_set_channel(uint8_t channel); +void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); -// Default RGB LED luminosity (in %) -#define RGBLUMINOSITY 30 // 30% +// defined in blescan.cpp +void bt_loop(void *ignore); -// OLED Display refresh cycle (in Milliseconds) -#define DISPLAYREFRESH_MS 40 // e.g. 40ms -> 1000/40 = 25 frames per second - -// LMIC settings -// define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored -// define hardware specifics settings in platformio.ini as build_flag for hardware environment - -// Select frequency band here according to national regulations -#define CFG_eu868 1 -//#define CFG_us915 1 - -// This is the SX1272/SX1273 radio, which is also used on the HopeRF -// RFM92 boards. -//#define CFG_sx1272_radio 1 -// This is the SX1276/SX1277/SX1278/SX1279 radio, which is also used on -// the HopeRF RFM95 boards. -//#define CFG_sx1276_radio 1 - -// 16 μs per tick -// LMIC requires ticks to be 15.5μs - 100 μs long -#define US_PER_OSTICK_EXPONENT 4 -#define US_PER_OSTICK (1 << US_PER_OSTICK_EXPONENT) -#define OSTICKS_PER_SEC (1000000 / US_PER_OSTICK) - -// Set this to 1 to enable some basic debug output (using printf) about -// RF settings used during transmission and reception. Set to 2 to -// enable more verbose output. Make sure that printf is actually -// configured (e.g. on AVR it is not by default), otherwise using it can -// cause crashing. -//#define LMIC_DEBUG_LEVEL 1 - -// Enable this to allow using printf() to print to the given serial port -// (or any other Print object). This can be easy for debugging. The -// current implementation only works on AVR, though. -//#define LMIC_PRINTF_TO Serial - -// Any runtime assertion failures are printed to this serial port (or -// any other Print object). If this is unset, any failures just silently -// halt execution. -#define LMIC_FAILURE_TO Serial - -// Uncomment this to disable all code related to joining -//#define DISABLE_JOIN -// Uncomment this to disable all code related to ping -#define DISABLE_PING -// Uncomment this to disable all code related to beacon tracking. -// Requires ping to be disabled too -#define DISABLE_BEACONS - -// Uncomment these to disable the corresponding MAC commands. -// Class A -//#define DISABLE_MCMD_DCAP_REQ // duty cycle cap -//#define DISABLE_MCMD_DN2P_SET // 2nd DN window param -//#define DISABLE_MCMD_SNCH_REQ // set new channel -// Class B -//#define DISABLE_MCMD_PING_SET // set ping freq, automatically disabled by DISABLE_PING -//#define DISABLE_MCMD_BCNI_ANS // next beacon start, automatical disabled by DISABLE_BEACON - -// In LoRaWAN, a gateway applies I/Q inversion on TX, and nodes do the -// same on RX. This ensures that gateways can talk to nodes and vice -// versa, but gateways will not hear other gateways and nodes will not -// hear other nodes. By uncommenting this macro, this inversion is -// disabled and this node can hear other nodes. If two nodes both have -// this macro set, they can talk to each other (but they can no longer -// hear gateways). This should probably only be used when debugging -// and/or when talking to the radio directly (e.g. like in the "raw" -// example). -//#define DISABLE_INVERT_IQ_ON_RX - -// This allows choosing between multiple included AES implementations. -// Make sure exactly one of these is uncommented. -// -// This selects the original AES implementation included LMIC. This -// implementation is optimized for speed on 32-bit processors using -// fairly big lookup tables, but it takes up big amounts of flash on the -// AVR architecture. -#define USE_ORIGINAL_AES -// -// This selects the AES implementation written by Ideetroon for their -// own LoRaWAN library. It also uses lookup tables, but smaller -// byte-oriented ones, making it use a lot less flash space (but it is -// also about twice as slow as the original). -// #define USE_IDEETRON_AES +// defined in main.cpp +void reset_counters(void); \ No newline at end of file diff --git a/src/paxcounter.conf b/src/paxcounter.conf new file mode 100644 index 00000000..bfae6146 --- /dev/null +++ b/src/paxcounter.conf @@ -0,0 +1,129 @@ +// ----- Paxcounter user config file ------ +// +// --> adapt to your needs and use case <-- + +// Verbose enables serial output +#define VERBOSE 1 // comment out to silence the device, for mute use build option + +// set this to include BLE counting and vendor filter functions +#define VENDORFILTER 1 // comment out if you want to count things, not people +#define BLECOUNTER 1 // comment out if you don't want BLE count + +// BLE scan parameters +#define BLESCANTIME 11 // [seconds] scan duration, see note below +#define BLESCANWINDOW 10 // [milliseconds] scan window, see below, 3 .. 10240, default 10 +#define BLESCANINTERVAL 10 // [milliseconds] how long to wait between scans, 3 .. 10240, default 10 + +/* Note: guide for setting bluetooth parameters +* +* |< Scan Window > |< Scan Window > |< Scan Window > | +* |< Scan Interval >|< Scan Interval >|< Scan Interval >| +* |< Scan duration >| +* +* Scan duration sets how long scanning should be going on, interrupting a wifi scan cycle. +* Scan window sets how much of the interval should be occupied by scanning. +* Scan interval is how long scanning should be done on each channel. BLE uses 3 channels for advertising. +* -> Adjust these values with power consumption in mind if power is limited. +*/ + +// WiFi scan parameters +#define WIFI_CHANNEL_MIN 1 // start channel number where scan begings +#define WIFI_CHANNEL_MAX 13 // total channel number to scan +#define WIFI_MY_COUNTRY "EU" // select locale for Wifi RF settings +#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. + +// Default LoRa Spreadfactor +#define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs +#define MAXLORARETRY 500 // maximum count of TX retries if LoRa busy +#define RCMDPORT 2 // LoRaWAN Port on which device listenes for remote commands + +// Default RGB LED luminosity (in %) +#define RGBLUMINOSITY 30 // 30% + +// OLED Display refresh cycle (in Milliseconds) +#define DISPLAYREFRESH_MS 40 // e.g. 40ms -> 1000/40 = 25 frames per second + +// LMIC settings +// define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored +// define hardware specifics settings in platformio.ini as build_flag for hardware environment + +// Select frequency band here according to national regulations +#define CFG_eu868 1 +//#define CFG_us915 1 + +// This is the SX1272/SX1273 radio, which is also used on the HopeRF +// RFM92 boards. +//#define CFG_sx1272_radio 1 +// This is the SX1276/SX1277/SX1278/SX1279 radio, which is also used on +// the HopeRF RFM95 boards. +//#define CFG_sx1276_radio 1 + +// 16 μs per tick +// LMIC requires ticks to be 15.5μs - 100 μs long +#define US_PER_OSTICK_EXPONENT 4 +#define US_PER_OSTICK (1 << US_PER_OSTICK_EXPONENT) +#define OSTICKS_PER_SEC (1000000 / US_PER_OSTICK) + +// Set this to 1 to enable some basic debug output (using printf) about +// RF settings used during transmission and reception. Set to 2 to +// enable more verbose output. Make sure that printf is actually +// configured (e.g. on AVR it is not by default), otherwise using it can +// cause crashing. +//#define LMIC_DEBUG_LEVEL 1 + +// Enable this to allow using printf() to print to the given serial port +// (or any other Print object). This can be easy for debugging. The +// current implementation only works on AVR, though. +//#define LMIC_PRINTF_TO Serial + +// Any runtime assertion failures are printed to this serial port (or +// any other Print object). If this is unset, any failures just silently +// halt execution. +#define LMIC_FAILURE_TO Serial + +// Uncomment this to disable all code related to joining +//#define DISABLE_JOIN +// Uncomment this to disable all code related to ping +#define DISABLE_PING +// Uncomment this to disable all code related to beacon tracking. +// Requires ping to be disabled too +#define DISABLE_BEACONS + +// Uncomment these to disable the corresponding MAC commands. +// Class A +//#define DISABLE_MCMD_DCAP_REQ // duty cycle cap +//#define DISABLE_MCMD_DN2P_SET // 2nd DN window param +//#define DISABLE_MCMD_SNCH_REQ // set new channel +// Class B +//#define DISABLE_MCMD_PING_SET // set ping freq, automatically disabled by DISABLE_PING +//#define DISABLE_MCMD_BCNI_ANS // next beacon start, automatical disabled by DISABLE_BEACON + +// In LoRaWAN, a gateway applies I/Q inversion on TX, and nodes do the +// same on RX. This ensures that gateways can talk to nodes and vice +// versa, but gateways will not hear other gateways and nodes will not +// hear other nodes. By uncommenting this macro, this inversion is +// disabled and this node can hear other nodes. If two nodes both have +// this macro set, they can talk to each other (but they can no longer +// hear gateways). This should probably only be used when debugging +// and/or when talking to the radio directly (e.g. like in the "raw" +// example). +//#define DISABLE_INVERT_IQ_ON_RX + +// This allows choosing between multiple included AES implementations. +// Make sure exactly one of these is uncommented. +// +// This selects the original AES implementation included LMIC. This +// implementation is optimized for speed on 32-bit processors using +// fairly big lookup tables, but it takes up big amounts of flash on the +// AVR architecture. +#define USE_ORIGINAL_AES +// +// This selects the AES implementation written by Ideetroon for their +// own LoRaWAN library. It also uses lookup tables, but smaller +// byte-oriented ones, making it use a lot less flash space (but it is +// also about twice as slow as the original). +// #define USE_IDEETRON_AES diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 6875ad8e..fe73c8aa 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -19,10 +19,6 @@ typedef struct { const bool store; } cmd_t; -// functions defined in configmanager.cpp -void eraseConfig(void); -void saveConfig(void); - // function defined in antenna.cpp #ifdef HAS_ANTENNA_SWITCH void antenna_select(const int8_t _ant); @@ -64,11 +60,8 @@ void set_reset(int val) { break; case 1: // reset MAC counter ESP_LOGI(TAG, "Remote command: reset MAC counter"); - macs.clear(); // clear all macs container - macs_total = 0; // reset all counters - macs_wifi = 0; - macs_ble = 0; - salt_reset(); // get new 16bit salt + reset_counters(); // clear macs + reset_salt(); // get new salt sprintf(display_lora, "Reset counter"); break; case 2: // reset device to factory settings From face65421aa46201115186e1c32de91565be21c6 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 19 Apr 2018 15:28:27 +0200 Subject: [PATCH 12/46] code sanitization --- src/main.cpp | 5 ++--- src/main.h | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4f4c0d31..91ffc5de 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,17 +37,16 @@ Refer to LICENSE.txt file in repository for more details. #include // needed for reading ESP32 chip attributes #include // needed for ESP_LOGx on arduino framework +// Initialize global variables configData_t cfg; // struct holds current device configuration osjob_t sendjob, initjob; // LMIC jobs - -// Initialize global variables -char display_lora[16], display_lmic[16]; // display buffers uint64_t uptimecounter = 0; // timer global for uptime counter uint32_t currentMillis = 0; // timer global for state machine uint8_t DisplayState, LEDcount = 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 enum states LEDState = LED_OFF; // LED state global for state machine bool joinstate = false; // LoRa network joined? global flag diff --git a/src/main.h b/src/main.h index 919550a2..26baff29 100644 --- a/src/main.h +++ b/src/main.h @@ -1,4 +1,3 @@ -#pragma once // program version - note: increment version after modifications to configData_t struct!! #define PROGVERSION "1.3.2" // use max 10 chars here! From f07cc7dcb2c18f27cabb3d357201953274462e26 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 19 Apr 2018 15:36:01 +0200 Subject: [PATCH 13/46] code sanitization --- src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 91ffc5de..caeca29a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -519,9 +519,11 @@ reset_salt(); // get new 16bit for salting hashes // run wifi task on core 0 and lora task on core 1 and bt task on core 0 ESP_LOGI(TAG, "Starting Lora task on core 1"); -xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 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); + #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"); From c6191f9a24c085e09d9272d3df6ff70dba808a83 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 19 Apr 2018 19:16:32 +0200 Subject: [PATCH 14/46] bugfixes --- platformio.ini | 4 ++-- src/macsniff.cpp | 2 +- src/main.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 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/macsniff.cpp b/src/macsniff.cpp index 30ada2b5..b73264c1 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -65,7 +65,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // Log scan result ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s", sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", - rssi, buff, hashedmac, macs_wifi, + rssi, buff, hashedmac, macs_wifi, macs_ble, added ? "new" : "known"); #ifdef VENDORFILTER diff --git a/src/main.cpp b/src/main.cpp index caeca29a..31d2d1cc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -366,7 +366,7 @@ uint64_t uptime() { void updateDisplay() { // timed display refresh according to refresh cycle setting - uint32_t previousDisplaymillis = currentMillis; + uint32_t previousDisplaymillis; if (currentMillis - previousDisplaymillis >= DISPLAYREFRESH_MS) { refreshDisplay(); @@ -527,7 +527,7 @@ xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 16384, ( void * ) 1, 1, NUL #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, NULL, 5, NULL, 0); + xTaskCreatePinnedToCore(bt_loop, "btscan", 16384, ( void * ) 1, 1, NULL, 0); } #endif From de2aaf9e6ab39eeb83e6c26032418cbe2c12890a Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 19 Apr 2018 21:02:42 +0200 Subject: [PATCH 15/46] cleanup --- README.md | 12 +++++++++--- platformio.ini | 4 ++-- src/main.cpp | 12 ++++-------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 372fc03f..0fa85e7a 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,13 @@ Hardware dependent settings (pinout etc.) are stored in board files in /hal dire These results where metered with software version 1.2.97 while continuously scanning wifi and ble, no LoRa TX’ing, OLED display (if present) on, 5V USB powered. -# Building +# Preparing -Use PlatformIO with your preferred IDE for development and building this code. +Before compiling the code, -Before compiling the code, **create file loraconf.h in your local /src directory** using the template [loraconf.sample.h](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/loraconf.sample.h) and populate it with your personal APPEUI und APPKEY for the LoRaWAN network. If you're using popular TheThingsNetwork you can copy&paste the keys from TTN console or output of ttnctl. +- **edit paxcounter.conf** and taylor settings in this file according to your needs and use case. Please take care of the duty cycle regulations of the LoRaWAN network you're going to use. + +- **create file loraconf.h in your local /src directory** using the template [loraconf.sample.h](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/loraconf.sample.h) and populate it with your personal APPEUI und APPKEY for the LoRaWAN network. If you're using popular TheThingsNetwork you can copy&paste the keys from TTN console or output of ttnctl. To join the network only method OTAA is supported, not ABP. The DEVEUI for OTAA will be derived from the device's MAC adress during device startup and is shown as well on the device's display (if it has one) as on the serial console for copying it to your LoRaWAN network server settings. @@ -60,6 +62,10 @@ If your device has a fixed DEVEUI enter this in your local loraconf.h file. Duri If your device has silicon **Unique ID** which is stored in serial EEPROM Microchip 24AA02E64 you don't need to change anything. The Unique ID will be read during startup and DEVEUI will be generated from it, overriding settings in loraconf.h. +# Building + +Use PlatformIO with your preferred IDE for development and building this code. + # Uploading To upload the code to your ESP32 board this needs to be switched from run to bootloader mode. Boards with USB bridge like Heltec and TTGO usually have an onboard logic which allows soft switching by the upload tool. In PlatformIO this happenes automatically.

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 diff --git a/src/main.cpp b/src/main.cpp index 31d2d1cc..e65ce176 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,12 +41,12 @@ 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 = 0; // timer global for state machine +uint32_t currentMillis = 0, previousDisplaymillis = 0 // timer globals for state machine uint8_t DisplayState, LEDcount = 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 +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 buffers enum states LEDState = LED_OFF; // LED state global for state machine bool joinstate = false; // LoRa network joined? global flag @@ -224,9 +224,6 @@ void sniffer_loop(void * pvParameters) { nloop=0; channel=0; // reset wifi scan + channel loop counter do_send(&sendjob); // Prepare and execute LoRaWAN data upload - //vTaskDelay(500/portTICK_PERIOD_MS); // tbd - is this delay really needed here? - //yield(); - // clear counter if not in cumulative counter mode if (cfg.countermode != 1) { reset_counters(); // clear macs container and reset all counters @@ -366,8 +363,7 @@ uint64_t uptime() { void updateDisplay() { // timed display refresh according to refresh cycle setting - uint32_t previousDisplaymillis; - + if (currentMillis - previousDisplaymillis >= DISPLAYREFRESH_MS) { refreshDisplay(); previousDisplaymillis += DISPLAYREFRESH_MS; From 14e68260e6ea55d1d87f463ad2adedc0dc00cd57 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 20 Apr 2018 10:27:13 +0200 Subject: [PATCH 16/46] bugfixes in LED blink routine --- src/macsniff.cpp | 6 ++-- src/main.cpp | 85 +++++++++++++++++++++++++----------------------- src/main.h | 6 ++-- 3 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/macsniff.cpp b/src/macsniff.cpp index b73264c1..3e5db96b 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -52,12 +52,14 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { if (added) { if (sniff_type == MAC_SNIFF_WIFI ) { macs_wifi++; // increment Wifi MACs counter - set_LED(COLOR_GREEN, 50, 0, 1); + if (joinstate) + blink_LED(COLOR_GREEN, 50, 0); } #ifdef BLECOUNTER else if (sniff_type == MAC_SNIFF_BLE ) { macs_ble++; // increment BLE Macs counter - set_LED(COLOR_MAGENTA, 50, 0, 1); + if (joinstate) + blink_LED(COLOR_MAGENTA, 50, 0); } #endif } diff --git a/src/main.cpp b/src/main.cpp index e65ce176..61a1f2da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,13 +41,14 @@ 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 = 0, previousDisplaymillis = 0 // timer globals for state machine -uint8_t DisplayState, LEDcount = 0; // globals for state machine -uint16_t LEDBlinkduration = 0, LEDInterval = 0, color=COLOR_NONE; // state machine variables +uint32_t currentMillis = millis(); // timer global for state machine +uint32_t 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 buffers -enum states LEDState = LED_OFF; // LED state global for state machine +enum states LEDState = LED_OFF, previousLEDState = LED_OFF; // LED state global for state machine bool joinstate = false; // LoRa network joined? global flag std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) @@ -67,12 +68,11 @@ int redirect_log(const char * fmt, va_list args) { } #endif -void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count) { +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 - LEDcount = set_count * 2; // number of on/off cycles before LED off - LEDState = set_count ? LED_ON : LED_OFF; // sets LED to off if 0 blinks + LEDState = LED_ON; // start blink } void reset_counters() { @@ -151,18 +151,18 @@ 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 - set_LED(COLOR_YELLOW, 20, 200, 5); + 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) - set_LED(COLOR_BLUE, 10, 500, 3); + 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 - set_LED(COLOR_RED, 200, 2000, 5); + blink_LED(COLOR_RED, 200, 2000); } else { // led off - set_LED(COLOR_NONE, 0, 0, 0); + blink_LED(COLOR_NONE, 0, 0); } vTaskDelay(10/portTICK_PERIOD_MS); @@ -334,23 +334,21 @@ uint64_t uptime() { snprintf(buff, sizeof(buff), "PAX:%-4d", (int) macs.size()); // convert 16-bit MAC counter to decimal counter value u8x8.draw2x2String(0, 0, buff); // display number on unique macs total Wifi + BLE u8x8.setCursor(0,4); - u8x8.printf("WIFI: %-4d", macs_wifi); + u8x8.printf("WIFI:%-4d", macs_wifi); #ifdef BLECOUNTER u8x8.setCursor(0,3); if (cfg.blescan) - u8x8.printf("BLTH: %-4d", macs_ble); + u8x8.printf("BLTH:%-4d", macs_ble); else - u8x8.printf("%-16s", "BLTH: off"); + u8x8.printf("%-16s", "BLTH:off"); #endif - // update wifi channel display (line 4) - u8x8.setCursor(11,4); - u8x8.printf("ch:%02i", channel); - - // update RSSI limiter status display (line 5) + // update RSSI limiter status & wifi channel display (line 5) u8x8.setCursor(0,5); - u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %-4d", cfg.rssilimit); + u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%-4d", cfg.rssilimit); + u8x8.setCursor(11,5); + u8x8.printf("ch:%02i", channel); // update LoRa status display (line 6) u8x8.setCursor(0,6); @@ -391,7 +389,7 @@ uint64_t uptime() { #ifdef HAS_LED void switchLED() { - enum states previousLEDState; + // led need to change state? avoid digitalWrite() for nothing if (LEDState != previousLEDState) { #ifdef LED_ACTIVE_LOW @@ -404,16 +402,25 @@ uint64_t uptime() { rgb_set_color(LEDState ? color : COLOR_NONE); #endif + ESP_LOGI(TAG, "switched LED: %d -> %d", previousLEDState, LEDState); + previousLEDState = LEDState; - LEDcount--; // decrement blink counter } + }; // switchLED() void switchLEDstate() { - if (!LEDcount) // no more blinks? -> switch off LED - LEDState = LED_OFF; - else if (LEDInterval) // blinks left? -> toggle LED and decrement blinks + + // 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 LEDState = ((currentMillis % LEDInterval) < LEDBlinkduration) ? LED_ON : LED_OFF; + else // only one blink + LEDState = (currentMillis < LEDBlinkduration) ? LED_ON : LED_OFF; + + ESP_LOGI(TAG, "current LEDState %d", LEDState); + } // switchLEDstate() #endif @@ -462,7 +469,7 @@ void setup() { // initialize led if needed #ifdef HAS_LED pinMode(HAS_LED, OUTPUT); - set_LED(COLOR_NONE, 0, 0, 0); // LED off + blink_LED(COLOR_NONE, 0, 0); // LED off #endif // initialize button handling if needed @@ -490,13 +497,13 @@ void setup() { u8x8.setPowerSave(!cfg.screenon); // set display off if disabled u8x8.draw2x2String(0, 0, "PAX:0"); u8x8.setCursor(0,4); - u8x8.printf("WIFI: 0"); + u8x8.printf("WIFI:0"); #ifdef BLECOUNTER u8x8.setCursor(0,3); - u8x8.printf("BLTH: 0"); + u8x8.printf("BLTH:0"); #endif u8x8.setCursor(0,5); - u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %d", cfg.rssilimit); + u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%d", cfg.rssilimit); sprintf(display_lora, "Join wait"); #endif @@ -541,11 +548,16 @@ do_send(&sendjob); // https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/ void loop() { - uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) - currentMillis = millis(); // timebase for state machine in milliseconds (32bit) - // 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 + #ifdef HAS_BUTTON readButton(); #endif @@ -554,13 +566,6 @@ void loop() { updateDisplay(); #endif - #ifdef HAS_LED - switchLEDstate(); - switchLED(); - #endif - - //sendPayload(); - -} + } /* end Aruino LOOP ------------------------------------------------------------ */ diff --git a/src/main.h b/src/main.h index 26baff29..d92ed7a2 100644 --- a/src/main.h +++ b/src/main.h @@ -6,14 +6,14 @@ //--- Declarations --- enum states { - LED_ON, - LED_OFF + LED_OFF, + LED_ON }; //--- Prototypes --- // defined in main.cpp -void set_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval, uint8_t set_count); +void blink_LED (uint16_t set_color, uint16_t set_blinkduration, uint16_t set_interval); void reset_counters(); // defined in configmanager.cpp From 7f4782914005299b1395c748c11241d8a544475a Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 20 Apr 2018 11:52:58 +0200 Subject: [PATCH 17/46] v1.3.2 --- src/main.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 61a1f2da..4cd60af2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -402,8 +402,6 @@ uint64_t uptime() { rgb_set_color(LEDState ? color : COLOR_NONE); #endif - ESP_LOGI(TAG, "switched LED: %d -> %d", previousLEDState, LEDState); - previousLEDState = LEDState; } @@ -419,8 +417,6 @@ uint64_t uptime() { else // only one blink LEDState = (currentMillis < LEDBlinkduration) ? LED_ON : LED_OFF; - ESP_LOGI(TAG, "current LEDState %d", LEDState); - } // switchLEDstate() #endif From 244eb10c2941180c90973552cdabecb031518972 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 24 Apr 2018 22:29:12 +0200 Subject: [PATCH 18/46] 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 19/46] 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 20/46] 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 21/46] 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 22/46] 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 23/46] 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 24/46] 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 25/46] 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 26/46] 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 27/46] 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 28/46] 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 29/46] 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 30/46] 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 From 19b9e4d721544d37fb3ee9fe22f1b27bb6097c90 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 18:32:36 +0200 Subject: [PATCH 31/46] wifi & lmic loops restructured --- platformio.ini | 4 +-- src/lorawan.cpp | 53 +++++++++++++++++---------------- src/main.cpp | 71 ++++++++++++++++++--------------------------- src/main.h | 2 +- src/paxcounter.conf | 6 ++-- 5 files changed, 61 insertions(+), 75 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/lorawan.cpp b/src/lorawan.cpp index 395c77e8..d0ffdd96 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -109,7 +109,7 @@ void printKeys(void) { void do_send(osjob_t* j){ uint8_t mydata[4]; - uint16_t data; + // Sum of unique WIFI MACs seen mydata[0] = (macs_wifi & 0xff00) >> 8; mydata[1] = macs_wifi & 0xff; @@ -123,12 +123,6 @@ void do_send(osjob_t* j){ mydata[3] = 0; #endif - // Total BLE+WIFI unique MACs seen - // TBD ? - //data = (uint16_t) macs.size(); - //mydata[4] = (macs_total & 0xff00) >> 8; - //mydata[5] = macs_total & 0xff; - // Check if there is not a current TX/RX job running if (LMIC.opmode & OP_TXRXPEND) { ESP_LOGI(TAG, "OP_TXRXPEND, not sending"); @@ -138,9 +132,17 @@ void do_send(osjob_t* j){ LMIC_setTxData2(1, mydata, sizeof(mydata), (cfg.countermode & 0x02)); ESP_LOGI(TAG, "Packet queued"); sprintf(display_lmic, "PACKET QUEUED"); + // clear counter if not in cumulative counter mode + if (cfg.countermode != 1) { + reset_counters(); // clear macs container and reset all counters + reset_salt(); // get new salt for salting hashes + } } - // Next TX is scheduled after TX_COMPLETE event. -} + + // Schedule next transmission + os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(SEND_SECS * 2), do_send); + +} // do_send() void onEvent (ev_t ev) { char buff[24]=""; @@ -161,8 +163,10 @@ void onEvent (ev_t ev) { case EV_REJOIN_FAILED: strcpy_P(buff, PSTR("REJOIN FAILED")); break; case EV_JOINED: + + joinstate=true; strcpy_P(buff, PSTR("JOINED")); - sprintf(display_lora, ""); // erase "Join Wait" message from display + // Disable link check validation (automatically enabled // during join, but not supported by TTN at this time). LMIC_setLinkCheckMode(0); @@ -170,26 +174,21 @@ void onEvent (ev_t ev) { LMIC_setAdrMode(cfg.adrmode); // Set data rate and transmit power (note: txpower seems to be ignored by the library) switch_lora(cfg.lorasf,cfg.txpower); - joinstate=true; + // show effective LoRa parameters after join ESP_LOGI(TAG, "ADR=%i, SF=%i, TXPOWER=%i", cfg.adrmode, cfg.lorasf, cfg.txpower); break; - case EV_TXCOMPLETE: - ESP_LOGI(TAG, "EV_TXCOMPLETE (includes waiting for RX windows)"); - if (LMIC.txrxFlags & TXRX_ACK) { - ESP_LOGI(TAG, "Received ack"); - sprintf(display_lmic, "RECEIVED ACK"); - - } else { - sprintf(display_lmic, "TX COMPLETE"); - } - if (LMIC.dataLen) { - ESP_LOGI(TAG, "Received %d bytes of payload", LMIC.dataLen); - sprintf(display_lora, "Rcvd %d bytes", LMIC.dataLen); + case EV_TXCOMPLETE: + + strcpy_P(buff, (LMIC.txrxFlags & TXRX_ACK) ? PSTR("RECEIVED ACK") : PSTR("TX COMPLETE")); + sprintf(display_lora, ""); // erase previous LoRa message from display + + if (LMIC.dataLen) { + ESP_LOGI(TAG, "Received %d bytes of payload, RSSI %d SNR %d", LMIC.dataLen, LMIC.rssi, (signed char)LMIC.snr / 4); // LMIC.snr = SNR twos compliment [dB] * 4 // LMIC.rssi = RSSI [dBm] (-196...+63) - sprintf(display_lmic, "RSSI %d SNR %d", LMIC.rssi, (signed char)LMIC.snr / 4 ); + sprintf(display_lora, "RSSI %d SNR %d", LMIC.rssi, (signed char)LMIC.snr / 4 ); // check if payload received on command port, then call remote command interpreter if ( (LMIC.txrxFlags & TXRX_PORT) && (LMIC.frame[LMIC.dataBeg-1] == RCMDPORT ) ) { @@ -203,6 +202,7 @@ void onEvent (ev_t ev) { } } break; + default: sprintf_P(buff, PSTR("UNKNOWN EVENT %d"), ev); break; } @@ -211,7 +211,6 @@ void onEvent (ev_t ev) { ESP_LOGI(TAG, "EV_%s", buff); sprintf(display_lmic, buff); } - - -} + +} // onEvent() diff --git a/src/main.cpp b/src/main.cpp index 70b1003e..15dbc0dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -142,14 +142,33 @@ void lorawan_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check + static uint16_t lorawait = 0; + while(1) { + // execute LMIC jobs os_runloop_once(); + // indicate LMIC state on LEDs if present #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) led_loop(); #endif - +/* + // check if payload is sent + while(LMIC.opmode & OP_TXRXPEND) { + if(!lorawait) + sprintf(display_lora, "LoRa wait"); + lorawait++; + // in case sending really fails: reset LMIC and rejoin network + if( (lorawait % MAXLORARETRY ) == 0) { + ESP_LOGI(TAG, "Payload not sent, resetting LMIC and rejoin"); + lorawait = 0; + LMIC_reset(); // Reset the MAC state. Session and pending data transfers will be discarded. + }; + vTaskDelay(1000/portTICK_PERIOD_MS); + yield(); + } +*/ vTaskDelay(10/portTICK_PERIOD_MS); yield(); } @@ -192,49 +211,15 @@ void sniffer_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check - char buff[16]; - int nloop=0, lorawait=0; - while (1) { - nloop++; // actual number of wifi loops, controls cycle when data is sent - - channel = (channel % WIFI_CHANNEL_MAX) + 1; // rotates variable channel 1..WIFI_CHANNEL_MAX - wifi_sniffer_set_channel(channel); - ESP_LOGD(TAG, "Wifi set channel %d", channel); - - // duration of one wifi scan loop reached? then send data and begin new scan cycle - if ( nloop >= ( (100 / cfg.wifichancycle) * (cfg.wifiscancycle * 2)) +1 ) { - - nloop=0; channel=0; // reset wifi scan + channel loop counter - do_send(&sendjob); // Prepare and execute LoRaWAN data upload - - // clear counter if not in cumulative counter mode - if (cfg.countermode != 1) { - reset_counters(); // clear macs container and reset all counters - reset_salt(); // get new salt for salting hashes - } - - // check if payload is sent - lorawait = 0; - while(LMIC.opmode & OP_TXRXPEND) { - if(!lorawait) - sprintf(display_lora, "LoRa wait"); - lorawait++; - // in case sending really fails: reset and rejoin network - if( (lorawait % MAXLORARETRY ) == 0) { - ESP_LOGI(TAG, "Payload not sent, trying reset and rejoin"); - esp_restart(); - }; - vTaskDelay(1000/portTICK_PERIOD_MS); - yield(); - } - sprintf(display_lora, ""); // clear LoRa wait message fromd display - - } // end of send data cycle - - vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS); - yield(); + for (channel = 1; channel <= WIFI_CHANNEL_MAX; channel++) { + // rotates variable channel 1..WIFI_CHANNEL_MAX + wifi_sniffer_set_channel(channel); + ESP_LOGD(TAG, "Wifi set channel %d", channel); + vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS); + yield(); + } } // end of infinite wifi channel rotation loop } @@ -556,7 +541,9 @@ ESP_LOGI(TAG, "Features %s", features); #endif os_init(); // setup LMIC +LMIC_reset(); // Reset the MAC state. Session and pending data transfers will be discarded. os_setCallback(&initjob, lora_init); // setup initial job & join network + wifi_sniffer_init(); // setup wifi in monitor mode and start MAC counting // initialize salt value using esp_random() called by random() in arduino-esp32 core diff --git a/src/main.h b/src/main.h index 99e27afe..64a27267 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.22" // use max 10 chars here! +#define PROGVERSION "1.3.23" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 30189b7b..e981fc3c 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -32,9 +32,9 @@ #define WIFI_MY_COUNTRY "EU" // select locale for Wifi RF settings #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. +// LoRa payload send cycle --> take care of duty cycle of LoRaWAN network! <-- +//#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 996f94d401275cda6b02ee9bcf5375996c1cd585 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 18:48:24 +0200 Subject: [PATCH 32/46] repair memory display --- src/macsniff.cpp | 4 ++-- src/main.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/macsniff.cpp b/src/macsniff.cpp index da69ba22..3595f18e 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 - float memlevel; // % of used heap mem + uint8_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 @@ -53,7 +53,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { if (added) { // Display heap memory left memlevel = ESP.getFreeHeap() / heapmem * 100; - sprintf(display_mem, "%d%%", memlevel); + sprintf(display_mem, "%i%%", memlevel); // increment counter and one blink led if (sniff_type == MAC_SNIFF_WIFI ) { macs_wifi++; // increment Wifi MACs counter diff --git a/src/main.cpp b/src/main.cpp index 15dbc0dc..2e8b8d21 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,7 +54,7 @@ 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 -const uint32_t heapmem = ESP.getFreeHeap(); // free heap memory after start (:= 100%) +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 3ca741c6dc6822fc7e057ff281712bebcef110f3 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 21:29:46 +0200 Subject: [PATCH 33/46] v1.3.3: repair memory display --- platformio.ini | 4 ++-- src/configmanager.cpp | 56 +++++++++++++++++++++---------------------- src/globals.h | 3 +-- src/lorawan.cpp | 2 +- src/macsniff.cpp | 8 ++----- src/main.cpp | 18 +++++++++----- src/main.h | 2 +- src/paxcounter.conf | 4 ++-- src/rcommand.cpp | 18 +++++++------- 9 files changed, 58 insertions(+), 57 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 diff --git a/src/configmanager.cpp b/src/configmanager.cpp index fecd37a1..628e4cd3 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -174,113 +174,113 @@ void loadConfig() { // overwrite defaults with valid values from NVRAM if( nvs_get_i8(my_handle, "lorasf", &flash8) == ESP_OK ) { cfg.lorasf = flash8; - ESP_LOGI(TAG, "lorasf = %i", flash8); + ESP_LOGI(TAG, "lorasf = %d", flash8); } else { - ESP_LOGI(TAG, "lorasf set to default %i", cfg.lorasf); + ESP_LOGI(TAG, "lorasf set to default %d", cfg.lorasf); saveConfig(); } if( nvs_get_i8(my_handle, "txpower", &flash8) == ESP_OK ) { cfg.txpower = flash8; - ESP_LOGI(TAG, "txpower = %i", flash8); + ESP_LOGI(TAG, "txpower = %d", flash8); } else { - ESP_LOGI(TAG, "txpower set to default %i", cfg.txpower); + ESP_LOGI(TAG, "txpower set to default %d", cfg.txpower); saveConfig(); } if( nvs_get_i8(my_handle, "adrmode", &flash8) == ESP_OK ) { cfg.adrmode = flash8; - ESP_LOGI(TAG, "adrmode = %i", flash8); + ESP_LOGI(TAG, "adrmode = %d", flash8); } else { - ESP_LOGI(TAG, "adrmode set to default %i", cfg.adrmode); + ESP_LOGI(TAG, "adrmode set to default %d", cfg.adrmode); saveConfig(); } if( nvs_get_i8(my_handle, "screensaver", &flash8) == ESP_OK ) { cfg.screensaver = flash8; - ESP_LOGI(TAG, "screensaver = %i", flash8); + ESP_LOGI(TAG, "screensaver = %d", flash8); } else { - ESP_LOGI(TAG, "screensaver set to default %i", cfg.screensaver); + ESP_LOGI(TAG, "screensaver set to default %d", cfg.screensaver); saveConfig(); } if( nvs_get_i8(my_handle, "screenon", &flash8) == ESP_OK ) { cfg.screenon = flash8; - ESP_LOGI(TAG, "screenon = %i", flash8); + ESP_LOGI(TAG, "screenon = %d", flash8); } else { - ESP_LOGI(TAG, "screenon set to default %i", cfg.screenon); + ESP_LOGI(TAG, "screenon set to default %d", cfg.screenon); saveConfig(); } if( nvs_get_i8(my_handle, "countermode", &flash8) == ESP_OK ) { cfg.countermode = flash8; - ESP_LOGI(TAG, "countermode = %i", flash8); + ESP_LOGI(TAG, "countermode = %d", flash8); } else { - ESP_LOGI(TAG, "countermode set to default %i", cfg.countermode); + ESP_LOGI(TAG, "countermode set to default %d", cfg.countermode); saveConfig(); } if( nvs_get_i8(my_handle, "wifiscancycle", &flash8) == ESP_OK ) { cfg.wifiscancycle = flash8; - ESP_LOGI(TAG, "wifiscancycle = %i", flash8); + ESP_LOGI(TAG, "wifiscancycle = %d", flash8); } else { - ESP_LOGI(TAG, "WIFI scan cycle set to default %i", cfg.wifiscancycle); + ESP_LOGI(TAG, "WIFI scan cycle set to default %d", cfg.wifiscancycle); saveConfig(); } if( nvs_get_i8(my_handle, "wifichancycle", &flash8) == ESP_OK ) { cfg.wifichancycle = flash8; - ESP_LOGI(TAG, "wifichancycle = %i", flash8); + ESP_LOGI(TAG, "wifichancycle = %d", flash8); } else { - ESP_LOGI(TAG, "WIFI channel cycle set to default %i", cfg.wifichancycle); + ESP_LOGI(TAG, "WIFI channel cycle set to default %d", cfg.wifichancycle); saveConfig(); } if( nvs_get_i8(my_handle, "wifiant", &flash8) == ESP_OK ) { cfg.wifiant = flash8; - ESP_LOGI(TAG, "wifiantenna = %i", flash8); + ESP_LOGI(TAG, "wifiantenna = %d", flash8); } else { - ESP_LOGI(TAG, "WIFI antenna switch set to default %i", cfg.wifiant); + ESP_LOGI(TAG, "WIFI antenna switch set to default %d", cfg.wifiant); saveConfig(); } if( nvs_get_i8(my_handle, "vendorfilter", &flash8) == ESP_OK ) { cfg.vendorfilter = flash8; - ESP_LOGI(TAG, "vendorfilter = %i", flash8); + ESP_LOGI(TAG, "vendorfilter = %d", flash8); } else { - ESP_LOGI(TAG, "Vendorfilter mode set to default %i", cfg.vendorfilter); + ESP_LOGI(TAG, "Vendorfilter mode set to default %d", cfg.vendorfilter); saveConfig(); } if( nvs_get_i8(my_handle, "rgblum", &flash8) == ESP_OK ) { cfg.rgblum = flash8; - ESP_LOGI(TAG, "rgbluminosity = %i", flash8); + ESP_LOGI(TAG, "rgbluminosity = %d", flash8); } else { - ESP_LOGI(TAG, "RGB luminosity set to default %i", cfg.rgblum); + ESP_LOGI(TAG, "RGB luminosity set to default %d", cfg.rgblum); saveConfig(); } if( nvs_get_i8(my_handle, "blescantime", &flash8) == ESP_OK ) { cfg.blescantime = flash8; - ESP_LOGI(TAG, "blescantime = %i", flash8); + ESP_LOGI(TAG, "blescantime = %d", flash8); } else { - ESP_LOGI(TAG, "BLEscantime set to default %i", cfg.blescantime); + ESP_LOGI(TAG, "BLEscantime set to default %d", cfg.blescantime); saveConfig(); } if( nvs_get_i8(my_handle, "blescanmode", &flash8) == ESP_OK ) { cfg.blescan = flash8; - ESP_LOGI(TAG, "BLEscanmode = %i", flash8); + ESP_LOGI(TAG, "BLEscanmode = %d", flash8); } else { - ESP_LOGI(TAG, "BLEscanmode set to default %i", cfg.blescan); + ESP_LOGI(TAG, "BLEscanmode set to default %d", cfg.blescan); saveConfig(); } if( nvs_get_i16(my_handle, "rssilimit", &flash16) == ESP_OK ) { cfg.rssilimit = flash16; - ESP_LOGI(TAG, "rssilimit = %i", flash16); + ESP_LOGI(TAG, "rssilimit = %d", flash16); } else { - ESP_LOGI(TAG, "rssilimit set to default %i", cfg.rssilimit); + ESP_LOGI(TAG, "rssilimit set to default %d", cfg.rssilimit); saveConfig(); } diff --git a/src/globals.h b/src/globals.h index 158fecf0..7035c7d2 100644 --- a/src/globals.h +++ b/src/globals.h @@ -48,12 +48,11 @@ extern uint8_t mydata[]; extern uint64_t uptimecounter; extern unsigned long currentMillis ; extern osjob_t sendjob; -extern char display_lora[], display_lmic[], display_mem[]; +extern char display_lora[], display_lmic[]; 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/lorawan.cpp b/src/lorawan.cpp index d0ffdd96..b4f09726 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -176,7 +176,7 @@ void onEvent (ev_t ev) { switch_lora(cfg.lorasf,cfg.txpower); // show effective LoRa parameters after join - ESP_LOGI(TAG, "ADR=%i, SF=%i, TXPOWER=%i", cfg.adrmode, cfg.lorasf, cfg.txpower); + ESP_LOGI(TAG, "ADR=%d, SF=%d, TXPOWER=%d", cfg.adrmode, cfg.lorasf, cfg.txpower); break; case EV_TXCOMPLETE: diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 3595f18e..331feb35 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -27,7 +27,6 @@ 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 - uint8_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 @@ -51,9 +50,6 @@ 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, "%i%%", memlevel); // increment counter and one blink led if (sniff_type == MAC_SNIFF_WIFI ) { macs_wifi++; // increment Wifi MACs counter @@ -72,10 +68,10 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { } // Log scan result - ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s -> %d Bytes left", + ESP_LOGI(TAG, "%s %s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d -> %d Bytes left", + added ? "new " : "known", sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH", rssi, buff, hashedmac, macs_wifi, macs_ble, - added ? "new " : "known", ESP.getFreeHeap()); #ifdef VENDORFILTER diff --git a/src/main.cpp b/src/main.cpp index 2e8b8d21..8aff95ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,7 +46,7 @@ unsigned long previousDisplaymillis = currentMillis; // Display refresh for stat 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 +char display_lora[16], display_lmic[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 LEDBlinkStarted = 0; // When (in millis() led blink started) @@ -54,7 +54,6 @@ 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 -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) @@ -314,15 +313,15 @@ 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 free memory display (line 4) + u8x8.setCursor(10,4); + u8x8.printf("%4dKB", ESP.getFreeHeap() / 1024); // update RSSI limiter status & wifi channel display (line 5) u8x8.setCursor(0,5); u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%-4d", cfg.rssilimit); u8x8.setCursor(11,5); - u8x8.printf("ch:%02i", channel); + u8x8.printf("ch:%02d", channel); // update LoRa status display (line 6) u8x8.setCursor(0,6); @@ -594,6 +593,13 @@ void loop() { updateDisplay(); #endif + // check free memory + if (ESP.getFreeHeap() <= MEM_LOW) { + do_send(&sendjob); // send count + reset_counters(); // clear macs container and reset all counters + reset_salt(); // get new salt for salting hashes + } + } /* end Aruino LOOP ------------------------------------------------------------ */ diff --git a/src/main.h b/src/main.h index 64a27267..8b0d5b60 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.23" // use max 10 chars here! +#define PROGVERSION "1.3.3" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- diff --git a/src/paxcounter.conf b/src/paxcounter.conf index e981fc3c..ec8c455a 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 --> take care of duty cycle of LoRaWAN network! <-- -//#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 MEM_LOW 2048 // [Bytes] memory threshold triggering send cycle // Default LoRa Spreadfactor #define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs diff --git a/src/rcommand.cpp b/src/rcommand.cpp index fe73c8aa..90894149 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -74,22 +74,22 @@ void set_reset(int val) { void set_rssi(int val) { cfg.rssilimit = val * -1; - ESP_LOGI(TAG, "Remote command: set RSSI limit to %i", cfg.rssilimit); + ESP_LOGI(TAG, "Remote command: set RSSI limit to %d", cfg.rssilimit); }; void set_wifiscancycle(int val) { cfg.wifiscancycle = val; - ESP_LOGI(TAG, "Remote command: set Wifi scan cycle duration to %i seconds", cfg.wifiscancycle*2); + ESP_LOGI(TAG, "Remote command: set Wifi scan cycle duration to %d seconds", cfg.wifiscancycle*2); }; void set_wifichancycle(int val) { cfg.wifichancycle = val; - ESP_LOGI(TAG, "Remote command: set Wifi channel switch interval to %i seconds", cfg.wifichancycle/100); + ESP_LOGI(TAG, "Remote command: set Wifi channel switch interval to %d seconds", cfg.wifichancycle/100); }; void set_blescantime(int val) { cfg.blescantime = val; - ESP_LOGI(TAG, "Remote command: set BLE scan time to %i seconds", cfg.blescantime); + ESP_LOGI(TAG, "Remote command: set BLE scan time to %d seconds", cfg.blescantime); }; void set_countmode(int val) { @@ -126,7 +126,7 @@ void set_display(int val) { }; void set_lorasf(int val) { - ESP_LOGI(TAG, "Remote command: set LoRa SF to %i", val); + ESP_LOGI(TAG, "Remote command: set LoRa SF to %d", val); switch_lora(val, cfg.txpower); }; @@ -178,7 +178,7 @@ void set_rgblum(int val) { }; void set_lorapower(int val) { - ESP_LOGI(TAG, "Remote command: set LoRa TXPOWER to %i", val); + ESP_LOGI(TAG, "Remote command: set LoRa TXPOWER to %d", val); switch_lora(cfg.lorasf, val); }; @@ -195,7 +195,7 @@ void get_config (int val) { memcpy(sendData, &cfg, size); LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port delete sendData; // free memory - ESP_LOGI(TAG, "%i bytes queued in send queue", size-1); + ESP_LOGI(TAG, "%d bytes queued in send queue", size-1); }; void get_uptime (int val) { @@ -205,7 +205,7 @@ void get_uptime (int val) { memcpy(sendData, (unsigned char*)&uptimecounter , size); LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port delete sendData; // free memory - ESP_LOGI(TAG, "%i bytes queued in send queue", size-1); + ESP_LOGI(TAG, "%d bytes queued in send queue", size-1); }; void get_cputemp (int val) { @@ -216,7 +216,7 @@ void get_cputemp (int val) { memcpy(sendData, (unsigned char*)&temp, size); LMIC_setTxData2(RCMDPORT, sendData, size-1, 0); // send data unconfirmed on RCMD Port delete sendData; // free memory - ESP_LOGI(TAG, "%i bytes queued in send queue", size-1); + ESP_LOGI(TAG, "%d bytes queued in send queue", size-1); }; // assign previously defined functions to set of numeric remote commands From eae0eec036ac83bebd331ddceaab66ffbf25fd52 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 21:33:04 +0200 Subject: [PATCH 34/46] update readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0fa85e7a..db0aa560 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts. 0x80 get device configuration -device answers with it's current configuration. The configuration is a C structure declared in file [globals.h](src/globals.h#L24-L41) with the following definition: +device answers with it's current configuration. The configuration is a C structure declared in file [globals.h](src/globals.h#L27-L44) with the following definition: byte 1: Lora SF (7..12) byte 2: Lora TXpower (2..15) From cc003c43f0a6dc0074e032c8f76c57acf9765231 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 21:38:50 +0200 Subject: [PATCH 35/46] removed var currentMillis --- src/globals.h | 1 - src/main.cpp | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/globals.h b/src/globals.h index 7035c7d2..6d8f393b 100644 --- a/src/globals.h +++ b/src/globals.h @@ -46,7 +46,6 @@ typedef struct { extern configData_t cfg; extern uint8_t mydata[]; extern uint64_t uptimecounter; -extern unsigned long currentMillis ; extern osjob_t sendjob; extern char display_lora[], display_lmic[]; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; diff --git a/src/main.cpp b/src/main.cpp index 8aff95ee..1e937e9c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,8 +41,7 @@ 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 -unsigned long currentMillis = millis(); // timer global for state machine -unsigned long previousDisplaymillis = currentMillis; // Display refresh for state machine +unsigned long previousDisplaymillis = millis(); // Display refresh for state machine 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 @@ -335,7 +334,7 @@ uint64_t uptime() { void updateDisplay() { // timed display refresh according to refresh cycle setting - if (currentMillis - previousDisplaymillis >= DISPLAYREFRESH_MS) { + if (millis() - previousDisplaymillis >= DISPLAYREFRESH_MS) { refreshDisplay(); previousDisplaymillis += DISPLAYREFRESH_MS; } @@ -579,7 +578,6 @@ 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) #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) led_loop(); From b9db1bf1dcdc0b6916f2bce1757475c8976f7c89 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 22:41:07 +0200 Subject: [PATCH 36/46] Display refresh & Button IRQ --- src/main.cpp | 59 ++++++++++++++++++++++++++++----------------- src/paxcounter.conf | 2 +- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1e937e9c..3d1a26ac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,7 +41,6 @@ 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 -unsigned long previousDisplaymillis = millis(); // Display refresh for state machine 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 @@ -53,11 +52,13 @@ 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 +hw_timer_t * timer = NULL; // configure hardware timer used for cyclic display refresh +portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; // sync main loop and ISR when modifying shared variable DisplayIRQ std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) -// this variable will be changed in the ISR, and read in main loop -static volatile bool ButtonTriggered = false; +// this variables will be changed in the ISR, and read in main loop +static volatile int ButtonPressed = 0, DisplayTimerIRQ = 0; // local Tag for logging static const char *TAG = "paxcnt"; @@ -180,6 +181,12 @@ void lorawan_loop(void * pvParameters) { #ifdef HAS_DISPLAY HAS_DISPLAY u8x8(OLED_RST, OLED_SCL, OLED_SDA); + // Display Refresh IRQ + void IRAM_ATTR DisplayIRQ() { + portENTER_CRITICAL_ISR(&timerMux); + DisplayTimerIRQ++; + portEXIT_CRITICAL_ISR(&timerMux); + } #endif #ifdef HAS_ANTENNA_SWITCH @@ -193,10 +200,11 @@ void lorawan_loop(void * pvParameters) { #endif #ifdef HAS_BUTTON - // Button Handling, board dependent -> perhaps to be moved to hal/<$board.h> + // Button IRQ // IRAM_ATTR necessary here, see https://github.com/espressif/arduino-esp32/issues/855 - void IRAM_ATTR isr_button_pressed(void) { - ButtonTriggered = true; } + void IRAM_ATTR ButtonIRQ() { + ButtonPressed++; + } #endif /* end hardware specific parts -------------------------------------------------------- */ @@ -332,26 +340,27 @@ uint64_t uptime() { } void updateDisplay() { - // timed display refresh according to refresh cycle setting - - if (millis() - previousDisplaymillis >= DISPLAYREFRESH_MS) { + // refresh display according to refresh cycle setting + if (DisplayTimerIRQ) { + portENTER_CRITICAL(&timerMux); + DisplayTimerIRQ--; + portEXIT_CRITICAL(&timerMux); + refreshDisplay(); - previousDisplaymillis += DISPLAYREFRESH_MS; - } - // set display on/off according to current device configuration - if (DisplayState != cfg.screenon) { - DisplayState = cfg.screenon; - u8x8.setPowerSave(!cfg.screenon); + + // set display on/off according to current device configuration + if (DisplayState != cfg.screenon) { + DisplayState = cfg.screenon; + u8x8.setPowerSave(!cfg.screenon); + } } } // updateDisplay() - - #endif // HAS_DISPLAY #ifdef HAS_BUTTON void readButton() { - if (ButtonTriggered) { - ButtonTriggered = false; + if (ButtonPressed) { + ButtonPressed--; ESP_LOGI(TAG, "Button pressed, resetting device to factory defaults"); eraseConfig(); esp_restart(); @@ -496,12 +505,12 @@ void setup() { strcat(features, "PU"); // install button interrupt (pullup mode) pinMode(HAS_BUTTON, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), isr_button_pressed, RISING); + attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, RISING); #else strcat(features, "PD"); // install button interrupt (pulldown mode) pinMode(HAS_BUTTON, INPUT_PULLDOWN); - attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), isr_button_pressed, FALLING); + attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, FALLING); #endif #endif @@ -528,9 +537,15 @@ void setup() { u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%d", cfg.rssilimit); sprintf(display_lora, "Join wait"); + + // setup Display IRQ, thanks to https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/ + timer = timerBegin(0, 80, true); // prescaler 80 -> divides 80 MHz CPU freq to 1 MHz, timer 0, count up + timerAttachInterrupt(timer, &DisplayIRQ, true); // interrupt handler DisplayIRQ, triggered by edge + timerAlarmWrite(timer, DISPLAYREFRESH_MS * 1000, true); // reload interrupt after each trigger of display refresh cycle + timerAlarmEnable(timer); // enable display interrupt #endif -// Display features compiled for +// show compiled features ESP_LOGI(TAG, "Features %s", features); // output LoRaWAN keys to console diff --git a/src/paxcounter.conf b/src/paxcounter.conf index ec8c455a..584e1ab4 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -45,7 +45,7 @@ #define RGBLUMINOSITY 30 // 30% // OLED Display refresh cycle (in Milliseconds) -#define DISPLAYREFRESH_MS 40 // e.g. 40ms -> 1000/40 = 25 frames per second +#define DISPLAYREFRESH_MS 40 // e.g. 40ms -> 1000/40 = 25 frames per second // LMIC settings // define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored From 9916d8f6daa008a1fffcfac369a2cfe96b74b4ab Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 23:27:21 +0200 Subject: [PATCH 37/46] changed wifi channel switching to hw interrupt --- src/main.cpp | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3d1a26ac..d50a7d1a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,13 +52,15 @@ 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 -hw_timer_t * timer = NULL; // configure hardware timer used for cyclic display refresh +hw_timer_t * displaytimer = NULL; // configure hardware timer used for cyclic display refresh +hw_timer_t * channelSwitch = NULL; // configure hardware timer used for wifi channel switching + portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; // sync main loop and ISR when modifying shared variable DisplayIRQ std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) // this variables will be changed in the ISR, and read in main loop -static volatile int ButtonPressed = 0, DisplayTimerIRQ = 0; +static volatile int ButtonPressed = 0, DisplayTimerIRQ = 0, ChannelTimerIRQ = 0; // local Tag for logging static const char *TAG = "paxcnt"; @@ -207,6 +209,10 @@ void lorawan_loop(void * pvParameters) { } #endif +void IRAM_ATTR ChannelSwitchIRQ() { + ChannelTimerIRQ++; +} + /* end hardware specific parts -------------------------------------------------------- */ @@ -219,12 +225,14 @@ void sniffer_loop(void * pvParameters) { while (1) { - for (channel = 1; channel <= WIFI_CHANNEL_MAX; channel++) { - // rotates variable channel 1..WIFI_CHANNEL_MAX + if (ChannelTimerIRQ) { + portENTER_CRITICAL(&timerMux); + ChannelTimerIRQ--; + portEXIT_CRITICAL(&timerMux); + // rotates variable channel 1..WIFI_CHANNEL_MAX + channel = (channel % WIFI_CHANNEL_MAX) + 1; wifi_sniffer_set_channel(channel); ESP_LOGD(TAG, "Wifi set channel %d", channel); - vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS); - yield(); } } // end of infinite wifi channel rotation loop @@ -539,12 +547,18 @@ void setup() { sprintf(display_lora, "Join wait"); // setup Display IRQ, thanks to https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/ - timer = timerBegin(0, 80, true); // prescaler 80 -> divides 80 MHz CPU freq to 1 MHz, timer 0, count up - timerAttachInterrupt(timer, &DisplayIRQ, true); // interrupt handler DisplayIRQ, triggered by edge - timerAlarmWrite(timer, DISPLAYREFRESH_MS * 1000, true); // reload interrupt after each trigger of display refresh cycle - timerAlarmEnable(timer); // enable display interrupt + displaytimer = timerBegin(0, 80, true); // prescaler 80 -> divides 80 MHz CPU freq to 1 MHz, timer 0, count up + timerAttachInterrupt(displaytimer, &DisplayIRQ, true); // interrupt handler DisplayIRQ, triggered by edge + timerAlarmWrite(displaytimer, DISPLAYREFRESH_MS * 1000, true); // reload interrupt after each trigger of display refresh cycle + timerAlarmEnable(displaytimer); // enable display interrupt #endif +// setup channel rotation IRQ, thanks to https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/ +channelSwitch = timerBegin(1, 80, true); // prescaler 80 -> divides 80 MHz CPU freq to 1 MHz, timer 1, count up +timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true); // interrupt handler, triggered by edge +timerAlarmWrite(channelSwitch, WIFI_CHANNEL_SWITCH_INTERVAL * 10000, true); // reload interrupt after each trigger of channel switch cycle +timerAlarmEnable(channelSwitch); // enable channel switching interrupt + // show compiled features ESP_LOGI(TAG, "Features %s", features); From ad4468739271c4512998a7f0f77d9ce013230725 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 23:44:29 +0200 Subject: [PATCH 38/46] watchdog reset cleanup --- src/blecsan.cpp | 3 +-- src/main.cpp | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 8dd4bb15..34036749 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -286,8 +286,7 @@ void bt_loop(void * pvParameters) while(1) { - vTaskDelay(10/portTICK_PERIOD_MS); - yield(); + vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog } end: diff --git a/src/main.cpp b/src/main.cpp index d50a7d1a..bdf6eca3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -167,11 +167,9 @@ void lorawan_loop(void * pvParameters) { LMIC_reset(); // Reset the MAC state. Session and pending data transfers will be discarded. }; vTaskDelay(1000/portTICK_PERIOD_MS); - yield(); } */ - vTaskDelay(10/portTICK_PERIOD_MS); - yield(); + vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog } } @@ -233,6 +231,8 @@ void sniffer_loop(void * pvParameters) { channel = (channel % WIFI_CHANNEL_MAX) + 1; wifi_sniffer_set_channel(channel); ESP_LOGD(TAG, "Wifi set channel %d", channel); + + vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog } } // end of infinite wifi channel rotation loop @@ -627,6 +627,8 @@ void loop() { reset_salt(); // get new salt for salting hashes } + vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog + } /* end Aruino LOOP ------------------------------------------------------------ */ From 9c10bdf5fe4cb2fa5cd98b927c8dbcc1f1df01f1 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 27 Apr 2018 23:57:16 +0200 Subject: [PATCH 39/46] v1.3.31 --- src/main.cpp | 2 ++ src/main.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index bdf6eca3..75187849 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -208,7 +208,9 @@ void lorawan_loop(void * pvParameters) { #endif void IRAM_ATTR ChannelSwitchIRQ() { + portENTER_CRITICAL(&timerMux); ChannelTimerIRQ++; + portEXIT_CRITICAL(&timerMux); } /* end hardware specific parts -------------------------------------------------------- */ diff --git a/src/main.h b/src/main.h index 8b0d5b60..48390d7e 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.3" // use max 10 chars here! +#define PROGVERSION "1.3.31" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- From 458bc8804110dfe698a09a8d7042796e60a9edec Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 28 Apr 2018 12:13:00 +0200 Subject: [PATCH 40/46] SF display & set Pin DIO2 to LMIC_unused_pin --- platformio.ini | 4 ++-- src/hal/heltec.h | 2 +- src/hal/lolin32_lora.h | 4 ++-- src/hal/lolin32lite_lora.h | 4 ++-- src/hal/lopy.h | 2 +- src/hal/lopy4.h | 2 +- src/hal/ttgov1.h | 2 +- src/hal/ttgov2.h | 2 +- src/main.cpp | 18 +++++++++++------- src/main.h | 6 ++++++ src/paxcounter.conf | 5 +++-- 11 files changed, 31 insertions(+), 20 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/hal/heltec.h b/src/hal/heltec.h index eb9aa31c..ed2e63db 100644 --- a/src/hal/heltec.h +++ b/src/hal/heltec.h @@ -17,7 +17,7 @@ #define RST 14 // ESP32 GPIO14 (Pin14) -- SX1276 NRESET (Pin7) Reset Trigger Input #define DIO0 26 // ESP32 GPIO26 (Pin15) -- SX1276 DIO0 (Pin8) used by LMIC for detecting LoRa RX_Done & TX_Done #define DIO1 33 // ESP32 GPIO33 (Pin13) -- SX1276 DIO1 (Pin9) used by LMIC for detecting LoRa RX_Timeout -#define DIO2 32 // ESP32 GPIO32 (Pin12) -- SX1276 DIO2 (Pin10) not used by LMIC for LoRa (Timeout for FSK only) +#define DIO2 LMIC_UNUSED_PIN // 32 ESP32 GPIO32 (Pin12) -- SX1276 DIO2 (Pin10) not used by LMIC for LoRa (Timeout for FSK only) // Hardware pin definitions for Heltec LoRa-32 Board with OLED SSD1306 I2C Display #define OLED_RST 16 // ESP32 GPIO16 (Pin16) -- SD1306 RST diff --git a/src/hal/lolin32_lora.h b/src/hal/lolin32_lora.h index a1bdbe09..ca5929af 100644 --- a/src/hal/lolin32_lora.h +++ b/src/hal/lolin32_lora.h @@ -25,8 +25,8 @@ #define RST 25 // ESP32 GPIO25 (Pin25) -- SX1276 NRESET (Pin7) Reset Trigger Input #define DIO0 27 // ESP32 GPIO27 (Pin27) -- SX1276 DIO0 (Pin8) used by LMIC for detecting LoRa RX_Done & TX_Done #define DIO1 26 // ESP32 GPIO26 (Pin26) -- SX1276 DIO1 (Pin9) used by LMIC for detecting LoRa RX_Timeout -#define DIO2 4 // ESP32 GPIO4 (Pin4) -- SX1276 DIO2 (Pin10) not used by LMIC for LoRa (Timeout for FSK only) -#define DIO5 35 // ESP32 GPIO35 (Pin35) -- SX1276 DIO5 not used by LMIC for LoRa (Timeout for FSK only) +#define DIO2 LMIC_UNUSED_PIN // 4 ESP32 GPIO4 (Pin4) -- SX1276 DIO2 (Pin10) not used by LMIC for LoRa (Timeout for FSK only) +#define DIO5 LMIC_UNUSED_PIN // 35 ESP32 GPIO35 (Pin35) -- SX1276 DIO5 not used by LMIC for LoRa (Timeout for FSK only) // Hardware pin definitions for LoRaNode32 Board with OLED I2C Display #define OLED_RST U8X8_PIN_NONE // Not reset pin diff --git a/src/hal/lolin32lite_lora.h b/src/hal/lolin32lite_lora.h index fe2adac8..51cd6f10 100644 --- a/src/hal/lolin32lite_lora.h +++ b/src/hal/lolin32lite_lora.h @@ -24,8 +24,8 @@ #define RST 25 // ESP32 GPIO25 (Pin25) -- SX1276 NRESET (Pin7) Reset Trigger Input #define DIO0 27 // ESP32 GPIO27 (Pin27) -- SX1276 DIO0 (Pin8) used by LMIC for detecting LoRa RX_Done & TX_Done #define DIO1 26 // ESP32 GPIO26 (Pin26) -- SX1276 DIO1 (Pin9) used by LMIC for detecting LoRa RX_Timeout -#define DIO2 4 // ESP32 GPIO4 (Pin4) -- SX1276 DIO2 (Pin10) not used by LMIC for LoRa (Timeout for FSK only) -#define DIO5 35 // ESP32 GPIO35 (Pin35) -- SX1276 DIO5 not used by LMIC for LoRa (Timeout for FSK only) +#define DIO2 LMIC_UNUSED_PIN // 4 ESP32 GPIO4 (Pin4) -- SX1276 DIO2 (Pin10) not used by LMIC for LoRa (Timeout for FSK only) +#define DIO5 LMIC_UNUSED_PIN // 35 ESP32 GPIO35 (Pin35) -- SX1276 DIO5 not used by LMIC for LoRa (Timeout for FSK only) // Hardware pin definitions for LoRaNode32 Board with OLED I2C Display #define OLED_RST U8X8_PIN_NONE // Not reset pin diff --git a/src/hal/lopy.h b/src/hal/lopy.h index fffb2294..3e34dc8f 100644 --- a/src/hal/lopy.h +++ b/src/hal/lopy.h @@ -12,7 +12,7 @@ #define RST 18 #define DIO0 23 // LoRa IRQ #define DIO1 23 // workaround -#define DIO2 23 // workaround +#define DIO2 LMIC_UNUSED_PIN // 23 workaround // select WIFI antenna (internal = onboard / external = u.fl socket) #define HAS_ANTENNA_SWITCH 16 // pin for switching wifi antenna diff --git a/src/hal/lopy4.h b/src/hal/lopy4.h index 4ff9ac72..223e26b5 100644 --- a/src/hal/lopy4.h +++ b/src/hal/lopy4.h @@ -12,7 +12,7 @@ #define RST LMIC_UNUSED_PIN #define DIO0 23 // LoRa IRQ #define DIO1 23 // workaround -#define DIO2 23 // workaround +#define DIO2 LMIC_UNUSED_PIN // 23 workaround // select WIFI antenna (internal = onboard / external = u.fl socket) #define HAS_ANTENNA_SWITCH 21 // pin for switching wifi antenna diff --git a/src/hal/ttgov1.h b/src/hal/ttgov1.h index ab6b4f2c..c8e1a12d 100644 --- a/src/hal/ttgov1.h +++ b/src/hal/ttgov1.h @@ -18,7 +18,7 @@ #define RST 14 // ESP32 GPIO14 (Pin14) -- SX1276 NRESET (Pin7) Reset Trigger Input #define DIO0 26 // ESP32 GPIO26 (Pin15) -- SX1276 DIO0 (Pin8) used by LMIC for detecting LoRa RX_Done & TX_Done #define DIO1 33 // ESP32 GPIO33 (Pin13) -- SX1276 DIO1 (Pin9) used by LMIC for detecting LoRa RX_Timeout -#define DIO2 32 // ESP32 GPIO32 (Pin12) -- SX1276 DIO2 (Pin10) not used by LMIC for LoRa (Timeout for FSK only) +#define DIO2 LMIC_UNUSED_PIN // 32 ESP32 GPIO32 (Pin12) -- SX1276 DIO2 (Pin10) not used by LMIC for LoRa (Timeout for FSK only) // Hardware pin definitions for TTGOv1 Board with OLED SSD1306 I2C Display #define OLED_RST 16 // ESP32 GPIO16 (Pin16) -- SD1306 Reset diff --git a/src/hal/ttgov2.h b/src/hal/ttgov2.h index 537990af..ebdfe87d 100644 --- a/src/hal/ttgov2.h +++ b/src/hal/ttgov2.h @@ -19,7 +19,7 @@ #define RST LMIC_UNUSED_PIN // connected to ESP32 RST/EN #define DIO0 26 // ESP32 GPIO26 wired on PCB to HPD13A #define DIO1 33 // HPDIO1 on pcb, needs to be wired external to GPIO33 -#define DIO2 32 // HPDIO2 on pcb, needs to be wired external to GPIO32 (not necessary for LoRa, only FSK) +#define DIO2 LMIC_UNUSED_PIN // 32 HPDIO2 on pcb, needs to be wired external to GPIO32 (not necessary for LoRa, only FSK) // Hardware pin definitions for TTGO V2 Board with OLED SSD1306 0,96" I2C Display #define OLED_RST U8X8_PIN_NONE // connected to CPU RST/EN diff --git a/src/main.cpp b/src/main.cpp index 75187849..3dd674b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -330,16 +330,20 @@ uint64_t uptime() { u8x8.printf("%-16s", "BLTH:off"); #endif - // update free memory display (line 4) - u8x8.setCursor(10,4); - u8x8.printf("%4dKB", ESP.getFreeHeap() / 1024); + // update LoRa SF display (line 3) + u8x8.setCursor(11,3); + u8x8.printf("SF:%c%c", lora_datarate[LMIC.datarate * 2], lora_datarate[LMIC.datarate * 2 + 1]); - // update RSSI limiter status & wifi channel display (line 5) - u8x8.setCursor(0,5); - u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%-4d", cfg.rssilimit); - u8x8.setCursor(11,5); + // update wifi channel display (line 4) + u8x8.setCursor(11,4); u8x8.printf("ch:%02d", channel); + // update RSSI limiter status & free memory display (line 5) + u8x8.setCursor(0,5); + u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%-4d", cfg.rssilimit); + u8x8.setCursor(10,5); + u8x8.printf("%4dKB", ESP.getFreeHeap() / 1024); + // update LoRa status display (line 6) u8x8.setCursor(0,6); u8x8.printf("%-16s", display_lora); diff --git a/src/main.h b/src/main.h index 48390d7e..7bdc094e 100644 --- a/src/main.h +++ b/src/main.h @@ -10,6 +10,12 @@ enum led_states { LED_ON }; +#if defined(CFG_eu868) + const char lora_datarate[] = {"1211100908077BFSNA"}; +#elif defined(CFG_us915) + const char lora_datarate[] = {"100908078CNA121110090807"}; +#endif + //--- Prototypes --- // defined in main.cpp diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 584e1ab4..16fbae40 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -33,7 +33,8 @@ #define WIFI_CHANNEL_SWITCH_INTERVAL 50 // [seconds/100] -> 0,5 sec. // LoRa payload send cycle --> take care of duty cycle of LoRaWAN network! <-- -#define SEND_SECS 120 // [seconds/2] -> 240 sec. +//#define SEND_SECS 120 // [seconds/2] -> 240 sec. +#define SEND_SECS 30 #define MEM_LOW 2048 // [Bytes] memory threshold triggering send cycle // Default LoRa Spreadfactor @@ -73,7 +74,7 @@ // enable more verbose output. Make sure that printf is actually // configured (e.g. on AVR it is not by default), otherwise using it can // cause crashing. -//#define LMIC_DEBUG_LEVEL 1 +#define LMIC_DEBUG_LEVEL 1 // Enable this to allow using printf() to print to the given serial port // (or any other Print object). This can be easy for debugging. The From 10afaba7d56d20eb0cb648e26d9733c2a9486b5a Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 28 Apr 2018 12:14:22 +0200 Subject: [PATCH 41/46] v1.3.32 --- platformio.ini | 4 ++-- src/main.h | 2 +- src/paxcounter.conf | 5 ++--- 3 files changed, 5 insertions(+), 6 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 diff --git a/src/main.h b/src/main.h index 7bdc094e..d833beb9 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.31" // use max 10 chars here! +#define PROGVERSION "1.3.32" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 16fbae40..584e1ab4 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -33,8 +33,7 @@ #define WIFI_CHANNEL_SWITCH_INTERVAL 50 // [seconds/100] -> 0,5 sec. // LoRa payload send cycle --> take care of duty cycle of LoRaWAN network! <-- -//#define SEND_SECS 120 // [seconds/2] -> 240 sec. -#define SEND_SECS 30 +#define SEND_SECS 120 // [seconds/2] -> 240 sec. #define MEM_LOW 2048 // [Bytes] memory threshold triggering send cycle // Default LoRa Spreadfactor @@ -74,7 +73,7 @@ // enable more verbose output. Make sure that printf is actually // configured (e.g. on AVR it is not by default), otherwise using it can // cause crashing. -#define LMIC_DEBUG_LEVEL 1 +//#define LMIC_DEBUG_LEVEL 1 // Enable this to allow using printf() to print to the given serial port // (or any other Print object). This can be easy for debugging. The From 326e01ba51941e77e66f7d320045a6d05d227296 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 28 Apr 2018 13:07:35 +0200 Subject: [PATCH 42/46] new cmd set payload sendcycle --- README.md | 6 +++--- platformio.ini | 4 ++-- src/configmanager.cpp | 14 +++++++------- src/globals.h | 2 +- src/lorawan.cpp | 2 +- src/main.h | 2 +- src/rcommand.cpp | 8 ++++---- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index db0aa560..7c5330fb 100644 --- a/README.md +++ b/README.md @@ -156,10 +156,10 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts. 1 = reset MAC counter to zero 2 = reset device to factory settings -0x0A set Wifi scan cycle and payload transmit cycle +0x0A set payload send cycle - 0 ... 255 duration of a wifi scan cycle in seconds/2, after this payload is sent - e.g. 120 -> 1 cycle runs for 240 seconds [default] + 0 ... 255 payload send cycle in seconds/2 + e.g. 120 -> payload is transmitted each 240 seconds [default] 0x0B set Wifi channel switch interval timer 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/configmanager.cpp b/src/configmanager.cpp index 628e4cd3..1d1533ff 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -25,7 +25,7 @@ void defaultConfig() { cfg.screenon = 1; // 0=disbaled, 1=enabled cfg.countermode = 0; // 0=cyclic, 1=cumulative, 2=cyclic confirmed cfg.rssilimit = 0; // threshold for rssilimiter, negative value! - cfg.wifiscancycle = SEND_SECS; // wifi scan cycle [seconds/2] + cfg.sendcycle = SEND_SECS; // payload send cycle [seconds/2] cfg.wifichancycle = WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100] cfg.blescantime = BLESCANTIME; // BLE scan cycle duration [seconds] cfg.blescan = 1; // 0=disabled, 1=enabled @@ -99,8 +99,8 @@ void saveConfig() { if( nvs_get_i8(my_handle, "countermode", &flash8) != ESP_OK || flash8 != cfg.countermode ) nvs_set_i8(my_handle, "countermode", cfg.countermode); - if( nvs_get_i8(my_handle, "wifiscancycle", &flash8) != ESP_OK || flash8 != cfg.wifiscancycle ) - nvs_set_i8(my_handle, "wifiscancycle", cfg.wifiscancycle); + if( nvs_get_i8(my_handle, "sendcycle", &flash8) != ESP_OK || flash8 != cfg.sendcycle ) + nvs_set_i8(my_handle, "sendcycle", cfg.sendcycle); if( nvs_get_i8(my_handle, "wifichancycle", &flash8) != ESP_OK || flash8 != cfg.wifichancycle ) nvs_set_i8(my_handle, "wifichancycle", cfg.wifichancycle); @@ -220,11 +220,11 @@ void loadConfig() { saveConfig(); } - if( nvs_get_i8(my_handle, "wifiscancycle", &flash8) == ESP_OK ) { - cfg.wifiscancycle = flash8; - ESP_LOGI(TAG, "wifiscancycle = %d", flash8); + if( nvs_get_i8(my_handle, "sendcycle", &flash8) == ESP_OK ) { + cfg.sendcycle = flash8; + ESP_LOGI(TAG, "sendcycle = %d", flash8); } else { - ESP_LOGI(TAG, "WIFI scan cycle set to default %d", cfg.wifiscancycle); + ESP_LOGI(TAG, "Payload send cycle set to default %d", cfg.sendcycle); saveConfig(); } diff --git a/src/globals.h b/src/globals.h index 6d8f393b..9e1773e3 100644 --- a/src/globals.h +++ b/src/globals.h @@ -33,7 +33,7 @@ typedef struct { int8_t screenon; // 0=disabled, 1=enabled int8_t countermode; // 0=cyclic unconfirmed, 1=cumulative, 2=cyclic confirmed int16_t rssilimit; // threshold for rssilimiter, negative value! - int8_t wifiscancycle; // wifi scan cycle [seconds/2] + int8_t sendcycle; // payload send cycle [seconds/2] int8_t wifichancycle; // wifi channel switch cycle [seconds/100] int8_t blescantime; // BLE scan cycle duration [seconds] int8_t blescan; // 0=disabled, 1=enabled diff --git a/src/lorawan.cpp b/src/lorawan.cpp index b4f09726..70c81ce5 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -140,7 +140,7 @@ void do_send(osjob_t* j){ } // Schedule next transmission - os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(SEND_SECS * 2), do_send); + os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(cfg.sendcycle * 2), do_send); } // do_send() diff --git a/src/main.h b/src/main.h index d833beb9..1f52a945 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.32" // use max 10 chars here! +#define PROGVERSION "1.3.34" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 90894149..4879125a 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -77,9 +77,9 @@ void set_rssi(int val) { ESP_LOGI(TAG, "Remote command: set RSSI limit to %d", cfg.rssilimit); }; -void set_wifiscancycle(int val) { - cfg.wifiscancycle = val; - ESP_LOGI(TAG, "Remote command: set Wifi scan cycle duration to %d seconds", cfg.wifiscancycle*2); +void set_sendcycle(int val) { + cfg.sendcycle = val; + ESP_LOGI(TAG, "Remote command: set payload send cycle to %d seconds", cfg.sendcycle*2); }; void set_wifichancycle(int val) { @@ -232,7 +232,7 @@ cmd_t table[] = { {0x07, set_loraadr, true}, {0x08, set_screensaver, true}, {0x09, set_reset, false}, - {0x0a, set_wifiscancycle, true}, + {0x0a, set_sendcycle, true}, {0x0b, set_wifichancycle, true}, {0x0c, set_blescantime, true}, {0x0d, set_vendorfilter, false}, From 626bcc874c74434d7b3e8abe0e75acbc5fb19276 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 28 Apr 2018 13:11:23 +0200 Subject: [PATCH 43/46] repair cfg.blescantime --- src/blecsan.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 34036749..4c2bb9af 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -100,7 +100,7 @@ static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_pa { case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: { // restart scan - status = esp_ble_gap_start_scanning(BLESCANTIME); + status = esp_ble_gap_start_scanning(cfg.blescantime); if (status != ESP_OK) { ESP_LOGE(TAG, "esp_ble_gap_start_scanning: rc=%d", status); @@ -112,7 +112,7 @@ static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_pa { if ( p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) // Inquiry complete, scan is done { // restart scan - status = esp_ble_gap_start_scanning (BLESCANTIME); + status = esp_ble_gap_start_scanning (cfg.blescantime); if (status != ESP_OK) { ESP_LOGE(TAG, "esp_ble_gap_start_scanning: rc=%d", status); From 37005a2087973e1e3447ec132ce41005f013ac6f Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 28 Apr 2018 13:13:27 +0200 Subject: [PATCH 44/46] update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c5330fb..500457b2 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,7 @@ device answers with it's current configuration. The configuration is a C structu byte 5: Display status (1=on, 0=off) byte 6: Counter mode (0=cyclic unconfirmed, 1=cumulative, 2=cyclic confirmed) bytes 7-8: RSSI limiter threshold value (negative) - byte 9: Wifi scan cycle duration in seconds/2 (0..255) + byte 9: Payload send cycle in seconds/2 (0..255) byte 10: Wifi channel switch interval in seconds/100 (0..255) byte 11: BLE scan cycle duration in seconds (0..255) byte 12: BLE scan mode (1=on, 0=0ff) From 589058d7c55637f74f958cbc193b5d2491be80f5 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 28 Apr 2018 14:09:27 +0200 Subject: [PATCH 45/46] repair wifichancycle --- src/antenna.cpp | 2 +- src/configmanager.cpp | 2 +- src/globals.h | 29 ++++++++++++------------ src/lorawan.cpp | 4 ++-- src/main.cpp | 4 ++-- src/main.h | 2 +- src/rcommand.cpp | 52 ++++++++++++++++++++++--------------------- 7 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/antenna.cpp b/src/antenna.cpp index 8bf94a2c..bcbf1703 100644 --- a/src/antenna.cpp +++ b/src/antenna.cpp @@ -21,7 +21,7 @@ void antenna_init(void) { gpio_config(&gpioconf); } -void antenna_select (const int8_t _ant) { +void antenna_select (const uint8_t _ant) { if (HAS_ANTENNA_SWITCH < 32) { if (_ant == ANTENNA_EXT) { GPIO_REG_WRITE(GPIO_OUT_W1TS_REG, 1 << HAS_ANTENNA_SWITCH); diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 1d1533ff..74c26206 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -13,7 +13,7 @@ esp_err_t err; // defined in antenna.cpp #ifdef HAS_ANTENNA_SWITCH - void antenna_select(const int8_t _ant); + void antenna_select(const uint8_t _ant); #endif // populate cfg vars with factory settings diff --git a/src/globals.h b/src/globals.h index 9e1773e3..2578596d 100644 --- a/src/globals.h +++ b/src/globals.h @@ -26,20 +26,20 @@ // Struct holding devices's runtime configuration typedef struct { - int8_t lorasf; // 7-12, lora spreadfactor - int8_t txpower; // 2-15, lora tx power - int8_t adrmode; // 0=disabled, 1=enabled - int8_t screensaver; // 0=disabled, 1=enabled - int8_t screenon; // 0=disabled, 1=enabled - int8_t countermode; // 0=cyclic unconfirmed, 1=cumulative, 2=cyclic confirmed + uint8_t lorasf; // 7-12, lora spreadfactor + uint8_t txpower; // 2-15, lora tx power + uint8_t adrmode; // 0=disabled, 1=enabled + uint8_t screensaver; // 0=disabled, 1=enabled + uint8_t screenon; // 0=disabled, 1=enabled + uint8_t countermode; // 0=cyclic unconfirmed, 1=cumulative, 2=cyclic confirmed int16_t rssilimit; // threshold for rssilimiter, negative value! - int8_t sendcycle; // payload send cycle [seconds/2] - int8_t wifichancycle; // wifi channel switch cycle [seconds/100] - int8_t blescantime; // BLE scan cycle duration [seconds] - int8_t blescan; // 0=disabled, 1=enabled - int8_t wifiant; // 0=internal, 1=external (for LoPy/LoPy4) - int8_t vendorfilter; // 0=disabled, 1=enabled - int8_t rgblum; // RGB Led luminosity (0..100%) + uint8_t sendcycle; // payload send cycle [seconds/2] + uint8_t wifichancycle; // wifi channel switch cycle [seconds/100] + uint8_t blescantime; // BLE scan cycle duration [seconds] + uint8_t blescan; // 0=disabled, 1=enabled + uint8_t wifiant; // 0=internal, 1=external (for LoPy/LoPy4) + uint8_t vendorfilter; // 0=disabled, 1=enabled + uint8_t rgblum; // RGB Led luminosity (0..100%) char version[10]; // Firmware version } configData_t; @@ -51,7 +51,8 @@ extern char display_lora[], display_lmic[]; 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 std::set macs; +extern hw_timer_t * channelSwitch; // hardware timer used for wifi channel switching #ifdef HAS_DISPLAY extern HAS_DISPLAY u8x8; diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 70c81ce5..a5e3e90a 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -14,8 +14,8 @@ static const char *TAG = "lorawan"; // functions defined in rcommand.cpp -void rcommand(int cmd, int arg); -void switch_lora(int sf, int tx); +void rcommand(uint8_t cmd, uint8_t arg); +void switch_lora(uint8_t sf, uint8_t tx); // DevEUI generator using devices's MAC address void gen_lora_deveui(uint8_t *pdeveui) { diff --git a/src/main.cpp b/src/main.cpp index 3dd674b4..5f360faa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -192,7 +192,7 @@ void lorawan_loop(void * pvParameters) { #ifdef HAS_ANTENNA_SWITCH // defined in antenna.cpp void antenna_init(); - void antenna_select(const int8_t _ant); + void antenna_select(const uint8_t _ant); #endif #ifndef BLECOUNTER @@ -562,7 +562,7 @@ void setup() { // setup channel rotation IRQ, thanks to https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/ channelSwitch = timerBegin(1, 80, true); // prescaler 80 -> divides 80 MHz CPU freq to 1 MHz, timer 1, count up timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true); // interrupt handler, triggered by edge -timerAlarmWrite(channelSwitch, WIFI_CHANNEL_SWITCH_INTERVAL * 10000, true); // reload interrupt after each trigger of channel switch cycle +timerAlarmWrite(channelSwitch, cfg.wifichancycle * 10000, true); // reload interrupt after each trigger of channel switch cycle timerAlarmEnable(channelSwitch); // enable channel switching interrupt // show compiled features diff --git a/src/main.h b/src/main.h index 1f52a945..8e6993e0 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.34" // use max 10 chars here! +#define PROGVERSION "1.3.35" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 4879125a..95ccee33 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -14,18 +14,18 @@ static const char *TAG = "rcommand"; // table of remote commands and assigned functions typedef struct { - const int nam; - void (*func)(int); + const uint8_t nam; + void (*func)(uint8_t); const bool store; } cmd_t; // function defined in antenna.cpp #ifdef HAS_ANTENNA_SWITCH - void antenna_select(const int8_t _ant); + void antenna_select(const uint8_t _ant); #endif // help function to assign LoRa datarates to numeric spreadfactor values -void switch_lora (int sf, int tx) { +void switch_lora (uint8_t sf, uint8_t tx) { if ( tx > 20 ) return; cfg.txpower = tx; switch (sf) { @@ -50,7 +50,7 @@ void switch_lora (int sf, int tx) { } // set of functions that can be triggered by remote commands -void set_reset(int val) { +void set_reset(uint8_t val) { switch (val) { case 0: // restart device ESP_LOGI(TAG, "Remote command: restart device"); @@ -72,27 +72,29 @@ void set_reset(int val) { } }; -void set_rssi(int val) { +void set_rssi(uint8_t val) { cfg.rssilimit = val * -1; ESP_LOGI(TAG, "Remote command: set RSSI limit to %d", cfg.rssilimit); }; -void set_sendcycle(int val) { +void set_sendcycle(uint8_t val) { cfg.sendcycle = val; ESP_LOGI(TAG, "Remote command: set payload send cycle to %d seconds", cfg.sendcycle*2); }; -void set_wifichancycle(int val) { +void set_wifichancycle(uint8_t val) { cfg.wifichancycle = val; - ESP_LOGI(TAG, "Remote command: set Wifi channel switch interval to %d seconds", cfg.wifichancycle/100); + // modify wifi channel rotation IRQ + timerAlarmWrite(channelSwitch, cfg.wifichancycle * 10000, true); // reload interrupt after each trigger of channel switch cycle + ESP_LOGI(TAG, "Remote command: set Wifi channel switch interval to %.1f seconds", cfg.wifichancycle/float(100)); }; -void set_blescantime(int val) { +void set_blescantime(uint8_t val) { cfg.blescantime = val; ESP_LOGI(TAG, "Remote command: set BLE scan time to %d seconds", cfg.blescantime); }; -void set_countmode(int val) { +void set_countmode(uint8_t val) { switch (val) { case 0: // cyclic unconfirmed cfg.countermode = 0; @@ -109,7 +111,7 @@ void set_countmode(int val) { } }; -void set_screensaver(int val) { +void set_screensaver(uint8_t val) { ESP_LOGI(TAG, "Remote command: set screen saver to %s ", val ? "on" : "off"); switch (val) { case 1: cfg.screensaver = val; break; @@ -117,7 +119,7 @@ void set_screensaver(int val) { } }; -void set_display(int val) { +void set_display(uint8_t val) { ESP_LOGI(TAG, "Remote command: set screen to %s", val ? "on" : "off"); switch (val) { case 1: cfg.screenon = val; break; @@ -125,12 +127,12 @@ void set_display(int val) { } }; -void set_lorasf(int val) { +void set_lorasf(uint8_t val) { ESP_LOGI(TAG, "Remote command: set LoRa SF to %d", val); switch_lora(val, cfg.txpower); }; -void set_loraadr(int val) { +void set_loraadr(uint8_t val) { ESP_LOGI(TAG, "Remote command: set LoRa ADR mode to %s", val ? "on" : "off"); switch (val) { case 1: cfg.adrmode = val; break; @@ -139,7 +141,7 @@ void set_loraadr(int val) { LMIC_setAdrMode(cfg.adrmode); }; -void set_blescan(int val) { +void set_blescan(uint8_t val) { ESP_LOGI(TAG, "Remote command: set BLE scan mode to %s", val ? "on" : "off"); switch (val) { case 0: @@ -152,7 +154,7 @@ void set_blescan(int val) { } }; -void set_wifiant(int val) { +void set_wifiant(uint8_t val) { ESP_LOGI(TAG, "Remote command: set Wifi antenna to %s", val ? "external" : "internal"); switch (val) { case 1: cfg.wifiant = val; break; @@ -163,7 +165,7 @@ void set_wifiant(int val) { #endif }; -void set_vendorfilter(int val) { +void set_vendorfilter(uint8_t val) { ESP_LOGI(TAG, "Remote command: set vendorfilter mode to %s", val ? "on" : "off"); switch (val) { case 1: cfg.vendorfilter = val; break; @@ -171,22 +173,22 @@ void set_vendorfilter(int val) { } }; -void set_rgblum(int val) { +void set_rgblum(uint8_t val) { // Avoid wrong parameters cfg.rgblum = (val>=0 && val<=100) ? (uint8_t) val : RGBLUMINOSITY; ESP_LOGI(TAG, "Remote command: set RGB Led luminosity %d", cfg.rgblum); }; -void set_lorapower(int val) { +void set_lorapower(uint8_t val) { ESP_LOGI(TAG, "Remote command: set LoRa TXPOWER to %d", val); switch_lora(cfg.lorasf, val); }; -void set_noop (int val) { +void set_noop (uint8_t val) { ESP_LOGI(TAG, "Remote command: noop - doing nothing"); }; -void get_config (int val) { +void get_config (uint8_t val) { ESP_LOGI(TAG, "Remote command: get configuration"); int size = sizeof(configData_t); // declare send buffer (char byte array) @@ -198,7 +200,7 @@ void get_config (int val) { ESP_LOGI(TAG, "%d bytes queued in send queue", size-1); }; -void get_uptime (int val) { +void get_uptime (uint8_t val) { ESP_LOGI(TAG, "Remote command: get uptime"); int size = sizeof(uptimecounter); unsigned char *sendData = new unsigned char[size]; @@ -208,7 +210,7 @@ void get_uptime (int val) { ESP_LOGI(TAG, "%d bytes queued in send queue", size-1); }; -void get_cputemp (int val) { +void get_cputemp (uint8_t val) { ESP_LOGI(TAG, "Remote command: get cpu temperature"); float temp = temperatureRead(); int size = sizeof(temp); @@ -245,7 +247,7 @@ cmd_t table[] = { }; // check and execute remote command -void rcommand(int cmd, int arg) { +void rcommand(uint8_t cmd, uint8_t arg) { int i = sizeof(table) / sizeof(table[0]); // number of commands in command table bool store_flag = false; while(i--) { From 221b06d3c67e25cee03b742689441ab0e7552518 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 28 Apr 2018 14:33:27 +0200 Subject: [PATCH 46/46] v1.3.4 --- platformio.ini | 4 ++-- src/main.h | 2 +- src/paxcounter.conf | 2 +- 3 files changed, 4 insertions(+), 4 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 diff --git a/src/main.h b/src/main.h index 8e6993e0..5ad9b39b 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.35" // use max 10 chars here! +#define PROGVERSION "1.3.4" // use max 10 chars here! #define PROGNAME "PAXCNT" //--- Declarations --- diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 584e1ab4..cc953b6b 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -34,7 +34,7 @@ // LoRa payload send cycle --> take care of duty cycle of LoRaWAN network! <-- #define SEND_SECS 120 // [seconds/2] -> 240 sec. -#define MEM_LOW 2048 // [Bytes] memory threshold triggering send cycle +#define MEM_LOW 2048 // [Bytes] low memory threshold triggering a send cycle // Default LoRa Spreadfactor #define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs