diff --git a/main/wifi.c b/main/wifi.c --- a/main/wifi.c +++ b/main/wifi.c @@ -10,29 +10,36 @@ #include "lwip/sys.h" +// Private variables // FreeRTOS event group to signal when we are connected static EventGroupHandle_t s_wifi_event_group; - static const char *TAG = "wifi station"; static int s_retry_num = 0; -static void __event_handler(void* arg, esp_event_base_t event_base, - int32_t event_id, void* event_data) +// Handler for wifi events +static void __event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); - } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { + } + else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) + { + if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) + { esp_wifi_connect(); s_retry_num++; ESP_LOGI(TAG, "retry to connect to the AP"); - } else { + } + else + { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } ESP_LOGI(TAG,"connect to the AP fail"); - } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + } + else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) + { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; @@ -40,6 +47,8 @@ static void __event_handler(void* arg, e } } + +// Initialize wifi and connect to network void wifi_init(void) { s_wifi_event_group = xEventGroupCreate();