#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 "esp_netif.h"
#include "lwip/err.h"
#include "lwip/sys.h"
const static char *TAG = "WIFI";
#define CONFIG_LOG_MAXIMUM_LEVEL 5
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
// WiFi event handler
static void wifi_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(); // s4
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
esp_wifi_connect(); // s4
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
}
}
// Main task
void main_task(void *pvParameter) {
esp_netif_ip_info_t ip_info;
// wait for connection
ESP_LOGI(TAG, "Waiting for connection to the Wi-Fi network...");
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
ESP_LOGI(TAG, "Connected!");
// Get and print the local IP address
esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info);
ESP_LOGW(TAG, "IP Address: " IPSTR "", IP2STR(&ip_info.ip));
ESP_LOGW(TAG, "Subnet mask: " IPSTR "", IP2STR(&ip_info.netmask));
ESP_LOGW(TAG, "Gateway: " IPSTR "", IP2STR(&ip_info.gw));
ESP_LOGI(TAG, "You can connect now to any web server online! :)");
while (1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
// Main application
void app_main() {
esp_log_level_set(TAG, ESP_LOG_VERBOSE);
ESP_LOGI(TAG, "ESP-IDF version used: %s", IDF_VER);
// Initialize NVS
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());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// 1 - Wi-Fi/LwIP Init Phase
// s1.1 Initialize the TCP/IP stack
esp_netif_init();
// s1.2 Create the default event loop
ESP_ERROR_CHECK(esp_event_loop_create_default());
// s1.3 Create the default Wi-Fi station
esp_netif_create_default_wifi_sta();
// s1.4 Initialize the Wi-Fi driver
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
// Create the event group to handle Wi-Fi events
wifi_event_group = xEventGroupCreate();
// 2 - Wi-Fi Configuration Phase
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL, NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&wifi_event_handler,
NULL, NULL));
// Configure Wi-Fi connection settings
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
// Set Wi-Fi mode to STA (Station)
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
// 3 - Wi-Fi Start Phase
ESP_ERROR_CHECK(esp_wifi_start());
// Start the main task
xTaskCreate(&main_task, "main_task", 2048, NULL, 5, NULL);
ESP_LOGI(TAG, "Connecting to %s", WIFI_SSID);
}