Skip to content

AutoConnectAux API

Only for AutoConnect

The following AutoConnectAux API are valid only for AutoConnect; they are not available for AutoConnectCore.

Constructor

AutoConnectAux

AutoConnectAux(const String& uri = String(""), const String& title = String(""), const bool menu = true, const AutoConnectElementVT addons = AutoConnectElementVT(), const bool responsive = true, const bool CORS = false)
Parameters
uriURI of this custom Web Page.
titlePage title of this custom Web page. It will appear on the auto connection menu and at the top of that page.
menuSpecifies whether to display this page on menu.
addonsReference to AutoConnectElement collection.
responsiveSpecifies whether to make HTTP response or not.
CORSInclude Access-Control-Allow-Origin:* in the HTTP response headers of the custom web page. This indicates that the response can be shared.

Public member functions

operator [ ]

AutoConnectElement& operator[](const char* name)

AutoConnectElement& operator[](const __FlashStringHelper* name)

AutoConnectElement& operator[](const String& name)
Returns a reference to the element specified by name. An operator [] is a shortcut for getElement function with the reference casting. Unlike getElement, which returns a pointer to that element, an operator [] returns a reference to that element. You also need to cast the return value to the actual type, just like the getElement function.
Parameter
nameName of the AutoConnectElements to be retrieved.
Return value
A reference to AutoConnectElement. It is different from the actual element type.

add

void add(AutoConnectElement& addon)

void add(AutoConnectElementVT addons)

Add an element to the AutoConnectAux. An added element is displayed on the custom Web page invoked from the AutoConnect menu.

Parameters
addonReference of AutoConnectElements. Specifies one of the AutoConnectElements classes.
addonsAn array list of reference of AutoConnectElements. The list initialization with braced-init-list of the std::vector can be used for the addons parameter cause the actual definition of type AutoConnectElementVT is std::vector<std::reference_wrapper<AutoConnectElement>>.

authentication

void authentication(const AC_AUTH_t auth)

Set to require authentication with access to a page. When you access a page that requires authentication, HTTP authentication will be performed according to the scheme specified with the auth parameter.

Parameters
authSpecifies authentication scheme with the following enumeration value.

  • AC_AUTH_BASIC : Basic scheme.
  • AC_AUTH_DIGEST : Digest scheme.

content

size_t content(void)

Returns the number of AutoConnectElements the AutoConnectAux contains.

Return value
A number of the registered AutoConnectElements.

fetchElement

void fetchElement(void)
Retrieve the values of the AutoConnectElements on the custom Web page. Refer to how to use the fetchElement.

getElement

T& getElement<T>(const String& name)

AutoConnectElement* getElement(const char* name)

AutoConnectElement* getElement(const __FlashStringHelper* name)

AutoConnectElement* getElement(const String& name)

Get a registered AutoConnectElement as specified name. If T is specified as an actual type of AutoConnectElements, it returns a reference to that instance.

Parameters
TActual type name of AutoConnectElements as AutoConnectButton, AutoConnectCheckbox, AutoConnectElement, AutoConnectFile, AutoConnectInput, AutoConnectRadio, AutoConnectSelect, AutoConnectSubmit, AutoConnectText.
nameName of the AutoConnectElements to be retrieved.
Return value
A reference of the AutoConnectElements. If a type is not specified returns a pointer.

getElements

AutoConnectElementVT& getElements(void)

Get vector of reference of all elements.

Return value
A reference to std::vector of reference to AutoConnecctElements.

The getElements returns a reference to std::vector of reference to AutoConnecctElements. This function is provided to handle AutoConnectElemets owned by AutoConnectAux in bulk, and you can use each method of std::vector for a return value.

// An example of getting type and name of all AutoConnectElements registered in AutoConnectAux.
AutoConnectAux aux;
// some code here...
AutoConnectElementVt& elements = aux.getElements();
for (AutoConnectElement& elm : elements) {
    Serial.printf("name<%s> as type %d\n", elm.name.c_str(), (int)elm.typeOf());
}

isMenu

bool isMenu(void)

Returns whether embedded in the menu or not. The isMenu is a function that complements the menu function.

Return value
trueThe custom Web page has been incorporated into the AutoConnect menu as a menu item.
falseThis custom Web page is not currently a menu item.

isValid

bool isValid(void)

Performs validation of all AutoConnectInput elements owned by AutoConnectAux and returns the result. The isValid function will return the true even if the AutoConnectAux does not own AutoConnectInputs.

Return value
trueValidation is successful. A value of all AutoConnectInputs match with each pattern.
falseSome elements failed validation.

load

bool load(const String& in)

bool load(PGM_P in)

bool load(const __FlashStringHelper* in)

bool load(Stream& in)

Load all AutoConnectElements elements from JSON document into AutoConnectAux as custom Web pages. The JSON document specified by the load function must be the document structure of AutoConnectAux. Its JSON document can describe multiple pages as an array.

Parameter
inSpecifies the JSON document to be load. The load function can input the following objects.

  • String : Read-only String
  • PROGMEM : Character array contained in the flash
  • Stream : An entity that inherits stream class, generally SPIFFS or SD.
Return value
trueJSON document as the custom Web pages successfully loaded.
falseJSON document loading failed.

Load multiple custom Web pages separately

Multiple custom Web pages can be loaded at once with JSON as an array. But it will consume a lot of memory. By loading a JSON document by page as much as possible, you can reduce memory consumption.

loadElement

bool loadElement(const String& in, const String& name = String(""))

bool loadElement(const String& in, std::vector<String> const& names)

bool loadElement(PGM_P in, const String& name = String(""))

bool loadElement(PGM_P in, std::vector<String> const& names)

bool loadElement(const __FlashStringHelper* in, const String& name = String(""))

bool loadElement(const __FlashStringHelper* in, std::vector<String> const& names)

bool loadElement(Stream& in, const String& name = String(""))

bool loadElement(Stream& in, std::vector<String> const& names)

Load specified element from JSON document into AutoConnectAux. The JSON document specified by the loadElement function must be the AutoConnectElement document structure. When loading from a JSON document that describes multiple elements, its description must be an array syntax.

Parameters
inSpecifies the JSON document to be load. The load function can input the following objects.

  • String : Read-only String
  • PROGMEM : Character array contained in the flash
  • Stream : An entity that inherits stream class, generally SPIFFS or SD.
nameSpecifies the name to be load. If the name is not specified, the loadElement function will load all elements contained in the JSON document.
names Specifies an array list of String indicating the name of the element to be loaded. The list initialization with braced-init-list of the std::vector can be used.
Return value
trueSpecified AutoConnectElements successfully loaded.
falseJSON document loading failed.

Maybe it is an array

Please note that the JSON document that is the input for loadElement is an array syntax of AutoConnectElements when there are multiple elements. For example, the following JSON document has a syntax error:

{
  "name": "Caption",
  "type": "ACText",
  "value": "Hello, world"
},
{
  "name": "Server",
  "type": "ACInput",
  "label": "Server address"
}
The outermost [, ] is missing.

void menu(const bool post)

Set or reset the display as menu item for this AutoConnectAux. This function programmatically manipulates the menu parameter of the AutoConnectAux constructor.

Parameter
trueShow on the menu.
falseHidden on the menu.

AutoConnectAux::menu and isMenu have no effect on AutoConnect built-in menu items

Some of AutoConnect's built-in pages make use of AutoConnectAux class. You can use the AutoConnect::aux or AutoConnect::locate function with those URLs to retrieve the AutoConnectAux for built-in pages, but the menu function does not show/hide the built-in items of the menu list.
Also, it is not recommended to use AutoConnect::aux or locate functions to get AutoConnect's built-in pages. Instructions on how to show/hide AutoConnect's built-in menu items can be found in the Applying the active menu items section.

on

void on(const AuxHandlerFunctionT handler, const AutoConnectExitOrder_t order = AC_EXIT_AHEAD)
Register the handler function of the AutoConnectAux.
Parameters
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.

onUpload

void onUpload<T&>(T handler)

void onUpload(PageBuilder::UploadFuncT uploadFunc)

Register the upload handler of the AutoConnectAux.

Parameters
TSpecifies a class name of the custom uploader inherited from AutoConnectUploadHandler class. Refer to the appendix for details.
handlerSpecifies the custom uploader inherited from AutoConnectUploadHandler class. Refer to the appendix for details.
uploadFuncA function that behaves when request to upload with the AutoConnectAux page. UploadFuncT type is defined by the following declaration.

void(const String&, const HTTPUpload&)

A data structure of the upload file as HTTPUpload. It is defined in the ESP8266WebServer (the WebServer for ESP32) library as follows:

typedef struct {
  HTTPUploadStatus status;
  String  filename;
  String  name;
  String  type;
  size_t  totalSize;
  size_t  currentSize;
  size_t  contentLength;
  uint8_t buf[HTTP_UPLOAD_BUFLEN];
} HTTPUpload;

Refer to 'To upload to a device other than Flash or SD' in section appendix for details.

redirect

void redirect(const char* url)

Generate a Location header field with the specified url and responds with a 302 response code to the client. This function is intended to be used from within the Custom Web Page handler. If the AutoConnectAux is going to redirect to another page without responding with page content, declare the responsive argument false in the AutoConnectAux constructor. With this construction, AutoConnectAux will not respond to HTTP responses. The redirect function can be useful in this situation to respond to a 302 redirect.

Parameter
urlSpecifies the URL to redirect a page to.

referer

AutoConnectAux& referer(void)

Returns a reference to the AutoConnectAux from which this AutoConnectAux was called.

Return value
A reference to the AutoConnectAux from which this AutoConnectAux was called. If the source of the transition is not an AutoConnectAux page, it returns a reference to itself.

release

bool release(const String& name)

Release a specified AutoConnectElement from AutoConnectAux. The release function is provided to dynamically change the structure of the custom Web pages with the Sketch. By combining the release function and the add function or the loadElement function, the Sketch can change the style of the custom Web page according to its behavior.

Parameter
nameSpecifies the name of AutoConnectElements to be released.
Return value
trueThe AutoConnectElement successfully released.
falseThe AutoConnectElement can not be released.

saveElement

size_t saveElement(Stream& out, std::vector<String> const& names = {})

Write elements of AutoConnectAux to the stream. The saveElement function outputs the specified AutoConnectElements as a JSON document using the prettyPrintTo function of the ArduinoJson library.

Parameters
outOutput stream to be output. SPIFFS, SD also Serial can be specified generally.
namesThe array of the name of AutoConnectElements to be output. If the names parameter is not specified, all AutoConnectElements registered in AutoConnectAux are output.
Return value
The number of bytes written.

The output format is pretty

The saveElement function outputs a prettified JSON document.

It is not complementary with loadElement

The saveElement function which missing the names parameter without name list to be saved that saves an entire AutoConnectAux element, not just AutoConnectElements. Its saved JSON document is not a complementary input to the loadElement function. The JSON document describing AutoConnectAux saved without the names parameter must be loaded by the AutoConnectAux::load function or AutoConnect::load function.

setElementValue

bool setElementValue(const String& name, const String value)

bool setElementValue(const String& name, std::vector<String> const& values)

Sets the value of the specified AutoConnectElement. If values is specified as a std::vector of String, the element that can set the values is the AutoConnectRadio or the AutoConnectSelect.

Parameters
nameSpecifies the name of the AutoConnectElements that you want to set the value.
valueSpecifies the value to be set.
valuesSpecifies a reference of a std::vector of String. It contains the values of the AutoConnectRadio or the AutoConnectSelect.
Return value
trueThe value has been set.
falseAutoConnectElements with the specified name are not registered. Or the type of the value is not consistent with the specified AutoConnectElements.

You can directly access the value member variable.

If you are gripping with the Sketch to the AutoConnectElements of the target that sets the value, you can access the value member variable directly. The following sketch code has the same effect.

AutoConnectAux aux;
// ... Griping the AutoConnectText here.
aux.setElementValue("TEXT_FIELD", "New value");
AutoConnectAux aux;
// ... Griping the AutoConnectText here.
AutoConnectText& text = aux.getElement<AutoConnectText>("TEXT_FIELD");
text.value = "New value";
The difference between the setElementValue and the value access with the getElement is the certainty of the registration state for the element. The getElement returns an empty object if the element is not registered then a sketch assigns the value to it. May occur unexpected results and crashes. You should use the setElementValue if its registration is unsettled.

setTitle

void setTitle(const String& title)

Set the title string of the custom Web page. This title will be displayed as the menu title of the custom Web page.

Parameter
titleTitle string to be display.

Not the menu title

The setTitle function is not set for the AutoConnect menu title. The effect of this function is that custom Web page only. To change the AutoConnect menu title use AutoConnectConfig::title.