Well this is basically one function call... so not sure what kind of snippet you need, but here's a piece of one of my applications:
Code: Select all
void init_wifi(wifi_mode_t mode)
{
const uint8_t protocol = WIFI_PROTOCOL_LR;
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(mode) );
wifi_event_group = xEventGroupCreate();
if (mode == WIFI_MODE_STA) {
ESP_ERROR_CHECK( esp_wifi_set_protocol(WIFI_IF_STA, protocol) );
wifi_config_t config = {
.sta = {
.ssid = ap_name,
.password = pass,
.bssid_set = false
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
false, true, portMAX_DELAY);
ESP_LOGI(TAG, "Connected to AP");
} else {
ESP_ERROR_CHECK( esp_wifi_set_protocol(WIFI_IF_AP, protocol) );
wifi_config_t config = {
.ap = {
.ssid = ap_name,
.password = pass,
.ssid_len = 0,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.ssid_hidden = false,
.max_connection = 3,
.beacon_interval = 100,
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_AP, &config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
}
Some variables are defined outside of the snippet, but hopefully you can figure it out.