diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..bfd3ae4
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+# Files generated by build
+/site
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b6e9a73
--- /dev/null
+++ b/README.md
@@ -0,0 +1,69 @@
+# AutoConnect for ESP8266
+
+An Arduino library for ESP8266 WLAN configuration at run time with web interface. [![Build Status](https://travis-ci.org/Hieromon/AutoConnect.svg?branch=master)](https://travis-ci.org/Hieromon/AutoConnect)
+
+## Overview
+
+To the dynamic configuration for joining to WLAN with SSID and PSK accordingly. It an Arduino library united with *ESP8266WebServer* class.
+Easily implementing the Web interface constituting the WLAN for ESP8266 WiFi connection. With this library to make a sketch easily which connects from ESP8266 to the access point at runtime by the web interface without hard-coded SSID and password.
+
+
+
+### No need pre-coded SSID & password
+
+It is no needed hard-coding in advance the SSID and Password into the sketch to connect between ESP8266 and WLAN. You can input SSID & Password from a smartphone via the web interface at runtime.
+
+### Simple usage
+
+AutoConnect control screen will be displayed automatically for establishing new connections. It aids by the captive portal when vested the connection cannot be detected.
By using the **AutoConnect menu**, to manage the connections convenient.
+
+### Store the established connection
+
+The connection authentication data as credentials are saved automatically in EEPROM of ESP8266 and You can select the past SSID from the [AutoConnect menu](menu.md).
+
+### Easy to embed in
+
+AutoConnect can be embedded easily into your sketch, just "**begin**" and "**handleClient**".
+
+### Lives with the your sketches
+
+The sketches which provide the web page using ESP8266WebServer there is, AutoConnect will not disturb it. AutoConnect can use an already instantiated ESP8266WebServer object, or itself can assign it.
+
+## Supported hardware
+
+Apply the [Arduino core](https://github.com/esp8266/Arduino) of the ESP8266 Community.
+
+- Generic ESP8266 modules
+- Adafruit HUZZAH ESP8266 (ESP-12)
+- ESP-WROOM-02
+- Heltec WiFi Kit 8
+- NodeMCU 0.9 (ESP-12) / NodeMCU 1.0 (ESP-12E)
+- Olimex MOD-WIFI-ESP8266
+- SparkFun Thing
+- SweetPea ESP-210
+
+## Simple usage
+
+### The AutoConnect menu
+
+
+
+### How embed the AutoConnect to the sketches you have
+
+Most simple approach to applying AutoConnect for the existing sketches, follow the below steps.
+
+
+
+## More usages and Documentation
+
+The documentation is available on https://Hieromon.github.io/AutoConnect, here are some shortcuts:
+
+- Installation.
+- Getting started.
+- Usage the Library.
+- API reference.
+- FAQ.
+
+## License
+
+License under the [MIT license](LICENSE).
\ No newline at end of file
diff --git a/docs/404.html b/docs/404.html
new file mode 100644
index 0000000..e7c904d
--- /dev/null
+++ b/docs/404.html
@@ -0,0 +1,435 @@
+
+
+
+
+
+
+
#include <AutoConnect.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_MENU_TITLE "AutoConnect" // Default AutoConnect menu title +#define AUTOCONNECT_URI "/_ac" // Default AutoConnect root path +
AutoConnect() +
AutoConnect default constructor. This entry activates WebServer internally and the web server is allocated internal.
+AutoConnect(ESP8266WebServer& webServer) +
Run the AutoConnect site using the externally ensured ESP 8266 WebServer. User's added URI handler response can be included in handleClient method. +
bool begin() +
bool begin(const char* ssid, const char* passphraase) +
bool begin(const char* ssid, const char* passphraase, unsinged long timeout) +
Starts establishing WiFi connection. Before establishing, start the Web server and DNS server for the captive portal. Then begins connection establishment in WIFI_STA mode. If connection can not established with the specified SSID and password, switch to WIFI_AP_STA mode and activate SoftAP. +
bool config(AutoConnectConfig& config) +
bool config(const char* ap, const char* password = nullptr) +
Sets SoftAP's WiFi configuration. +
void end() +
Stops AutoConnect captive portal service. Release ESP8266WebServer and DNSServer.
+Attention to end
+The end function releases the instance of ESP8266WebServer and DNSServer. It can not process them after the end function.
+void handleClient() +
Handling for the AutoConnect web interface. Invoke the handleClient of the parent web server to process client request of the AutoConnect WEB interface. No effects when the web server is not available.
+void handleRequest() +
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 ESP8266::handleClient.
+void home(String uri) +
Put a user site's home URI. The URI specified by home is linked from "HOME" in the AutoConnect portal menu. +
Returns the reference of the ESP8266WebServer which is allocated in AutoConnect automatically.
+ESP8266WebServer& host() +
&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(); +
portal.host().handleClient(); +
void onDetect(DetectExit_ft fn) +
An fn specifies the function called when the captive portal starts. Its prototype declaration is defined as "DetectFunc_ft".
+typedef std::function<bool(IPAddress softapIP)> DetectExit_ft +
void onNotFound(ESP8266WebServer::THandlerFunction fn) +
AutoConnectConfig(); +
AutoConnectConfig(const char* ap, const char* password); +
SoftAP's SSID. +
Sets IP address for Soft AP in captive portal. When AutoConnect fails the initial WiFi.begin, it starts the captive portal with the IP address specified this. +
Reset ESP8266 module automatically when WLAN disconnected. +
The credential saved automatically at the connection establishment. +
The channel number of WIFi when SoftAP starts. +
See Application note
+Espressif Systems had announced the application note about Wi-Fi channel selection.
+Sets gateway address for Soft AP in captive portal. When AutoConnect fails the initial WiFi.begin, it starts the captive portal with the IP address specified this. +
Sets SoftAP to hidden SSID. +
Sets the home path of user sketch. This path would be linked from 'HOME' in the AutoConnect menu. +
Sets subnet mask for Soft AP in captive portal. When AutoConnect fails the initial WiFi.begin, it starts the captive portal with the IP address specified this. +
Sets password for SoftAP. The length should be from 8 to up to 63. The default value is 12345678. +
AutoConenct Portal; +AutoConenctConfig Config("", "passpass"); // SoftAp name is determined at runtime +Config.apid = ESP.hostname(); // Retrieve host name to SotAp identification +Config.apip = IPAddress(192,168,10,101); // Sets SoftAP IP address +Config.gateway = IPAddress(192,168,10,1); // Sets WLAN router IP address +Config.netmask = IPAddress(255,255,255,0); // Sets WLAN scope +Config.autoSave = AC_SAVECREDENTIAL_NEVER; // No save credential +Config.homeUri = "/index.html" // Sets home path of the sketch application +Portal.config(Config); // Configure AutoConnect +Portal.begin(); // Starts and behaves captive portal +
The library presents two PNG icons which can be used to embed a hyperlink to the AutoConnect menu.
+To reference the icon, use the AUTOCONNECT_LINK macro in the sketch. It expands into the string literal as an HTML <a></a>
tag with PNG embedded of the AutoConnect menu hyperlinks. Icon type is specified by the parameter of the macro.
Usage
+String html = "<html>"; +html += AUTOCONNECT_LINK(BAR_32); +html += "</html>"; +server.send(200, "text/html", html); +
Here holds two case examples. Both examples perform the same function. Only how to incorporate the AutoConnect into the sketch differs. Also included in the sample folder, HandlePortal.ino also shows how to use the PageBuilder library for HTML assemblies.
+Uses the web interface to light the LED connected to the NodeMCU module D0 port (which could be referred to as BUILTIN_LED), the following animation is it.
+Access to the ESP8266 module connected WiFi from the browser then the page contains the current value of the D0 port would be displayed. The page has the buttons to switch the port value. The LED blinks according to the value of the button that was clicked. This example is a typical sketch of manipulating ESP8266's GPIO via WLN.
+ +Embed AutoConnect library into this sketch. There are few places to be changed. And you can use AutoConnect's captive portal function to establish a connection freely to other WiFi spots.
+Bind to ESP8266WebServer, performs handleClient with handleRequest.
+ +In what situations should the handleRequest be used.
+It is something needs to be done immediately after the handle client. It is better to call only AutoConnect::handleClient whenever possible.
+Declare only AutoConnect, performs handleClient.
+ + + + + + + + + +If you can access the AutoConnect root path as http://ESP8266IPADDRESS/_ac from browser, probably the sketch uses ESP8266WebServer::handleClient() without AutoConnect::handleRequest().
+For AutoConnect menus to work properly, call AutoConnect::handleRequest() after ESP8266WebServer::handleClient() invoked, or use AutoConnect::handleClient(). AutoConnect::handleClient() is equivalent ESP8266WebServer::handleClient combinated AutoConnect::handleRequest().
Captive portal detection could not be trapped. It is necessary to disconnect and reset ESP8266 to clear memorized connection data in ESP8266. Also, It may be displayed on the smartphone if the connection information of esp8266ap is wrong. In that case, delete the connection information of esp8266ap memorized by the smartphone once.
+Maybe it is successfully connected at the first WiFi.begin. ESP8266 remembers the SSID successfully connected and will use at the next. It means SoftAP will only start up when the first WiFi.begin() fails.
+The saved SSID would be cleared by WiFi.disconnect() with WIFI_STA mode. If you do not want automatic reconnection, you can erase the memorized SSID with the following simple sketch.
+#include <ESP8266WiFi.h> + +void setup() { + delay(1000); + Serial.begin(115200); + WiFi.mode(WIFI_STA); + delay(100); + WiFi.begin(); + if (WiFi.waitForConnectResult() == WL_CONNECTED) { + WiFi.disconnect(); + while (WiFi.status() == WL_CONNECTED) + delay(100); + } + Serial.println("WiFi disconnected."); +} + +void loop() { + delay(1000); +} +
Please try ESPShaker. It is ESP8266 interactive serial command processor.
Probably WiFi.begin failed with the specified SSID. Activating the debug printing will help you to track down the cause.
+An esp8266 is default SSID name for SoftAP of captive portal and password is 12345678. You can change both by using AutoConnectConfig.
+If ESP8266 hang up after reset by AutoConnect menu, perhaps manual reset is not yet. Especially if it is not manual reset yet after uploading the sketch, the boot mode will stay 'Uart Download'. There is some discussion about this on the Github's ESP8266 core: https://github.com/esp8266/Arduino/issues/1017
+If you received the following message, the boot mode is still sketch uploaded. It needs to the manual reset once.
+ets Jan 8 2013,rst cause:2, boot mode:(1,6) or (1,7) +ets Jan 8 2013,rst cause:4, boot mode:(1,6) or (1,7) +wdt reset +
The correct boot mode for starting the sketch is (3, x).
+Make some sketches for erasing the EEPROM area, or some erasing utility is needed. You can prepare the sketch to erase the saved credential with AutoConnectCredential. The AutoConnectCrendential class provides the access method to the saved credential in EEPROM and library source file is including it.
+A class description of AutoConnectCredentail is follows.
+#include <AutoConnectCredential.h> + +AutoConnectCredential(); +
uint8_t entries()
+ Returns number of entries as contained credentials.
int8_t load(const char* ssid, struct station_config* config)
+ Load a credential entry specified ssid to config. Returns -1 as unsuccessfully loaded.
bool load(int8_t entry, struct station_config* config)
+ Load a credential entry to config. The entry parameter specify to index of the entry.
bool save(const struct station_config* config)
+ Save a credential entry stored in config to EEPROM. Returns the true as succeeded.
bool del(const char* ssid)
+ Delete a credential entry specified ssid. Returns the true as successfully deleted.
extern "C" { +#include <user_interface.h> +} +
struct station_config { + uint8 ssid[32]; + uint8 password[64]; + uint8 bssid_set; + uint8 bssid[6]; +}; +
Byte offset | +Length | +Value | +
---|---|---|
0 | +8 | +AC_CREDT | +
8 | +1 | +Number of contained entries (uint8_t) | +
9 | +2 | +Container size, excluding size of AC_CREDT and size of the number of entries(width for uint16_t type). | +
11 | +variable | +SSID terminated by 0x00. Max length is 32 bytes. | +
variable | +variable | +Password plain text terminated by 0x00. Max length is 64 bytes. | +
variable | +6 | +BSSID | +
variable | ++ | Contained the next entries. (Continuation SSID+Password+BSSID) | +
variable | +1 | +0x00. End of container. | +
Hint
+With the ESPShaker, you can access EEPROM interactively from the serial monitor, and of course you can erase saved credentials.
+Link button to AutoConnect menu can be embedded into Sketch's web page. The root path of the menu is /_ac by default and embed the following <a></a>
tag in the generating HTML.
<a style="background-color:SteelBlue; display:inline-block; padding:7px 13px; text-decoration:none;" href="/_ac">MENU</a> +
It increases about 57K bytes compared to the case without AutoConnect. A sketch size of the most simple example introduced at the Getting started is about 330K bytes. (270K byte without AutoConnect)
+It consumes about 2K bytes in the static and about 12K bytes are consumed at the moment when menu executed.
+Because AutoConnect does not send a login success response to the captive portal requests from the smartphone. The login success response varies iOS, Android and Windows. By analyzing the request URL of different login success inquiries for each OS, the correct behavior can be implemented, but not yet. Please resets ESP8266 from the AutoConnect menu.
+There may be 0xff as an invalid data in the credential saving area. The 0xff area would be reused. ↩
+Open the Arduino IDE, write the following sketch and upload it. The feature of this sketch is that the SSID and Password are not coded.
+#include <ESP8266WiFi.h> +#include <ESP8266WebServer.h> +#include <AutoConnect.h> + +ESP8266WebServer Server; +AutoConnect Portal(Server); + +void rootPage() { + char content[] = "Hello, world"; + Server.send(200, "text/plain", content); +} + +void setup() { + delay(1000); + Serial.begin(115200); + Serial.println(); + + Server.on("/", rootPage); + if (Portal.begin()) { + Serial.println("WiFi connected: " + WiFi.localIP().toString()); + } +} + +void loop() { + Portal.handleClient(); +} +
After about 30 seconds, if the ESP8266 cannot connect to nearby Wi-Fi spot, you pull out your smartphone and open Wi-Fi settings from the Settings Apps. You can see the esp8266ap in the list of "CHOOSE A NETWORK...". Then tap the esp8266ap and enter password 12345678, a something screen pops up automatically as shown below.
++
This is the AutoConnect statistics screen. This screen displays the current status of the established connection, WiFi mode, IP address, free memory size, and etc. Also, the hamburger icon is the control menu of AutoConnect seems at the upper right. By tap the hamburger icon, the control menu appears as the below.
+Here, tap "Configure new AP" to connect the new access point then the SSID configuration screen would be shown. Enter the SSID and Passphrase and tap apply to start connecting the access point.
+ +After connection established, the current status screen will appear. It is already connected to WLAN with WiFi mode as WIFI_AP_STA and the IP connection status is displayed there including the SSID. Then at this screen, you have two options for the next step.
+For one, continues execution of the sketch while keeping this connection. You can access ESP8266 via browser through the established IP address after cancel to "Log in" by upper right on the screen.
+Or, "RESET" can be selected. The ESP8266 resets and reboots. After that, immediately before the connection will be restored automatically with WIFI_STA mode.
The IP address of ESP8266 would be displayed on the serial monitor after connection recovered. Please access its address from the browser. The "Hello, world" page will respond. It's the page that was handled by in the sketch with "on" function of ESP8266WebServer.
+ + + + + + + + + +An Arduino library for ESP8266 WLAN configuration at run time with web interface.
+To the dynamic configuration for joining to WLAN with SSID and PSK accordingly. It an Arduino library united with ESP8266WebServer class. +Easily implementing the Web interface constituting the WLAN for ESP8266 WiFi connection. With this library to make a sketch easily which connects from ESP8266 to the access point at runtime by the web interface without hard-coded SSID and password.
++
It is no needed hard-coding in advance the SSID and Password into the sketch to connect between ESP8266 and WLAN. You can input SSID & Password from a smartphone via the web interface at runtime.
+AutoConnect control screen will be displayed automatically for establishing new connections. It aids by the captive portal when vested the connection cannot be detected.
By using the AutoConnect menu, to manage the connections convenient.
The connection authentication data as credentials are saved automatically in EEPROM of ESP8266 and You can select the past SSID from the AutoConnect menu.
+AutoConnect can be embedded easily into your sketch, just "begin" and "handleClient".
+The sketches which provide the web page using ESP8266WebServer there is, AutoConnect will not disturb it. AutoConnect can use an already instantiated ESP8266WebServer object, or itself can assign it.
+About flash size on the module
+The AutoConnect sketch size is relatively large. Large flash capacity is necessary. 512Kbyte (4Mbits) flash inclusion module such as ESP-01 is not recommended.
+AutoConnect requires the following environment and libraries.
+Arduino IDE
+The current upstream at the 1.8 level or later is needed. Please install from the official Arduino IDE download page. This step is not required if you already have a modern version.
+ESP8266 Arduino core
+AutoConnect targets sketches made on the assumption of ESP8266 Community's Arduino core. The latest release is recommended.
+Install third-party platform using the Boards Manager of Arduino IDE. Package URL is http://arduino.esp8266.com/stable/package_esp8266com_index.json
Additional necessary library
+The PageBuilder library to build HTML for ESP8266WebServer is needed.
+To install the PageBuilder library into your Arduino IDE, you can use the Library Manager. Select the board of ESP8266 series in the Arduino IDE, open the library manager and search keyword 'pagebuilder' in the topic 'Communication', then you can see the PageBuilder. The latest version or 1.0.0 later is required.
Clone or download from the AutoConnect GitHub repository.
+ +When you select Download, you can import it to Arduino IDE immediately. After downloaded, the AutoConnect-master.zip file will be saved in your download folder. Then in the Arduino IDE, navigate to "Sketch > Include Library". At the top of the drop down list, select the option to "Add .ZIP Library...". Details for Arduino official page.
+ + + + + + + + + +MIT License
+Copyright © 2018 Hieromon Ikasamo
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE.
+Acknowledgments
+The Luxbar is licensed under the MIT License.
+https://github.com/balzss/luxbar
Luxbar
+The AutoConnect menu is developed using the LuxBar which is licensed under the MIT License. See the License.
+The AutoConnect menu appears when you access the AutoConnect root path. It is assigned to "/_ac" located on ESP8266 local IP address by default. This location can be changed in the sketch. The following screen will appear at access to http://{localIP}/_ac
as the root path. This is the statistics of the current WiFi connection. You can access the menu from the here. (e.g. http://192.168.244.1/_ac
for SoftAP mode.)
+To invoke the menu tap at right on top.
What's local IP?
+A local IP means Local IP at connection established or SoftAP's IP.
+Currently, AutoConnect supports four menus. Undermost menu returns to home path of its sketch.
+Scan all available access point and display it. Strength and security of the detected AP are marked. The is indicated for the SSID that needs a security key. "Hidden:" means the number of hidden SSIDs discovered.
+Enter SSID and Passphrase and tap "apply" to try connection.
Once it was established connection, its SSID and Password will be stored to the EEPROM of ESP8266 automatically. The Open SSIDs menu reads the saved SSID credentials from the EEPROM. The stored credential data are listed by the SSID as shown below. Its label is a clickable button. To tap the SSID button starts connection it.
+ +Disconnect ESP8266 from the current connection. After the menu tapped, AutoConnect menu cannot be accessed. Once disconnected, you will need to set the SSID again to connect to the WLAN.
+It can also reset the ESP8266 automatically after being disconnected from the API used in the sketch.
+Reset the ESP8266, it will be rebooted. After rebooting complete, the ESP8266 begins establishing the previous connection by WIFI_STA mode and the esp8266ap as SoftAP disappears from the WLAN.
+ +Not every module will be rebooted normally
+The Reset menu is using the ESP.reset() function. This is an almost hardware reset. In order to resume the sketch normally, the state of GPIO0 is important. Since this depends on the circuit implementation for each module, not every module will be rebooted normally. See also FAQ.
+How embed the AutoConnect to the sketches you have. Most simple approach to applying AutoConnect for the existing sketches, follow the below steps.
+ + Insert #include <AutoConnect.h>
to behind of #include <ESP8266WebServer.h>
.
+ Insert AutoConnect
PORTAL(WEBSERVER);
to behind of ESP8266WebServer
WEBSERVER;
declaration.1
+ Remove WiFi.begin(
SSID
,
PSK
)
and the subsequent logic for the connection status check.
+ Replace WEBSERVER
.begin()
to PORTAL
.begin()
.2
+ Replace WEBSERVER
.handleClient()
to PORTAL
.handleClient()
.3
+ If the connection successful logic is needed, you can check the return value as true
or false
of PORTAL
.begin()
.
ESP8266WebServer.h
and AutoConnect.h
ESP8266WebServer::on
.AutoConnect::begin()
.AutoConnect::handleClient()
,ESP8266WebServer::handleClient()
then AutoConnect::handleRequest()
.Two options are available for AutoConnect constructor.
+AutoConnect VARIABLE(&ESP8266WebServer); +
AutoConnect VARIABLE; +
Parameter with ESP8266WebServer variable: An ESP8266WebServer object variable must be declared in the sketch. AutoConnect uses its variable for handling the AutoConnect menu.
+With no parameter: The sketch does not declare ESP8266WebServer object. In this case, AutoConnect allocates an instance of the ESP8266WebServer internally and the logic sequence of the sketch is somewhat different as the above. To register a URL handler function by ESP8266WebServer::on should be performed after AutoConnect::begin.
+AutoConnect performs WiFi.begin for establishing a connection with WLAN internally. There is no need for a general process to establish a connection with WiFi.begin in a sketch.
+AutoConnect::begin internally executes ESP8266WebServer::begin too and it starts DNS server to behave as a captive portal. So the sketch does not need to call ESP8266WebServer::begin.
+Why DNS Server Starts
+AutoConnect traps the detection of captive portals and directs them to the AutoConnect menu to achieve a connection with the WLAN interactively. In order to trap, it temporarily responds SoftAP address to all DNS queries. When the connection with the WLAN is successfully established, the DNS server will stop.
+SSID and Password can also specify by AutoConnect::begin. ESP8266 uses provided SSID and Password explicitly. If the connection false with specified SSID with Password then a captive portal is activated. SSID and Password are not present, ESP8266 SDK will attempt to connect using the still effectual SSID and password. Usually, it succeeds.
+AutoConnect is designed to coexist with the process for handling the web pages by user sketches. The page processing function which will send an HTML to the client invoked by the "on" function is the same as when using ESP8266WebServer natively.
+Both classes member function name is the same: handleClient, but behavior is different. Using the AutoConnect embedded along with ESP8266WebServer::handleClient has limitations. Refer to the below section for details.
+The interoperable process with an ESP8266WebServer depends on the parameters of the AutoConnect constructor.
+Declaration parameter | +Use ESP8266WebServer::handleClient | +Use AutoConnect::handleClient | +
---|---|---|
None | +AutoConnect menu not available. host() is needed. |
+AutoConnect menu available. host() is needed. |
+
Reference to ESP8266WebServer | +AutoConnect menu not available. host() not necessary. |
+AutoConnect menu available. host() not necessary. |
+
By declaration for the AutoConnect variable with no parameter: The ESP8266WebServer instance is hosted by AutoConnect automatically then the sketches use AutoConnect::host as API to get it after AutoConnect::begin performed.
+By declaration for the AutoConnect variable with the reference of ESP8266WebServer: AutoConnect will use it. The sketch can use it is too.
+In use ESP8266WebServer::handleClient(): AutoConnect menu can be dispatched but not works normally. It is necessary to call AutoConnect::handleRequest after ESP8255WebServer::handleClient invoking.
+In use AutoConnect::handleClient(): The handleClient() process and the AutoConnect menu is available without calling ESP8266WebServer::handleClient.
+Why AutoConnect::handleRequest is needed when using ESP8266::handleClient
+The AutoConnect menu function may affect WiFi connection state. It follows that the menu process must execute outside ESP8266WebServer::handleClient.
+AutoConnect::handleClient is equivalent ESP8266WebServer::handleClient included AutoConnect::handleRequest.
Registering the "not found" handler is a different way than ESP8266. 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.
+The captive portal will only be activated if the first WiFi::begin fails. Sketch can detect with the 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(); +} +
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(); +} +
By default, AutoConnect saves the credentials of the established connection in EEPROM. You can disable it with the autoSave parameter specified by AutoConnect::config.
+AutoConnect Portal; +AutoConnectConfig Config; +Config.autoSave = AC_SAVECREDENTIAL_NEVER; +Portal.config(Config); +Portal.begin(); +
AutoConnect::config before AutoConnect::begin
+AutoConnect::config must be executed before AutoConnect::begin.
+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
+
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.
+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.
+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.
+ +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 AutoConnectConfig API for details.
+"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.
+ +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"
+
Each VARIABLE conforms to the actual declaration in the sketches. ↩
+WiFi SSID and Password can be specified AutoConnect::begin() too. ↩
+Replacement the handleClient method is not indispensable. AutoConnect can still connect with the captive portal as it is ESP8266WebServer::handleClient. But it can not valid AutoConnect menu. ↩
+