Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update feature asyncwebserver and fix network scanning #1389

Open
wants to merge 41 commits into
base: feature_asyncwebserver
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c987ee9
abstract httpsend
Feb 23, 2022
e9d0012
abstract httpserver setup
Feb 23, 2022
8ecf057
remove old content length headers
Feb 24, 2022
d8c81ec
macro test
Feb 24, 2022
fd7f400
Revert "Both infoids and _ap_password are intialized by check time. (…
Feb 24, 2022
0aecb22
Fix old sdk esp8266 error w softapssid
Feb 24, 2022
e6d21b8
use macro for webserver routes fstring helper
Feb 24, 2022
713fcfc
disable country in example
tablatronix Mar 10, 2022
8f2c7ae
remove test
tablatronix Mar 10, 2022
acdad80
add callback debuggin
tablatronix Mar 11, 2022
e830fa2
Adding a data attribute for ssid for input value
tablatronix Mar 11, 2022
0d61a6b
esp32 crashes on cb route shared pointer
tablatronix Mar 11, 2022
0994c4b
regression, webserver callbacks before reset crashes
tablatronix Mar 11, 2022
1f3b2e3
typo
tablatronix Mar 14, 2022
d70f358
add check to dissallow starting cp if already running
tablatronix Mar 14, 2022
eb5ba12
#1371
tablatronix Mar 14, 2022
47ce011
Merge branch 'master' of https://github.com/tzapu/WiFiManager
tablatronix Mar 14, 2022
b436830
remove bad example
tablatronix Mar 14, 2022
e9a02d4
add some button feedback
tablatronix Mar 25, 2022
da02b59
update template styles, add versionng to info
tablatronix Mar 25, 2022
9cec5a2
bump version
tablatronix Mar 25, 2022
9182622
oops
tablatronix Mar 26, 2022
278c57b
fixes #1379
tablatronix Mar 28, 2022
ea4ee02
Output "Unknown" if Arduino/ESP-IDF version is unknown (#1382)
thorrak Mar 29, 2022
88b97c7
#1383 fix
tablatronix Mar 29, 2022
6a85acd
add customMenuHTML
tablatronix Mar 29, 2022
ba63365
Merge branch 'master' of https://github.com/tzapu/WiFiManager
tablatronix Mar 29, 2022
9cd81e3
debug
tablatronix Mar 29, 2022
918e02d
add resetsetting delay, some complaints its not working shrug
tablatronix Mar 29, 2022
c939374
Merge branch 'master' of https://github.com/tzapu/WiFiManager
tablatronix Mar 29, 2022
1e62862
fixes #1386
tablatronix Apr 7, 2022
379ffee
clean up variable declarations
tablatronix Apr 7, 2022
590e212
better fix $1386
tablatronix Apr 7, 2022
7bffda3
adds option to keep cfgp open on wifi save
tablatronix Apr 9, 2022
5b78c6c
Merge branch 'master' into feature_asyncwebserver
ovidiupruteanu Apr 9, 2022
cb86f7a
Fix merge and bring it closer to master
ovidiupruteanu Apr 10, 2022
bb26713
Set _preloadwifiscan and _asyncScan to true
ovidiupruteanu Apr 10, 2022
d831e89
Revert removal of hardcoded async = true in WiFi_scanNetworks
ovidiupruteanu Apr 11, 2022
483b834
Fix #1383
tablatronix Apr 12, 2022
26badef
Add .idea to .gitignore for JetBrains IDEs
ovidiupruteanu Apr 12, 2022
943475a
Merge branch 'master' into feature_asyncwebserver
ovidiupruteanu Apr 12, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.cache
.pioenvs
.piolibdeps
.idea
.vscode
!.vscode/extensions.json
/platformio_override.ini
Expand Down
337 changes: 192 additions & 145 deletions WiFiManager.cpp

Large diffs are not rendered by default.

119 changes: 66 additions & 53 deletions WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@
#define WM_WEBSERVERSHIM // use webserver shim lib
#define WM_ASYNCWEBSERVER // use async webserver

#define G(string_literal) (String(FPSTR(string_literal)).c_str())

#define STRING2(x) #x
#define STRING(x) STRING2(x)

// #include <esp_idf_version.h>
#ifdef ESP_IDF_VERSION
// #pragma message "ESP_IDF_VERSION_MAJOR = " STRING(ESP_IDF_VERSION_MAJOR)
// #pragma message "ESP_IDF_VERSION_MINOR = " STRING(ESP_IDF_VERSION_MINOR)
// #pragma message "ESP_IDF_VERSION_PATCH = " STRING(ESP_IDF_VERSION_PATCH)
#define VER_IDF_STR STRING(ESP_IDF_VERSION_MAJOR) "." STRING(ESP_IDF_VERSION_MINOR) "." STRING(ESP_IDF_VERSION_PATCH)
#endif

// #include "esp_arduino_version.h"
#ifdef ESP_ARDUINO_VERSION
// #pragma message "ESP_ARDUINO_VERSION_MAJOR = " STRING(ESP_ARDUINO_VERSION_MAJOR)
// #pragma message "ESP_ARDUINO_VERSION_MINOR = " STRING(ESP_ARDUINO_VERSION_MINOR)
// #pragma message "ESP_ARDUINO_VERSION_PATCH = " STRING(ESP_ARDUINO_VERSION_PATCH)
#define VER_ARDUINO_STR STRING(ESP_ARDUINO_VERSION_MAJOR) "." STRING(ESP_ARDUINO_VERSION_MINOR) "." STRING(ESP_ARDUINO_VERSION_PATCH)
#else
// #include <core_version.h>
// #pragma message "ESP_ARDUINO_VERSION_GIT = " STRING(ARDUINO_ESP32_GIT_VER)// 0x46d5afb1
// #pragma message "ESP_ARDUINO_VERSION_DESC = " STRING(ARDUINO_ESP32_GIT_DESC) // 1.0.6
#define VER_ARDUINO_STR "Unknown"
// #pragma message "ESP_ARDUINO_VERSION_REL = " STRING(ARDUINO_ESP32_RELEASE) //"1_0_6"
#endif

#ifdef ESP8266

extern "C" {
Expand All @@ -70,28 +97,6 @@

#elif defined(ESP32)

// #define STRING2(x) #x
// #define STRING(x) STRING2(x)

// // #include <esp_idf_version.h>
// #ifdef ESP_IDF_VERSION
// #pragma message "ESP_IDF_VERSION_MAJOR = " STRING(ESP_IDF_VERSION_MAJOR)
// #pragma message "ESP_IDF_VERSION_MINOR = " STRING(ESP_IDF_VERSION_MINOR)
// #pragma message "ESP_IDF_VERSION_PATCH = " STRING(ESP_IDF_VERSION_PATCH)
// #endif

// // #include "esp_arduino_version.h"
// #ifdef ESP_ARDUINO_VERSION
// #pragma message "ESP_ARDUINO_VERSION_MAJOR = " STRING(ESP_ARDUINO_VERSION_MAJOR)
// #pragma message "ESP_ARDUINO_VERSION_MINOR = " STRING(ESP_ARDUINO_VERSION_MINOR)
// #pragma message "ESP_ARDUINO_VERSION_PATCH = " STRING(ESP_ARDUINO_VERSION_PATCH)
// #else
// #include <core_version.h>
// #pragma message "ESP_ARDUINO_VERSION_GIT = " STRING(ARDUINO_ESP32_GIT_VER)// 0x46d5afb1
// #pragma message "ESP_ARDUINO_VERSION_DESC = " STRING(ARDUINO_ESP32_GIT_DESC) // 1.0.6
// // #pragma message "ESP_ARDUINO_VERSION_REL = " STRING(ARDUINO_ESP32_RELEASE) //"1_0_6"
// #endif

#include <WiFi.h>
#include <esp_wifi.h>
#include <Update.h>
Expand All @@ -113,6 +118,7 @@
#endif
#endif
#endif

#ifdef WM_ERASE_NVS
#include <nvs.h>
#include <nvs_flash.h>
Expand Down Expand Up @@ -301,9 +307,12 @@ class WiFiManager
// setConfigPortalTimeout is ignored in this mode, user is responsible for closing configportal
void setConfigPortalBlocking(boolean shouldBlock);

//add custom html at inside <head> for all pages
void setCustomHeadElement(const char* html);

//if this is set, customise style
void setCustomHeadElement(const char* element);
void setCustomMenuHTML(const char* html);

//if this is true, remove duplicated Access Points - defaut true
void setRemoveDuplicateAPs(boolean removeDuplicates);

Expand Down Expand Up @@ -336,7 +345,10 @@ class WiFiManager

// if true (default) then start the config portal from autoConnect if connection failed
void setEnableConfigPortal(boolean enable);


// if true (default) then stop the config portal from autoConnect when wifi is saved
void setDisableConfigPortal(boolean enable);

// set a custom hostname, sets sta and ap dhcp client id for esp32, and sta for esp8266
bool setHostname(const char * hostname);
bool setHostname(String hostname);
Expand Down Expand Up @@ -393,7 +405,7 @@ class WiFiManager
void debugPlatformInfo();

// helper for html
String htmlEntities(String str);
String htmlEntities(String str, bool whitespace = false);

// set the country code for wifi settings, CN
void setCountry(String cc);
Expand Down Expand Up @@ -441,8 +453,8 @@ class WiFiManager

std::unique_ptr<WM_WebServer> server;


private:
// vars
std::vector<uint8_t> _menuIds;
std::vector<const char *> _menuIdsParams = {"wifi","param","info","exit"};
std::vector<const char *> _menuIdsUpdate = {"wifi","param","info","update","exit"};
Expand All @@ -457,9 +469,16 @@ class WiFiManager
IPAddress _sta_static_sn;
IPAddress _sta_static_dns;

unsigned long _configPortalStart = 0; // ms config portal start time (updated for timeouts)
unsigned long _webPortalAccessed = 0; // ms last web access time
uint8_t _lastconxresult = WL_IDLE_STATUS; // store last result when doing connect operations
int _numNetworks = 0; // init index for numnetworks wifiscans
unsigned long _lastscan = 0; // ms for timing wifi scans
unsigned long _startscan = 0; // ms for timing wifi scans
unsigned long _startconn = 0; // ms for timing wifi connects

// defaults
const byte DNS_PORT = 53;
const byte HTTP_PORT = 80;
String _apName = "no-net";
String _apPassword = "";
String _ssid = ""; // var temp ssid
Expand All @@ -471,32 +490,26 @@ class WiFiManager
unsigned long _configPortalTimeout = 0; // ms close config portal loop if set (depending on _cp/webClientCheck options)
unsigned long _connectTimeout = 0; // ms stop trying to connect to ap if set
unsigned long _saveTimeout = 0; // ms stop trying to connect to ap on saves, in case bugs in esp waitforconnectresult
unsigned long _configPortalStart = 0; // ms config portal start time (updated for timeouts)
unsigned long _webPortalAccessed = 0; // ms last web access time

WiFiMode_t _usermode = WIFI_STA; // Default user mode
String _wifissidprefix = FPSTR(S_ssidpre); // auto apname prefix prefix+chipid
uint8_t _lastconxresult = WL_IDLE_STATUS; // store last result when doing connect operations
int _numNetworks = 0; // init index for numnetworks wifiscans
unsigned long _lastscan = 0; // ms for timing wifi scans
unsigned long _startscan = 0; // ms for timing wifi scans
int _cpclosedelay = 2000; // delay before wifisave, prevents captive portal from closing to fast.
bool _cleanConnect = false; // disconnect before connect in connectwifi, increases stability on connects
bool _connectonsave = true; // connect to wifi when saving creds
bool _disableSTA = false; // disable sta when starting ap, always
bool _disableSTAConn = true; // disable sta when starting ap, if sta is not connected ( stability )
bool _channelSync = false; // use same wifi sta channel when starting ap
int32_t _apChannel = 0; // channel to use for ap
int32_t _apChannel = 0; // default channel to use for ap, 0 for auto
bool _apHidden = false; // store softap hidden value
uint16_t _httpPort = 80; // port for webserver
// uint8_t _retryCount = 0; // counter for retries, probably not needed if synchronous
uint8_t _connectRetries = 1; // number of sta connect retries, force reconnect, wait loop (connectimeout) does not always work and first disconnect bails
unsigned long _startconn = 0; // ms for timing wifi connects
bool _aggresiveReconn = false; // use an agrressive reconnect strategy, WILL delay conxs
// on some conn failure modes will add delays and many retries to work around esp and ap bugs, ie, anti de-auth protections
bool _allowExit = true; // allow exit non blocking

#ifdef ESP32
wifi_event_id_t wm_event_id;
wifi_event_id_t wm_event_id = 0;
static uint8_t _lastconxresulttmp; // tmp var for esp32 callback
#endif

Expand All @@ -506,8 +519,8 @@ class WiFiManager

// parameter options
int _minimumQuality = -1; // filter wifiscan ap by this rssi
int _staShowStaticFields = 0; // ternary 1=always show static ip fields, 0=only if set, -1=never(cannot change ips via web!)
int _staShowDns = 0; // ternary 1=always show dns, 0=only if set, -1=never(cannot change dns via web!)
int _staShowStaticFields = 0; // ternary 1=always show static ip fields, 0=only if set, -1=never(cannot change ips via web!)
int _staShowDns = 0; // ternary 1=always show dns, 0=only if set, -1=never(cannot change dns via web!)
boolean _removeDuplicateAPs = true; // remove dup aps from wifiscan
boolean _showPassword = false; // show or hide saved password on wifi form, might be a security issue!
boolean _shouldBreakAfterConfig = false; // stop configportal on save failure
Expand All @@ -522,10 +535,12 @@ class WiFiManager
boolean _showInfoErase = true; // info page erase button
boolean _showInfoUpdate = true; // info page update button
boolean _showBack = false; // show back button
boolean _enableConfigPortal = true; // use config portal if autoconnect failed
boolean _enableConfigPortal = true; // FOR autoconnect - start config portal if autoconnect failed
boolean _disableConfigPortal = true; // FOR autoconnect - stop config portal if cp wifi save
String _hostname = ""; // hostname for esp8266 for dhcp, and or MDNS

const char* _customHeadElement = ""; // store custom head element html from user
const char* _customHeadElement = ""; // store custom head element html from user isnide <head>
const char* _customMenuHTML = ""; // store custom head element html from user inside <>
String _bodyClass = ""; // class to add to body
String _title = FPSTR(S_brand); // app title - default WiFiManager

Expand All @@ -542,8 +557,8 @@ class WiFiManager
// async enables asyncronous scans, so they do not block anything
// the refresh button bypasses cache
// no aps found is problematic as scans are always going to want to run, leading to page load delays
boolean _preloadwifiscan = false; // preload wifiscan if true
boolean _asyncScan = false; // perform wifi network scan async
boolean _preloadwifiscan = true; // preload wifiscan if true
boolean _asyncScan = true; // perform wifi network scan async
unsigned int _scancachetime = 30000; // ms cache time for background scans

boolean _disableIpFields = false; // modify function of setShow_X_Fields(false), forces ip fields off instead of default show if set, eg. _staShowStaticFields=-1
Expand All @@ -559,8 +574,6 @@ class WiFiManager
void setupConfigPortal();
bool shutdownConfigPortal();
bool setupHostname(bool restart);
void setupHTTPServer();
void teardownHTTPServer();

#ifdef NO_EXTRA_4K_HEAP
boolean _tryWPS = false; // try WPS on save failure, unsupported
Expand All @@ -569,6 +582,8 @@ class WiFiManager

bool startAP();
void setupDNSD();
void setupHTTPServer();
void teardownHTTPServer();

uint8_t connectWifi(String ssid, String pass, bool connect = true);
bool setSTAConfig();
Expand All @@ -580,25 +595,23 @@ class WiFiManager
void updateConxResult(uint8_t status);

// webserver handlers
void HTTPSend(AsyncWebServerRequest *request, String content);
void handleRoot(AsyncWebServerRequest *request);
void handleWifi(AsyncWebServerRequest *request,bool scan);
void handleWifi(AsyncWebServerRequest *request, boolean scan);
void handleWifiSave(AsyncWebServerRequest *request);
void handleInfo(AsyncWebServerRequest *request);
void handleReset(AsyncWebServerRequest *request);
void handleNotFound(AsyncWebServerRequest *request);
void handleExit(AsyncWebServerRequest *request);
void handleClose(AsyncWebServerRequest *request);
// void handleErase(AsyncWebServerRequest *request);
void handleErase(AsyncWebServerRequest *request, bool opt);
void handleErase(AsyncWebServerRequest *request, boolean opt);
void handleParam(AsyncWebServerRequest *request);
void handleWiFiStatus(AsyncWebServerRequest *request);
void handleRequest();
void handleParamSave(AsyncWebServerRequest *request);
void doParamSave(AsyncWebServerRequest *request);

void handleRequest();
void HTTPSend(AsyncWebServerRequest *request, String page);


boolean captivePortal(AsyncWebServerRequest *request);
boolean configPortalHasTimeout();
uint8_t processConfigPortal();
Expand Down Expand Up @@ -665,8 +678,8 @@ class WiFiManager
String getInfoData(String id);

// flags
boolean connect;
boolean abort;
boolean connect = false;
boolean abort = false;
boolean reset = false;
boolean configPortalActive = false;
boolean webPortalActive = false;
Expand Down
2 changes: 1 addition & 1 deletion examples/Basic/Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void setup() {

// reset settings - wipe stored credentials for testing
// these are stored by the esp library
//wm.resetSettings();
wm.resetSettings();

// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ void setup() {
else {
Serial.println("Configportal running");
}

wm.startConfigPortal();
// wm.startWebPortal();
}

void loop() {
Expand Down
Loading