Skip to content

Commit cb4870b

Browse files
Klaus K WiltingKlaus K Wilting
Klaus K Wilting
authored and
Klaus K Wilting
committed
testing
1 parent 5d883e1 commit cb4870b

12 files changed

+72
-53
lines changed

LICENSE

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Parts of the source files in this repository are made available under different
2020
listed below. Refer to each individual source file for more details.
2121

2222
------------------------------------------------------------------------------------------------
23-
wifisniffer.cpp
23+
wifiscan.cpp
2424

25-
Parts of wifisniffer.cpp were derived or taken from
25+
Prior art was used for wifiscan.cpp and taken from
2626

2727
* Copyright (c) 2017, Łukasz Marcin Podkalicki <[email protected]>
2828
* ESP32/016 WiFi Sniffer

platformio.ini

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
; ---> SELECT TARGET PLATFORM HERE! <---
88
[platformio]
9-
env_default = generic
9+
;env_default = generic
1010
;env_default = ebox
1111
;env_default = heltec
1212
;env_default = ttgov1
1313
;env_default = ttgov2
1414
;env_default = ttgov21old
15-
;env_default = ttgov21new
15+
env_default = ttgov21new
1616
;env_default = ttgobeam
1717
;env_default = lopy
1818
;env_default = lopy4
@@ -26,10 +26,10 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng
2626

2727
[common]
2828
; for release_version use max. 10 chars total, use any decimal format like "a.b.c"
29-
release_version = 1.5.6
29+
release_version = 1.5.7
3030
; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running!
3131
; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose
32-
debug_level = 0
32+
debug_level = 3
3333
; UPLOAD MODE: select esptool to flash via USB/UART, select custom to upload to cloud for OTA
3434
upload_protocol = esptool
3535
;upload_protocol = custom

src/button.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ void IRAM_ATTR ButtonIRQ() {
1313
}
1414

1515
void readButton() {
16-
if (ButtonPressedIRQ) {
1716
portENTER_CRITICAL(&timerMux);
1817
ButtonPressedIRQ = 0;
1918
portEXIT_CRITICAL(&timerMux);
2019
ESP_LOGI(TAG, "Button pressed");
2120
payload.reset();
2221
payload.addButton(0x01);
2322
SendData(BUTTONPORT);
24-
}
2523
}
2624
#endif

src/display.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ void init_display(const char *Productname, const char *Version) {
9191

9292
void refreshtheDisplay() {
9393

94+
portENTER_CRITICAL(&timerMux);
95+
DisplayTimerIRQ = 0;
96+
portEXIT_CRITICAL(&timerMux);
97+
9498
// set display on/off according to current device configuration
9599
if (DisplayState != cfg.screenon) {
96100
DisplayState = cfg.screenon;
@@ -114,7 +118,8 @@ void refreshtheDisplay() {
114118
// update Battery status (line 2)
115119
#ifdef HAS_BATTERY_PROBE
116120
u8x8.setCursor(0, 2);
117-
u8x8.printf(batt_voltage > 4000 ? "B:USB " : "B:%.1fV", batt_voltage / 1000.0);
121+
u8x8.printf(batt_voltage > 4000 ? "B:USB " : "B:%.1fV",
122+
batt_voltage / 1000.0);
118123
#endif
119124

120125
// update GPS status (line 2)
@@ -191,13 +196,4 @@ void IRAM_ATTR DisplayIRQ() {
191196
portEXIT_CRITICAL_ISR(&timerMux);
192197
}
193198

194-
void updateDisplay() {
195-
if (DisplayTimerIRQ) {
196-
portENTER_CRITICAL(&timerMux);
197-
DisplayTimerIRQ = 0;
198-
portEXIT_CRITICAL(&timerMux);
199-
refreshtheDisplay();
200-
}
201-
}
202-
203199
#endif // HAS_DISPLAY

src/display.h

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ extern HAS_DISPLAY u8x8;
99
void init_display(const char *Productname, const char *Version);
1010
void refreshtheDisplay(void);
1111
void DisplayKey(const uint8_t *key, uint8_t len, bool lsb);
12-
void updateDisplay(void);
1312
void DisplayIRQ(void);
1413

1514
#endif

src/globals.h

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ extern volatile uint8_t SendCycleTimerIRQ, HomeCycleIRQ, DisplayTimerIRQ,
5353
extern std::array<uint64_t, 0xff>::iterator it;
5454
extern std::array<uint64_t, 0xff> beacons;
5555

56+
extern SemaphoreHandle_t xWifiChannelSwitchSemaphore;
57+
5658
#ifdef HAS_GPS
5759
extern TaskHandle_t GpsTask;
5860
#include "gps.h"

src/lorawan.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ void lorawan_loop(void *pvParameters) {
247247
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
248248

249249
while (1) {
250-
os_runloop_once(); // execute LMIC jobs
250+
//vTaskSuspendAll();
251+
os_runloop_once(); // execute LMIC jobs
252+
//xTaskResumeAll();
251253
vTaskDelay(2 / portTICK_PERIOD_MS); // yield to CPU
252254
}
253255
}

src/main.cpp

+27-11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ gpsloop 0 2 read data from GPS over serial or i2c
3232
IDLE 1 0 Arduino loop() -> used for LED switching
3333
loraloop 1 1 runs the LMIC stack
3434
statemachine 1 3 switches application process logic
35+
wifiloop 0 4 rotates wifi channels
3536
3637
ESP32 hardware timers
3738
==========================
@@ -58,7 +59,9 @@ hw_timer_t *channelSwitch, *displaytimer, *sendCycle, *homeCycle;
5859
uint8_t volatile ButtonPressedIRQ = 0, ChannelTimerIRQ = 0,
5960
SendCycleTimerIRQ = 0, DisplayTimerIRQ = 0, HomeCycleIRQ = 0;
6061

61-
TaskHandle_t StateTask = NULL;
62+
TaskHandle_t stateMachineTask = NULL;
63+
64+
SemaphoreHandle_t xWifiChannelSwitchSemaphore;
6265

6366
// RTos send queues for payload transmit
6467
#ifdef HAS_LORA
@@ -245,11 +248,6 @@ void setup() {
245248
timerAlarmEnable(displaytimer);
246249
#endif
247250

248-
// setup channel rotation trigger IRQ using esp32 hardware timer 1
249-
channelSwitch = timerBegin(1, 800, true);
250-
timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true);
251-
timerAlarmWrite(channelSwitch, cfg.wifichancycle * 1000, true);
252-
253251
// setup send cycle trigger IRQ using esp32 hardware timer 2
254252
sendCycle = timerBegin(2, 8000, true);
255253
timerAttachInterrupt(sendCycle, &SendCycleIRQ, true);
@@ -260,6 +258,12 @@ void setup() {
260258
timerAttachInterrupt(homeCycle, &homeCycleIRQ, true);
261259
timerAlarmWrite(homeCycle, HOMECYCLE * 10000, true);
262260

261+
// setup channel rotation trigger IRQ using esp32 hardware timer 1
262+
xWifiChannelSwitchSemaphore = xSemaphoreCreateBinary();
263+
channelSwitch = timerBegin(1, 800, true);
264+
timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true);
265+
timerAlarmWrite(channelSwitch, cfg.wifichancycle * 1000, true);
266+
263267
// enable timers
264268
// caution, see: https://github.com/espressif/arduino-esp32/issues/1313
265269
yield();
@@ -326,18 +330,30 @@ void setup() {
326330

327331
// start wifi in monitor mode and start channel rotation task on core 0
328332
ESP_LOGI(TAG, "Starting Wifi...");
329-
// esp_event_loop_init(NULL, NULL);
330-
// ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
331333
wifi_sniffer_init();
332334
// initialize salt value using esp_random() called by random() in
333335
// arduino-esp32 core. Note: do this *after* wifi has started, since
334336
// function gets it's seed from RF noise
335337
get_salt(); // get new 16bit for salting hashes
336338

339+
// start wifi channel rotation task
340+
xTaskCreatePinnedToCore(switchWifiChannel, /* task function */
341+
"wifiloop", /* name of task */
342+
3048, /* stack size of task */
343+
NULL, /* parameter of the task */
344+
4, /* priority of the task */
345+
NULL, /* task handle*/
346+
0); /* CPU core */
347+
337348
// start state machine
338349
ESP_LOGI(TAG, "Starting Statemachine...");
339-
xTaskCreatePinnedToCore(stateMachine, "stateloop", 2048, (void *)1, 3,
340-
&StateTask, 1);
350+
xTaskCreatePinnedToCore(stateMachine, /* task function */
351+
"stateloop", /* name of task */
352+
2048, /* stack size of task */
353+
(void *)1, /* parameter of the task */
354+
3, /* priority of the task */
355+
&stateMachineTask, /* task handle */
356+
1); /* CPU core */
341357

342358
} // setup()
343359

@@ -350,4 +366,4 @@ void loop() {
350366

351367
// give yield to CPU
352368
vTaskDelay(2 / portTICK_PERIOD_MS);
353-
}
369+
}

src/statemachine.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ void stateMachine(void *pvParameters) {
1010
while (1) {
1111

1212
#ifdef HAS_BUTTON
13-
readButton();
13+
if (ButtonPressedIRQ)
14+
readButton();
1415
#endif
1516

1617
#ifdef HAS_DISPLAY
17-
updateDisplay();
18+
if (DisplayTimerIRQ)
19+
refreshtheDisplay();
1820
#endif
1921

20-
// check wifi scan cycle and if due rotate channel
21-
if (ChannelTimerIRQ)
22-
switchWifiChannel(channel);
2322
// check housekeeping cycle and if due do the work
2423
if (HomeCycleIRQ)
2524
doHousekeeping();

src/statemachine.h

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
#include "cyclic.h"
99

1010
void stateMachine(void *pvParameters);
11+
void stateMachineInit();
1112

1213
#endif

src/wifiscan.cpp

+22-16
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ void wifi_sniffer_init(void) {
3030
cfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM
3131
wifi_promiscuous_filter_t filter = {
3232
.filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // we need only MGMT frames
33-
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // configure Wifi with cfg
33+
34+
// esp_event_loop_init(NULL, NULL);
35+
// ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
36+
37+
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // configure Wifi with cfg
3438
ESP_ERROR_CHECK(
3539
esp_wifi_set_country(&wifi_country)); // set locales for RF and channels
3640
ESP_ERROR_CHECK(
@@ -43,20 +47,22 @@ void wifi_sniffer_init(void) {
4347
ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // now switch on monitor mode
4448
}
4549

46-
// Wifi channel rotation
47-
void switchWifiChannel(uint8_t volatile &ch) {
48-
portENTER_CRITICAL(&timerMux);
49-
ChannelTimerIRQ = 0;
50-
portEXIT_CRITICAL(&timerMux);
51-
// rotates variable channel 1..WIFI_CHANNEL_MAX
52-
ch = (ch % WIFI_CHANNEL_MAX) + 1;
53-
esp_wifi_set_channel(ch, WIFI_SECOND_CHAN_NONE);
54-
ESP_LOGD(TAG, "Wifi set channel %d", ch);
50+
// IRQ Handler
51+
void ChannelSwitchIRQ() {
52+
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
53+
// unblock wifi channel rotation task
54+
xSemaphoreGiveFromISR(xWifiChannelSwitchSemaphore, &xHigherPriorityTaskWoken);
5555
}
5656

57-
// IRQ handler
58-
void IRAM_ATTR ChannelSwitchIRQ() {
59-
portENTER_CRITICAL(&timerMux);
60-
ChannelTimerIRQ++;
61-
portEXIT_CRITICAL(&timerMux);
62-
}
57+
// Wifi channel rotation task
58+
void switchWifiChannel(void * parameter) {
59+
while (1) {
60+
// task in block state to wait for channel switch timer interrupt event
61+
xSemaphoreTake(xWifiChannelSwitchSemaphore, portMAX_DELAY);
62+
// rotates variable channel 1..WIFI_CHANNEL_MAX
63+
channel = (channel % WIFI_CHANNEL_MAX) + 1;
64+
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
65+
ESP_LOGD(TAG, "Wifi set channel %d", channel);
66+
}
67+
vTaskDelete(NULL);
68+
}

src/wifiscan.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ typedef struct {
2828
void wifi_sniffer_init(void);
2929
void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type);
3030
void ChannelSwitchIRQ(void);
31-
void switchWifiChannel(uint8_t volatile &ch);
31+
void switchWifiChannel(void * parameter);
3232

3333
#endif

0 commit comments

Comments
 (0)