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 Hotspot
- - Version: 003
- - Source Code compiled for: ESP32 Wrover Module
- - Source Code created on: 2026-03-14 08:19:22
- ********* 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. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* [POTA] Include POTA.h and secrets.h. Init POTA in */
- /* setup() with WiFi creds from secrets.h. Call */
- /* pota.loop() in loop(). Use pota.dashboard.setWidge */
- /* tConfigCallback(setupDashboard) and sendUpdates() */
- /* in loop(). */
- /****** SYSTEM REQUIREMENT 3 *****/
- /* [POTA] MANDATORY OTA: call */
- /* pota.checkAndPerformOTA() in setup() after */
- /* begin(). Register pota.onOTAAvailable(cb) where cb */
- /* sets a bool flag. In loop() when flag is true call */
- /* pota.restart(). Without OTA the device cannot */
- /* update remotely. */
- /****** SYSTEM REQUIREMENT 4 *****/
- /* [POTA] Dashboard monitor widget: */
- /* lastStatusPrintTime as TIME_DIFFERENCE_CARD. Use */
- /* pota.dashboard.setValue() to send value to */
- /* dashboard. extra="auto". */
- /****** SYSTEM REQUIREMENT 5 *****/
- /* [POTA] Dashboard monitor widget: */
- /* previousClientCount as GENERIC_CARD. Use */
- /* pota.dashboard.setValue() to send value to */
- /* dashboard. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** 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 */
- /* SR2.1: Include POTA.h header file */
- /* SR2.2: Include secrets.h header file */
- /* SR2.3: Initialize POTA in setup() with WiFi credentials */
- /* SR2.4: Call pota.loop() in main loop() */
- /* SR2.5: Set up dashboard widget configuration callback */
- /* SR2.6: Send dashboard updates in loop() */
- /* SR3.1: Call pota.checkAndPerformOTA() in setup() after begin() */
- /* SR3.2: Register OTA available callback handler */
- /* SR3.3: Set OTA available flag when update is available */
- /* SR3.4: Call pota.restart() when OTA flag is true */
- /* SR4.1: Configure lastStatusPrintTime as TIME_DIFFERENCE_CARD widget */
- /* SR4.2: Send lastStatusPrintTime value to dashboard with auto extra */
- /* SR5.1: Configure previousClientCount as GENERIC_CARD widget */
- /* SR5.2: Send previousClientCount value to dashboard */
- /****** END SUBSYSTEM REQUIREMENTS *****/
- /* Include POTA library for Over-The-Air update functionality */
- #include "POTA.h"
- /* Include secrets header for WiFi credentials and authentication tokens */
- #include "secrets.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;
- /* Dashboard update interval in milliseconds */
- const unsigned long DASHBOARD_UPDATE_INTERVAL = 2000;
- /****** GLOBAL VARIABLES *****/
- /* Timestamp for last status print */
- unsigned long lastStatusPrintTime = 0;
- /* Previously connected clients count for change detection */
- int previousClientCount = 0;
- /* Timestamp for last dashboard update */
- unsigned long lastDashboardUpdateTime = 0;
- /* Flag to indicate OTA update is available and ready to apply */
- bool otaUpdateAvailable = false;
- /* POTA instance for Over-The-Air update management */
- POTA pota;
- /* Dashboard widget IDs - stored when widgets are created */
- uint8_t lastStatusPrintTimeWidget = 0;
- uint8_t previousClientCountWidget = 0;
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeSerialCommunication(void);
- void initializeWiFiAccessPoint(void);
- void printAPStatus(void);
- void printConnectedClients(void);
- void printClientInformation(void);
- void setupDashboard(void);
- void sendDashboardUpdates(void);
- void handleOTAAvailable(const char* version);
- /****** 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();
- /* Initialize POTA with WiFi credentials from secrets.h */
- Serial.println("\n[POTA] Initializing POTA...");
- pota.begin(DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET, WIFI_SSID, WIFI_PASSWORD);
- /* Register dashboard widget configuration callback */
- pota.dashboard.setWidgetConfigCallback(setupDashboard);
- /* Register OTA available callback handler with correct signature */
- pota.onOTAAvailable(handleOTAAvailable);
- /* Check for available OTA updates and perform if ready */
- Serial.println("[POTA] Checking for OTA updates...");
- pota.checkAndPerformOTA();
- Serial.println("[POTA] POTA initialization complete!");
- Serial.println("Device is ready for Over-The-Air updates.");
- }
- /****** MAIN LOOP FUNCTION *****/
- void loop(void)
- {
- /* Call POTA loop for WiFi and OTA event handling */
- pota.loop();
- /* Check if OTA update is available and ready to apply */
- if (otaUpdateAvailable)
- {
- /* Reset the OTA flag */
- otaUpdateAvailable = false;
- /* Restart device to apply the OTA update */
- Serial.println("\n[OTA] Restarting device to apply update...");
- pota.restart();
- }
- /* 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();
- }
- /* Check if it's time to send dashboard updates */
- if (millis() - lastDashboardUpdateTime >= DASHBOARD_UPDATE_INTERVAL)
- {
- /* Update last dashboard update timestamp */
- lastDashboardUpdateTime = millis();
- /* Send widget values to POTA dashboard */
- sendDashboardUpdates();
- }
- /* 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 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)
- {
- /* Create WiFi station list structure to retrieve client information */
- wifi_sta_list_t clientList;
- /* Create TCPIP adapter station list structure to receive client data */
- tcpip_adapter_sta_list_t adapterList;
- /* Get the list of connected clients with both required parameters */
- tcpip_adapter_get_sta_list(&clientList, &adapterList);
- /* Iterate through each connected client and print their information */
- for (int i = 0; i < adapterList.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 (adapterList.sta[i].mac[j] < 16)
- {
- Serial.print("0");
- }
- Serial.print(adapterList.sta[i].mac[j], HEX);
- /* Add colon separator between MAC address octets */
- if (j < 5)
- {
- Serial.print(":");
- }
- }
- Serial.println();
- }
- /* Free the allocated memory for client list */
- free(adapterList.sta);
- }
- /****** SETUP DASHBOARD WIDGETS *****/
- void setupDashboard(void)
- {
- /* Configure lastStatusPrintTime widget as TIME_DIFFERENCE_CARD */
- /* This widget displays the time elapsed since last status print */
- /* addWidget returns the widget ID which we store for later updates */
- lastStatusPrintTimeWidget = pota.dashboard.addWidget(TIME_DIFFERENCE_CARD, "Last Status Print Time", 0, "auto");
- /* Configure previousClientCount widget as GENERIC_CARD */
- /* This widget displays the number of previously connected clients */
- /* Store the returned widget ID for use in sendDashboardUpdates() */
- previousClientCountWidget = pota.dashboard.addWidget(GENERIC_CARD, "Previous Client Count");
- }
- /****** SEND DASHBOARD UPDATES *****/
- void sendDashboardUpdates(void)
- {
- /* Send lastStatusPrintTime value to dashboard */
- /* Calculate time difference in milliseconds since last status print */
- unsigned long timeDifference = millis() - lastStatusPrintTime;
- /* Send the time difference value to the dashboard widget using its ID */
- pota.dashboard.setValue(lastStatusPrintTimeWidget, (int)timeDifference);
- /* Send previousClientCount value to dashboard */
- /* Send the current count of previously connected clients using its ID */
- pota.dashboard.setValue(previousClientCountWidget, previousClientCount);
- }
- /****** HANDLE OTA AVAILABLE CALLBACK *****/
- void handleOTAAvailable(const char* version)
- {
- /* Set flag to indicate that an OTA update is available */
- /* This flag will trigger a device restart in the main loop */
- otaUpdateAvailable = true;
- /* Log OTA availability and version information to serial monitor */
- Serial.println("\n[OTA] Over-The-Air update is available!");
- Serial.print("[OTA] New firmware version: ");
- Serial.println(version);
- Serial.println("[OTA] Device will restart to apply the update.");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment