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