Skip to content

Advanced usage

Advanced usage

404 handler

Registering the "not found" handler is a different way than ESP8266WebServer. The onNotFound of ESP8266WebServer does not work with AutoConnect. AutoConnect overrides ESP8266WebServer::onNotFound to handle a captive portal. To register "not found" handler, use AutoConnect::onNotFound.

Auto save Credential

By default, AutoConnect saves the credentials of the established connection in EEPROM. You can disable it with the autoSave parameter specified by AutoConnectConfig.

AutoConnect       Portal;
AutoConnectConfig Config;
Config.autoSave = AC_SAVECREDENTIAL_NEVER;
Portal.config(Config);
Portal.begin();

Captive portal start detection

The captive portal will only be activated if the first WiFi::begin fails. Sketch can detect with the AutoConnect::onDetect funciton that the captive portal has started. For example, the sketch can be written like as follows that turns on the LED at the start captive portal.

AutoConnect Portal;

bool startCP(IPAddress ip) {
  digitalWrite(BUILTIN_LED, HIGH);
  Serial.println("C.P. started, IP:" + WiFi.localIP().toString());
  return true;
}

void setup() {
  Serial.begin(115200);
  pinMode(BUILTIN_LED, output);
  digitalWrite(BUILTIN_LED, LOW);
  Portal.onDetect(startCP);
  if (Portal.begin()) {
    digitalWrite(BUILTIN_LED, LOW);
  }
}

void loop() {
  Portal.handleClient();
}

Combination with mDNS

With mDNS library, you can access to ESP8266 by name instead of IP address after connection. The sketch can start the MDNS responder after AutoConnect::begin.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
AutoConnect Portal;

void setup() {
  if (Portal.begin()) {
    if (MDNS.begin("esp8266")) {
      MDNS.addService("http", "tcp", 80);
    }
  }
}

void loop() {
  Portal.handleClient();
}

Debug print

You can output AutoConnect monitor messages to the Serial. A monitor message activation switch is in an include header file AutoConnect.h of library source. Define AC_DEBUG macro to output monitor messages.

#define AC_DEBUG

Disable the captive portal

It can also prevent the captive portal from starting even if the connection at the first WiFi.begin fails. In this case, the behavior of AutoConnect::begin is the same as WiFi.begin.

For disabling the captive portal, autoRise sets to false with AutoConnectConfig.

AutoConnect       portal;
AutoConnectConfig acConfig;

acConfig.autoRaise = false;
portal.config(acConfig);
portal.begin();

Move the saving area of EEPROM for the credentials

By default, the credentials saving area is occupied from the beginning of EEPROM. ESP8266 Arduino core document says that:

The following diagram illustrates flash layout used in Arduino environment:


  |--------------|-------|---------------|--|--|--|--|--|
  ^              ^       ^               ^     ^
  Sketch    OTA update   File system   EEPROM  WiFi config (SDK)

and

EEPROM library uses one sector of flash located just after the SPIFFS.

So in the default state, the credential storage area used by AutoConnect conflicts with data area owned by the user sketch. It will be destroyed together saved data in EEPROM by user sketch and AutoConnect each other. But you can move the storage area to avoid this.

The boundaryOffset in AutoConnectConfig specifies the start offset of the credentials storage area. The default value is 0.

Refers the hosted ESP8266WebServer

Constructing an AutoConnect object variable without parameters then creates and starts an ESP8266WebServer inside the AutoConnect. This object variable could be referred by AutoConnect::host() function to access ESP8266WebServer instance as like below.

AutoConnect Portal;

Portal.begin();
ESP8266WebServer& server = Portal.host();
server.send(200, "text/plain", "Hello, world");

When host() is valid

The host() can be referred at after AutoConnect::begin.

Usage for automatically instantiated ESP8266WebServer

The sketch can handle URL requests using ESP8266WebServer that AutoConnect started internally. ESP8266WebServer instantiated dynamically by AutoConnect can be referred to by AutoConnect::host function. The sketch can use the 'on' function, 'send' function, 'client' function and others by ESP8266WebServer reference of its return value.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <AutoConnect.h>

AutoConnect       Portal;

void handleRoot() {
  ESP8266WebServer& IntServer = Portal.host();
  IntServer.send(200, "text/html", "Hello, world");
}

void handleNotFound() {
  ESP8266WebServer& IntServer = Portal.host();
  IntServer.send(404, "text/html", "Unknown.");
}

void setup() {
  bool r = Portal.begin();
  if (r) {
    ESP8266WebServer& IntServer = Portal.host();
    IntServer.on("/", handleRoot);
    Portal.onNotFound(handleNotFound);    // For only onNotFound.
  }
}

void loop() {
  Portal.host().handleClient();
  Portal.handleRequest();
  /* or following one line code is equ.
  Portal.handleClient();
  */
}

ESP8266WebServer function should be called after AutoConnect::begin

The sketch cannot refer to an instance of ESP8266WebServer until AutoConnect::begin completes successfully.

Do not use with ESP8266WebServer::begin

ESP8266WebServer is already running inside the AutoConnect.

Use with the PageBuilder library

In ordinary, the URL handler will respond the request by sending some HTML. PageBuilder library is HTML assembly aid. it can handle predefined HTML as like a template and simplify an HTML string assemble logic, and also the generated HTML send automatically.

An example sketch used with the PageBuilder as follows and it explains how it aids for the HTML generating. Details for Github repository.

Configuration functions

Configuration for Soft AP

AutoConnect will activate SoftAP at failed initial WiFi.Begin. It SoftAP settings are stored in AutoConnectConfig as the following parameters. The sketch could be configured SoftAP using these parameters, refer the AutoConnectConfig API for details.

  • IP address of SoftAP activated.
  • Gateway IP address.
  • Subnet mask.
  • SSID for SoftAP.
  • Password for SoftAP.
  • Channel.
  • Hidden attribute.
  • Auto save credential.
  • Offset address of the credentials storage area in EEPROM.
  • Length of start up time after reset.
  • Automatic starting the captive portal.
  • Auto reset after connection establishment.
  • Home URL of the user sketch application.

AutoConnect::config before AutoConnect::begin

AutoConnect::config must be executed before AutoConnect::begin.

Assign user sketch's home path

"HOME" for returning to the user's sketch homepage is displayed at the bottom of the AutoConnect menu. It could be set using the AutoConnect::home function.

Relocate the AutoConnect home path

A home path of AutoConnect is _ac by default. You can access from the browser with http://IPADDRESS/_ac. You can change the home path by revising AUTOCONNECT_URI macro in the include header file as AutoConnect.h.

#define AUTOCONNECT_URI         "/_ac"