wifi & lmic loops restructured

This commit is contained in:
Klaus K Wilting 2018-04-27 18:32:36 +02:00
parent bf14b061b7
commit 19b9e4d721
5 changed files with 61 additions and 75 deletions

View File

@ -11,9 +11,9 @@
; ---> SELECT TARGET PLATFORM HERE! <--- ; ---> SELECT TARGET PLATFORM HERE! <---
[platformio] [platformio]
env_default = heltec_wifi_lora_32 ;env_default = heltec_wifi_lora_32
;env_default = ttgov1 ;env_default = ttgov1
;env_default = ttgov2 env_default = ttgov2
;env_default = lopy ;env_default = lopy
;env_default = lopy4 ;env_default = lopy4
;env_default = lolin32lite_lora ;env_default = lolin32lite_lora

View File

@ -109,7 +109,7 @@ void printKeys(void) {
void do_send(osjob_t* j){ void do_send(osjob_t* j){
uint8_t mydata[4]; uint8_t mydata[4];
uint16_t data;
// Sum of unique WIFI MACs seen // Sum of unique WIFI MACs seen
mydata[0] = (macs_wifi & 0xff00) >> 8; mydata[0] = (macs_wifi & 0xff00) >> 8;
mydata[1] = macs_wifi & 0xff; mydata[1] = macs_wifi & 0xff;
@ -123,12 +123,6 @@ void do_send(osjob_t* j){
mydata[3] = 0; mydata[3] = 0;
#endif #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 // Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) { if (LMIC.opmode & OP_TXRXPEND) {
ESP_LOGI(TAG, "OP_TXRXPEND, not sending"); ESP_LOGI(TAG, "OP_TXRXPEND, not sending");
@ -138,10 +132,18 @@ void do_send(osjob_t* j){
LMIC_setTxData2(1, mydata, sizeof(mydata), (cfg.countermode & 0x02)); LMIC_setTxData2(1, mydata, sizeof(mydata), (cfg.countermode & 0x02));
ESP_LOGI(TAG, "Packet queued"); ESP_LOGI(TAG, "Packet queued");
sprintf(display_lmic, "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) { void onEvent (ev_t ev) {
char buff[24]=""; 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_REJOIN_FAILED: strcpy_P(buff, PSTR("REJOIN FAILED")); break;
case EV_JOINED: case EV_JOINED:
joinstate=true;
strcpy_P(buff, PSTR("JOINED")); strcpy_P(buff, PSTR("JOINED"));
sprintf(display_lora, ""); // erase "Join Wait" message from display
// Disable link check validation (automatically enabled // Disable link check validation (automatically enabled
// during join, but not supported by TTN at this time). // during join, but not supported by TTN at this time).
LMIC_setLinkCheckMode(0); LMIC_setLinkCheckMode(0);
@ -170,26 +174,21 @@ void onEvent (ev_t ev) {
LMIC_setAdrMode(cfg.adrmode); LMIC_setAdrMode(cfg.adrmode);
// Set data rate and transmit power (note: txpower seems to be ignored by the library) // Set data rate and transmit power (note: txpower seems to be ignored by the library)
switch_lora(cfg.lorasf,cfg.txpower); switch_lora(cfg.lorasf,cfg.txpower);
joinstate=true;
// show effective LoRa parameters after join // 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=%i, SF=%i, TXPOWER=%i", cfg.adrmode, cfg.lorasf, cfg.txpower);
break; break;
case EV_TXCOMPLETE: 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 { strcpy_P(buff, (LMIC.txrxFlags & TXRX_ACK) ? PSTR("RECEIVED ACK") : PSTR("TX COMPLETE"));
sprintf(display_lmic, "TX COMPLETE"); sprintf(display_lora, ""); // erase previous LoRa message from display
}
if (LMIC.dataLen) { if (LMIC.dataLen) {
ESP_LOGI(TAG, "Received %d bytes of payload", LMIC.dataLen); ESP_LOGI(TAG, "Received %d bytes of payload, RSSI %d SNR %d", LMIC.dataLen, LMIC.rssi, (signed char)LMIC.snr / 4);
sprintf(display_lora, "Rcvd %d bytes", LMIC.dataLen);
// LMIC.snr = SNR twos compliment [dB] * 4 // LMIC.snr = SNR twos compliment [dB] * 4
// LMIC.rssi = RSSI [dBm] (-196...+63) // 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 // check if payload received on command port, then call remote command interpreter
if ( (LMIC.txrxFlags & TXRX_PORT) && (LMIC.frame[LMIC.dataBeg-1] == RCMDPORT ) ) { if ( (LMIC.txrxFlags & TXRX_PORT) && (LMIC.frame[LMIC.dataBeg-1] == RCMDPORT ) ) {
@ -203,6 +202,7 @@ void onEvent (ev_t ev) {
} }
} }
break; break;
default: sprintf_P(buff, PSTR("UNKNOWN EVENT %d"), ev); break; default: sprintf_P(buff, PSTR("UNKNOWN EVENT %d"), ev); break;
} }
@ -212,6 +212,5 @@ void onEvent (ev_t ev) {
sprintf(display_lmic, buff); sprintf(display_lmic, buff);
} }
} // onEvent()
}

View File

@ -142,14 +142,33 @@ void lorawan_loop(void * pvParameters) {
configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check
static uint16_t lorawait = 0;
while(1) { while(1) {
// execute LMIC jobs
os_runloop_once(); os_runloop_once();
// indicate LMIC state on LEDs if present
#if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED)
led_loop(); led_loop();
#endif #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); vTaskDelay(10/portTICK_PERIOD_MS);
yield(); yield();
} }
@ -192,49 +211,15 @@ void sniffer_loop(void * pvParameters) {
configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check
char buff[16];
int nloop=0, lorawait=0;
while (1) { while (1) {
nloop++; // actual number of wifi loops, controls cycle when data is sent for (channel = 1; channel <= WIFI_CHANNEL_MAX; channel++) {
// rotates variable channel 1..WIFI_CHANNEL_MAX
channel = (channel % WIFI_CHANNEL_MAX) + 1; // rotates variable channel 1..WIFI_CHANNEL_MAX
wifi_sniffer_set_channel(channel); wifi_sniffer_set_channel(channel);
ESP_LOGD(TAG, "Wifi set channel %d", 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); vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS);
yield(); yield();
}
} // end of infinite wifi channel rotation loop } // end of infinite wifi channel rotation loop
} }
@ -556,7 +541,9 @@ ESP_LOGI(TAG, "Features %s", features);
#endif #endif
os_init(); // setup LMIC 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 os_setCallback(&initjob, lora_init); // setup initial job & join network
wifi_sniffer_init(); // setup wifi in monitor mode and start MAC counting 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 // initialize salt value using esp_random() called by random() in arduino-esp32 core

View File

@ -1,6 +1,6 @@
// program version - note: increment version after modifications to configData_t struct!! // 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" #define PROGNAME "PAXCNT"
//--- Declarations --- //--- Declarations ---

View File

@ -32,9 +32,9 @@
#define WIFI_MY_COUNTRY "EU" // select locale for Wifi RF settings #define WIFI_MY_COUNTRY "EU" // select locale for Wifi RF settings
#define WIFI_CHANNEL_SWITCH_INTERVAL 50 // [seconds/100] -> 0,5 sec. #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 120 // [seconds/2] -> 240 sec.
//#define SEND_SECS 30 // [seconds/2] -> 60 sec. #define SEND_SECS 30 // [seconds/2] -> 60 sec.
// Default LoRa Spreadfactor // Default LoRa Spreadfactor
#define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs #define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs