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 395c77e8..b4f09726 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); + ESP_LOGI(TAG, "ADR=%d, SF=%d, TXPOWER=%d", 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/macsniff.cpp b/src/macsniff.cpp index da69ba22..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 - 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,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, "%d%%", 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 70b1003e..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) @@ -142,14 +141,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 +210,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 } @@ -329,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); @@ -556,7 +540,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 @@ -607,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 99e27afe..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.22" // 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 30189b7b..ec8c455a 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 +// 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 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