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("
")); }