Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
],
"license": "MIT",
"frameworks": "arduino",
"platforms": ["espressif8266"]
"platforms": ["espressif8266", "espressif32"]
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ sentence=Minimal WiFi Manager with captive portal.
paragraph=Library for WiFi managemnent and credential storage for ESP8266 boards.
category=Communication
url=https://github.com/localrice/EasyWiFi
architectures=esp8266
architectures=esp8266,esp32
19 changes: 16 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
default_envs = nodemcuv2

[env]
framework = arduino
upload_speed = 115200
monitor_speed = 115200

; ESP8266
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
board_build.filesystem = littlefs

upload_speed = 115200
monitor_speed = 115200
; ESP32
[env:esp32]
platform = espressif32
board = esp32dev
lib_deps =
LITTLEFS_esp32
board_build.filesystem = littlefs
20 changes: 8 additions & 12 deletions src/EasyWiFi.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "EasyWiFi.h"
#include <algorithm>

ESP8266WebServer server(80);
DNSServer dnsServer;
const byte DNS_PORT = 53;

const char* const EasyWiFi::CREDENTIAL_FILE = "/wifi_credentials.txt";

const char *defaultCSS = R"rawliteral(
Expand Down Expand Up @@ -143,8 +139,8 @@ EasyWiFi::EasyWiFi() {}
*/
void EasyWiFi::begin() {
Serial.println("EasyWiFi: begin()");
if (!LittleFS.begin()) {
Serial.println("EasyWiFi: Failed to mount LittleFS");
if (!LittleFS.begin(true)) {
Serial.println("EasyWiFi: Failed to mount FS");
return;
}
loadCredentials();
Expand Down Expand Up @@ -237,7 +233,7 @@ void EasyWiFi::tryConnect(bool preferNext) {
}
}

const unsigned long timeout = 10000;
const unsigned long timeout = 10000;
for (size_t offset = 0; offset < totalNetworks; ++offset) {
const size_t index = (startIndex + offset) % totalNetworks;
applyActiveCredential(index);
Expand All @@ -262,7 +258,7 @@ void EasyWiFi::startPortal() {
WiFi.softAP(APName, APPassword);
Serial.printf("AP started: %s (secured) \n",APName);
} else {
WiFi.softAP(APName);
WiFi.softAP(APName);
Serial.printf("AP started: %s (open) \n",APName);
}

Expand All @@ -282,7 +278,7 @@ void EasyWiFi::startPortal() {
server.send(200, "text/html", html);
});


server.on("/save", HTTP_POST, [this]() {
String newSsid = server.arg("ssid");
String newPassword = server.arg("password");
Expand All @@ -296,9 +292,9 @@ void EasyWiFi::startPortal() {
server.send(400, "text/html", "<h1>SSID cannot be empty</h1>");
}
});

server.on("/scan", [this]() {
int networks = WiFi.scanNetworks();
int networks = WiFi.scanNetworks();
Serial.printf("Scan complete: %d", networks);
String json = "[";

Expand Down Expand Up @@ -424,7 +420,7 @@ void EasyWiFi::loadCredentials() {
if (line.length() > 0) {
credentials.push_back({line, legacyPassword});
}
break;
break;
}

credentials.push_back({line.substring(0, separatorIndex), line.substring(separatorIndex + 1)});
Expand Down
38 changes: 27 additions & 11 deletions src/EasyWiFi.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
#ifndef EASYWIFI_H
#define EASYWIFI_H

#include <Arduino.h>
#include <LittleFS.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <vector>
#include <functional>
#ifndef EASYWIFI_H
#define EASYWIFI_H

#include <Arduino.h>
#include <vector>
#include <functional>

#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <LittleFS.h>
using EasyWiFiWebServer = ESP8266WebServer;
#elif defined(ESP32)
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <LittleFS.h>
using EasyWiFiWebServer = WebServer;
#else
#error "EasyWiFi: supports only ESP8266 and ESP32 platforms"
#endif

class EasyWiFi {
public:
Expand All @@ -20,7 +32,7 @@ class EasyWiFi {
void saveCredentials(const char* networkSsid, const char* networkPassword);
void loadCredentials();
void printCredentials();

// to set Access Point (AP) credentials.
void setAP(const char* name, const char* password = "");

Expand Down Expand Up @@ -76,6 +88,10 @@ class EasyWiFi {
std::vector<Credential> credentials;
int activeCredentialIndex = -1;

EasyWiFiWebServer server{80};
DNSServer dnsServer;
const byte DNS_PORT = 53;

void tryConnect(bool preferNext = false);
void startPortal();
void stopPortal();
Expand Down