From 8e2631b2d131b7c97b9f27b0300ec3986a169654 Mon Sep 17 00:00:00 2001 From: Snorky Date: Thu, 9 Jan 2025 19:28:30 +0100 Subject: [PATCH] First commit --- CMakeLists.txt | 8 ++ README.md | 134 ++++++++++++++++++++++++++++++++ main/CMakeLists.txt | 3 + main/Kconfig.projbuild | 67 ++++++++++++++++ main/esp-project.code-workspace | 12 +++ main/idf_component.yml | 5 ++ main/main.c | 27 +++++++ main/wifi_connect.c | 112 ++++++++++++++++++++++++++ main/wifi_connect.h | 44 +++++++++++ sdkconfig.ci | 2 + sdkconfig.ci.esp32c2_xtal26m | 2 + sdkconfig.defaults | 1 + 12 files changed, 417 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 main/CMakeLists.txt create mode 100644 main/Kconfig.projbuild create mode 100644 main/esp-project.code-workspace create mode 100644 main/idf_component.yml create mode 100644 main/main.c create mode 100644 main/wifi_connect.c create mode 100644 main/wifi_connect.h create mode 100644 sdkconfig.ci create mode 100644 sdkconfig.ci.esp32c2_xtal26m create mode 100644 sdkconfig.defaults diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8a54292 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) +project(wifi_station) diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ffd038 --- /dev/null +++ b/README.md @@ -0,0 +1,134 @@ + +# Projet ESP + +Projet sur une board esp32-8048S043. Le code suivant est fonctionnel. Il s'appuie sur le code officiel du github `esp-idf`. + +* [github esp-idf](https://github.com/espressif/esp-idf) +* [Lien du code](https://github.com/espressif/esp-idf/tree/master/examples/wifi/getting_started/station) + + +## Configurer vos accés wifi. + +Tout se passe dans le fichier `wifi_connect.h` + +Modifier les `#define` suivant: + +* #define CONFIG_ESP_WIFI_SSID "YourSSID" +* #define CONFIG_ESP_WIFI_PASS "YourPASSWORD" + +### Build et Flash + +Build the project and flash it to the board, then run the monitor tool to view the serial output: + +Pour build et flasher le board, connectez la via usb et lancer la commande suivante: + +`idf.py -p PORT flash monitor` + +Pour sortir du mode monitor saissez les touches ``Ctrl-]`` + +Un peu de doc car la doc ca fait du bien. :) + +* [ESP-IDF Getting Started Guide on ESP32](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html) +* [ESP-IDF Getting Started Guide on ESP32-S2](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/get-started/index.html) +* [ESP-IDF Getting Started Guide on ESP32-C3](https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/get-started/index.html) + +## Example de sortie + +``` +I (27) boot: ESP-IDF v5.4 2nd stage bootloader +I (27) boot: compile time Jan 9 2025 18:48:28 +I (27) boot: Multicore bootloader +I (27) boot: chip revision: v0.2 +I (30) boot: efuse block revision: v1.3 +I (33) boot.esp32s3: Boot SPI Speed : 80MHz +I (37) boot.esp32s3: SPI Mode : DIO +I (41) boot.esp32s3: SPI Flash Size : 2MB +I (45) boot: Enabling RNG early entropy source... +I (49) boot: Partition Table: +I (52) boot: ## Label Usage Type ST Offset Length +I (58) boot: 0 nvs WiFi data 01 02 00009000 00006000 +I (65) boot: 1 phy_init RF data 01 01 0000f000 00001000 +I (71) boot: 2 factory factory app 00 00 00010000 00100000 +I (78) boot: End of partition table +I (81) esp_image: segment 0: paddr=00010020 vaddr=3c080020 size=189f0h (100848) map +I (106) esp_image: segment 1: paddr=00028a18 vaddr=3fc99a00 size=04948h ( 18760) load +I (110) esp_image: segment 2: paddr=0002d368 vaddr=40374000 size=02cb0h ( 11440) load +I (113) esp_image: segment 3: paddr=00030020 vaddr=42000020 size=77458h (488536) map +I (204) esp_image: segment 4: paddr=000a7480 vaddr=40376cb0 size=12cb8h ( 76984) load +I (220) esp_image: segment 5: paddr=000ba140 vaddr=600fe100 size=0001ch ( 28) load +I (229) boot: Loaded app from partition at offset 0x10000 +I (229) boot: Disabling RNG early entropy source... +I (239) cpu_start: Multicore app +I (249) cpu_start: Pro cpu start user code +I (249) cpu_start: cpu freq: 160000000 Hz +I (249) app_init: Application information: +I (249) app_init: Project name: wifi_station +I (253) app_init: App version: 1 +I (256) app_init: Compile time: Jan 9 2025 18:47:46 +I (261) app_init: ELF file SHA256: 47f4e7b75... +I (266) app_init: ESP-IDF: v5.4 +I (269) efuse_init: Min chip rev: v0.0 +I (273) efuse_init: Max chip rev: v0.99 +I (277) efuse_init: Chip rev: v0.2 +I (281) heap_init: Initializing. RAM available for dynamic allocation: +I (287) heap_init: At 3FCA1F08 len 00047808 (286 KiB): RAM +I (293) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM +I (298) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM +I (303) heap_init: At 600FE11C len 00001ECC (7 KiB): RTCRAM +I (309) spi_flash: detected chip: gd +I (311) spi_flash: flash io: dio +W (314) spi_flash: Detected size(16384k) larger than the size in the binary image header(2048k). Using the size in the binary image header. +I (327) sleep_gpio: Configure to isolate all GPIO pins in sleep state +I (333) sleep_gpio: Enable automatic switching of GPIO sleep configuration +I (340) main_task: Started on CPU0 +I (360) main_task: Calling app_main() +I (390) Main: ESP_WIFI_MODE_STA +I (390) pp: pp rom version: e7ae62f +I (390) net80211: net80211 rom version: e7ae62f +I (400) wifi:wifi driver task: 3fcabfec, prio:23, stack:6656, core=0 +I (410) wifi:wifi firmware version: 48ea317a7 +I (410) wifi:wifi certification version: v7.0 +I (410) wifi:config NVS flash: enabled +I (410) wifi:config nano formatting: disabled +I (420) wifi:Init data frame dynamic rx buffer num: 32 +I (420) wifi:Init static rx mgmt buffer num: 5 +I (420) wifi:Init management short buffer num: 32 +I (430) wifi:Init dynamic tx buffer num: 32 +I (430) wifi:Init static tx FG buffer num: 2 +I (440) wifi:Init static rx buffer size: 1600 +I (440) wifi:Init static rx buffer num: 10 +I (450) wifi:Init dynamic rx buffer num: 32 +I (450) wifi_init: rx ba win: 6 +I (450) wifi_init: accept mbox: 6 +I (450) wifi_init: tcpip mbox: 32 +I (460) wifi_init: udp mbox: 6 +I (460) wifi_init: tcp mbox: 6 +I (460) wifi_init: tcp tx win: 5760 +I (470) wifi_init: tcp rx win: 5760 +I (470) wifi_init: tcp mss: 1440 +I (470) wifi_init: WiFi IRAM OP enabled +I (480) wifi_init: WiFi RX IRAM OP enabled +I (510) phy_init: phy_version 680,a6008b2,Jun 4 2024,16:41:10 +I (540) wifi:mode : sta (84:fc:e6:6b:a0:cc) +I (540) wifi:enable tsf +I (540) wifi_connect: wifi_init_sta finished. +I (1000) wifi:new:<1,0>, old:<1,0>, ap:<255,255>, sta:<1,0>, prof:1, snd_ch_cfg:0x0 +I (1000) wifi:state: init -> auth (0xb0) +I (1020) wifi:state: auth -> assoc (0x0) +I (1050) wifi:state: assoc -> run (0x10) +I (1060) wifi:idx:0 (ifx:0, 44:d4:54:71:cb:80), tid:0, ssn:0, winSize:64 +I (1070) wifi:idx:1 (ifx:0, 44:d4:54:71:cb:80), tid:5, ssn:0, winSize:64 +I (1080) wifi:connected with Freebox_nomap, aid = 1, channel 1, BW20, bssid = 44:d4:54:71:cb:80 +I (1090) wifi:security: WPA2-PSK, phy: bgn, rssi: -46 +I (1100) wifi:pm start, type: 1 + +I (1100) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us +I (1100) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 25000, mt_pti: 0, mt_time: 10000 +I (1130) wifi:AP's beacon interval = 102400 us, DTIM period = 1 +I (4400) esp_netif_handlers: sta ip: 192.168.1.101, mask: 255.255.255.0, gw: 192.168.1.254 +I (4400) wifi_connect: got ip:192.168.1.101 +I (4400) wifi_connect: connected to ap SSID:YourSSID password:YourPassword +I (4410) Main: End of main +I (4410) main_task: Returned from app_main() + +``` diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..6a03dfe --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "wifi_connect.c" "main.c" + PRIV_REQUIRES esp_wifi nvs_flash + INCLUDE_DIRS ".") diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild new file mode 100644 index 0000000..5f26f13 --- /dev/null +++ b/main/Kconfig.projbuild @@ -0,0 +1,67 @@ +menu "Example Configuration" + + config ESP_WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + + config ESP_WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + + choice ESP_WIFI_SAE_MODE + prompt "WPA3 SAE mode selection" + default ESP_WPA3_SAE_PWE_BOTH + help + Select mode for SAE as Hunt and Peck, H2E or both. + config ESP_WPA3_SAE_PWE_HUNT_AND_PECK + bool "HUNT AND PECK" + config ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT + bool "H2E" + config ESP_WPA3_SAE_PWE_BOTH + bool "BOTH" + endchoice + + config ESP_WIFI_PW_ID + string "PASSWORD IDENTIFIER" + depends on ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT|| ESP_WPA3_SAE_PWE_BOTH + default "" + help + password identifier for SAE H2E + + config ESP_MAXIMUM_RETRY + int "Maximum retry" + default 5 + help + Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent. + + choice ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD + prompt "WiFi Scan auth mode threshold" + default ESP_WIFI_AUTH_WPA2_PSK + help + The weakest authmode to accept in the scan mode. + This value defaults to ESP_WIFI_AUTH_WPA2_PSK incase password is present and ESP_WIFI_AUTH_OPEN is used. + Please select ESP_WIFI_AUTH_WEP/ESP_WIFI_AUTH_WPA_PSK incase AP is operating in WEP/WPA mode. + + config ESP_WIFI_AUTH_OPEN + bool "OPEN" + config ESP_WIFI_AUTH_WEP + bool "WEP" + config ESP_WIFI_AUTH_WPA_PSK + bool "WPA PSK" + config ESP_WIFI_AUTH_WPA2_PSK + bool "WPA2 PSK" + config ESP_WIFI_AUTH_WPA_WPA2_PSK + bool "WPA/WPA2 PSK" + config ESP_WIFI_AUTH_WPA3_PSK + bool "WPA3 PSK" + config ESP_WIFI_AUTH_WPA2_WPA3_PSK + bool "WPA2/WPA3 PSK" + config ESP_WIFI_AUTH_WAPI_PSK + bool "WAPI PSK" + endchoice + +endmenu diff --git a/main/esp-project.code-workspace b/main/esp-project.code-workspace new file mode 100644 index 0000000..91d8889 --- /dev/null +++ b/main/esp-project.code-workspace @@ -0,0 +1,12 @@ +{ + "folders": [ + { + "path": ".." + } + ], + "settings": { + "files.associations": { + "wifi_connect.h": "c" + } + } +} \ No newline at end of file diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..467e9fc --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,5 @@ +dependencies: + + idf: + version: '>=4.1.0' + diff --git a/main/main.c b/main/main.c new file mode 100644 index 0000000..a7ee213 --- /dev/null +++ b/main/main.c @@ -0,0 +1,27 @@ +#include "nvs_flash.h" +#include "esp_log.h" +#include "lwip/err.h" +#include "lwip/sys.h" +#include "wifi_connect.h" + +#include "esp_system.h" +#include "esp_err.h" + +static const char *TAG = "Main"; + +void app_main(void) +{ + + //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); + + ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); + wifi_init_sta(); + + ESP_LOGI(TAG,"End of main"); +} diff --git a/main/wifi_connect.c b/main/wifi_connect.c new file mode 100644 index 0000000..257bc42 --- /dev/null +++ b/main/wifi_connect.c @@ -0,0 +1,112 @@ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "wifi_connect.h" +#include "esp_log.h" + + + +/* FreeRTOS event group to signal when we are connected*/ +static EventGroupHandle_t s_wifi_event_group; + +/* The event group allows multiple bits for each event, but we only care about two events: + * - we are connected to the AP with an IP + * - we failed to connect after the maximum amount of retries */ +#define WIFI_CONNECTED_BIT BIT0 +#define WIFI_FAIL_BIT BIT1 + +static const char *TAG = "wifi_connect"; + +static int s_retry_num = 0; + + +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { + esp_wifi_connect(); + s_retry_num++; + ESP_LOGI(TAG, "retry to connect to the AP"); + } else { + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); + } + ESP_LOGI(TAG,"connect to the AP fail"); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); + s_retry_num = 0; + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); + } +} + +void wifi_init_sta(void) +{ + s_wifi_event_group = xEventGroupCreate(); + + 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)); + + esp_event_handler_instance_t instance_any_id; + esp_event_handler_instance_t instance_got_ip; + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, + ESP_EVENT_ANY_ID, + &event_handler, + NULL, + &instance_any_id)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, + IP_EVENT_STA_GOT_IP, + &event_handler, + NULL, + &instance_got_ip)); + + wifi_config_t wifi_config = { + .sta = { + .ssid = EXAMPLE_ESP_WIFI_SSID, + .password = EXAMPLE_ESP_WIFI_PASS, + /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (password len => 8). + * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value + * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to + * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards. + */ + .threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD, + .sae_pwe_h2e = ESP_WIFI_SAE_MODE, + .sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); + ESP_ERROR_CHECK(esp_wifi_start() ); + + ESP_LOGI(TAG, "wifi_init_sta finished."); + + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum + * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, + pdFALSE, + pdFALSE, + portMAX_DELAY); + + /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually + * happened. */ + if (bits & WIFI_CONNECTED_BIT) { + ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + } else if (bits & WIFI_FAIL_BIT) { + ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", + EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + } else { + ESP_LOGE(TAG, "UNEXPECTED EVENT"); + } +} \ No newline at end of file diff --git a/main/wifi_connect.h b/main/wifi_connect.h new file mode 100644 index 0000000..184c996 --- /dev/null +++ b/main/wifi_connect.h @@ -0,0 +1,44 @@ +#include "esp_event.h" + + +#define EXAMPLE_ESP_WIFI_SSID "Freebox_nomap" +#define EXAMPLE_ESP_WIFI_PASS "anthedoniconfutarissuggestu2insignans" +/* #define EXAMPLE_ESP_WIFI_SSID "TheGame" +#define EXAMPLE_ESP_WIFI_PASS "totototoQ" */ +#define EXAMPLE_ESP_MAXIMUM_RETRY 3 + +#if CONFIG_ESP_WPA3_SAE_PWE_HUNT_AND_PECK +#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HUNT_AND_PECK +#define EXAMPLE_H2E_IDENTIFIER "" +#elif CONFIG_ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT +#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_HASH_TO_ELEMENT +#define EXAMPLE_H2E_IDENTIFIER CONFIG_ESP_WIFI_PW_ID +#elif CONFIG_ESP_WPA3_SAE_PWE_BOTH +#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_BOTH +#define EXAMPLE_H2E_IDENTIFIER CONFIG_ESP_WIFI_PW_ID +#endif +#if CONFIG_ESP_WIFI_AUTH_OPEN +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN +#elif CONFIG_ESP_WIFI_AUTH_WEP +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP +#elif CONFIG_ESP_WIFI_AUTH_WPA_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK +#elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK +#elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK +#elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK +#elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK +#elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK +#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK +#endif + + + + +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data); + +void wifi_init_sta(void); \ No newline at end of file diff --git a/sdkconfig.ci b/sdkconfig.ci new file mode 100644 index 0000000..b416e9f --- /dev/null +++ b/sdkconfig.ci @@ -0,0 +1,2 @@ +CONFIG_ESP_WIFI_SSID="ssid_${IDF_TARGET}_${CI_PIPELINE_ID}" +CONFIG_ESP_WIFI_PASSWORD="password_${IDF_TARGET}_${CI_PIPELINE_ID}" diff --git a/sdkconfig.ci.esp32c2_xtal26m b/sdkconfig.ci.esp32c2_xtal26m new file mode 100644 index 0000000..172f022 --- /dev/null +++ b/sdkconfig.ci.esp32c2_xtal26m @@ -0,0 +1,2 @@ +CONFIG_IDF_TARGET="esp32c2" +CONFIG_XTAL_FREQ_26=y diff --git a/sdkconfig.defaults b/sdkconfig.defaults new file mode 100644 index 0000000..42b9e74 --- /dev/null +++ b/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n