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