pleasedontcode

**WiFi Monitor** rev_09

Mar 14th, 2026
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **WiFi Monitor**
  13.     - Version: 007
  14.     - Source Code NOT compiled for: Adafruit QT Py ESP32-C3
  15.     - Source Code created on: 2026-03-14 08:48:19
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* ESP32 C3 creates a WiFi Access Point (AP) with */
  22.     /* configurable SSID and password. Print AP status */
  23.     /* and connected client information to serial */
  24.     /* monitor. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /****** SUBSYSTEM REQUIREMENTS *****/
  29. /* SR1.1: Configure WiFi Access Point with SSID */
  30. /* SR1.2: Configure WiFi Access Point with Password */
  31. /* SR1.3: Initialize Serial communication at 115200 baud */
  32. /* SR1.4: Print AP SSID to serial monitor */
  33. /* SR1.5: Print AP IP address to serial monitor */
  34. /* SR1.6: Print AP status (active/inactive) to serial monitor */
  35. /* SR1.7: Monitor and print connected client information */
  36. /* SR1.8: Periodically display number of connected clients */
  37. /* SR1.9: Handle client connection/disconnection events */
  38. /****** END SUBSYSTEM REQUIREMENTS *****/
  39.  
  40. #include <WiFi.h>
  41. /* Include ESP-IDF headers for advanced WiFi functionality */
  42. #include "esp_wifi.h"
  43.  
  44. /****** CONFIGURABLE PARAMETERS *****/
  45. /* WiFi Access Point SSID - can be modified */
  46. const char* AP_SSID = "ESP32_WiFi_AP";
  47.  
  48. /* WiFi Access Point Password - can be modified */
  49. const char* AP_PASSWORD = "12345678";
  50.  
  51. /* WiFi Access Point Channel */
  52. const int AP_CHANNEL = 1;
  53.  
  54. /* WiFi Access Point Maximum connected clients */
  55. const int AP_MAX_CLIENTS = 4;
  56.  
  57. /* Serial Monitor Baud Rate */
  58. const long SERIAL_BAUD_RATE = 115200;
  59.  
  60. /* Status print interval in milliseconds */
  61. const unsigned long STATUS_PRINT_INTERVAL = 5000;
  62.  
  63. /****** GLOBAL VARIABLES *****/
  64. /* Timestamp for last status print */
  65. unsigned long lastStatusPrintTime = 0;
  66.  
  67. /* Previously connected clients count for change detection */
  68. int previousClientCount = 0;
  69.  
  70. /****** FUNCTION PROTOTYPES *****/
  71. void setup(void);
  72. void loop(void);
  73. void initializeSerialCommunication(void);
  74. void initializeWiFiAccessPoint(void);
  75. void printAPStatus(void);
  76. void printConnectedClients(void);
  77. void printClientInformation(void);
  78.  
  79. /****** SETUP FUNCTION *****/
  80. void setup(void)
  81. {
  82.     /* Initialize Serial communication for monitoring */
  83.     initializeSerialCommunication();
  84.    
  85.     /* Initialize WiFi Access Point with configured parameters */
  86.     initializeWiFiAccessPoint();
  87.    
  88.     /* Print initial AP status and configuration */
  89.     delay(1000);
  90.     printAPStatus();
  91. }
  92.  
  93. /****** MAIN LOOP FUNCTION *****/
  94. void loop(void)
  95. {
  96.     /* Check if it's time to print status update */
  97.     if (millis() - lastStatusPrintTime >= STATUS_PRINT_INTERVAL)
  98.     {
  99.         /* Update last print timestamp */
  100.         lastStatusPrintTime = millis();
  101.        
  102.         /* Print current AP status and connected clients */
  103.         printAPStatus();
  104.         printConnectedClients();
  105.     }
  106.    
  107.     /* Small delay to prevent watchdog timeout */
  108.     delay(100);
  109. }
  110.  
  111. /****** SERIAL COMMUNICATION INITIALIZATION *****/
  112. void initializeSerialCommunication(void)
  113. {
  114.     /* Begin Serial communication at configured baud rate */
  115.     Serial.begin(SERIAL_BAUD_RATE);
  116.    
  117.     /* Wait for Serial port to be ready */
  118.     delay(100);
  119.    
  120.     /* Print startup message to serial monitor */
  121.     Serial.println("\n\n");
  122.     Serial.println("========================================");
  123.     Serial.println("ESP32-C3 WiFi Access Point Configuration");
  124.     Serial.println("========================================");
  125. }
  126.  
  127. /****** WIFI ACCESS POINT INITIALIZATION *****/
  128. void initializeWiFiAccessPoint(void)
  129. {
  130.     /* Set device mode to WiFi Access Point (AP) mode */
  131.     WiFi.mode(WIFI_AP);
  132.    
  133.     /* Configure WiFi Access Point with SSID and password */
  134.     /* Parameters: SSID, Password, Channel, SSID Hidden (false = visible), Max Clients */
  135.     boolean apStartSuccess = WiFi.softAP(
  136.         AP_SSID,
  137.         AP_PASSWORD,
  138.         AP_CHANNEL,
  139.         false,
  140.         AP_MAX_CLIENTS
  141.     );
  142.    
  143.     /* Check if AP startup was successful */
  144.     if (apStartSuccess)
  145.     {
  146.         Serial.println("\n[INFO] WiFi Access Point started successfully!");
  147.     }
  148.     else
  149.     {
  150.         Serial.println("\n[ERROR] Failed to start WiFi Access Point!");
  151.     }
  152.    
  153.     /* Print configuration details */
  154.     Serial.println("\n--- WiFi AP Configuration ---");
  155.     Serial.print("SSID: ");
  156.     Serial.println(AP_SSID);
  157.     Serial.print("Password: ");
  158.     Serial.println(AP_PASSWORD);
  159.     Serial.print("Channel: ");
  160.     Serial.println(AP_CHANNEL);
  161.     Serial.print("Max Clients: ");
  162.     Serial.println(AP_MAX_CLIENTS);
  163. }
  164.  
  165. /****** PRINT ACCESS POINT STATUS *****/
  166. void printAPStatus(void)
  167. {
  168.     /* Print timestamp */
  169.     Serial.print("\n[");
  170.     Serial.print(millis() / 1000);
  171.     Serial.print("s] --- AP Status Update ---\n");
  172.    
  173.     /* Get and print AP IP address */
  174.     IPAddress apIP = WiFi.softAPIP();
  175.     Serial.print("AP IP Address: ");
  176.     Serial.println(apIP);
  177.    
  178.     /* Get and print AP MAC address */
  179.     Serial.print("AP MAC Address: ");
  180.     Serial.println(WiFi.softAPmacAddress());
  181.    
  182.     /* Get and print AP status (active/inactive) */
  183.     wifi_mode_t currentMode = WiFi.getMode();
  184.     if (currentMode == WIFI_AP)
  185.     {
  186.         Serial.println("AP Status: ACTIVE");
  187.     }
  188.     else
  189.     {
  190.         Serial.println("AP Status: INACTIVE");
  191.     }
  192.    
  193.     /* Get and print number of connected clients */
  194.     int connectedClients = WiFi.softAPgetStationNum();
  195.     Serial.print("Connected Clients: ");
  196.     Serial.println(connectedClients);
  197.    
  198.     /* Check if number of clients has changed */
  199.     if (connectedClients != previousClientCount)
  200.     {
  201.         previousClientCount = connectedClients;
  202.        
  203.         if (connectedClients > 0)
  204.         {
  205.             Serial.println("[EVENT] Client(s) connected!");
  206.         }
  207.         else
  208.         {
  209.             Serial.println("[EVENT] No clients connected.");
  210.         }
  211.     }
  212. }
  213.  
  214. /****** PRINT CONNECTED CLIENTS INFORMATION *****/
  215. void printConnectedClients(void)
  216. {
  217.     /* Get number of connected clients */
  218.     int clientCount = WiFi.softAPgetStationNum();
  219.    
  220.     /* Only print detailed info if clients are connected */
  221.     if (clientCount > 0)
  222.     {
  223.         Serial.println("\n--- Connected Client Details ---");
  224.         printClientInformation();
  225.     }
  226.     else
  227.     {
  228.         Serial.println("No clients currently connected to the AP.");
  229.     }
  230.    
  231.     Serial.println("=====================================");
  232. }
  233.  
  234. /****** PRINT INDIVIDUAL CLIENT INFORMATION *****/
  235. void printClientInformation(void)
  236. {
  237.     /* Get list of connected clients using ESP32 WiFi API */
  238.     /* Allocate memory for station list */
  239.     wifi_sta_list_t clients;
  240.    
  241.     /* Call the ESP-IDF function to get connected stations */
  242.     esp_err_t result = esp_wifi_ap_get_sta_list(&clients);
  243.    
  244.     /* Check if the API call was successful */
  245.     if (result != ESP_OK)
  246.     {
  247.         Serial.print("[WARNING] Failed to get station list, error code: ");
  248.         Serial.println(result);
  249.         return;
  250.     }
  251.    
  252.     /* Iterate through each connected client and print their information */
  253.     for (int i = 0; i < clients.num; i++)
  254.     {
  255.         Serial.print("Client ");
  256.         Serial.print(i + 1);
  257.         Serial.print(" - MAC Address: ");
  258.        
  259.         /* Print MAC address of the client */
  260.         for (int j = 0; j < 6; j++)
  261.         {
  262.             if (clients.sta[i].mac[j] < 16)
  263.             {
  264.                 Serial.print("0");
  265.             }
  266.             Serial.print(clients.sta[i].mac[j], HEX);
  267.            
  268.             /* Add colon separator between MAC address octets */
  269.             if (j < 5)
  270.             {
  271.                 Serial.print(":");
  272.             }
  273.         }
  274.        
  275.         Serial.println();
  276.     }
  277.    
  278.     /* Free allocated memory for station list */
  279.     free(clients.sta);
  280. }
  281.  
  282. /* END CODE */
  283.  
Advertisement
Add Comment
Please, Sign In to add comment