#include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "lwip/err.h" #include "lwip/sys.h" #include "lwip/ip_addr.h" #include "lwip/netif.h" #include "lwip/sockets.h" #include "lwip/dhcp.h" /* WiFi credentials */ #define EXAMPLE_ESP_WIFI_SSID "UCLA_WEB" #define EXAMPLE_ESP_WIFI_PASS "" // Empty password for the open WiFi #define EXAMPLE_ESP_MAXIMUM_RETRY 5 // You can adjust the maximum retries if needed /* Static IP Configuration */ #define EXAMPLE_STATIC_IP "192.168.1.100" #define EXAMPLE_NETMASK "255.255.255.0" #define EXAMPLE_GATEWAY "192.168.1.1" /* Event group for signaling connection events */ static EventGroupHandle_t s_wifi_event_group; /* Define bits for connected and failed states */ #define WIFI_CONNECTED_BIT BIT0 #define WIFI_FAIL_BIT BIT1 static const char *TAG = "wifi station"; static int s_retry_num = 0; /* Function to configure static IP */ static void configure_static_ip(esp_netif_t *netif) { esp_netif_dhcpc_stop(netif); // Stop DHCP client esp_netif_ip_info_t ip_info; ip4addr_aton(EXAMPLE_STATIC_IP, &ip_info.ip); ip4addr_aton(EXAMPLE_NETMASK, &ip_info.netmask); ip4addr_aton(EXAMPLE_GATEWAY, &ip_info.gw); ESP_ERROR_CHECK(esp_netif_set_ip_info(netif, &ip_info)); ESP_LOGI(TAG, "Static IP configured: " IPSTR, IP2STR(&ip_info.ip)); } /* Event handler to handle 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(); // Attempt to connect to the network } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { esp_wifi_connect(); // Retry connection s_retry_num++; ESP_LOGI(TAG, "Retrying to connect to the AP"); } else { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); // Set fail bit if max retries are reached } ESP_LOGI(TAG,"Connection to AP failed"); } 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 address: " IPSTR, IP2STR(&event->ip_info.ip)); // Log IP address when obtained s_retry_num = 0; // Reset retry count xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); // Set connected bit } } /* WiFi initialization for station mode */ void wifi_init_sta(void) { s_wifi_event_group = xEventGroupCreate(); // Create an event group for connection status ESP_ERROR_CHECK(esp_netif_init()); // Initialize network interface ESP_ERROR_CHECK(esp_event_loop_create_default()); // Initialize default event loop esp_netif_t *netif = esp_netif_create_default_wifi_sta(); // Create WiFi station interface configure_static_ip(netif); // Set static IP wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); // Initialize WiFi configuration ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // Initialize WiFi driver esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id)); // Register event handler for WiFi events ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip)); // Register event handler for IP event wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_ESP_WIFI_SSID, // SSID of the network .password = EXAMPLE_ESP_WIFI_PASS, // Password of the network (empty for open network) .threshold.authmode = WIFI_AUTH_OPEN, // Open authentication mode for open WiFi }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); // Set WiFi mode to station (client) ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); // Set WiFi configuration ESP_ERROR_CHECK(esp_wifi_start()); // Start WiFi ESP_LOGI(TAG, "WiFi initialization completed."); /* Wait for either successful connection (WIFI_CONNECTED_BIT) or failure (WIFI_FAIL_BIT) */ EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY); if (bits & WIFI_CONNECTED_BIT) { ESP_LOGI(TAG, "Successfully connected to SSID: %s", EXAMPLE_ESP_WIFI_SSID); } else if (bits & WIFI_FAIL_BIT) { ESP_LOGI(TAG, "Failed to connect to SSID: %s", EXAMPLE_ESP_WIFI_SSID); } else { ESP_LOGE(TAG, "Unexpected event occurred."); } } void app_main(void) { // Initialize NVS (non-volatile storage) esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); // Erase NVS if necessary ret = nvs_flash_init(); // Re-initialize NVS } ESP_ERROR_CHECK(ret); ESP_LOGI(TAG, "Starting WiFi Station Mode"); wifi_init_sta(); // Initialize and connect to WiFi }