advait_projects/station/main/station_example_main.c
2025-03-05 16:19:41 -08:00

124 lines
5.2 KiB
C

#include <string.h>
#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 "esp_mac.h"
/* WiFi credentials */
#define STATIC_IP "192.168.1.100"
#define STATIC_GW "192.168.1.1"
#define STATIC_NETMASK "255.255.255.0"
#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
/* 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;
/* 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
}
}
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_create_default_wifi_sta(); // Create WiFi station interface
/* Set static IP, gateway, and subnet mask before WiFi driver initialization */
esp_netif_ip_info_t ip_info;
ip_info.ip.addr = esp_ip4addr_aton(STATIC_IP); // Set static IP
ip_info.gw.addr = esp_ip4addr_aton(STATIC_GW); // Set gateway
ip_info.netmask.addr = esp_ip4addr_aton(STATIC_NETMASK); // Set subnet mask
esp_netif_set_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info);
// Set up Wi-Fi configuration
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
// Apply static IP, netmask, and gateway settings
esp_netif_set_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ip_info);
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
}