Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **WiFi Monitor**
- - Version: 007
- - Source Code NOT compiled for: Adafruit QT Py ESP32-C3
- - Source Code created on: 2026-03-14 08:48:19
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* ESP32 C3 creates a WiFi Access Point (AP) with */
- /* configurable SSID and password. Print AP status */
- /* and connected client information to serial */
- /* monitor. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** SUBSYSTEM REQUIREMENTS *****/
- /* SR1.1: Configure WiFi Access Point with SSID */
- /* SR1.2: Configure WiFi Access Point with Password */
- /* SR1.3: Initialize Serial communication at 115200 baud */
- /* SR1.4: Print AP SSID to serial monitor */
- /* SR1.5: Print AP IP address to serial monitor */
- /* SR1.6: Print AP status (active/inactive) to serial monitor */
- /* SR1.7: Monitor and print connected client information */
- /* SR1.8: Periodically display number of connected clients */
- /* SR1.9: Handle client connection/disconnection events */
- /****** END SUBSYSTEM REQUIREMENTS *****/
- #include <WiFi.h>
- /* Include ESP-IDF headers for advanced WiFi functionality */
- #include "esp_wifi.h"
- /****** CONFIGURABLE PARAMETERS *****/
- /* WiFi Access Point SSID - can be modified */
- const char* AP_SSID = "ESP32_WiFi_AP";
- /* WiFi Access Point Password - can be modified */
- const char* AP_PASSWORD = "12345678";
- /* WiFi Access Point Channel */
- const int AP_CHANNEL = 1;
- /* WiFi Access Point Maximum connected clients */
- const int AP_MAX_CLIENTS = 4;
- /* Serial Monitor Baud Rate */
- const long SERIAL_BAUD_RATE = 115200;
- /* Status print interval in milliseconds */
- const unsigned long STATUS_PRINT_INTERVAL = 5000;
- /****** GLOBAL VARIABLES *****/
- /* Timestamp for last status print */
- unsigned long lastStatusPrintTime = 0;
- /* Previously connected clients count for change detection */
- int previousClientCount = 0;
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeSerialCommunication(void);
- void initializeWiFiAccessPoint(void);
- void printAPStatus(void);
- void printConnectedClients(void);
- void printClientInformation(void);
- /****** SETUP FUNCTION *****/
- void setup(void)
- {
- /* Initialize Serial communication for monitoring */
- initializeSerialCommunication();
- /* Initialize WiFi Access Point with configured parameters */
- initializeWiFiAccessPoint();
- /* Print initial AP status and configuration */
- delay(1000);
- printAPStatus();
- }
- /****** MAIN LOOP FUNCTION *****/
- void loop(void)
- {
- /* Check if it's time to print status update */
- if (millis() - lastStatusPrintTime >= STATUS_PRINT_INTERVAL)
- {
- /* Update last print timestamp */
- lastStatusPrintTime = millis();
- /* Print current AP status and connected clients */
- printAPStatus();
- printConnectedClients();
- }
- /* Small delay to prevent watchdog timeout */
- delay(100);
- }
- /****** SERIAL COMMUNICATION INITIALIZATION *****/
- void initializeSerialCommunication(void)
- {
- /* Begin Serial communication at configured baud rate */
- Serial.begin(SERIAL_BAUD_RATE);
- /* Wait for Serial port to be ready */
- delay(100);
- /* Print startup message to serial monitor */
- Serial.println("\n\n");
- Serial.println("========================================");
- Serial.println("ESP32-C3 WiFi Access Point Configuration");
- Serial.println("========================================");
- }
- /****** WIFI ACCESS POINT INITIALIZATION *****/
- void initializeWiFiAccessPoint(void)
- {
- /* Set device mode to WiFi Access Point (AP) mode */
- WiFi.mode(WIFI_AP);
- /* Configure WiFi Access Point with SSID and password */
- /* Parameters: SSID, Password, Channel, SSID Hidden (false = visible), Max Clients */
- boolean apStartSuccess = WiFi.softAP(
- AP_SSID,
- AP_PASSWORD,
- AP_CHANNEL,
- false,
- AP_MAX_CLIENTS
- );
- /* Check if AP startup was successful */
- if (apStartSuccess)
- {
- Serial.println("\n[INFO] WiFi Access Point started successfully!");
- }
- else
- {
- Serial.println("\n[ERROR] Failed to start WiFi Access Point!");
- }
- /* Print configuration details */
- Serial.println("\n--- WiFi AP Configuration ---");
- Serial.print("SSID: ");
- Serial.println(AP_SSID);
- Serial.print("Password: ");
- Serial.println(AP_PASSWORD);
- Serial.print("Channel: ");
- Serial.println(AP_CHANNEL);
- Serial.print("Max Clients: ");
- Serial.println(AP_MAX_CLIENTS);
- }
- /****** PRINT ACCESS POINT STATUS *****/
- void printAPStatus(void)
- {
- /* Print timestamp */
- Serial.print("\n[");
- Serial.print(millis() / 1000);
- Serial.print("s] --- AP Status Update ---\n");
- /* Get and print AP IP address */
- IPAddress apIP = WiFi.softAPIP();
- Serial.print("AP IP Address: ");
- Serial.println(apIP);
- /* Get and print AP MAC address */
- Serial.print("AP MAC Address: ");
- Serial.println(WiFi.softAPmacAddress());
- /* Get and print AP status (active/inactive) */
- wifi_mode_t currentMode = WiFi.getMode();
- if (currentMode == WIFI_AP)
- {
- Serial.println("AP Status: ACTIVE");
- }
- else
- {
- Serial.println("AP Status: INACTIVE");
- }
- /* Get and print number of connected clients */
- int connectedClients = WiFi.softAPgetStationNum();
- Serial.print("Connected Clients: ");
- Serial.println(connectedClients);
- /* Check if number of clients has changed */
- if (connectedClients != previousClientCount)
- {
- previousClientCount = connectedClients;
- if (connectedClients > 0)
- {
- Serial.println("[EVENT] Client(s) connected!");
- }
- else
- {
- Serial.println("[EVENT] No clients connected.");
- }
- }
- }
- /****** PRINT CONNECTED CLIENTS INFORMATION *****/
- void printConnectedClients(void)
- {
- /* Get number of connected clients */
- int clientCount = WiFi.softAPgetStationNum();
- /* Only print detailed info if clients are connected */
- if (clientCount > 0)
- {
- Serial.println("\n--- Connected Client Details ---");
- printClientInformation();
- }
- else
- {
- Serial.println("No clients currently connected to the AP.");
- }
- Serial.println("=====================================");
- }
- /****** PRINT INDIVIDUAL CLIENT INFORMATION *****/
- void printClientInformation(void)
- {
- /* Get list of connected clients using ESP32 WiFi API */
- /* Allocate memory for station list */
- wifi_sta_list_t clients;
- /* Call the ESP-IDF function to get connected stations */
- esp_err_t result = esp_wifi_ap_get_sta_list(&clients);
- /* Check if the API call was successful */
- if (result != ESP_OK)
- {
- Serial.print("[WARNING] Failed to get station list, error code: ");
- Serial.println(result);
- return;
- }
- /* Iterate through each connected client and print their information */
- for (int i = 0; i < clients.num; i++)
- {
- Serial.print("Client ");
- Serial.print(i + 1);
- Serial.print(" - MAC Address: ");
- /* Print MAC address of the client */
- for (int j = 0; j < 6; j++)
- {
- if (clients.sta[i].mac[j] < 16)
- {
- Serial.print("0");
- }
- Serial.print(clients.sta[i].mac[j], HEX);
- /* Add colon separator between MAC address octets */
- if (j < 5)
- {
- Serial.print(":");
- }
- }
- Serial.println();
- }
- /* Free allocated memory for station list */
- free(clients.sta);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment