Skip to content

Commit c4c563f

Browse files
committed
added modeless mode
1 parent 5cf84a0 commit c4c563f

File tree

3 files changed

+139
-11
lines changed

3 files changed

+139
-11
lines changed

ESPAsyncWiFiManager.cpp

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ const char* AsyncWiFiManagerParameter::getCustomHTML() {
6262

6363
AsyncWiFiManager::AsyncWiFiManager(AsyncWebServer *server, DNSServer *dns) :server(server), dnsServer(dns) {
6464
wifiSSIDs = NULL;
65-
wifiSSIDscan=true;
65+
wifiSSIDscan=true;
66+
_modeless=false;
6667
}
6768

6869
void AsyncWiFiManager::addParameter(AsyncWiFiManagerParameter *p) {
@@ -215,6 +216,84 @@ if (wifiSSIDscan)
215216
}
216217
}
217218
}
219+
220+
221+
void AsyncWiFiManager::startConfigPortalModeless(char const *apName, char const *apPassword) {
222+
223+
_modeless =true;
224+
225+
226+
227+
//setup AP
228+
WiFi.mode(WIFI_AP_STA);
229+
DEBUG_WM("SET AP STA");
230+
231+
// try to connect
232+
if (connectWifi("", "") == WL_CONNECTED) {
233+
DEBUG_WM(F("IP Address:"));
234+
DEBUG_WM(WiFi.localIP());
235+
//connected
236+
237+
}
238+
239+
240+
_apName = apName;
241+
_apPassword = apPassword;
242+
243+
//notify we entered AP mode
244+
if ( _apcallback != NULL) {
245+
_apcallback(this);
246+
}
247+
248+
connect = false;
249+
setupConfigPortal();
250+
int scannow= -1 ;
251+
252+
}
253+
254+
void AsyncWiFiManager::loop(){
255+
dnsServer->processNextRequest();
256+
if (_modeless)
257+
{
258+
if ( millis() > scannow + 60000)
259+
{
260+
DEBUG_WM(F("About to scan()"));
261+
scan();
262+
scannow= millis() ;
263+
}
264+
if (connect) {
265+
connect = false;
266+
//delay(2000);
267+
DEBUG_WM(F("Connecting to new AP"));
268+
269+
// using user-provided _ssid, _pass in place of system-stored ssid and pass
270+
if (connectWifi(_ssid, _pass) != WL_CONNECTED) {
271+
DEBUG_WM(F("Failed to connect."));
272+
} else {
273+
//connected
274+
// alanswx - should we have a config to decide if we should shut down AP?
275+
// WiFi.mode(WIFI_STA);
276+
//notify that configuration has changed and any optional parameters should be saved
277+
if ( _savecallback != NULL) {
278+
//todo: check if any custom parameters actually exist, and check if they really changed maybe
279+
_savecallback();
280+
}
281+
return;
282+
}
283+
284+
if (_shouldBreakAfterConfig) {
285+
//flag set to exit after config after trying to connect
286+
//notify that configuration has changed and any optional parameters should be saved
287+
if ( _savecallback != NULL) {
288+
//todo: check if any custom parameters actually exist, and check if they really changed maybe
289+
_savecallback();
290+
}
291+
return;
292+
}
293+
}
294+
}
295+
}
296+
218297
boolean AsyncWiFiManager::startConfigPortal(char const *apName, char const *apPassword) {
219298
//setup AP
220299
WiFi.mode(WIFI_AP_STA);
@@ -235,15 +314,15 @@ boolean AsyncWiFiManager::startConfigPortal(char const *apName, char const *apP
235314
//DNS
236315
dnsServer->processNextRequest();
237316

238-
//
239-
// we should do a scan every so often here
240-
//
241-
if ( millis() > scannow + 10000)
242-
{
243-
DEBUG_WM(F("About to scan()"));
244-
scan();
245-
scannow= millis() ;
246-
}
317+
//
318+
// we should do a scan every so often here
319+
//
320+
if ( millis() > scannow + 10000)
321+
{
322+
DEBUG_WM(F("About to scan()"));
323+
scan();
324+
scannow= millis() ;
325+
}
247326

248327

249328
if (connect) {

ESPAsyncWiFiManager.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ class AsyncWiFiManager
8585
AsyncWiFiManager(AsyncWebServer * server, DNSServer *dns);
8686

8787
void scan();
88-
88+
void loop();
89+
8990
boolean autoConnect();
9091
boolean autoConnect(char const *apName, char const *apPassword = NULL);
9192

9293
//if you want to always start the config portal, without trying to connect first
9394
boolean startConfigPortal(char const *apName, char const *apPassword = NULL);
95+
void startConfigPortalModeless(char const *apName, char const *apPassword);
9496

9597
// get the AP name of the config portal, so it can be used in the callback
9698
String getConfigPortalSSID();
@@ -133,6 +135,10 @@ class AsyncWiFiManager
133135
DNSServer *dnsServer;
134136
AsyncWebServer *server;
135137

138+
139+
boolean _modeless;
140+
int scannow;
141+
136142
//const int WM_DONE = 0;
137143
//const int WM_WAIT = 10;
138144

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
2+
3+
//needed for library
4+
#include <DNSServer.h>
5+
#include <ESPAsyncWebServer.h>
6+
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
7+
8+
AsyncWebServer server(80);
9+
DNSServer dns;
10+
AsyncWiFiManager wifiManager(&server,&dns);
11+
12+
void setup() {
13+
// put your setup code here, to run once:
14+
Serial.begin(115200);
15+
16+
//WiFiManager
17+
//Local intialization. Once its business is done, there is no need to keep it around
18+
//reset saved settings
19+
//wifiManager.resetSettings();
20+
21+
//set custom ip for portal
22+
//wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
23+
24+
//fetches ssid and pass from eeprom and tries to connect
25+
//if it does not connect it starts an access point with the specified name
26+
//here "AutoConnectAP"
27+
//and goes into a blocking loop awaiting configuration
28+
//wifiManager.autoConnect("AutoConnectAP");
29+
//or use this for auto generated name ESP + ChipID
30+
//wifiManager.autoConnect();
31+
wifiManager.startConfigPortalModeless("ModelessAP", "Password");
32+
33+
34+
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
35+
request->send(200, "text/plain", "Hello World");
36+
});
37+
38+
}
39+
40+
void loop() {
41+
// put your main code here, to run repeatedly:
42+
wifiManager.loop();
43+
}

0 commit comments

Comments
 (0)