-
-
Save anonymous/46dd1ba65482900b922d97426d2c1352 to your computer and use it in GitHub Desktop.
wifi handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static esp_err_t wifi_eventHandler(void *ctx, system_event_t *event) | |
{ | |
switch(event->event_id) { | |
case SYSTEM_EVENT_STA_START: | |
esp_wifi_connect(); | |
break; | |
case SYSTEM_EVENT_STA_GOT_IP: | |
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); | |
wifi_connected = true; | |
break; | |
case SYSTEM_EVENT_STA_DISCONNECTED: | |
/* This is a workaround as ESP32 WiFi libs don't currently | |
auto-reassociate. */ //<---- (https://github.com/espressif/esp-idf/issues/177) | |
wifi_connected = false; | |
ESP_LOGI("wifi", "Wifi disconnect, lets try to reconnect"); | |
ESP_ERROR_CHECK( esp_wifi_stop() ); | |
ESP_ERROR_CHECK( esp_wifi_deinit() ) | |
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) ); | |
wifi_config_t wifi_config = { | |
.sta = { | |
.ssid = DEFAULT_SSID, | |
.password = DEFAULT_PWD, | |
}, | |
}; | |
ESP_LOGI("wifi", "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); | |
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); | |
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); | |
ESP_ERROR_CHECK( esp_wifi_start() ); | |
//esp_wifi_connect(); | |
//xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); | |
break; | |
case SYSTEM_EVENT_STA_LOST_IP: | |
ESP_LOGI("wifi", "IP lost, lets try to reconnect"); | |
ESP_ERROR_CHECK( esp_wifi_stop() ); | |
ESP_ERROR_CHECK( esp_wifi_deinit() ) | |
wifi_init_config_t cfg2 = WIFI_INIT_CONFIG_DEFAULT(); | |
ESP_ERROR_CHECK( esp_wifi_init(&cfg2) ); | |
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); | |
wifi_config_t wifi_config2 = { | |
.sta = { | |
.ssid = DEFAULT_SSID, | |
.password = DEFAULT_PWD, | |
}, | |
}; | |
ESP_LOGI("wifi", "Setting WiFi configuration SSID %s...", wifi_config2.sta.ssid); | |
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); | |
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config2) ); | |
ESP_ERROR_CHECK( esp_wifi_start() ); | |
break; | |
default: | |
break; | |
} | |
return ESP_OK; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment