首页
随机
最近更改
特殊页面
社群首页
参数设置
关于WHY42
免责声明
WHY42
搜索
用户菜单
登录
欢迎来到Riguz的小站!这是一个私人wiki,用来记录一些我的笔记。
查看“︁ESP32 connect to WIFI”︁的源代码
←
ESP32 connect to WIFI
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
= Hardcode wifi connection = Wi-Fi Station<ref>https://github.com/espressif/esp-idf/tree/v5.4.1/examples/wifi/getting_started/station</ref> == Add components == Add required components "nvs_flash esp_event esp_wifi": <syntaxhighlight lang="cmake"> idf_component_register(SRCS ${SOURCES} PRIV_REQUIRES spi_flash REQUIRES driver esp_timer esp_lcd nvs_flash esp_event esp_wifi INCLUDE_DIRS "include") </syntaxhighlight> == Start nvs == <syntaxhighlight lang="cpp"> void walle::init_flash() { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_LOGW(TAG, "Erasing NVS flash to fix corruption"); ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); } </syntaxhighlight> == Start wifi == Configure wifi: <syntaxhighlight lang="cpp"> void network::init_wifi() { 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; // Passed this as data! ESP_ERROR_CHECK(esp_event_handler_instance_register( WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler_static, this, &instance_any_id)); ESP_ERROR_CHECK(esp_event_handler_instance_register( IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler_static, this, &instance_got_ip)); wifi_config_t wifi_config = { .sta = { .ssid = CONFIG_ESP_WIFI_SSID, .password = CONFIG_ESP_WIFI_PASSWORD, .threshold = {.authmode = WIFI_AUTH_WPA_WPA2_PSK}, // H2E is more safe, to use old WPA2 device, use WPA3_SAE_PWE_BOTH .sae_pwe_h2e = WPA3_SAE_PWE_BOTH, }, }; 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 initialized"); } </syntaxhighlight> Note that since it's cpp, we need to wrap the handler registor: <syntaxhighlight lang="cpp"> void network::event_handler_static(void *arg, esp_event_base_t base, int32_t event_id, void *event_data) { network *self = static_cast<network *>(arg); if (self) { self->event_handler(base, event_id, event_data); } else { ESP_LOGE(TAG, "failed to cast *arg -> network instance, network handler " "will not work!"); } } </syntaxhighlight> = Configure wifi via bluetooth = Smart config<ref>https://docs.espressif.com/projects/esp-idf/en/v5.4.1/esp32s3/api-reference/network/esp_smartconfig.html</ref> <ref>https://github.com/espressif/esp-idf/tree/v5.4.1/examples/wifi/smart_config</ref> * Downoad [https://github.com/EspressifApp/EsptouchForAndroid/releases/tag/v2.4.0 EspTouch] * [[Category:ESP32]] [[Category:Embedded]]
返回
ESP32 connect to WIFI
。