mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-15 15:41:21 +10:00
added example for on demand config portal
cleanup
This commit is contained in:
+11
-10
@@ -49,23 +49,24 @@ void WiFiManager::addParameter(WiFiManagerParameter *p) {
|
||||
DEBUG_WM(p->getID());
|
||||
}
|
||||
|
||||
void WiFiManager::setupConfigPortal(char const *apName, char const *apPasswd) {
|
||||
void WiFiManager::setupConfigPortal(char const *apName, char const *apPassword) {
|
||||
dnsServer.reset(new DNSServer());
|
||||
server.reset(new ESP8266WebServer(80));
|
||||
|
||||
DEBUG_WM(F(""));
|
||||
_apName = apName;
|
||||
_apPasswd = apPasswd;
|
||||
_apPassword = apPassword;
|
||||
start = millis();
|
||||
|
||||
DEBUG_WM(F("Configuring access point... "));
|
||||
DEBUG_WM(_apName);
|
||||
if (_apPasswd != NULL) {
|
||||
if (strlen(_apPasswd) < 8 || strlen(_apPasswd) > 63) {
|
||||
if (_apPassword != NULL) {
|
||||
if (strlen(_apPassword) < 8 || strlen(_apPassword) > 63) {
|
||||
// fail passphrase to short or long!
|
||||
DEBUG_WM(F("Invalid AccessPoint password"));
|
||||
DEBUG_WM(F("Invalid AccessPoint password. Ignoring"));
|
||||
_apPassword = NULL;
|
||||
}
|
||||
DEBUG_WM(_apPasswd);
|
||||
DEBUG_WM(_apPassword);
|
||||
}
|
||||
|
||||
//optional soft ip config
|
||||
@@ -74,8 +75,8 @@ void WiFiManager::setupConfigPortal(char const *apName, char const *apPasswd) {
|
||||
WiFi.softAPConfig(_ip, _gw, _sn);
|
||||
}
|
||||
|
||||
if (_apPasswd != NULL) {
|
||||
WiFi.softAP(_apName, _apPasswd);//password option
|
||||
if (_apPassword != NULL) {
|
||||
WiFi.softAP(_apName, _apPassword);//password option
|
||||
} else {
|
||||
WiFi.softAP(_apName);
|
||||
}
|
||||
@@ -136,7 +137,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
|
||||
|
||||
connect = false;
|
||||
setupConfigPortal(apName, apPassword);
|
||||
|
||||
DEBUG_WM("start loop");
|
||||
while (timeout == 0 || millis() < start + timeout) {
|
||||
//DNS
|
||||
dnsServer->processNextRequest();
|
||||
@@ -168,7 +169,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
|
||||
|
||||
server.reset();
|
||||
dnsServer.reset();
|
||||
|
||||
DEBUG_WM("end loop");
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -59,7 +59,7 @@ class WiFiManager
|
||||
WiFiManager();
|
||||
|
||||
boolean autoConnect();
|
||||
boolean autoConnect(char const *apName, char const *apPasswd = NULL);
|
||||
boolean autoConnect(char const *apName, char const *apPassword = NULL);
|
||||
|
||||
//if you want to always start the config portal, without trying to connect first
|
||||
boolean startConfigPortal(char const *apName, char const *apPassword = NULL);
|
||||
@@ -96,11 +96,11 @@ class WiFiManager
|
||||
|
||||
const String HTTP_HEAD = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>{v}</title>";
|
||||
|
||||
void setupConfigPortal(char const *apName, char const *apPasswd);
|
||||
void setupConfigPortal(char const *apName, char const *apPassword);
|
||||
|
||||
int _eepromStart;
|
||||
const char* _apName = "no-net";
|
||||
const char* _apPasswd = NULL;
|
||||
const char* _apPassword = NULL;
|
||||
String _ssid = "";
|
||||
String _pass = "";
|
||||
unsigned long timeout = 0;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||
|
||||
//needed for library
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||
|
||||
|
||||
//callback notifying us of the need to save config
|
||||
void saveConfigCallback () {
|
||||
Serial.println("Should save config");
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.println("\n Starting");
|
||||
|
||||
//read gpio 4 state on startup
|
||||
pinMode(4, INPUT);
|
||||
int pinState = digitalRead(4);
|
||||
|
||||
//if gpio 4 is high startup config mode
|
||||
if (pinState == LOW) {
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
|
||||
//set config save notify callback
|
||||
wifiManager.setSaveConfigCallback(saveConfigCallback);
|
||||
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
//in seconds
|
||||
//wifiManager.setTimeout(120);
|
||||
|
||||
//it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
|
||||
//WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
if (!wifiManager.startConfigPortal("OnDemandAP")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
delay(3000);
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user