From 13bdeb3589da4a086ea2d2bd3d4d1bd0506738a2 Mon Sep 17 00:00:00 2001 From: KonradIT Date: Fri, 4 Oct 2024 15:19:01 +0200 Subject: [PATCH 1/3] feat: periodically write to serial the frequency scan results --- platformio.ini | 1 + src/main.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index 08579af..4995061 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,6 +28,7 @@ monitor_speed = 115200 board_build.f_cpu = 240000000 lib_deps = ropg/Heltec_ESP32_LoRa_v3@^0.9.1 + bblanchon/ArduinoJson@^7.2.0 build_flags = -DHELTEC_POWER_BUTTON -DHELTEC diff --git a/src/main.cpp b/src/main.cpp index 8ecbd0f..2be3d4d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,6 +24,9 @@ // #define HELTEC_NO_DISPLAY #include +#include +#include +#include // #define OSD_ENABLED true // #define WIFI_SCANNING_ENABLED true @@ -202,7 +205,7 @@ bool single_page_scan = false; bool SOUND_ON = false; // #define PRINT_DEBUG -#define PRINT_PROFILE_TIME +// #define PRINT_PROFILE_TIME #ifdef PRINT_PROFILE_TIME uint64_t loop_start = 0; @@ -211,6 +214,10 @@ uint64_t scan_time = 0; uint64_t scan_start_time = 0; #endif +// log data via serial console, JSON format: +#define LOG_DATA_JSON true +int LOG_DATA_JSON_INTERVAL = 100; + uint64_t x, y, range_item, w = WATERFALL_START, i = 0; int osd_x = 1, osd_y = 2, col = 0, max_bin = 32; uint64_t ranges_count = 0; @@ -435,6 +442,49 @@ void init_radio() delay(50); } +struct FrequencyRange +{ + uint64_t begin; + uint64_t end; +} frequencyRange; + +void logToSerialTask(void *parameter) +{ + JsonDocument doc; + char jsonOutput[200]; + + for (;;) + { + if (frequencyRange.begin != frequencyRange.end) + { + String max_result = "-999"; + int highest_value_scanned = 999; + + for (int rssi_item = 0; rssi_item < STEPS / WINDOW_SIZE; rssi_item++) + { + if (max_x_window[rssi_item] != 0 && + max_x_window[rssi_item] < highest_value_scanned) + { + max_result = "-" + String(max_x_window[rssi_item]); + highest_value_scanned = max_x_window[rssi_item]; + } + } + if (max_result == "-999") + { + continue; + } + + doc["low_range_freq"] = frequencyRange.begin; + doc["high_range_freq"] = frequencyRange.end; + doc["value"] = max_result; + + serializeJson(doc, jsonOutput); + Serial.println(jsonOutput); + } + vTaskDelay(LOG_DATA_JSON_INTERVAL / portTICK_PERIOD_MS); + } +} + void setup(void) { #ifdef LILYGO @@ -587,6 +637,10 @@ void setup(void) #ifdef OSD_ENABLED osd.clear(); #endif + +#ifdef LOG_DATA_JSON + xTaskCreate(logToSerialTask, "LOG_DATA_JSON", 2048, NULL, 1, NULL); +#endif } // Formula to translate 33 bin to approximate RSSI value @@ -798,13 +852,11 @@ void loop(void) drone_detected_frequency_start = 0; ranges_count = 0; - // reset scan time +// reset scan time +#ifdef PRINT_PROFILE_TIME scan_time = 0; - // general purpose loop counter loop_cnt++; - -#ifdef PRINT_PROFILE_TIME loop_start = millis(); #endif @@ -1043,6 +1095,9 @@ void loop(void) max_rssi_x * 2); } + frequencyRange.begin = drone_detected_frequency_start; + frequencyRange.end = drone_detected_frequency_end; + if (DRAW_DETECTION_TICKS == true) { // draw vertical line on top of display for "drone detected" @@ -1230,10 +1285,10 @@ void loop(void) // Serial.println("----"); #endif - loop_time = millis() - loop_start; joy_btn_clicked = false; #ifdef PRINT_PROFILE_TIME + loop_time = millis() - loop_start; Serial.printf("LOOP: %lld ms; SCAN: %lld ms;\n ", loop_time, scan_time); #endif // No WiFi and BT Scan Without OSD From 40ceb82431d6aa6afb24a8ab60a504d06f9dd296 Mon Sep 17 00:00:00 2001 From: KonradIT Date: Fri, 4 Oct 2024 15:28:00 +0200 Subject: [PATCH 2/3] naming, use int highest value gotten variable for comparison, only keep track of frequency ranges if json feature is enabled --- src/main.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2be3d4d..bacbf1e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -442,11 +442,11 @@ void init_radio() delay(50); } -struct FrequencyRange +struct frequency_scan_result { uint64_t begin; uint64_t end; -} frequencyRange; +} frequency_scan_result; void logToSerialTask(void *parameter) { @@ -455,7 +455,7 @@ void logToSerialTask(void *parameter) for (;;) { - if (frequencyRange.begin != frequencyRange.end) + if (frequency_scan_result.begin != frequency_scan_result.end) { String max_result = "-999"; int highest_value_scanned = 999; @@ -469,13 +469,13 @@ void logToSerialTask(void *parameter) highest_value_scanned = max_x_window[rssi_item]; } } - if (max_result == "-999") + if (highest_value_scanned == 999) { continue; } - doc["low_range_freq"] = frequencyRange.begin; - doc["high_range_freq"] = frequencyRange.end; + doc["low_range_freq"] = frequency_scan_result.begin; + doc["high_range_freq"] = frequency_scan_result.end; doc["value"] = max_result; serializeJson(doc, jsonOutput); @@ -1095,9 +1095,10 @@ void loop(void) max_rssi_x * 2); } - frequencyRange.begin = drone_detected_frequency_start; - frequencyRange.end = drone_detected_frequency_end; - +#ifdef LOG_DATA_JSON + frequency_scan_result.begin = drone_detected_frequency_start; + frequency_scan_result.end = drone_detected_frequency_end; +#endif if (DRAW_DETECTION_TICKS == true) { // draw vertical line on top of display for "drone detected" From 6d02c4121757413272be6c6e9f2513daaadaa9b6 Mon Sep 17 00:00:00 2001 From: KonradIT Date: Mon, 7 Oct 2024 19:51:16 +0200 Subject: [PATCH 3/3] Added a delay when frequency scan result is invalid --- src/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.cpp b/src/main.cpp index bacbf1e..480c3b2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -471,6 +471,7 @@ void logToSerialTask(void *parameter) } if (highest_value_scanned == 999) { + vTaskDelay(LOG_DATA_JSON_INTERVAL / portTICK_PERIOD_MS); continue; }