Skip to content

AutoConnect API

Include headers

AutoConnect.h

#include <AutoConnect.h>

Defined macros

They contain in AutoConnectDefs.h.

#define AC_DEBUG                                // Monitor message output activation
#define AC_DEBUG_PORT           Serial          // Default message output device
#define AUTOCONNECT_AP_IP       0x01F4A8C0      // Default SoftAP IP
#define AUTOCONNECT_AP_GW       0x01F4A8C0      // Default SoftAP Gateway IP
#define AUTOCONNECT_AP_NM       0x00FFFFFF      // Default subnet mask
#define AUTOCONNECT_DNSPORT     53              // Default DNS port at captive portal
#define AUTOCONNECT_HTTPPORT    80              // Default HTTP
#define AUTOCONNECT_MENU_TITLE  "AutoConnect"   // Default AutoConnect menu title
#define AUTOCONNECT_STARTUPTIME 10              // Default waiting time[s] for after reset
#define AUTOCONNECT_URI         "/_ac"          // Default AutoConnect root path
#define AUTOCONNECT_TIMEOUT     30000           // Default connection timeout[ms]
#define AUTOCONNECT_CAPTIVEPORTAL_TIMEOUT  0    // Captive portal timeout value
#define AUTOCONNECT_USE_JSON                    // Allow AutoConnect elements to be handled by JSON format

Macros placement moved

Source code placement of the above macros provided for user sketch changed from v0.9.7. The new code is in AutoConnectDefs.h.

Constructors

AutoConnect

AutoConnect()

AutoConnect default constructor. This entry internally allocates the ESP8266WebServer for ESP8266 or WebServer for ESP32 and is activated internally.

  • For ESP8266
AutoConnect(ESP8266WebServer& webServer)
  • For ESP32
AutoConnect(WebServer& webServer)

Run the AutoConnect site using the externally ensured ESP8266WebServer for ESP8266 or WebServer for ESP32.

The handleClient function of AutoConnect can include the response of the URI handler added by the user using the "on" function of ESP8266WebServer/WebServer. If ESP8266WebServer/WebServer is assigned internally by AutoConnect, the sketch can obtain that reference with the host function.

Parameter
webServerA reference of ESP8266WebServer or WebServer instance.

Public member functions

aux

AutoConnectAux* aux(const String& uri) const
Returns a pointer to AutoConnectAux with the URI specified by uri. If AutoConnectAux with that URI is not bound, it returns nullptr.
Parameter
uriA string of the URI.
Return value
A Pointer of the AutoConnectAux instance.

begin

bool begin()
bool begin(const char* ssid, const char* passphrase)
bool begin(const char* ssid, const char* passphrase, unsigned long timeout)

Starts establishing the WiFi connection. The WiFi mode at this time is WIFI_STA.
AutoConnect first invokes WiFi.begin. If the ssid and the passphrase are missing, its WiFi.begin has no SSID and Password. Regardless of the result, ESP8266WebServer/WebServer will start immediately after the first WiFi.begin.
The captive portal will not be started if the connection has been established with first WiFi.begin. If the connection cannot establish, switch to WIFI_AP_STA mode and activate SoftAP. Then DNS server starts.

Parameters
ssidSSID to be connected.
passphrasePassword for connection.
timeoutA time out value in milliseconds for waiting connection.
Return value
trueConnection established, AutoConnect service started with WIFI_STA mode.
falseCould not connected, Captive portal started with WIFI_AP_STA mode.

config

bool config(AutoConnectConfig& config)
bool config(const char* ap, const char* password = nullptr)

Set SoftAP's WiFi configuration and static IP configuration.

Parameters
configReference to AutoConnectConfig containing SoftAP's parameters and static IP parameters.
apSSID for SoftAP. The default value is esp8266ap for ESP8266, esp32ap for ESP32.
passwordPassword for SodtAP. The default value is 12345678.
Return value
trueSuccessfully configured.
falseConfiguration parameter is invalid, some values out of range.

end

void end(void)

Stops AutoConnect captive portal service. Release ESP8266WebServer/WebServer and DNSServer.

Attention to end

The end function releases the instance of ESP8266WebServer/WebServer and DNSServer. It can not process them after the end function.

handleClient

void handleClient(void)

Process the AutoConnect menu interface. The handleClient() function of the ESP8266WebServer/WebServer hosted by AutoConnect is also called from within AutoConnect, and the client request handlers contained in the user sketch are also handled.

handleRequest

void handleRequest(void)

Handling for the AutoConnect menu request.

About used in combination with handleClient

The handleRequest function is not supposed to use with AutoConnect::handleClient. It should be used with ESP8266WebServer::handleClient or WebServer::handleClient.

home

void home(String& uri)

Put a user site's home URI. The URI specified by home is linked from "HOME" in the AutoConnect menu.

Parameter
uriA URI string of user site's home path.

host

  • For ESP8266
ESP8266WebServer& host(void)
  • For ESP32
WebServer& host(void)

Returns the reference of the ESP8266WebServer/WebServer which is allocated in AutoConnect automatically.

Return value
A reference of the ESP8266WebServer/WebServer.

&reference is not a pointer

A reference cannot be re-assigned, and must be assigned at initialization. It's like as bind as alias.

ESP8266WebServer& server = portal.host();
server.handleClient();
or
portal.host().handleClient();

join

void join(AutoConnectAux& aux)
void join(std::vector<std::reference_wrapper<AutoConnectAux>> aux)
Join the AutoConnectAux object to AutoConnect. AutoConnectAux objects can be joined one by one, or joined altogether. The AutoConnectAux object joined by the join function can be handled from the AutoConnect menu.
Parameter
auxReference to AutoConnectAux. It can be std::vector of std::reference_wrapper of AutoConnectAux with list initialization.

load

bool load(const String& aux)
bool load(PGM_P aux)
bool load(const __FlashStringHelper* aux)
bool load(Stream& aux)
Load JSON document of AutoConnectAux which contains AutoConnectElements. If there is a syntax error in the JSON document, false is returned.
Parameter
auxThe input string to be loaded.
Return value
trueThe JSON document as AutoConnectAux successfully loaded.
falseLoading JSON document unsuccessful, probably syntax errors have occurred or insufficient memory. You can diagnose the cause of loading failure using the ArduinoJson Assistant.

on

bool on(const String& uri, const AuxHandlerFunctionT handler, AutoConnectExitOrder_t order = AC_EXIT_AHEAD)
Register the handler function of the AutoConnectAux.
Parameters
uriA string of the URI assigned to the AutoConnectAux page.
handlerA function that behaves when a request to the AutoConnectAux page occurs. AuxHandlerFunctionT type is defined by the following declaration.

String handler(AutoConnectAux&, PageArgument&)

orderSpecifies when the handler is called with the following enumeration value.
  • AC_EXIT_AHEAD : Called before AutoConnect generates the HTML of the page. You set the value of AutoConnectElements in the handler then its value will be displayed on the page.
  • AC_EXIT_LATER : Called after AutoConnect generates the HTML of the page. You can append to HTML generated by AutoConnect.
  • AC_EXIT_BOTH : Called even before generating HTML and after generated.

It is not ESP8266WebServer::on, not WebServer::on for ESP32.

This function effects to AutoConnectAux only. However, it coexists with that of ESP8266WebServer::on or WebServer::on of ESP32.

onDetect

void onDetect(DetectExit_ft fn)
Register the function which will call from AutoConnect at the start of the captive portal.
Parameter
fnFunction called at the captive portal start.

An fn specifies the function called when the captive portal starts. Its prototype declaration is defined as "DetectExit_ft".

typedef std::function<bool(IPAddress softapIP)>  DetectExit_ft
Parameter
softapIPAn IP address of SoftAP for the captive portal.
Return value
trueContinues captive portal handling.
falseCancel the captive portal. AutoConnect::begin function will return with a false.

onNotFound

  • For ESP8266
void onNotFound(ESP8266WebServer::THandlerFunction fn)
  • For ESP32

void onNotFound(WebServer::THandlerFunction fn)
Register the handler function for undefined URL request detected.
Parameter
fnA function of the "not found" handler.

where

String where(void)
Returns an uri string of the AutoConnectAux uri object of the custom Web page that caused the request to the page.
AutoConnect identifies the URI (ie. the referrer URI) that caused the request each time from the client occurs and will save the URI If the request source is a custom Web page of AutoConnectAux. The where function returns a pointer of AutoConnectAux which is a URI of a least recent request from the custom Web page.
This function is provided to access the fields (ie. the AutoConnectElements) with a custom Web page handler of a page and is available only for request source that is the custom Web pages. It is invalid for HTTP requests from individual pages registered with the on handler of ESP8266WebServer/WebServer for ESP32. In other words, this function only returns the AutoConnecAux page which is a least recently displayed.
Return value
An uri string of the AutoConnectAux that caused the request the page.

The where function usage is described in the section Where to pick up the values.