Uniot Core
0.8.1
Loading...
Searching...
No Matches
uniot::ConfigCaptivePortal Class Reference

Complete captive portal implementation for device configuration. More...

#include <ConfigCaptivePortal.h>

Inheritance diagram for uniot::ConfigCaptivePortal:
[legend]

Public Member Functions

 ConfigCaptivePortal (const IPAddress &apIp, AwsEventHandler wsHandler=nullptr)
 Construct a new ConfigCaptivePortal.
 
bool start ()
 Start the captive portal services.
 
void stop ()
 Stop all captive portal services.
 
AsyncWebServer * get ()
 Get the underlying AsyncWebServer instance.
 
void wsTextAll (const String &message)
 Send text message to all connected WebSocket clients.
 
void wsText (uint32_t clientId, const String &message)
 Send text message to a specific WebSocket client.
 
void wsCloseAll ()
 Close all WebSocket connections.
 
void wsEnable (bool enable)
 Enable or disable WebSocket functionality.
 
bool wsClientsActive (unsigned long window=30000) const
 Check if WebSocket clients are actively connected.
 
const IPAddress & ip () const
 Get the IP address of the captive portal.
 
virtual void execute (short _) override
 Execute periodic captive portal maintenance tasks.
 
- Public Member Functions inherited from uniot::IExecutor
virtual ~IExecutor ()
 Virtual destructor for proper cleanup.
 

Detailed Description

Complete captive portal implementation for device configuration.

This class implements a full-featured captive portal that combines DNS redirection, HTTP serving, WebSocket communication, and mDNS discovery. It's designed to provide a seamless configuration experience where users connecting to the device's WiFi network are automatically redirected to the configuration interface.

The captive portal operates by:

  1. Running a DNS server that redirects all queries to the device IP
  2. Serving a web-based configuration interface via HTTP
  3. Enabling real-time communication through WebSockets
  4. Advertising the device via mDNS for local discovery

All operations are designed to be non-blocking and suitable for embedded systems with limited resources.

Constructor & Destructor Documentation

◆ ConfigCaptivePortal()

uniot::ConfigCaptivePortal::ConfigCaptivePortal ( const IPAddress & apIp,
AwsEventHandler wsHandler = nullptr )
inline

Construct a new ConfigCaptivePortal.

Parameters
apIpIP address for the access point and web server
wsHandlerOptional WebSocket event handler callback

Initializes all components of the captive portal including DNS server, HTTP server, and WebSocket handler. The WebSocket is automatically added to the HTTP server and configured with the provided event handler.

Member Function Documentation

◆ execute()

virtual void uniot::ConfigCaptivePortal::execute ( short _)
inlineoverridevirtual

Execute periodic captive portal maintenance tasks.

Parameters
_Unused parameter (required by IExecutor interface)

Performs essential maintenance operations including:

  • Processing DNS requests for domain redirection
  • Cleaning up disconnected WebSocket clients
  • Updating mDNS service (ESP8266 only)

This method should be called regularly from the main loop or task scheduler.

Implements uniot::IExecutor.

◆ get()

AsyncWebServer * uniot::ConfigCaptivePortal::get ( )
inline

Get the underlying AsyncWebServer instance.

Return values
AsyncWebServer*Pointer to the HTTP server for adding custom routes

Provides access to the web server for adding custom request handlers, routes, and other HTTP-specific configuration.

◆ ip()

const IPAddress & uniot::ConfigCaptivePortal::ip ( ) const
inline

Get the IP address of the captive portal.

Return values
constIPAddress& Reference to the portal's IP address

◆ start()

bool uniot::ConfigCaptivePortal::start ( )
inline

Start the captive portal services.

Return values
booltrue if all services started successfully, false otherwise

Sequentially starts all captive portal components:

  1. DNS server for domain redirection
  2. WebSocket event handling (if handler provided)
  3. HTTP web server
  4. mDNS advertisement

If any component fails to start, the method returns false and the portal remains in a stopped state.

◆ stop()

void uniot::ConfigCaptivePortal::stop ( )
inline

Stop all captive portal services.

Gracefully shuts down all components in reverse order:

  1. mDNS service
  2. DNS server (with memory cleanup workaround)
  3. HTTP web server
  4. WebSocket connections

Includes workarounds for known memory issues with DNSServer cleanup.

◆ wsClientsActive()

bool uniot::ConfigCaptivePortal::wsClientsActive ( unsigned long window = 30000) const
inline

Check if WebSocket clients are actively connected.

Parameters
windowTime window in milliseconds for considering clients active (default: 30 seconds)
Return values
booltrue if clients are connected and recently active, false otherwise

Determines if there are WebSocket clients connected and whether they've been active within the specified time window. This helps distinguish between truly active clients and stale connections.

◆ wsCloseAll()

void uniot::ConfigCaptivePortal::wsCloseAll ( )
inline

Close all WebSocket connections.

Disables the WebSocket server and forcibly closes all active client connections. Used during shutdown or when switching operating modes.

◆ wsEnable()

void uniot::ConfigCaptivePortal::wsEnable ( bool enable)
inline

Enable or disable WebSocket functionality.

Parameters
enabletrue to enable WebSocket server, false to disable

Controls whether the WebSocket server accepts new connections and processes existing ones. Useful for temporarily suspending WebSocket operations without stopping the entire portal.

◆ wsText()

void uniot::ConfigCaptivePortal::wsText ( uint32_t clientId,
const String & message )
inline

Send text message to a specific WebSocket client.

Parameters
clientIdID of the target WebSocket client
messageText message to send

Sends a text message to a specific WebSocket client identified by its client ID. Useful for targeted responses and private communication.

◆ wsTextAll()

void uniot::ConfigCaptivePortal::wsTextAll ( const String & message)
inline

Send text message to all connected WebSocket clients.

Parameters
messageText message to broadcast

Broadcasts a text message to all currently connected WebSocket clients. This is commonly used for status updates and configuration responses.


The documentation for this class was generated from the following file: