/**
 ******************************************************************************
 * @file     WiFi_AP.ino
 * @brief    ESP32-C5-N16R4 act as WiFi Access Point and monitor client connections
 * @version  V1.1
 * @date     2025-10-14
 * @author   liangjingheng
 * @company  Waveshare
 * @license  MIT
 ******************************************************************************
 * Enhanced Features:
 * 1. Create WiFi Access Point with specified SSID ("ESP32-C5-N16R4") and password
 * 2. Monitor client connection/disconnection events via WiFi event handler
 * 3. Print connected device's MAC address, assigned IP address via serial port
 * 4. Provide real-time status feedback for network events
 ******************************************************************************
 */

#include <WiFi.h>
#include <esp_wifi.h>  // ESP-IDF library to access connected client info

// WiFi Access Point credentials
const char* ssid     = "ESP32-C5-N16R4";   //your ap name
const char* password = "waveshare";       // your AP password 

// Convert MAC address from byte array to readable string (XX:XX:XX:XX:XX:XX)
String formatMacAddress(const uint8_t* mac) {
    char macStr[18];
    snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", 
             mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    return String(macStr);
}

// Handle WiFi events (connection, disconnection, IP assignment)
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
    switch (event) {
        case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
            // Print MAC address when a new device connects to AP
            Serial.println("New device connected:");
            Serial.print("MAC Address: ");
            Serial.println(formatMacAddress(info.wifi_ap_staconnected.mac));
            break;

        case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
            // Print message when a device disconnects from AP
            Serial.println("Device disconnected from AP");
            break;

        case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
            // Print IP address assigned to connected device
            Serial.print("Assigned IP to device: ");
            Serial.println(IPAddress(info.got_ip.ip_info.ip.addr));
            break;

        default:
            break;
    }
}

void setup() {
    // Initialize serial communication (115200 baud rate) for debugging
    Serial.begin(115200); 

    // Start WiFi Access Point with specified SSID and password
    WiFi.softAP(ssid, password);

    // Register event handler to monitor client connection status
    WiFi.onEvent(WiFiEvent);

    // Short delay to stabilize WiFi AP setup
    delay(1000);

    // Print AP initialization status to serial console
    Serial.println("WiFi Access Point initialized successfully");
    Serial.print("AP SSID: ");
    Serial.println(ssid);
    Serial.print("AP Password: ");
    Serial.println(password);
}

void loop() {                           
    // No active operation in loop; CPU remains idle
    delay(1000);  // Small delay to reduce unnecessary CPU usage
}
