From 6ae1765d55d082100545859af613ed47952a9da3 Mon Sep 17 00:00:00 2001 From: Juraj Michalek Date: Wed, 25 Sep 2024 09:33:47 +0200 Subject: [PATCH] add Wi-Fi test --- .github/workflows/build.yml | 3 +- main/esp32-c3-lua-test.c | 81 +++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cdfee9f..9b6def6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,7 +27,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - board: [esp32c3-devkit, esp32c2-devkit] + #board: [esp32c3-devkit, esp32c2-devkit] + board: [esp32c3-devkit] fail-fast: false steps: diff --git a/main/esp32-c3-lua-test.c b/main/esp32-c3-lua-test.c index 0aa0a43..c1f2ef5 100644 --- a/main/esp32-c3-lua-test.c +++ b/main/esp32-c3-lua-test.c @@ -1,10 +1,19 @@ #include +#include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_heap_caps.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "nvs_flash.h" +#include "esp_log.h" + +#define WIFI_SCAN_LIST_SIZE 10 + +static const char *TAG = "WIFI_SCAN"; // Function to log memory usage with the message at the end void log_memory_usage(const char *message) @@ -57,7 +66,7 @@ void run_lua_qr_code_test(const char *lua_script, const char *test_name) // Retrieve the QR code string from the Lua stack const char *qr_code_string = lua_tostring(L, -1); if (qr_code_string) { - printf("Mock QR Code:\n%s\n", qr_code_string); + printf("QR Code:\n%s\n", qr_code_string); } lua_pop(L, lua_gettop(L)); } @@ -69,6 +78,59 @@ void run_lua_qr_code_test(const char *lua_script, const char *test_name) printf("End of Lua QR Code test: %s\n", test_name); } +// Function to initialize and perform Wi-Fi scan +void scan_wifi_networks(void) +{ + printf("Starting Wi-Fi scan...\n"); + + // Initialize NVS + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK(ret); + + // Initialize Wi-Fi + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + esp_netif_create_default_wifi_sta(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + // Start Wi-Fi in STA mode + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_start()); + + // Set the Wi-Fi scan parameters + wifi_scan_config_t scan_config = { + .ssid = NULL, + .bssid = NULL, + .channel = 0, + .show_hidden = true + }; + + // Perform Wi-Fi scan + ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, true)); + + // Get the scan results + uint16_t ap_count = WIFI_SCAN_LIST_SIZE; + wifi_ap_record_t ap_info[WIFI_SCAN_LIST_SIZE]; + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); + + // Print the scan results + printf("Found %d access points:\n", ap_count); + for (int i = 0; i < ap_count; i++) { + printf("SSID: %s, RSSI: %d\n", ap_info[i].ssid, ap_info[i].rssi); + } + + ESP_ERROR_CHECK(esp_wifi_stop()); + ESP_ERROR_CHECK(esp_wifi_deinit()); + + printf("Wi-Fi scan completed.\n"); +} + void app_main(void) { // Test 1: Simple Lua script @@ -76,7 +138,7 @@ void app_main(void) run_lua_test(simple_script, "Simple Script"); // Test 2: Simple Lua script to create a mock QR code representation - const char *mock_qr_code_script = + const char *mock_qr_code_script = "local function generate_mock_qr(text)\n" " local result = ''\n" " for i = 1, #text do\n" @@ -95,9 +157,22 @@ void app_main(void) "end\n" "local qr_string = generate_mock_qr('Hello, ESP32-C3!')\n" "return qr_string"; - run_lua_qr_code_test(mock_qr_code_script, "Mock QR Code Script"); + // Perform Wi-Fi scan + scan_wifi_networks(); + + // Test 3: Lua script to compute Fibonacci sequence + const char *fibonacci_script = + "local function fibonacci(n)\n" + " if n <= 1 then return n end\n" + " return fibonacci(n - 1) + fibonacci(n - 2)\n" + "end\n" + "local fib_10 = fibonacci(10)\n" + "print('Fibonacci of 10 is: ' .. fib_10)\n" + "return fib_10"; + run_lua_test(fibonacci_script, "Fibonacci Script"); + printf("End of testing application.\n"); // Prevent the task from ending