创建页面,内容为“ Wi-Fi Station<ref>https://github.com/espressif/esp-idf/tree/v5.4.1/examples/wifi/getting_started/station</ref> Category:ESP32 Category:Embedded” |
|||
(未显示同一用户的2个中间版本) | |||
第1行: | 第1行: | ||
= Hardcode wifi connection = | |||
Wi-Fi Station<ref>https://github.com/espressif/esp-idf/tree/v5.4.1/examples/wifi/getting_started/station</ref> | 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:ESP32]] | ||
[[Category:Embedded]] | [[Category:Embedded]] |
2025年4月5日 (六) 15:55的最新版本
Hardcode wifi connection
Wi-Fi Station[1]
Add components
Add required components "nvs_flash esp_event esp_wifi":
idf_component_register(SRCS ${SOURCES}
PRIV_REQUIRES spi_flash
REQUIRES driver esp_timer esp_lcd nvs_flash esp_event esp_wifi
INCLUDE_DIRS "include")
Start nvs
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);
}
Start wifi
Configure wifi:
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");
}
Note that since it's cpp, we need to wrap the handler registor:
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!");
}
}
Configure wifi via bluetooth
- Downoad EspTouch