From 51cd72375f10d3b48d2dd84ae69e8dbc71f074a4 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Fri, 31 Aug 2018 17:37:16 +0900 Subject: [PATCH 01/17] Supports ESP32 --- examples/Credential/Credential.ino | 10 ++++ examples/HandleClient/HandleClient.ino | 12 +++- examples/HandlePortal/HandlePortal.ino | 11 +++- examples/Simple/Simple.ino | 9 +++ examples/mqttRSSI/mqttRSSI.ino | 6 +- library.json | 12 ++-- library.properties | 8 +-- src/AutoConnect.cpp | 60 +++++++++++++------ src/AutoConnect.h | 38 ++++++++---- src/AutoConnectCredential.cpp | 2 +- src/AutoConnectCredential.h | 13 ++++- src/AutoConnectPage.cpp | 80 +++++++++++++++++++++++--- 12 files changed, 209 insertions(+), 52 deletions(-) diff --git a/examples/Credential/Credential.ino b/examples/Credential/Credential.ino index 89c4bdf..f187ab8 100644 --- a/examples/Credential/Credential.ino +++ b/examples/Credential/Credential.ino @@ -14,13 +14,23 @@ This sketch uses PageBuilder to support handling of operation pages. */ +#if defined(ARDUINO_ARCH_ESP8266) #include #include +#elif defined(ARDUINO_ARCH_ESP32) +#include +#include +#endif #include #include #include +#if defined(ARDUINO_ARCH_ESP8266) ESP8266WebServer Server; +#elif defined(ARDUINO_ARCH_ESP32) +WebServer Server; +#endif + AutoConnect Portal(Server); String viewCredential(PageArgument&); String delCredential(PageArgument&); diff --git a/examples/HandleClient/HandleClient.ino b/examples/HandleClient/HandleClient.ino index 1d5d1cb..f8b81d3 100644 --- a/examples/HandleClient/HandleClient.ino +++ b/examples/HandleClient/HandleClient.ino @@ -7,11 +7,21 @@ https://opensource.org/licenses/MIT */ +#if defined(ARDUINO_ARCH_ESP8266) #include #include +#elif defined(ARDUINO_ARCH_ESP32) +#include +#include +#endif #include -ESP8266WebServer server; +#if defined(ARDUINO_ARCH_ESP8266) +ESP8266WebServer Server; +#elif defined(ARDUINO_ARCH_ESP32) +WebServer Server; +#endif + AutoConnect portal(server); void handleRoot() { diff --git a/examples/HandlePortal/HandlePortal.ino b/examples/HandlePortal/HandlePortal.ino index 105f28d..00357e9 100644 --- a/examples/HandlePortal/HandlePortal.ino +++ b/examples/HandlePortal/HandlePortal.ino @@ -14,8 +14,13 @@ It will help you understand AutoConnect usage. */ +#if defined(ARDUINO_ARCH_ESP8266) #include #include +#elif defined(ARDUINO_ARCH_ESP32) +#include +#include +#endif #include AutoConnect portal; @@ -64,7 +69,7 @@ void handleRoot() { } void handleGPIO() { - ESP8266WebServer& server = portal.host(); + WebServerClass& server = portal.host(); if (server.arg("v") == "low") digitalWrite(BUILTIN_LED, LOW); else if (server.arg("v") == "high") @@ -73,7 +78,7 @@ void handleGPIO() { } void sendRedirect(String uri) { - ESP8266WebServer& server = portal.host(); + WebServerClass& server = portal.host(); server.sendHeader("Location", uri, true); server.send(302, "text/plain", ""); server.client().stop(); @@ -97,7 +102,7 @@ void setup() { // Starts user web site included the AutoConnect portal. portal.onDetect(atDetect); if (portal.begin()) { - ESP8266WebServer& server = portal.host(); + WebServerClass& server = portal.host(); server.on("/", handleRoot); server.on("/io", handleGPIO); Serial.println("Started, IP:" + WiFi.localIP().toString()); diff --git a/examples/Simple/Simple.ino b/examples/Simple/Simple.ino index 160a6ac..9cab7e5 100644 --- a/examples/Simple/Simple.ino +++ b/examples/Simple/Simple.ino @@ -7,12 +7,21 @@ https://opensource.org/licenses/MIT */ +#if defined(ARDUINO_ARCH_ESP8266) #include #include +#elif defined(ARDUINO_ARCH_ESP32) +#include +#include +#endif #include #include +#if defined(ARDUINO_ARCH_ESP8266) ESP8266WebServer Server; +#elif defined(ARDUINO_ARCH_ESP32) +WebServer Server; +#endif AutoConnect Portal(Server); AutoConnectConfig Config; // Enable autoReconnect supported on v0.9.4 diff --git a/examples/mqttRSSI/mqttRSSI.ino b/examples/mqttRSSI/mqttRSSI.ino index de14ff2..052347a 100644 --- a/examples/mqttRSSI/mqttRSSI.ino +++ b/examples/mqttRSSI/mqttRSSI.ino @@ -1,5 +1,5 @@ /* - ESP8266 publish the RSSI as the WiFi signal strength to ThingSpeak channel. + ESP8266/ESP32 publish the RSSI as the WiFi signal strength to ThingSpeak channel. This example is for explaining how to use the AutoConnect library. In order to execute this example, the ThingSpeak account is needed. Sing up @@ -13,7 +13,11 @@ https://opensource.org/licenses/MIT */ +#if defined(ARDUINO_ARCH_ESP8266) #include +#elif defined(ARDUINO_ARCH_ESP32) +#include +#endif #include #include diff --git a/library.json b/library.json index 517a7b9..2d6981c 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { "name": "AutoConnect", "keywords": "communication, http, server, web, wifi, wi-fi", - "description": "ESP8266 WLAN configuration at runtime with web interface.", + "description": "ESP8266/ESP32 WLAN configuration at runtime with web interface.", "homepage": "https://hieromon.github.io/AutoConnect", "repository": { @@ -12,11 +12,15 @@ [ { "name": "PageBuilder", - "version": ">=1.0.0" + "version": ">=1.1.0" } ], "frameworks": "arduino", - "platforms": "espressif8266", - "version": "0.9.4", + "platforms": + [ + "espressif8266", + "espressif32" + ], + "version": "0.9.5", "license": "MIT" } diff --git a/library.properties b/library.properties index 5bc5689..5ac94dc 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=AutoConnect -version=0.9.4 +version=0.9.5 author=Hieromon Ikasamo maintainer=Hieromon Ikasamo -sentence=ESP8266 WLAN configuration at runtime with web interface. +sentence=ESP8266/ESP32 WLAN configuration at runtime with web interface. paragraph=A library for easy implementing the Web interface constituting the WLAN for ESP8266 WiFi connection. With this library to make a sketch which connects from ESP8266 to the access point at runtime by the web interface without hardcoded SSID and password. category=Communication url=https://github.com/Hieromon/AutoConnect.git -dependencies=ESP8266WebServer, ESP8266WiFi -architectures=esp8266 +dependencies=PageBuilder +architectures=esp8266, esp32 diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index d0a226b..6beef39 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -2,12 +2,24 @@ * AutoConnect class implementation. * @file AutoConnect.cpp * @author hieromon@gmail.com - * @version 0.9.4 - * @date 2018-05-05 + * @version 0.9.5 + * @date 2018-08-27 * @copyright MIT license. */ #include "AutoConnect.h" +#ifdef ARDUINO_ARCH_ESP32 +#include +#endif + +/** + * An actual reset function dependent on the architecture + */ +#if defined(ARDUINO_ARCH_ESP8266) +#define SOFT_RESET() ESP.reset() +#elif defined(ARDUINO_ARCH_ESP32) +#define SOFT_RESET() ESP.restart() +#endif /** * AutoConnect default constructor. This entry activates WebServer @@ -25,7 +37,7 @@ AutoConnect::AutoConnect() { * User's added URI handler response can be included in handleClient method. * @param webServer A reference of ESP8266WebServer instance. */ -AutoConnect::AutoConnect(ESP8266WebServer& webServer) { +AutoConnect::AutoConnect(WebServerClass& webServer) { _initialize(); _webServer.reset(&webServer); _dnsServer.reset(nullptr); @@ -91,7 +103,9 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long AC_DBG("failed\n"); return false; } +#ifdef ARDUINO_ARCH_ESP8266 AC_DBG("DHCP client(%s)\n", wifi_station_dhcpc_status() == DHCP_STOPPED ? "STOPPED" : "STARTED"); +#endif // Try to connect by STA immediately. if (ssid == nullptr && passphrase == nullptr) @@ -126,7 +140,7 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long // Change WiFi working mode, Enable AP with STA WiFi.setAutoConnect(false); - WiFi.disconnect(); + _disconnectWiFi(true); WiFi.mode(WIFI_AP_STA); delay(100); @@ -236,7 +250,7 @@ void AutoConnect::end() { /** * Returns the current hosted ESP8266WebServer. */ -ESP8266WebServer& AutoConnect::host() { +WebServerClass& AutoConnect::host() { return *_webServer; } @@ -247,9 +261,9 @@ void AutoConnect::_startWebServer() { // Boot Web server if (!_webServer) { // Only when hosting WebServer internally - _webServer.reset(new ESP8266WebServer(AUTOCONNECT_HTTPPORT)); + _webServer.reset(new WebServerClass(AUTOCONNECT_HTTPPORT)); _webServerAlloc = AC_WEBSERVER_HOSTED; - AC_DBG("ESP8266WebServer allocated\n"); + AC_DBG("WebServer allocated\n"); } // Discard the original the not found handler to redirect captive portal detection. // It is supposed to evacuate but ESP8266WebServer::_notFoundHandler is not accessible. @@ -300,10 +314,8 @@ void AutoConnect::handleRequest() { // Handling processing requests to AutoConnect. if (_rfConnect) { // Leave from the AP currently. - if (WiFi.status() == WL_CONNECTED) { - WiFi.disconnect(); - delay(100); - } + if (WiFi.status() == WL_CONNECTED) + _disconnectWiFi(true); // An attempt to establish a new AP. AC_DBG("Request for %s\n", (const char*)_credential.ssid); @@ -332,18 +344,14 @@ void AutoConnect::handleRequest() { _stopPortal(); AC_DBG("Reset"); delay(1000); - ESP.reset(); + SOFT_RESET(); delay(1000); } if (_rfDisconnect) { // Disconnect from the current AP. _stopPortal(); - WiFi.disconnect(); - while (WiFi.status() == WL_CONNECTED) { - delay(100); - yield(); - } + _disconnectWiFi(true); AC_DBG("Disconnected"); // Reset disconnection request, restore the menu title. _rfDisconnect = false; @@ -351,7 +359,7 @@ void AutoConnect::handleRequest() { if (_apConfig.autoReset) { delay(1000); - ESP.reset(); + SOFT_RESET(); delay(1000); } } @@ -369,7 +377,7 @@ void AutoConnect::onDetect(DetectExit_ft fn) { * Register the handler function for undefined url request detected. * @param fn A function of the not found handler. */ -void AutoConnect::onNotFound(ESP8266WebServer::THandlerFunction fn) { +void AutoConnect::onNotFound(WebServerClass::THandlerFunction fn) { _notFoundHandler = fn; } @@ -629,3 +637,17 @@ wl_status_t AutoConnect::_waitForConnect(unsigned long timeout) { AC_DBG("%s IP:%s\n", wifiStatus == WL_CONNECTED ? "established" : "time out", WiFi.localIP().toString().c_str()); return wifiStatus; } + +/** + * Disconnects the station from an associated access point. + * @param wifiOff The station mode turning switch. + */ +void AutoConnect::_disconnectWiFi(bool wifiOff) { +#if defined(ARDUINO_ARCH_ESP8266) + WiFi.disconnect(wifiOff); +#elif defined(ARDUINO_ARCH_ESP32) + WiFi.disconnect(wifiOff, true); +#endif + while (WiFi.status() == WL_CONNECTED) + delay(100); +} \ No newline at end of file diff --git a/src/AutoConnect.h b/src/AutoConnect.h index 89f07f8..909027e 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -2,8 +2,8 @@ * Declaration of AutoConnect class and accompanying AutoConnectConfig class. * @file AutoConnect.h * @author hieromon@gmail.com - * @version 0.9.4 - * @date 2018-05-05 + * @version 0.9.5 + * @date 2018-08-27 * @copyright MIT license. */ @@ -14,18 +14,25 @@ #include #include #include +#if defined(ARDUINO_ARCH_ESP8266) #include #include -#include -#include extern "C" { #include } +using WebServerClass = ESP8266WebServer; +#elif defined(ARDUINO_ARCH_ESP32) +#include +#include +using WebServerClass = WebServer; +#endif +#include +#include #include "AutoConnectPage.h" #include "AutoConnectCredential.h" // Uncomment the following AC_DEBUG to enable debug output. -//#define AC_DEBUG +#define AC_DEBUG // Debug output destination can be defined externally with AC_DEBUG_PORT #ifndef AC_DEBUG_PORT @@ -38,7 +45,11 @@ extern "C" { #endif #ifndef AUTOCONNECT_APID +#if defined(ARDUINO_ARCH_ESP8266) #define AUTOCONNECT_APID "esp8266ap" +#elif defined(ARDUINO_ARCH_ESP32) +#define AUTOCONNECT_APID "esp32ap" +#endif #endif #ifndef AUTOCONNECT_PSK @@ -199,7 +210,7 @@ class AutoConnectConfig { class AutoConnect { public: AutoConnect(); - AutoConnect(ESP8266WebServer& webServer); + AutoConnect(WebServerClass& webServer); ~AutoConnect(); bool config(AutoConnectConfig& Config); bool config(const char* ap, const char* password = nullptr); @@ -209,11 +220,11 @@ class AutoConnect { void end(); void handleClient(); void handleRequest(); - ESP8266WebServer& host(); + WebServerClass& host(); typedef std::function DetectExit_ft; void onDetect(DetectExit_ft fn); - void onNotFound(ESP8266WebServer::THandlerFunction fn); + void onNotFound(WebServerClass::THandlerFunction fn); protected: enum _webServerAllocateType { @@ -241,14 +252,17 @@ class AutoConnect { bool _captivePortal(); bool _isIP(String ipStr); wl_status_t _waitForConnect(unsigned long timeout); + void _disconnectWiFi(bool wifiOff); /** Utilities */ + static uint32_t _getChipId(); + static uint32_t _getFlashChipRealSize(); static String _toMACAddressString(const uint8_t mac[]); static unsigned int _toWiFiQuality(int32_t rssi); DetectExit_ft _onDetectExit; - ESP8266WebServer::THandlerFunction _notFoundHandler; + WebServerClass::THandlerFunction _notFoundHandler; - std::unique_ptr _webServer; + std::unique_ptr _webServer; std::unique_ptr _dnsServer; AC_WEBSERVER_TYPE _webServerAlloc; @@ -321,7 +335,11 @@ class AutoConnect { String _token_OPEN_SSID(PageArgument& args); String _token_UPTIME(PageArgument& args); +#if defined(ARDUINO_ARCH_ESP8266) friend class ESP8266WebServer; +#elif defined(ARDUINO_ARCH_ESP32) + friend class WebServer; +#endif }; #endif // _AUTOCONNECT_H_ diff --git a/src/AutoConnectCredential.cpp b/src/AutoConnectCredential.cpp index 1ed56c8..f05a96a 100644 --- a/src/AutoConnectCredential.cpp +++ b/src/AutoConnectCredential.cpp @@ -2,7 +2,7 @@ * AutoConnectCredential class implementation. * @file AutoConnectCredential.cpp * @author hieromon@gmail.com - * @version 1.0.0 + * @version 0.9.5 * @date 2018-02-17 * @copyright MIT license. */ diff --git a/src/AutoConnectCredential.h b/src/AutoConnectCredential.h index 77949b4..10a8052 100644 --- a/src/AutoConnectCredential.h +++ b/src/AutoConnectCredential.h @@ -2,7 +2,7 @@ * Declaration of AutoConnectCredential class. * @file AutoConnectCredential.h * @author hieromon@gmail.com - * @version 1.0.0 + * @version 0.9.5 * @date 2018-02-17 * @copyright MIT license. */ @@ -11,9 +11,20 @@ #define _AUTOCONNECTCREDENTIAL_H_ #include +#if defined(ARDUINO_ARCH_ESP8266) extern "C" { #include } +#elif defined(ARDUINO_ARCH_ESP32) +#include +struct station_config { + uint8_t ssid[32]; + uint8_t password[64]; + uint8_t bssid_set; + uint8_t bssid[6]; + wifi_fast_scan_threshold_t threshold; +}; +#endif /** Credential storage area offset specifier in EEPROM. * By defining AC_IDENTIFIER_OFFSET macro in the user sketch, the credential diff --git a/src/AutoConnectPage.cpp b/src/AutoConnectPage.cpp index 88832e9..09d33c8 100644 --- a/src/AutoConnectPage.cpp +++ b/src/AutoConnectPage.cpp @@ -2,18 +2,24 @@ * AutoConnect portal site web page implementation. * @file AutoConnectPage.h * @author hieromon@gmail.com - * @version 0.9.4 - * @date 2018-04-22 + * @version 0.9.5 + * @date 2018-08-27 * @copyright MIT license. */ +#if defined(ARDUINO_ARCH_ESP8266) #include -#include "AutoConnect.h" -#include "AutoConnectPage.h" -#include "AutoConnectCredential.h" extern "C" { #include } +#elif defined(ARDUINO_ARCH_ESP32) +#include +#include +#define ENC_TYPE_NONE WIFI_AUTH_OPEN +#endif +#include "AutoConnect.h" +#include "AutoConnectPage.h" +#include "AutoConnectCredential.h" /**< Basic CSS common to all pages */ const char AutoConnect::_CSS_BASE[] PROGMEM = { @@ -723,6 +729,24 @@ const char AutoConnect::_PAGE_DISCONN[] PROGMEM = { "" }; +uint32_t AutoConnect::_getChipId() { +#if defined(ARDUINO_ARCH_ESP8266) + return ESP.getChipId(); +#elif defined(ARDUINO_ARCH_ESP32) + uint64_t chipId; + chipId = ESP.getEfuseMac(); + return (uint32_t)(chipId >> 32); +#endif +} + +uint32_t AutoConnect::_getFlashChipRealSize() { +#if defined(ARDUINO_ARCH_ESP8266) + return ESP.getFlashChipRealSize(); +#elif defined(ARDUINO_ARCH_ESP32) + return (uint32_t)spi_flash_get_chip_size(); +#endif +} + String AutoConnect::_token_CSS_BASE(PageArgument& args) { return String(_CSS_BASE); } @@ -789,7 +813,10 @@ String AutoConnect::_token_WIFI_STATUS(PageArgument& args) { } String AutoConnect::_token_STATION_STATUS(PageArgument& args) { + uint8_t st; const char* wlStatusSymbol; + +#if defined(ARDUINO_ARCH_ESP8266) static const char *wlStatusSymbols[] = { "IDLE", "CONNECTING", @@ -798,7 +825,7 @@ String AutoConnect::_token_STATION_STATUS(PageArgument& args) { "CONNECT_FAIL", "GOT_IP" }; - uint8_t st = wifi_station_get_connect_status(); + st = wifi_station_get_connect_status(); switch (st) { case STATION_IDLE: wlStatusSymbol = wlStatusSymbols[0]; @@ -819,6 +846,43 @@ String AutoConnect::_token_STATION_STATUS(PageArgument& args) { wlStatusSymbol = wlStatusSymbols[5]; break; } + +#elif defined(ARDUINO_ARCH_ESP32) + static const char *wlStatusSymbols[] = { + "IDLE", + "NO_SSID_AVAIL", + "SCAN_COMPLETED", + "CONNECTED", + "CONNECT_FAILED", + "CONNECTION_LOST", + "DISCONNECTED" + }; + st = WiFi.status(); + switch (st) { + case WL_IDLE_STATUS: + wlStatusSymbol = wlStatusSymbols[0]; + break; + case WL_NO_SSID_AVAIL: + wlStatusSymbol = wlStatusSymbols[1]; + break; + case WL_SCAN_COMPLETED: + wlStatusSymbol = wlStatusSymbols[2]; + break; + case WL_CONNECTED: + wlStatusSymbol = wlStatusSymbols[3]; + break; + case WL_CONNECT_FAILED: + wlStatusSymbol = wlStatusSymbols[4]; + break; + case WL_CONNECTION_LOST: + wlStatusSymbol = wlStatusSymbols[5]; + break; + case WL_DISCONNECTED: + wlStatusSymbol = wlStatusSymbols[6]; + break; + } +#endif + return "(" + String(st) + ")" + String(wlStatusSymbol); } @@ -864,11 +928,11 @@ String AutoConnect::_token_CPU_FREQ(PageArgument& args) { } String AutoConnect::_token_FLASH_SIZE(PageArgument& args) { - return String(ESP.getFlashChipRealSize()); + return String(_getFlashChipRealSize()); } String AutoConnect::_token_CHIP_ID(PageArgument& args) { - return String(ESP.getChipId()); + return String(_getChipId()); } String AutoConnect::_token_FREE_HEAP(PageArgument& args) { From 0a9c49bd711842e115012288e8fb8b3fadbe4407 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Mon, 3 Sep 2018 17:45:39 +0900 Subject: [PATCH 02/17] Supports ESP32 --- README.md | 28 +++++++++--- examples/FSBrowser/FSBrowser.ino | 50 ++++++++++++++++++++++ examples/HandlePortalEX/HandlePortalEX.ino | 9 ++++ mkdocs/index.md | 4 +- src/AutoConnect.cpp | 34 +++++++++------ src/AutoConnectPage.cpp | 9 ++-- 6 files changed, 106 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 1571e06..046a3f2 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ -# AutoConnect for ESP8266 +# AutoConnect for ESP8266/ESP32 -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) +An Arduino library for ESP8266/ESP32 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. +To the dynamic configuration for joining to WLAN with SSID and PSK accordingly. It an Arduino library united with *ESP8266WebServer* class of ESP8266 or *WebServer* class of ESP32. +Easily implementing the Web interface constituting the WLAN for ESP8266/ESP32 WiFi connection. With this library to make a sketch easily which connects from ESP8266/ESP32 to the access point at runtime by the web interface without hard-coded SSID and password.
Overview   Captiveportal
### 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. +It is no needed hard-coding in advance the SSID and Password into the sketch to connect between ESP8266/ESP32 and WLAN. You can input SSID & Password from a smartphone via the web interface at runtime. ### Simple usage @@ -19,7 +19,7 @@ AutoConnect control screen will be displayed automatically for establishing new ### 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](https://hieromon.github.io/AutoConnect/menu/index.html). +The connection authentication data as credentials are saved automatically in EEPROM of ESP8266/ESP32 and You can select the past SSID from the [AutoConnect menu](https://hieromon.github.io/AutoConnect/menu/index.html). ### Easy to embed in @@ -27,7 +27,7 @@ AutoConnect can be embedded easily into your sketch, just "**begin**" and "**han ### 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. +The sketches which provide the web page using ESP8266WebServer/WebServer there is, AutoConnect will not disturb it. AutoConnect can use an already instantiated ESP8266WebServer object(ESP8266) or WebServer object(ESP32), or itself can assign it. ## Supported hardware @@ -42,6 +42,16 @@ Apply the [Arduino core](https://github.com/esp8266/Arduino) of the ESP8266 Comm - SparkFun Thing - SweetPea ESP-210 +Alter the platform applying the [arduino-esp32](https://github.com/espressif/arduino-esp32) for the ESP32 modules. + +- ESP32Dev Board +- SparkFun ESP32 Thing +- WEMOS LOLIN D32 +- Ai-Thinker NodeMCU-32S +- Heltec WiFi Kit 32 +- M5Stack +- And other ESP32 modules supported by the Additional Board Manager URLs of the Arduino-IDE. + ## Simple usage ### The AutoConnect menu @@ -68,6 +78,10 @@ Full documentation is available on https://Hieromon.github.io/AutoConnect, some ## Change log +### [0.9.5] Aug. 27, 2018 +- Supports the espressif arduino-esp32 core. +- Fixed that crash may occur if the number of stored credentials in the EEPROM is smaller than the number of found WiFi networks. + ### [0.9.4] May 5, 2018. - Automatically focus passphrase after selecting SSID with Configure New AP. - Supports AutoConnectConfig::autoReconnect option, it will scan the WLAN when it can not connect to the default SSID, apply the applicable credentials if it is saved, and try reconnecting. diff --git a/examples/FSBrowser/FSBrowser.ino b/examples/FSBrowser/FSBrowser.ino index 16105b2..9eeadf7 100644 --- a/examples/FSBrowser/FSBrowser.ino +++ b/examples/FSBrowser/FSBrowser.ino @@ -22,11 +22,19 @@ access the sample web page at http://esp8266fs.local edit the page by going to http://esp8266fs.local/edit */ +#if defined(ARDUINO_ARCH_ESP8266) #include #include #include #include #include +#elif defined(ARDUINO_ARCH_ESP32) +#include +#include +#include +#include +#include +#endif //Add a below line for AutoConnect. #include @@ -34,9 +42,15 @@ const char* ssid = "wifi-ssid"; const char* password = "wifi-password"; +#if defined(ARDUINO_ARCH_ESP8266) const char* host = "esp8266fs"; ESP8266WebServer server(80); +#elif defined(ARDUINO_ARCH_ESP32) +const char* host = "esp32fs"; + +WebServer server(80); +#endif //Add a below line for AutoConnect. AutoConnect portal(server); //holds the current upload @@ -144,10 +158,15 @@ void handleFileList() { String path = server.arg("dir"); DBG_OUTPUT_PORT.println("handleFileList: " + path); +#if defined(ARDUINO_ARCH_ESP8266) Dir dir = SPIFFS.openDir(path); +#elif defined(ARDUINO_ARCH_ESP32) + File root = SPIFFS.open(path); +#endif path = String(); String output = "["; +#if defined(ARDUINO_ARCH_ESP8266) while(dir.next()){ File entry = dir.openFile("r"); if (output != "[") output += ','; @@ -159,6 +178,22 @@ void handleFileList() { output += "\"}"; entry.close(); } +#elif defined(ARDUINO_ARCH_ESP32) + if(root.isDirectory()){ + File file = root.openNextFile(); + while(file){ + if (output != "[") { + output += ','; + } + output += "{\"type\":\""; + output += (file.isDirectory()) ? "dir" : "file"; + output += "\",\"name\":\""; + output += String(file.name()).substring(1); + output += "\"}"; + file = root.openNextFile(); + } + } +#endif output += "]"; server.send(200, "text/json", output); @@ -170,12 +205,23 @@ void setup(void){ DBG_OUTPUT_PORT.setDebugOutput(true); SPIFFS.begin(); { +#if defined(ARDUINO_ARCH_ESP8266) Dir dir = SPIFFS.openDir("/"); while (dir.next()) { String fileName = dir.fileName(); size_t fileSize = dir.fileSize(); DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); } +#elif defined(ARDUINO_ARCH_ESP32) + File root = SPIFFS.open("/"); + File file = root.openNextFile(); + while(file){ + String fileName = file.name(); + size_t fileSize = file.size(); + DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); + file = root.openNextFile(); + } +#endif DBG_OUTPUT_PORT.printf("\n"); } @@ -222,7 +268,11 @@ void setup(void){ String json = "{"; json += "\"heap\":"+String(ESP.getFreeHeap()); json += ", \"analog\":"+String(analogRead(A0)); +#if defined(ARDUINO_ARCH_ESP8266) json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16))); +#elif defined(ARDUINO_ARCH_ESP32) + json += ", \"gpio\":" + String((uint32_t)(0)); +#endif json += "}"; server.send(200, "text/json", json); json = String(); diff --git a/examples/HandlePortalEX/HandlePortalEX.ino b/examples/HandlePortalEX/HandlePortalEX.ino index 660bc20..d1b33d1 100644 --- a/examples/HandlePortalEX/HandlePortalEX.ino +++ b/examples/HandlePortalEX/HandlePortalEX.ino @@ -15,12 +15,21 @@ It will help you understand AutoConnect usage. */ +#if defined(ARDUINO_ARCH_ESP8266) #include #include +#elif defined(ARDUINO_ARCH_ESP32) +#include +#include +#endif #include #include +#if defined(ARDUINO_ARCH_ESP8266) ESP8266WebServer server; +#elif defined(ARDUINO_ARCH_ESP32) +WebServer server; +#endif AutoConnect portal(server); static const char PROGMEM mold_page[] = R"*lit( diff --git a/mkdocs/index.md b/mkdocs/index.md index df13247..da13983 100644 --- a/mkdocs/index.md +++ b/mkdocs/index.md @@ -1,6 +1,6 @@ -# AutoConnect for ESP8266 +# AutoConnect for ESP8266/ESP32 -An Arduino library for ESP8266 WLAN configuration at run time with web interface. +An Arduino library for ESP8266/ESP32 WLAN configuration at run time with web interface. ## Overview diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index 6beef39..07ab8d4 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -321,16 +321,20 @@ void AutoConnect::handleRequest() { AC_DBG("Request for %s\n", (const char*)_credential.ssid); WiFi.begin((const char*)_credential.ssid, (const char*)_credential.password); if (_waitForConnect(_portalTimeout) == WL_CONNECTED) { - memcpy(_credential.bssid, WiFi.BSSID(), sizeof(station_config::bssid)); - _currentHostIP = WiFi.localIP(); - _redirectURI = String(AUTOCONNECT_URI_SUCCESS); - - // Save current credential - if (_apConfig.autoSave == AC_SAVECREDENTIAL_AUTO) { - AutoConnectCredential credit(_apConfig.boundaryOffset); - credit.save(&_credential); - AC_DBG("%s credential saved\n", _credential.ssid); + if (WiFi.BSSID() != NULL) { + memcpy(_credential.bssid, WiFi.BSSID(), sizeof(station_config::bssid)); + _currentHostIP = WiFi.localIP(); + _redirectURI = String(AUTOCONNECT_URI_SUCCESS); + + // Save current credential + if (_apConfig.autoSave == AC_SAVECREDENTIAL_AUTO) { + AutoConnectCredential credit(_apConfig.boundaryOffset); + credit.save(&_credential); + AC_DBG("%s credential saved\n", _credential.ssid); + } } + else + AC_DBG("%s has no BSSID, saving is unavailable\n", _credential.ssid); } else { _currentHostIP = WiFi.softAPIP(); @@ -342,7 +346,7 @@ void AutoConnect::handleRequest() { if (_rfReset) { // Reset or disconnect by portal operation result _stopPortal(); - AC_DBG("Reset"); + AC_DBG("Reset\n"); delay(1000); SOFT_RESET(); delay(1000); @@ -352,7 +356,7 @@ void AutoConnect::handleRequest() { // Disconnect from the current AP. _stopPortal(); _disconnectWiFi(true); - AC_DBG("Disconnected"); + AC_DBG("Disconnected\n"); // Reset disconnection request, restore the menu title. _rfDisconnect = false; _menuTitle = String(AUTOCONNECT_MENU_TITLE); @@ -391,11 +395,12 @@ bool AutoConnect::_loadAvailCredential() { if (credential.entries() >= 0) { // Scan the vicinity only when the saved credentials are existing. int8_t nn = WiFi.scanNetworks(false, true); + AC_DBG("%d network(s) found\n", (int)nn); if (nn > 0) { // Determine valid credentials by BSSID. for (uint8_t i = 0; i < credential.entries(); i++) { credential.load(i, &_credential); - for (uint8_t n = 0; n <= nn; n++) { + for (uint8_t n = 0; n < nn; n++) { if (!memcmp(_credential.bssid, WiFi.BSSID(n), sizeof(station_config::bssid))) return true; } @@ -419,6 +424,7 @@ void AutoConnect::_stopPortal() { } WiFi.softAPdisconnect(false); + AC_DBG("SoftAP stopped\n"); } /** @@ -585,9 +591,9 @@ bool AutoConnect::_isIP(String ipStr) { * @retval MAC address string in XX:XX:XX:XX:XX:XX format. */ String AutoConnect::_toMACAddressString(const uint8_t mac[]) { - String macAddr = ""; + String macAddr = ""; for (uint8_t i = 0; i < 6; i++) { - char buf[3]; + char buf[3]; sprintf(buf, "%02X", mac[i]); macAddr += buf; if (i < 5) diff --git a/src/AutoConnectPage.cpp b/src/AutoConnectPage.cpp index 09d33c8..db4833b 100644 --- a/src/AutoConnectPage.cpp +++ b/src/AutoConnectPage.cpp @@ -967,17 +967,16 @@ String AutoConnect::_token_OPEN_SSID(PageArgument& args) { struct station_config entry; String ssidList = ""; + uint8_t* bssid = WiFi.BSSID(); for (uint8_t i = 0; i < credit.entries(); i++) { credit.load(i, &entry); + AC_DBG("A credential #%d loaded\n", (int)i); ssidList += String(F("")); ssidList += String(F("
")); } From 2dde5154d03c4418f5b82623bb9bd7a86dc34f7e Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Tue, 4 Sep 2018 12:46:48 +0900 Subject: [PATCH 03/17] Supports ESP32 --- mkdocs/index.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/mkdocs/index.md b/mkdocs/index.md index da13983..991955f 100644 --- a/mkdocs/index.md +++ b/mkdocs/index.md @@ -4,14 +4,14 @@ An Arduino library for ESP8266/ESP32 WLAN configuration at run time with web int ## Overview -To the dynamic configuration for joining to WLAN with SSID and PSK accordingly. It an Arduino library united with *ESP8266WebServer* class. -Easy 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. +To the dynamic configuration for joining to WLAN with SSID and PSK accordingly. It an Arduino library united with *ESP8266WebServer* class for ESP8266 or *WebServer* class for ESP32. +Easy implementing the Web interface constituting the WLAN for ESP8266/ESP32 WiFi connection. With this library to make a sketch easily which connects from ESP8266/ESP32 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. +It is no needed hard-coding in advance the SSID and Password into the sketch to connect between ESP8266/ESP32 and WLAN. You can input SSID & Password from a smartphone via the web interface at runtime. ### Simple usage @@ -19,7 +19,7 @@ Easy implementing the Web interface constituting the WLAN for ESP8266 WiFi conne ### 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). +The connection authentication data as credentials are saved automatically in EEPROM of ESP8266/ESP32 and You can select the past SSID from the [AutoConnect menu](menu.md). ### Easy to embed in @@ -27,7 +27,7 @@ Easy implementing the Web interface constituting the WLAN for ESP8266 WiFi conne ### 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. +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.
This efficacy can also be applied to ESP32. The corresponding class for ESP32 will be the WebServer.
## Installation @@ -43,6 +43,13 @@ Easy implementing the Web interface constituting the WLAN for ESP8266 WiFi conne * [X] Olimex MOD-WIFI-ESP8266 * [X] SparkFun Thing * [X] SweetPea ESP-210 + * [X] ESP32Dev Board (applying the Espressif's arduino-esp32 core) + * [X] SparkFun ESP32 Thing + * [X] WEMOS LOLIN D32 + * [X] Ai-Thinker NodeMCU-32S + * [X] Heltec WiFi Kit 32 + * [X] M5Stack + * [X] And other ESP8266/ESP32 modules supported by the Additional Board Manager URLs of the Arduino-IDE. !!! info "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. @@ -60,6 +67,11 @@ The current upstream at the 1.8 level or later is needed. Please install from th AutoConnect targets sketches made on the assumption of [ESP8266 Community's Arduino core](https://github.com/esp8266/Arduino). The [latest release](https://github.com/esp8266/Arduino/releases/latest) 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 + ESP32 Arduino core + +Also, to apply AutoConnect to ESP32, the [arduino-esp32 core](https://github.com/espressif/arduino-esp32) provided by Espressif is needed. The [latest release](https://github.com/espressif/arduino-esp32/releases/latest) is recommended. +Install third-party platform using the *Boards Manager* of Arduino IDE. You can add multiple URLs into *Additional Board Manager URLs* field, separating them with commas. Package URL is https://dl.espressif.com/dl/package_esp32_index.json + Additional necessary library The [PageBuilder](https://github.com/Hieromon/PageBuilder) library to build HTML for ESP8266WebServer is needed. From 679f1798d7e818f68489b6428fd58b87d49c04f4 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Tue, 4 Sep 2018 21:57:49 +0900 Subject: [PATCH 04/17] Supports ESP32 --- src/AutoConnectPage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AutoConnectPage.cpp b/src/AutoConnectPage.cpp index db4833b..7df3e8f 100644 --- a/src/AutoConnectPage.cpp +++ b/src/AutoConnectPage.cpp @@ -981,7 +981,7 @@ String AutoConnect::_token_OPEN_SSID(PageArgument& args) { } if (ssidList.length() == 0) { - ssidList = String(PSTR("

No credential saved.

")); + ssidList = String(PSTR("

No saved credentials.

")); } return ssidList; } From cbdfe4a557227acd0093eb8d75116ab9ff00a524 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Wed, 5 Sep 2018 01:32:57 +0900 Subject: [PATCH 05/17] Supports ESP32 --- mkdocs.yml | 4 ++-- mkdocs/advancedusage.md | 2 +- mkdocs/gettingstarted.md | 10 ++++++---- mkdocs/index.md | 6 +----- mkdocs/menu.md | 14 +++++++------- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index 1de866f..8e55049 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,6 +1,6 @@ # Project information -site_name: 'AutoConnect for ESP8266' -site_description: 'ESP8266 WLAN configuration at run time with web interface' +site_name: 'AutoConnect for ESP8266/ESP32' +site_description: 'ESP8266/ESP32 WLAN configuration at run time with web interface' site_author: 'Hieromon Ikasamo' site_url: 'https://Hieromon.github.io/AutoConnect/' diff --git a/mkdocs/advancedusage.md b/mkdocs/advancedusage.md index e749fe0..d1be22e 100644 --- a/mkdocs/advancedusage.md +++ b/mkdocs/advancedusage.md @@ -6,7 +6,7 @@ Registering the "not found" handler is a different way than ESP8266WebServer. Th ### Automatic reconnect -When the captive portal is started, SoftAP starts and the STA is disconnected. The current SSID setting memorized in ESP8266 will be lost. +When the captive portal is started, SoftAP starts and the STA is disconnected. The current SSID setting memorized in ESP8266/ESP32 will be lost. If the [**autoReconnect**](api.md#autoreconnect) option of the [**AutoConnectConfig**](api.md#autoconnectconfig-api) class is enabled, it automatically attempts to reconnect to the disconnected past access point. When the autoReconnect option is specified, AutoConnect will not start SoftAP immediately if the first WiFi.begin fails. It will scan WiFi signal and the same connection information as the detected BSSID is stored in EEPROM as AutoConnect's credentials, explicitly apply it with WiFi.begin and rerun. ```arduino hl_lines="3" diff --git a/mkdocs/gettingstarted.md b/mkdocs/gettingstarted.md index 5074f0b..8b7a271 100644 --- a/mkdocs/gettingstarted.md +++ b/mkdocs/gettingstarted.md @@ -3,11 +3,11 @@ 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. ```arduino -#include -#include +#include // Replace with WiFi.h for ESP32 +#include // Replace with WebServer.h for ESP32 #include -ESP8266WebServer Server; +ESP8266WebServer Server; // Replace with WebServer for ESP32 AutoConnect Portal(Server); void rootPage() { @@ -33,7 +33,9 @@ void loop() { ### Run at first -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. +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** [^1] in the list of *"CHOOSE A NETWORK..."*. Then tap the esp8266ap and enter password **12345678**, a something screen pops up automatically as shown below. + +[^1]:When applied to ESP32, **esp32ap** diff --git a/mkdocs/index.md b/mkdocs/index.md index 991955f..80eec1f 100644 --- a/mkdocs/index.md +++ b/mkdocs/index.md @@ -79,10 +79,6 @@ To install the PageBuilder library into your Arduino IDE, you can use the *Libra -!!! warning "Library Manager of Arduino IDE" - The version of PageBuilder may not have been updated yet. If the library manager shows only version 0.93.0, please install the latest PageBuilder version than 1.0.0 from the Github repository. [The PageBuilder release](https://github.com/Hieromon/PageBuilder/releases/latest) of the Github repository is the latest version. The [PlatformIO library](https://platformio.org/lib/show/2010/PageBuilder) has the latest version of 1.0.0. - In 0.93.0, the PageBuilder class has a compile error about without the 'cancel' method. - ### Install the AutoConnect Clone or download from the [AutoConnect GitHub repository](https://github.com/Hieromon/AutoConnect). @@ -94,7 +90,7 @@ When you select Download, you can import it to Arduino IDE immediately. After do !!! info "Supported by Library manager." - AutoConnect was added to the Arduino IDE library manager as of March 17, 2018. It can be used with the PlatformIO library also. + AutoConnect was added to the Arduino IDE library manager. It can be used with the PlatformIO library also. + +
+
+
    +
  1. +

    When applied to ESP32, esp32ap 

    +
  2. +
+
diff --git a/docs/index.html b/docs/index.html index a33fbfd..f713f07 100644 --- a/docs/index.html +++ b/docs/index.html @@ -10,7 +10,7 @@ - + @@ -42,7 +42,7 @@ - AutoConnect for ESP8266 + AutoConnect for ESP8266/ESP32 @@ -101,7 +101,7 @@ - + Skip to content @@ -110,7 +110,7 @@