Supports ESP32

pull/11/head
Hieromon Ikasamo 6 years ago
parent 8aaac85894
commit 51cd72375f
  1. 10
      examples/Credential/Credential.ino
  2. 12
      examples/HandleClient/HandleClient.ino
  3. 11
      examples/HandlePortal/HandlePortal.ino
  4. 9
      examples/Simple/Simple.ino
  5. 6
      examples/mqttRSSI/mqttRSSI.ino
  6. 12
      library.json
  7. 8
      library.properties
  8. 60
      src/AutoConnect.cpp
  9. 38
      src/AutoConnect.h
  10. 2
      src/AutoConnectCredential.cpp
  11. 13
      src/AutoConnectCredential.h
  12. 80
      src/AutoConnectPage.cpp

@ -14,13 +14,23 @@
This sketch uses PageBuilder to support handling of operation pages.
*/
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WebServer.h>
#endif
#include <AutoConnect.h>
#include <AutoConnectCredential.h>
#include <PageBuilder.h>
#if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer Server;
#elif defined(ARDUINO_ARCH_ESP32)
WebServer Server;
#endif
AutoConnect Portal(Server);
String viewCredential(PageArgument&);
String delCredential(PageArgument&);

@ -7,11 +7,21 @@
https://opensource.org/licenses/MIT
*/
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WebServer.h>
#endif
#include <AutoConnect.h>
ESP8266WebServer server;
#if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer Server;
#elif defined(ARDUINO_ARCH_ESP32)
WebServer Server;
#endif
AutoConnect portal(server);
void handleRoot() {

@ -14,8 +14,13 @@
It will help you understand AutoConnect usage.
*/
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WebServer.h>
#endif
#include <AutoConnect.h>
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());

@ -7,12 +7,21 @@
https://opensource.org/licenses/MIT
*/
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WebServer.h>
#endif
#include <time.h>
#include <AutoConnect.h>
#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

@ -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 <ESP8266WiFi.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#endif
#include <PubSubClient.h>
#include <AutoConnect.h>

@ -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"
}

@ -1,10 +1,10 @@
name=AutoConnect
version=0.9.4
version=0.9.5
author=Hieromon Ikasamo <hieromon@gmail.com>
maintainer=Hieromon Ikasamo <hieromon@gmail.com>
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

@ -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 <esp_wifi.h>
#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);
}

@ -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 <memory>
#include <functional>
#include <DNSServer.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <PageBuilder.h>
extern "C" {
#include <user_interface.h>
}
using WebServerClass = ESP8266WebServer;
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WebServer.h>
using WebServerClass = WebServer;
#endif
#include <EEPROM.h>
#include <PageBuilder.h>
#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<bool(IPAddress)> 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<ESP8266WebServer> _webServer;
std::unique_ptr<WebServerClass> _webServer;
std::unique_ptr<DNSServer> _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_

@ -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.
*/

@ -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 <Arduino.h>
#if defined(ARDUINO_ARCH_ESP8266)
extern "C" {
#include <user_interface.h>
}
#elif defined(ARDUINO_ARCH_ESP32)
#include <esp_wifi.h>
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

@ -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 <ESP8266WiFi.h>
#include "AutoConnect.h"
#include "AutoConnectPage.h"
#include "AutoConnectCredential.h"
extern "C" {
#include <user_interface.h>
}
#elif defined(ARDUINO_ARCH_ESP32)
#include <esp_spi_flash.h>
#include <WiFi.h>
#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 = {
"</html>"
};
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) {

Loading…
Cancel
Save