You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AutoConnect/mkdocs/api.md

360 lines
15 KiB

## <i class="fa fa-code"></i> Include headers
7 years ago
### AutoConnect.h
7 years ago
```cpp
#include <AutoConnect.h>
```
## <i class="fa fa-code"></i> Defined macros
They contain in ```AutoConnectDefs.h```.
7 years ago
```cpp
#define AC_DEBUG // Monitor message output activation
#define AC_DEBUG_PORT Serial // Default message output device
#define AUTOCONNECT_AP_IP 0x011CD9AC // Default SoftAP IP
#define AUTOCONNECT_AP_GW 0x011CD9AC // Default SoftAP Gateway IP
7 years ago
#define AUTOCONNECT_AP_NM 0x00FFFFFF // Default subnet mask
#define AUTOCONNECT_DNSPORT 53 // Default DNS port at captive portal
#define AUTOCONNECT_HTTPPORT 80 // Default HTTP
7 years ago
#define AUTOCONNECT_MENU_TITLE "AutoConnect" // Default AutoConnect menu title
#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_STARTUPTIME 30 // Default waiting time[s] for after reset
#define AUTOCONNECT_USE_JSON // Allow AutoConnect elements to be handled by JSON format
#define AUTOCONNECT_USE_UPDATE // Indicator of whether to use the AutoConnectUpdate feature.
#define AUTOCONNECT_UPDATE_PORT 8000 // Available HTTP port number for the update
#define AUTOCONNECT_UPDATE_TIMEOUT 8000 // HTTP client timeout limitation for the update [ms]
#define AUTOCONNECT_TICKER_PORT LED_BUILTIN // Ticker port
#endif
7 years ago
```
!!! note "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```.
## <i class="fa fa-code"></i> Constructors
7 years ago
### AutoConnect
7 years ago
```cpp
AutoConnect()
```
<a id="withparameter"></a>
6 years ago
AutoConnect default constructor. This entry internally allocates the ESP8266WebServer for ESP8266 or WebServer for ESP32 and is activated internally.
- For ESP8266
7 years ago
```cpp
AutoConnect(ESP8266WebServer& webServer)
```
6 years ago
- For ESP32
```cpp
AutoConnect(WebServer& webServer)
```
Run the AutoConnect site using the externally ensured ESP8266WebServer for ESP8266 or WebServer for ESP32.
5 years ago
The [**handleClient**](api.md#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**](api.md#host) function.
7 years ago
<dl class="apidl">
6 years ago
<dt>**Parameter**</dt>
<dd><span class="apidef">webServer</span><span class="apidesc">A reference of ESP8266WebServer or WebServer instance.</span></dd>
7 years ago
</dl>
## <i class="fa fa-code"></i> Public member functions
7 years ago
### <i class="fa fa-caret-right"></i> aux
```cpp
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**.
<dl class="apidl">
6 years ago
<dt>**Parameter**</dt>
<dd><span class="apidef">uri</span><span class="apidesc">A string of the URI.</span></dd>
<dt>**Return value**</dt>
<dd>A Pointer of the AutoConnectAux instance.</dd>
</dl>
### <i class="fa fa-caret-right"></i> begin
7 years ago
```cpp
bool begin()
```
```cpp
6 years ago
bool begin(const char* ssid, const char* passphrase)
7 years ago
```
```cpp
6 years ago
bool begin(const char* ssid, const char* passphrase, unsigned long timeout)
7 years ago
```
6 years ago
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.
7 years ago
<dl class="apidl">
<dt>**Parameters**</dt>
<dd><span class="apidef">ssid</span><span class="apidesc">SSID to be connected.</span></dd>
<dd><span class="apidef">passphrase</span><span class="apidesc">Password for connection.</span></dd>
<dd><span class="apidef">timeout</span><span class="apidesc">A time out value in milliseconds for waiting connection.</span></dd>
7 years ago
<dt>**Return value**</dt>
<dd><span class="apidef">true</span><span class="apidesc">Connection established, AutoConnect service started with WIFI_STA mode.</span></dd>
<dd><span class="apidef">false</span><span class="apidesc">Could not connected, Captive portal started with WIFI_AP_STA mode.</span></dd>
7 years ago
</dl>
### <i class="fa fa-caret-right"></i> config
7 years ago
```cpp
bool config(AutoConnectConfig& config)
```
```cpp
bool config(const char* ap, const char* password = nullptr)
```
Set SoftAP's WiFi configuration and static IP configuration.
7 years ago
<dl class="apidl">
<dt>**Parameters**</dt>
<dd><span class="apidef">config</span><span class="apidesc">Reference to [**AutoConnectConfig**](apiconfig.md) containing SoftAP's parameters and static IP parameters.</span></dd>
<dd><span class="apidef">ap</span><span class="apidesc">SSID for SoftAP. The default value is **esp8266ap** for ESP8266, **esp32ap** for ESP32.</span></dd>
<dd><span class="apidef">password</span><span class="apidesc">Password for SodtAP. The default value is **12345678**.</span></dd>
7 years ago
<dt>**Return value**</dt>
<dd><span class="apidef">true</span><span class="apidesc">Successfully configured.</span></dd>
<dd><span class="apidef">false</span><span class="aidesc">Configuration parameter is invalid, some values out of range.</span></dd>
7 years ago
</dl>
### <i class="fa fa-caret-right"></i> enableMenu
```cpp
void enableMenu(const uint16_t items)
```
Enable the [AutoConnect menu](menu.md) items specified by the items parameter with logical OR value using **AC_MENUITEM_t** constant.
<dl class="apidl">
<dt>**Parameter**</dt>
<dd><span class="apidef">items</span><span class="apidesc">Specify the combined value of **AC_MENUITEM_t** of the items applying to the AutoConnect menu. It provides the value calculated from the **logical OR** by the AC_MENUITEM_t value of each item applied as a menu. AC_MENUITEM_t is enumeration type to identify each menu item and it has the below values.</span></dd>
: - **AC_MENUITEM_CONFIGNEW** :
[Configure new AP](menu.md#configure-new-ap)
: - **AC_MENUITEM_OPENSSIDS** :
[Open SSIDs](menu.md#open-ssids)
: - **AC_MENUITEM_DISCONNECT** :
[Disconnect](menu.md#disconnect)
: - **AC_MENUITEM_RESET** :
[Reset...](menu.md#reset)
: - **AC_MENUITEM_HOME** :
[HOME](menu.md#home)
: - **AC_MENUITEM_DEVINFO** :
[Device statistics](menu.md#where-the-from) as AutoConnect root page
</dl>
!!! note "It is added, not replaced."
The initial configuration of the AutoConnect menu items:
`AC_MENUITEM_CONFIGNEW | AC_MENUITEM_OPENSSIDS | AC_MENUITEM_DISCONNECT | AC_MENUITEM_RESET | AC_MENUITEM_HOME`
The enableMenu function adds an indication of the specified items to the current. Therefore, use the [disableMenu](#disableMenu) to remove the specified item from the initial menu.
### <i class="fa fa-caret-right"></i> end
7 years ago
```cpp
6 years ago
void end(void)
7 years ago
```
6 years ago
Stops AutoConnect captive portal service. Release ESP8266WebServer/WebServer and DNSServer.
7 years ago
!!! warning "Attention to end"
6 years ago
The end function releases the instance of ESP8266WebServer/WebServer and DNSServer. It can not process them after the end function.
7 years ago
### <i class="fa fa-caret-right"></i> disableMenu
```cpp
void disableMenu(const uint16_t items)
```
Disable the [AutoConnect menu](menu.md) items specified by the items parameter with logical OR value using **AC_MENUITEM_t** constant.
<dl class="apidl">
<dt>**Parameter**</dt>
<dd><span class="apidef">items</span><span class="apidesc">Specify the combined value of **AC_MENUITEM_t** of the items deleting from the AutoConnect menu. It provides the value calculated from the **logical OR** by the AC_MENUITEM_t value of each item. Refer to the [enableMenu](#enablemenu) about AC_MENUITEM_t.</span></dd>
</dl>
### <i class="fa fa-caret-right"></i> handleClient
7 years ago
```cpp
6 years ago
void handleClient(void)
7 years ago
```
6 years ago
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.
7 years ago
### <i class="fa fa-caret-right"></i> handleRequest
7 years ago
```cpp
6 years ago
void handleRequest(void)
7 years ago
```
Handling for the AutoConnect menu request.
!!! warning "About used in combination with handleClient"
6 years ago
The handleRequest function is not supposed to use with AutoConnect::handleClient. It should be used with ESP8266WebServer::handleClient or WebServer::handleClient.
7 years ago
### <i class="fa fa-caret-right"></i> home
7 years ago
```cpp
void home(String& uri)
7 years ago
```
Put a user site's home URI. The URI specified by home is linked from "HOME" in the AutoConnect menu.
7 years ago
<dl class="apidl">
6 years ago
<dt>**Parameter**</dt>
<dd><span class="apidef">uri</span><span class="aidesc">A URI string of user site's home path.</span></dd>
7 years ago
</dl>
### <i class="fa fa-caret-right"></i> host
6 years ago
- For ESP8266
7 years ago
```cpp
6 years ago
ESP8266WebServer& host(void)
7 years ago
```
6 years ago
- For ESP32
```cpp
6 years ago
WebServer& host(void)
6 years ago
```
Returns the reference of the ESP8266WebServer/WebServer which is allocated in AutoConnect automatically.
7 years ago
<dl class="apidl">
<dt>**Return value**</dt>
6 years ago
<dd>A reference of the ESP8266WebServer/WebServer.</dd>
7 years ago
</dl>
!!! note "&reference is not a pointer"
A reference cannot be re-assigned, and must be assigned at initialization. It's like as bind as alias.
```cpp
ESP8266WebServer& server = portal.host();
server.handleClient();
```
or
```cpp
portal.host().handleClient();
```
### <i class="fa fa-caret-right"></i> join
```cpp
void join(AutoConnectAux& aux)
```
```cpp
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.
<dl class="apidl">
6 years ago
<dt>**Parameter**</dt>
<dd><span class="apidef">aux</span><span class="apidesc">Reference to AutoConnectAux. It can be std::vector of std::reference_wrapper of AutoConnectAux with [list initialization](https://en.cppreference.com/w/cpp/language/list_initialization).</span></dd>
</dl>
### <i class="fa fa-caret-right"></i> load
```cpp
bool load(const String& aux)
```
```cpp
bool load(PGM_P aux)
```
```cpp
bool load(const __FlashStringHelper* aux)
```
```cpp
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.
<dl class="apidl">
6 years ago
<dt>**Parameter**</dt>
<dd><span class="apidef">aux</span><span class="apidesc">The input string to be loaded.</span></dd>
<dt>**Return value**</dt>
<dd><span class="apidef">true</span><span class="apidesc">The JSON document as AutoConnectAux successfully loaded.</span></dd>
<dd><span class="apidef">false</span><span class="apidesc">Loading JSON document unsuccessful, probably syntax errors have occurred or insufficient memory. You can diagnose the cause of loading failure using the [ArduinoJson Assistant](https://arduinojson.org/v5/assistant/).</span></dd>
</dl>
### <i class="fa fa-caret-right"></i> on
```cpp
bool on(const String& uri, const AuxHandlerFunctionT handler, AutoConnectExitOrder_t order = AC_EXIT_AHEAD)
```
Register the handler function of the AutoConnectAux.
<dl class="apidl">
<dt>**Parameters**</dt>
<dd><span class="apidef">uri</span><span class="apidesc">A string of the URI assigned to the AutoConnectAux page.</span></dd>
<dd><span class="apidef">handler</span><span class="apidesc">A function that behaves when a request to the AutoConnectAux page occurs. AuxHandlerFunctionT type is defined by the following declaration.<p class=""apidesc">`String handler(AutoConnectAux&, PageArgument&)`</p></span></dd>
<dd><span class="apidef">order</span><span class="apidesc">Specifies when the handler is called with the following enumeration value.</span></dd>
: - **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.
</dl>
!!! caution "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.
### <i class="fa fa-caret-right"></i> onDetect
7 years ago
```cpp
void onDetect(DetectExit_ft fn)
```
Register the function which will call from AutoConnect at the start of the captive portal.
<dl class="apidl">
6 years ago
<dt>**Parameter**</dt>
<dd><span class="apidef">fn</span><span class="apidesc">Function called at the captive portal start.</span></dd>
7 years ago
</dl>
An *fn* specifies the function called when the captive portal starts. Its prototype declaration is defined as "*DetectExit_ft*".
7 years ago
```cpp
typedef std::function<bool(IPAddress softapIP)> DetectExit_ft
```
<dl class="apidl">
6 years ago
<dt>**Parameter**</dt>
<dd><span class="apidef">softapIP</span><span class="apidesc">An IP address of SoftAP for the captive portal.</span></dd>
6 years ago
<dt>**Return value**</dt>
<dd><span class="apidef">true</span><span class="apidesc">Continues captive portal handling.</span></dd>
<dd><span class="apidef">false</span><span class="apidesc">Cancel the captive portal. AutoConnect::begin function will return with a false.</span></dd>
7 years ago
</dl>
### <i class="fa fa-caret-right"></i> onNotFound
7 years ago
6 years ago
- For ESP8266
7 years ago
```cpp
void onNotFound(ESP8266WebServer::THandlerFunction fn)
```
6 years ago
- For ESP32
```cpp
void onNotFound(WebServer::THandlerFunction fn)
```
7 years ago
Register the handler function for undefined URL request detected.
<dl class="apidl">
6 years ago
<dt>**Parameter**</dt>
<dd><span class="apidef">fn</span><span class="apidesc">A function of the "not found" handler.</span></dd>
7 years ago
</dl>
### <i class="fa fa-caret-right"></i> where
```cpp
String where(void)
```
Returns an uri string of the AutoConnectAux uri object of the custom Web page that caused the request to the page.<br>
6 years ago
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.<br>
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.
<dl class="apidl">
6 years ago
<dt>**Return value**</dt>
<dd>An uri string of the AutoConnectAux that caused the request the page.</dd>
</dl>
6 years ago
The **where** function usage is described in the section [*Where to pick up the values*](achandling.md#where-to-pick-up-the-values).