diff --git a/mkdocs/advancedusage.md b/mkdocs/advancedusage.md index 79cc4b5..f905e4b 100644 --- a/mkdocs/advancedusage.md +++ b/mkdocs/advancedusage.md @@ -214,63 +214,79 @@ void loop() { } ``` -### Casts the HTML pages to be add-on into the menu +### Capture the legacy web pages as items into the menu -If your sketch handles web pages, you can embed the pages into the AutoConnect menu in continuance enjoying the utility of the WiFi connection feature. Unlike the custom Web pages by [AutoConnectElements](acelements.md), this allows to legacy web pages registered by *ESP8266WebServer::on* or *WebServer::on* function. +You can embed the ordinary page processed by the ESP8266WebServer request handler as an item into the AutoConnect menu. AutoConnect allows capturing the legacy web pages for ESP8266WebServer or WebServer of ESP32 and extends the menu containing these items, also Sketch can embed the legacy pages using the [*AutoConnect::append*](api.md#append) function. -To implement embedding your legacy web pages to the AutoConnect menu, you can use AutoConnectAux only constructed with the URI of the page to be embedding. AutoConnectElements is not required. The basic procedure for this as follows: +To process a web page using the ESP8266WebServer class (The WebServer class for ESP32), you usually register a handler with URI by the `on` function. The [*AutoConnect::append*](api.md#append) function creates internally an [**AutoConnectAux**](acintro.md) depended on its URI and integrates into the menu. -1. Declare AutoConnectAux for each legacy page. It includes the URI of the page and item string which will display in the AutoConnect menu. -2. Sketch the legacy page handlers. -3. Register those handler functions to ESP8266WebServer/WebServer with the **on** function. -4. Register AutoConnectAux declared with #1 to AutoConnect using [*AutoConnect::join*](api.md#join) function. It serves as a menu item. -5. [Begin](api.md#begin) the portal. -6. Performs [*AutoConnect::handleClient*](api.md#handleClient) in the **loop** function. +The following code has a mixture of both AutoConnectAux and the legacy web page. An AutoConnectAux page is menued automatically with the [*AutoConnect::join*](api.md#join) or [*AutoConnect::load*](api.md#load) function, and a legacy page is integrated by the [*AutoConnect::append*](api.md#append) function. -```cpp hl_lines="10 28 32" +```cpp hl_lines="26 35" #include #include #include -ESP8266WebServer server; - -// Declaration for casting legacy page to AutoConnect menu. -// Specifies an uri and the menu label. -AutoConnect portal(server); -AutoConnectAux hello("/hello", "Hello"); // Step #1 as the above procedure +ESP8266WebServer server; +AutoConnect portal(server); + +// Definitions of AutoConnectAux page +static const char PAGE[] PROGMEM = R"( +{ + "title": "PAGE", + "uri": "/page", + "menu": true, + "element": [ + { + "name": "cap", + "type": "ACText", + "value": "This is a custom web page." + } + ] +} +)"; -// Step #2 as the above procedure -// A conventional web page driven by the ESP8266WebServer::on handler. -// This is a legacy. -void handleHello() { - server.send(200, "text/html", String(F( +void setup() { + // The Web page handler located to /hello + server.on("/hello", [](){ + server.send(200, "text/html", String(F( "" "" -"Hello, world" +"

Hello, world

" "" - ))); -} + ))); + }); -void setup() { - // Step #3 as the above procedure - // Register the "on" handler as usual to ESP8266WebServer. - // Match this URI with the URI of AutoConnectAux to cast. - server.on("/hello", handleHello); - - // Step #4 as the above procedure - // Joins AutoConnectAux to cast the page via the handleRoot to AutoConnect. - portal.join({ hello }); - portal.begin(); // Step #5 as the above procedure + portal.append("/hello", "HELLO"); // Adds an item as HELLO into the menu + portal.load(FPSTR(PAGE)); // Load AutoConnectAux custom web page + portal.begin(); } void loop() { - portal.handleClient(); // Step #6 as the above procedure + portal.handleClient(); } + ``` - + -For more details, see section [Constructing the menu](menuize.md) of Examples page. +The [*AutoConnect::append*](api.md#append) function also has the third parameter that directly specifies the request handler. It has similar efficacy to calling the append and `ESP8266WebSever::on` at once. + +```cpp +portal.append("/hello", "HELLO", [](){ + server.send(200, "text/html", String(F( +"" +"" +"

Hello, world

" +"" + ))); +}); +``` + +For more details, see section [Attach the menu](menuize.md) of Examples page. + +!!! note "Necessary ESP8266WebServer/WebServer has materialized" + The WebServer must have instantiated for calling with a request handler parameter. AutoConnect can instantiate and host a WebServer internally, but in that case, the point in time to call the [AutoConnct::append](api.md#append) function with a request handler parameter must be after [AutoConnect::begin](api.md#begin). ### Change the menu labels @@ -357,7 +373,7 @@ Combining these two parameters allows you to filter the destination AP when mult Specified with the Sketch - Not efective + Not effective By AutoConnect::begin parameters Use the specified value of AutoConnectConfig @@ -384,6 +400,44 @@ You can output AutoConnect monitor messages to the **Serial**. A monitor message #define AC_DEBUG ``` +### Detect WiFi connection establishment with a router + +[*AutoConnect::onConnect*](api.md#onconnect) allows the Sketch to detect a WiFi connection to a router. The Sketch uses [*AutoConnect::onConnect*] to register a function to call when WiFi connected. +For example, as the following Sketch, this can be combined with [*AutoConnectConfig::retainPortal*](apiconfig.md#retainportal) to stop **SoftAP** in a **loop()**. It avoids blocking in the captive portal state by AutoConnect and allows the loop to run even without a WiFi connection. + +```cpp hl_lines="13 14 15 16 17 18 19" +#include +#include +#include + +AutoConnect Portal; +AutoConnectConfig config; + +void setup() { + Serial.begin(115200); + config.portalTimeout = 1; + config.retainPortal = true; + portal.config(config); + portal.onConnect([](IPAddress& ip){ + Serial.printf("Connected %s\n", ip.toString().c_str()); + if (WiFi.getMode() == WI_AP_STA) { + WiFi.softAPdisconnect(false); + WiFi.mode(WIFI_STA); + } + }); + portal.begin(); +} + +void loop() { + // Here, the Sketch can execute without WiFi connection. + // It avoids blocking the state by the captive portal even if the captive portal is available. + portal.handleClient(); +} +``` + +!!! note "It is not an event" + AutoConnect::onConnect has the same effect on the Sketch as the [WiFi.onStationModeConnected](https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-class.html#onevent), but AutoConnect does not use the event. Sketch can use `WiFi.onEvent` independently of AutoConnect. + ### Disable the captive portal It can also prevent the captive portal from starting even if the connection at the 1st-WiFi.begin fails. In this case, [*AutoConnect::begin*](api.md#begin) behaves same as *WiFi.begin*. @@ -519,7 +573,7 @@ Here section describes how to launch on demand the captive portal, and suggests portal = nullptr; } } - // Here, ordinally sketch logic. + // Here, ordinary sketch logic. } ``` @@ -638,7 +692,7 @@ The Sketch HOME path is closely related to the [bootUri](apiconfig.md#booturi) t AutoConnect features a built-in OTA function to update ESP module firmware. You can easily make the Sketch that equips OTA and able to operate with the AutoConnect menu. - + [*AutoConnectConfig::ota*](apiconfig.md#ota) specifies to import the [built-in OTA update class](otabrowser.md) into the Sketch. See the [Updates with the Web Browser](otabrowser.md) chapter for details. @@ -684,7 +738,7 @@ Also, you can specify the SSID, password for SoftAP with the constructor of the ```cpp hl_lines="2" AutoConnect portal; -AutoConnectConfig config("ap_portal", "new_passwrod"); +AutoConnectConfig config("ap_portal", "new_password"); void setup() { portal.config(config); diff --git a/mkdocs/api.md b/mkdocs/api.md index d33d485..cfb688b 100644 --- a/mkdocs/api.md +++ b/mkdocs/api.md @@ -68,6 +68,40 @@ The [**handleClient**](api.md#handleclient) function of AutoConnect can include ## Public member functions +### append + +- ESP8266/ESP32 Common + +```cpp +AutoConnectAux* append(const String& uri, const String& title) +``` + +- For ESP8266 + +```cpp +AutoConnectAux* append(const String& uri, const String& title, ESP8266WebServer::THandlerFunction handler) +``` + +- For ESP32 + +```cpp +AutoConnectAux* append(const String& uri, const String& title, WebServer::THandlerFunction handler) +``` +Creates an AutoConnectAux dynamically with the specified URI and integrates it into the menu. Calls with a request handler parameter can use this function as menu registration for a legacy page of ESP8266WebServer/WebServer. If the **handler** parameter specified, also it will register the request handler for the ESP8266WebServer/WebServer. +AutoConnect manages the menu items using a sequence list, and this function always adds the item to the end of the list. Therefore, the order of the menu items is the additional order. +Returns the pointer to created AutoConnectAux instance, the `nullptr` if an AutoConnectAux with the same URI already exists. +
+
**Parameter**
+
uriA string of the URI.
+
titleTitle for menu item.
+
handlerRequest handler function as type of **ESP8266WebServer::THandlerFunction**/**WebServer::THandlerFunction**.
+
**Return value**
+
A Pointer to a created AutoConnectAux instance.
+
+ +!!! note "Necessary ESP8266WebServer/WebServer has materialized" + The WebServer must have instantiated for calling with a request handler parameter. AutoConnect can instantiate and host a WebServer internally, but in that case, the point in time to call the append function with a request handler parameter must be after AutoConnect::begin. + ### aux ```cpp @@ -126,13 +160,42 @@ Set SoftAP's WiFi configuration and static IP configuration.
falseConfiguration parameter is invalid, some values out of range.
+### detach +```cpp +AutoConnectAux* detach(const String& uri) +``` +Detach the AutoConnectAux with the specified URI from the management of AutoConnect. An unmanaged AutoConnectAux will no longer appear in menu items, and its page handler will no longer respond even if the URI is accessed directly. +
+
**Parameter**
+
uriURI of AutoConnectAux to be detached.
+
+ +If the request handler registered in the detaching AutoConnectAux is for a legacy page of the ESP8266WebServer/WebServer, the URI is still valid after detaching. AutoConnect does not delete the request handler registered to ESP8266WebServer/WebServer with the `on` function. (It cannot be removed) + +!!! hint "Deleting the AutoConnectAux" + You can use the return value from the AotoConnect::detach to delete the AutoConnectAux instance which dynamically created with the [AutoConnect::append](api.md#append). + +### 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. +This function only works for AutoConnect primary menu items. It has no effect on disable for AutoConnectAux items. To disable the items by AutoConnectAux, use the [AutoConnectAux::menu](apiaux.md#menu) function. +
+
**Parameter**
+
itemsSpecify 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.
+
+ ### 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. +Enable the [AutoConnect menu](menu.md) items specified by the items parameter with logical OR value using **AC_MENUITEM_t** constant. +This function only works for AutoConnect primary menu items. It has no effect on enable for AutoConnectAux items. To enable the items by AutoConnectAux, use the [AutoConnectAux::menu](apiaux.md#menu) function.
**Parameter**
itemsSpecify 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.
@@ -166,18 +229,6 @@ Stops AutoConnect captive portal service. Release ESP8266WebServer/WebServer and !!! warning "Attention to end" The end function releases the instance of ESP8266WebServer/WebServer and DNSServer. It can not process them after the end function. -### 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. -
-
**Parameter**
-
itemsSpecify 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.
-
- ### getEEPROMUsedSize ```cpp @@ -314,6 +365,28 @@ Register the handler function of the AutoConnectAux. !!! 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. +### onConnct + +```cpp +void onConnect(ConnectExit_ft fn) +``` +Register the function which will call from AutoConnect at the WiFi connection established. +
+
**Parameter**
+
fnFunction called at the WiFi connected.
+
+ +An *fn* specifies the function called when the WiFi connected. Its prototype declaration is defined as "*ConnectExit_ft*". + +```cpp +typedef std::function ConnectExit_ft; +``` +
+
**Parameter**
+
localIPAn IP address of the ESP module as STA.
+ +
+ ### onDetect ```cpp diff --git a/mkdocs/images/addmenu.gif b/mkdocs/images/addmenu.gif new file mode 100644 index 0000000..def9e74 Binary files /dev/null and b/mkdocs/images/addmenu.gif differ diff --git a/mkdocs/images/castmenu.png b/mkdocs/images/castmenu.png deleted file mode 100644 index 1110395..0000000 Binary files a/mkdocs/images/castmenu.png and /dev/null differ diff --git a/mkdocs/menu.md b/mkdocs/menu.md index b4e438d..5e86bb0 100644 --- a/mkdocs/menu.md +++ b/mkdocs/menu.md @@ -123,4 +123,4 @@ The AutoConnect menu can contain your sketch's web pages as extra items as a cus
-You can improve your sketches by extending the AutoConnect menu by adding the legacy web pages according to the procedure described in section [*Advanced Usage*](advancedusage.md#casts-the-html-pages-to-be-add-on-into-the-menu). +AutoConnect allows capturing the extra pages handled with ESP8266WebServer or WebServer's legacy into the AutoConnect menu. See Section [*Advanced Usage*](advancedusage.md#capture-the-legacy-web-pages-as-items-into-the-menu) for detailed instructions on how to add the extra pages into its menu.