Explorar o código

added modeless mode

Alan Steremberg %!s(int64=8) %!d(string=hai) anos
pai
achega
c4c563f229
Modificáronse 3 ficheiros con 139 adicións e 11 borrados
  1. 89 10
      ESPAsyncWiFiManager.cpp
  2. 7 1
      ESPAsyncWiFiManager.h
  3. 43 0
      examples/ModelessConnect/ModelessConnect.ino

+ 89 - 10
ESPAsyncWiFiManager.cpp

@@ -62,7 +62,8 @@ const char* AsyncWiFiManagerParameter::getCustomHTML() {
 
 AsyncWiFiManager::AsyncWiFiManager(AsyncWebServer *server, DNSServer *dns) :server(server), dnsServer(dns) {
   wifiSSIDs = NULL;
-wifiSSIDscan=true;
+  wifiSSIDscan=true;
+  _modeless=false;
 }
 
 void AsyncWiFiManager::addParameter(AsyncWiFiManagerParameter *p) {
@@ -215,6 +216,84 @@ if (wifiSSIDscan)
 }
 }
 }
+
+
+void AsyncWiFiManager::startConfigPortalModeless(char const *apName, char const *apPassword) {
+
+  _modeless =true;
+  
+  
+  
+  //setup AP
+  WiFi.mode(WIFI_AP_STA);
+  DEBUG_WM("SET AP STA");
+
+  // try to connect
+	if (connectWifi("", "") == WL_CONNECTED)   {
+	DEBUG_WM(F("IP Address:"));
+	DEBUG_WM(WiFi.localIP());
+	//connected
+	
+	}
+
+  
+  _apName = apName;
+  _apPassword = apPassword;
+
+  //notify we entered AP mode
+  if ( _apcallback != NULL) {
+    _apcallback(this);
+  }
+
+  connect = false;
+  setupConfigPortal();
+  int scannow= -1 ;
+
+}
+
+void AsyncWiFiManager::loop(){
+    dnsServer->processNextRequest();
+    if (_modeless)
+    {
+		if ( millis() > scannow + 60000)
+		{
+		DEBUG_WM(F("About to scan()"));
+		scan();
+		scannow= millis() ;
+		}
+		if (connect) {
+		  connect = false;
+		  //delay(2000);
+		  DEBUG_WM(F("Connecting to new AP"));
+
+		  // using user-provided  _ssid, _pass in place of system-stored ssid and pass
+		  if (connectWifi(_ssid, _pass) != WL_CONNECTED) {
+			DEBUG_WM(F("Failed to connect."));
+		  } else {
+			//connected
+			// alanswx - should we have a config to decide if we should shut down AP?
+			// WiFi.mode(WIFI_STA);
+			//notify that configuration has changed and any optional parameters should be saved
+			if ( _savecallback != NULL) {
+			  //todo: check if any custom parameters actually exist, and check if they really changed maybe
+			  _savecallback();
+			}
+			return;
+		  }
+
+		  if (_shouldBreakAfterConfig) {
+			//flag set to exit after config after trying to connect
+			//notify that configuration has changed and any optional parameters should be saved
+			if ( _savecallback != NULL) {
+			  //todo: check if any custom parameters actually exist, and check if they really changed maybe
+			  _savecallback();
+			}
+			return;
+		  }
+		}
+   }
+}
+
 boolean  AsyncWiFiManager::startConfigPortal(char const *apName, char const *apPassword) {
   //setup AP
   WiFi.mode(WIFI_AP_STA);
@@ -235,15 +314,15 @@ boolean  AsyncWiFiManager::startConfigPortal(char const *apName, char const *apP
     //DNS
     dnsServer->processNextRequest();
 
-//
-//  we should do a scan every so often here
-//
-if ( millis() > scannow + 10000)
-{
-DEBUG_WM(F("About to scan()"));
-scan();
-scannow= millis() ;
-}
+	//
+	//  we should do a scan every so often here
+	//
+	if ( millis() > scannow + 10000)
+	{
+	DEBUG_WM(F("About to scan()"));
+	scan();
+	scannow= millis() ;
+	}
 
 
     if (connect) {

+ 7 - 1
ESPAsyncWiFiManager.h

@@ -85,12 +85,14 @@ class AsyncWiFiManager
     AsyncWiFiManager(AsyncWebServer * server, DNSServer *dns);
 
     void          scan();
-
+    void          loop();
+     
     boolean       autoConnect();
     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);
+    void startConfigPortalModeless(char const *apName, char const *apPassword);
 
     // get the AP name of the config portal, so it can be used in the callback
     String        getConfigPortalSSID();
@@ -133,6 +135,10 @@ class AsyncWiFiManager
     DNSServer      *dnsServer;
     AsyncWebServer *server;
 
+
+    boolean         _modeless;
+    int             scannow;
+    
     //const int     WM_DONE                 = 0;
     //const int     WM_WAIT                 = 10;
 

+ 43 - 0
examples/ModelessConnect/ModelessConnect.ino

@@ -0,0 +1,43 @@
+#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
+
+//needed for library
+#include <DNSServer.h>
+#include <ESPAsyncWebServer.h>
+#include <ESPAsyncWiFiManager.h>         //https://github.com/tzapu/WiFiManager
+
+AsyncWebServer server(80);
+DNSServer dns;
+AsyncWiFiManager wifiManager(&server,&dns);
+
+void setup() {
+    // put your setup code here, to run once:
+    Serial.begin(115200);
+
+    //WiFiManager
+    //Local intialization. Once its business is done, there is no need to keep it around
+    //reset saved settings
+    //wifiManager.resetSettings();
+    
+    //set custom ip for portal
+    //wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
+
+    //fetches ssid and pass from eeprom and tries to connect
+    //if it does not connect it starts an access point with the specified name
+    //here  "AutoConnectAP"
+    //and goes into a blocking loop awaiting configuration
+    //wifiManager.autoConnect("AutoConnectAP");
+    //or use this for auto generated name ESP + ChipID
+    //wifiManager.autoConnect();
+    wifiManager.startConfigPortalModeless("ModelessAP", "Password");
+
+
+ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
+    request->send(200, "text/plain", "Hello World");
+  });
+    
+}
+
+void loop() {
+    // put your main code here, to run repeatedly:
+    wifiManager.loop();
+}