This is the code used for initializing wifi:
Code: Select all
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(esp_event_loop_init(nullptr, nullptr));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, nullptr));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, nullptr));
wifi_init_config_t init_cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&init_cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
wifi_config_t wifi_config = {};
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
wifi_config.sta.pmf_cfg.capable = true;
wifi_config.sta.pmf_cfg.required = false;
strcpy((char *)wifi_config.sta.ssid, ssid.c_str());
strcpy((char *)wifi_config.sta.password, password.c_str());
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
Code: Select all
void wifi_event_handler(void *, esp_event_base_t base, int32_t id, void *)
{
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START)
{
LOG_D("Wifi started");
esp_err_t err = esp_wifi_connect();
if (err != ESP_OK)
{
LOG_D("esp_wifi_connect error: %s", esp_err_to_name(err));
xSemaphoreTake(wifiWorkingMutex, portMAX_DELAY);
wifiWorking = false;
xSemaphoreGive(wifiWorkingMutex);
}
}
else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED)
{
LOG_D("Disconnected");
xSemaphoreTake(wifiWorkingMutex, portMAX_DELAY);
wifiWorking = false;
xSemaphoreGive(wifiWorkingMutex);
esp_err_t err = esp_wifi_connect();
if (err != ESP_OK)
{
LOG_D("esp_wifi_connect error: %s", esp_err_to_name(err));
}
}
else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP)
{
LOG_D("Got IP");
xSemaphoreTake(wifiWorkingMutex, portMAX_DELAY);
wifiWorking = true;
xSemaphoreGive(wifiWorkingMutex);
}
}
Code: Select all
4,6c4,6
< I wifi:wifi driver task: 3ffc4d60, prio:23, stack:6656, core=0
< V esp_adapter: thread sem create: sem=0x3ffc4ec8
< V esp_adapter: thread sem get: sem=0x3ffc4ec8
---
> I wifi:wifi driver task: 3ffc4d84, prio:23, stack:6656, core=0
> V esp_adapter: thread sem create: sem=0x3ffc4eec
> V esp_adapter: thread sem get: sem=0x3ffc4eec
33,35c33,35
< V esp_adapter: thread sem get: sem=0x3ffc4ec8
< V esp_adapter: thread sem get: sem=0x3ffc4ec8
< V esp_adapter: thread sem get: sem=0x3ffc4ec8
---
> V esp_adapter: thread sem get: sem=0x3ffc4eec
> V esp_adapter: thread sem get: sem=0x3ffc4eec
> V esp_adapter: thread sem get: sem=0x3ffc4eec
62,65c62,64
< E event: system event loop not initialized via esp_event_loop_init
< 6125D 1c event: running post WIFI_EVENT:2 with handler 0x400d3b54 on loop 0x3ffbd6dc
< V esp_adapter: thread sem create: sem=0x3ffc9e00
< V esp_adapter: thread sem get: sem=0x3ffc9e00
---
> 6116 D 1c event: running post WIFI_EVENT:2 with handler 0x400d3b54 on loop 0x3ffbd6dc
> V esp_adapter: thread sem create: sem=0x3ffc9e58
> V esp_adapter: thread sem get: sem=0x3ffc9e58
71a71
> D event: running post SYSTEM_EVENT:2 with handler 0x400eb48c on loop 0x3ffbd6dc
77c77
< I wifi:security: WPA2-PSK, phy: bgn, rssi: -76
---
> I wifi:security: WPA2-PSK, phy: bgn, rssi: -74
81,82d80
< E event: system event loop not initialized via esp_event_loop_init
< I wifi:AP's beacon interval = 25600 us, DTIM period = 1
83a82
> I wifi:AP's beacon interval = 25600 us, DTIM period = 1
85c84
< V esp_adapter: thread sem get: sem=0x3ffc9e00
---
> V esp_adapter: thread sem get: sem=0x3ffc9e58
95c94
< D tcpip_adapter: if0 start ip lost tmr: no need start because netif=0x3ffc9e90 interval=120 ip=0
---
> D tcpip_adapter: if0 start ip lost tmr: no need start because netif=0x3ffc9ee8 interval=120 ip=0
99a99
> D event: running post SYSTEM_EVENT:4 with handler 0x400eb48c on loop 0x3ffbd6dc
103d102
< E event: system event loop not initialized via esp_event_loop_init
106a106
> D event: running post SYSTEM_EVENT:7 with handler 0x400eb48c on loop 0x3ffbd6dc