pleasedontcode

# WiFi Hotspot rev_05

Mar 14th, 2026
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.82 KB | None | 0 0
  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 Hotspot
  13.     - Version: 003
  14.     - Source Code compiled for: ESP32 Wrover Module
  15.     - Source Code created on: 2026-03-14 08:19:22
  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. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* [POTA] Include POTA.h and secrets.h. Init POTA in */
  27.     /* setup() with WiFi creds from secrets.h. Call */
  28.     /* pota.loop() in loop(). Use pota.dashboard.setWidge */
  29.     /* tConfigCallback(setupDashboard) and sendUpdates() */
  30.     /* in loop(). */
  31. /****** SYSTEM REQUIREMENT 3 *****/
  32.     /* [POTA] MANDATORY OTA: call */
  33.     /* pota.checkAndPerformOTA() in setup() after */
  34.     /* begin(). Register pota.onOTAAvailable(cb) where cb */
  35.     /* sets a bool flag. In loop() when flag is true call */
  36.     /* pota.restart(). Without OTA the device cannot */
  37.     /* update remotely. */
  38. /****** SYSTEM REQUIREMENT 4 *****/
  39.     /* [POTA] Dashboard monitor widget: */
  40.     /* lastStatusPrintTime as TIME_DIFFERENCE_CARD. Use */
  41.     /* pota.dashboard.setValue() to send value to */
  42.     /* dashboard. extra="auto". */
  43. /****** SYSTEM REQUIREMENT 5 *****/
  44.     /* [POTA] Dashboard monitor widget: */
  45.     /* previousClientCount as GENERIC_CARD. Use */
  46.     /* pota.dashboard.setValue() to send value to */
  47.     /* dashboard. */
  48. /****** END SYSTEM REQUIREMENTS *****/
  49.  
  50.  
  51. /* START CODE */
  52.  
  53. /****** SUBSYSTEM REQUIREMENTS *****/
  54. /* SR1.1: Configure WiFi Access Point with SSID */
  55. /* SR1.2: Configure WiFi Access Point with Password */
  56. /* SR1.3: Initialize Serial communication at 115200 baud */
  57. /* SR1.4: Print AP SSID to serial monitor */
  58. /* SR1.5: Print AP IP address to serial monitor */
  59. /* SR1.6: Print AP status (active/inactive) to serial monitor */
  60. /* SR1.7: Monitor and print connected client information */
  61. /* SR1.8: Periodically display number of connected clients */
  62. /* SR1.9: Handle client connection/disconnection events */
  63. /* SR2.1: Include POTA.h header file */
  64. /* SR2.2: Include secrets.h header file */
  65. /* SR2.3: Initialize POTA in setup() with WiFi credentials */
  66. /* SR2.4: Call pota.loop() in main loop() */
  67. /* SR2.5: Set up dashboard widget configuration callback */
  68. /* SR2.6: Send dashboard updates in loop() */
  69. /* SR3.1: Call pota.checkAndPerformOTA() in setup() after begin() */
  70. /* SR3.2: Register OTA available callback handler */
  71. /* SR3.3: Set OTA available flag when update is available */
  72. /* SR3.4: Call pota.restart() when OTA flag is true */
  73. /* SR4.1: Configure lastStatusPrintTime as TIME_DIFFERENCE_CARD widget */
  74. /* SR4.2: Send lastStatusPrintTime value to dashboard with auto extra */
  75. /* SR5.1: Configure previousClientCount as GENERIC_CARD widget */
  76. /* SR5.2: Send previousClientCount value to dashboard */
  77. /****** END SUBSYSTEM REQUIREMENTS *****/
  78.  
  79. /* Include POTA library for Over-The-Air update functionality */
  80. #include "POTA.h"
  81.  
  82. /* Include secrets header for WiFi credentials and authentication tokens */
  83. #include "secrets.h"
  84.  
  85. /****** CONFIGURABLE PARAMETERS *****/
  86. /* WiFi Access Point SSID - can be modified */
  87. const char* AP_SSID = "ESP32_WiFi_AP";
  88.  
  89. /* WiFi Access Point Password - can be modified */
  90. const char* AP_PASSWORD = "12345678";
  91.  
  92. /* WiFi Access Point Channel */
  93. const int AP_CHANNEL = 1;
  94.  
  95. /* WiFi Access Point Maximum connected clients */
  96. const int AP_MAX_CLIENTS = 4;
  97.  
  98. /* Serial Monitor Baud Rate */
  99. const long SERIAL_BAUD_RATE = 115200;
  100.  
  101. /* Status print interval in milliseconds */
  102. const unsigned long STATUS_PRINT_INTERVAL = 5000;
  103.  
  104. /* Dashboard update interval in milliseconds */
  105. const unsigned long DASHBOARD_UPDATE_INTERVAL = 2000;
  106.  
  107. /****** GLOBAL VARIABLES *****/
  108. /* Timestamp for last status print */
  109. unsigned long lastStatusPrintTime = 0;
  110.  
  111. /* Previously connected clients count for change detection */
  112. int previousClientCount = 0;
  113.  
  114. /* Timestamp for last dashboard update */
  115. unsigned long lastDashboardUpdateTime = 0;
  116.  
  117. /* Flag to indicate OTA update is available and ready to apply */
  118. bool otaUpdateAvailable = false;
  119.  
  120. /* POTA instance for Over-The-Air update management */
  121. POTA pota;
  122.  
  123. /* Dashboard widget IDs - stored when widgets are created */
  124. uint8_t lastStatusPrintTimeWidget = 0;
  125. uint8_t previousClientCountWidget = 0;
  126.  
  127. /****** FUNCTION PROTOTYPES *****/
  128. void setup(void);
  129. void loop(void);
  130. void initializeSerialCommunication(void);
  131. void initializeWiFiAccessPoint(void);
  132. void printAPStatus(void);
  133. void printConnectedClients(void);
  134. void printClientInformation(void);
  135. void setupDashboard(void);
  136. void sendDashboardUpdates(void);
  137. void handleOTAAvailable(const char* version);
  138.  
  139. /****** SETUP FUNCTION *****/
  140. void setup(void)
  141. {
  142.     /* Initialize Serial communication for monitoring */
  143.     initializeSerialCommunication();
  144.    
  145.     /* Initialize WiFi Access Point with configured parameters */
  146.     initializeWiFiAccessPoint();
  147.    
  148.     /* Print initial AP status and configuration */
  149.     delay(1000);
  150.     printAPStatus();
  151.    
  152.     /* Initialize POTA with WiFi credentials from secrets.h */
  153.     Serial.println("\n[POTA] Initializing POTA...");
  154.     pota.begin(DEVICE_TYPE, FIRMWARE_VERSION, AUTH_TOKEN, SERVER_SECRET, WIFI_SSID, WIFI_PASSWORD);
  155.    
  156.     /* Register dashboard widget configuration callback */
  157.     pota.dashboard.setWidgetConfigCallback(setupDashboard);
  158.    
  159.     /* Register OTA available callback handler with correct signature */
  160.     pota.onOTAAvailable(handleOTAAvailable);
  161.    
  162.     /* Check for available OTA updates and perform if ready */
  163.     Serial.println("[POTA] Checking for OTA updates...");
  164.     pota.checkAndPerformOTA();
  165.    
  166.     Serial.println("[POTA] POTA initialization complete!");
  167.     Serial.println("Device is ready for Over-The-Air updates.");
  168. }
  169.  
  170. /****** MAIN LOOP FUNCTION *****/
  171. void loop(void)
  172. {
  173.     /* Call POTA loop for WiFi and OTA event handling */
  174.     pota.loop();
  175.    
  176.     /* Check if OTA update is available and ready to apply */
  177.     if (otaUpdateAvailable)
  178.     {
  179.         /* Reset the OTA flag */
  180.         otaUpdateAvailable = false;
  181.        
  182.         /* Restart device to apply the OTA update */
  183.         Serial.println("\n[OTA] Restarting device to apply update...");
  184.         pota.restart();
  185.     }
  186.    
  187.     /* Check if it's time to print status update */
  188.     if (millis() - lastStatusPrintTime >= STATUS_PRINT_INTERVAL)
  189.     {
  190.         /* Update last print timestamp */
  191.         lastStatusPrintTime = millis();
  192.        
  193.         /* Print current AP status and connected clients */
  194.         printAPStatus();
  195.         printConnectedClients();
  196.     }
  197.    
  198.     /* Check if it's time to send dashboard updates */
  199.     if (millis() - lastDashboardUpdateTime >= DASHBOARD_UPDATE_INTERVAL)
  200.     {
  201.         /* Update last dashboard update timestamp */
  202.         lastDashboardUpdateTime = millis();
  203.        
  204.         /* Send widget values to POTA dashboard */
  205.         sendDashboardUpdates();
  206.     }
  207.    
  208.     /* Small delay to prevent watchdog timeout */
  209.     delay(100);
  210. }
  211.  
  212. /****** SERIAL COMMUNICATION INITIALIZATION *****/
  213. void initializeSerialCommunication(void)
  214. {
  215.     /* Begin Serial communication at configured baud rate */
  216.     Serial.begin(SERIAL_BAUD_RATE);
  217.    
  218.     /* Wait for Serial port to be ready */
  219.     delay(100);
  220.    
  221.     /* Print startup message to serial monitor */
  222.     Serial.println("\n\n");
  223.     Serial.println("========================================");
  224.     Serial.println("ESP32 WiFi Access Point Configuration");
  225.     Serial.println("========================================");
  226. }
  227.  
  228. /****** WIFI ACCESS POINT INITIALIZATION *****/
  229. void initializeWiFiAccessPoint(void)
  230. {
  231.     /* Set device mode to WiFi Access Point (AP) mode */
  232.     WiFi.mode(WIFI_AP);
  233.    
  234.     /* Configure WiFi Access Point with SSID and password */
  235.     /* Parameters: SSID, Password, Channel, SSID Hidden (false = visible), Max Clients */
  236.     boolean apStartSuccess = WiFi.softAP(
  237.         AP_SSID,
  238.         AP_PASSWORD,
  239.         AP_CHANNEL,
  240.         false,
  241.         AP_MAX_CLIENTS
  242.     );
  243.    
  244.     /* Check if AP startup was successful */
  245.     if (apStartSuccess)
  246.     {
  247.         Serial.println("\n[INFO] WiFi Access Point started successfully!");
  248.     }
  249.     else
  250.     {
  251.         Serial.println("\n[ERROR] Failed to start WiFi Access Point!");
  252.     }
  253.    
  254.     /* Print configuration details */
  255.     Serial.println("\n--- WiFi AP Configuration ---");
  256.     Serial.print("SSID: ");
  257.     Serial.println(AP_SSID);
  258.     Serial.print("Password: ");
  259.     Serial.println(AP_PASSWORD);
  260.     Serial.print("Channel: ");
  261.     Serial.println(AP_CHANNEL);
  262.     Serial.print("Max Clients: ");
  263.     Serial.println(AP_MAX_CLIENTS);
  264. }
  265.  
  266. /****** PRINT ACCESS POINT STATUS *****/
  267. void printAPStatus(void)
  268. {
  269.     /* Print timestamp */
  270.     Serial.print("\n[");
  271.     Serial.print(millis() / 1000);
  272.     Serial.print("s] --- AP Status Update ---\n");
  273.    
  274.     /* Get and print AP IP address */
  275.     IPAddress apIP = WiFi.softAPIP();
  276.     Serial.print("AP IP Address: ");
  277.     Serial.println(apIP);
  278.    
  279.     /* Get and print AP MAC address */
  280.     Serial.print("AP MAC Address: ");
  281.     Serial.println(WiFi.softAPmacAddress());
  282.    
  283.     /* Get and print AP status (active/inactive) */
  284.     wifi_mode_t currentMode = WiFi.getMode();
  285.     if (currentMode == WIFI_AP)
  286.     {
  287.         Serial.println("AP Status: ACTIVE");
  288.     }
  289.     else
  290.     {
  291.         Serial.println("AP Status: INACTIVE");
  292.     }
  293.    
  294.     /* Get and print number of connected clients */
  295.     int connectedClients = WiFi.softAPgetStationNum();
  296.     Serial.print("Connected Clients: ");
  297.     Serial.println(connectedClients);
  298.    
  299.     /* Check if number of clients has changed */
  300.     if (connectedClients != previousClientCount)
  301.     {
  302.         previousClientCount = connectedClients;
  303.        
  304.         if (connectedClients > 0)
  305.         {
  306.             Serial.println("[EVENT] Client(s) connected!");
  307.         }
  308.         else
  309.         {
  310.             Serial.println("[EVENT] No clients connected.");
  311.         }
  312.     }
  313. }
  314.  
  315. /****** PRINT CONNECTED CLIENTS INFORMATION *****/
  316. void printConnectedClients(void)
  317. {
  318.     /* Get number of connected clients */
  319.     int clientCount = WiFi.softAPgetStationNum();
  320.    
  321.     /* Only print detailed info if clients are connected */
  322.     if (clientCount > 0)
  323.     {
  324.         Serial.println("\n--- Connected Client Details ---");
  325.         printClientInformation();
  326.     }
  327.     else
  328.     {
  329.         Serial.println("No clients currently connected to the AP.");
  330.     }
  331.    
  332.     Serial.println("=====================================");
  333. }
  334.  
  335. /****** PRINT INDIVIDUAL CLIENT INFORMATION *****/
  336. void printClientInformation(void)
  337. {
  338.     /* Create WiFi station list structure to retrieve client information */
  339.     wifi_sta_list_t clientList;
  340.     /* Create TCPIP adapter station list structure to receive client data */
  341.     tcpip_adapter_sta_list_t adapterList;
  342.    
  343.     /* Get the list of connected clients with both required parameters */
  344.     tcpip_adapter_get_sta_list(&clientList, &adapterList);
  345.    
  346.     /* Iterate through each connected client and print their information */
  347.     for (int i = 0; i < adapterList.num; i++)
  348.     {
  349.         Serial.print("Client ");
  350.         Serial.print(i + 1);
  351.         Serial.print(" - MAC Address: ");
  352.        
  353.         /* Print MAC address of the client */
  354.         for (int j = 0; j < 6; j++)
  355.         {
  356.             if (adapterList.sta[i].mac[j] < 16)
  357.             {
  358.                 Serial.print("0");
  359.             }
  360.             Serial.print(adapterList.sta[i].mac[j], HEX);
  361.            
  362.             /* Add colon separator between MAC address octets */
  363.             if (j < 5)
  364.             {
  365.                 Serial.print(":");
  366.             }
  367.         }
  368.        
  369.         Serial.println();
  370.     }
  371.    
  372.     /* Free the allocated memory for client list */
  373.     free(adapterList.sta);
  374. }
  375.  
  376. /****** SETUP DASHBOARD WIDGETS *****/
  377. void setupDashboard(void)
  378. {
  379.     /* Configure lastStatusPrintTime widget as TIME_DIFFERENCE_CARD */
  380.     /* This widget displays the time elapsed since last status print */
  381.     /* addWidget returns the widget ID which we store for later updates */
  382.     lastStatusPrintTimeWidget = pota.dashboard.addWidget(TIME_DIFFERENCE_CARD, "Last Status Print Time", 0, "auto");
  383.    
  384.     /* Configure previousClientCount widget as GENERIC_CARD */
  385.     /* This widget displays the number of previously connected clients */
  386.     /* Store the returned widget ID for use in sendDashboardUpdates() */
  387.     previousClientCountWidget = pota.dashboard.addWidget(GENERIC_CARD, "Previous Client Count");
  388. }
  389.  
  390. /****** SEND DASHBOARD UPDATES *****/
  391. void sendDashboardUpdates(void)
  392. {
  393.     /* Send lastStatusPrintTime value to dashboard */
  394.     /* Calculate time difference in milliseconds since last status print */
  395.     unsigned long timeDifference = millis() - lastStatusPrintTime;
  396.    
  397.     /* Send the time difference value to the dashboard widget using its ID */
  398.     pota.dashboard.setValue(lastStatusPrintTimeWidget, (int)timeDifference);
  399.    
  400.     /* Send previousClientCount value to dashboard */
  401.     /* Send the current count of previously connected clients using its ID */
  402.     pota.dashboard.setValue(previousClientCountWidget, previousClientCount);
  403. }
  404.  
  405. /****** HANDLE OTA AVAILABLE CALLBACK *****/
  406. void handleOTAAvailable(const char* version)
  407. {
  408.     /* Set flag to indicate that an OTA update is available */
  409.     /* This flag will trigger a device restart in the main loop */
  410.     otaUpdateAvailable = true;
  411.    
  412.     /* Log OTA availability and version information to serial monitor */
  413.     Serial.println("\n[OTA] Over-The-Air update is available!");
  414.     Serial.print("[OTA] New firmware version: ");
  415.     Serial.println(version);
  416.     Serial.println("[OTA] Device will restart to apply the update.");
  417. }
  418.  
  419. /* END CODE */
  420.  
Advertisement
Add Comment
Please, Sign In to add comment