diff --git a/PersWiFiManager.cpp b/PersWiFiManager.cpp index 29fb5f0..abc2d04 100644 --- a/PersWiFiManager.cpp +++ b/PersWiFiManager.cpp @@ -5,69 +5,279 @@ #include "PersWiFiManager.h" +#if defined(ESP32) +#include +#endif + #ifdef WIFI_HTM_PROGMEM -const char wifi_htm[] PROGMEM = R"=====(ESP WiFi







Back |Home
)====="; +const char wifi_htm[] PROGMEM = R"=====( + + + + + + + ESP WiFi + + + + +
+ +

+
+ +
+ +

+
+ +

+ +
+

+
+ Back |Home +
+ + + +)====="; #endif -PersWiFiManager::PersWiFiManager(ESP8266WebServer& s, DNSServer& d) { +#if defined(ESP8266) +PersWiFiManager::PersWiFiManager(ESP8266WebServer& s, DNSServer& d): _connectHandler(nullptr), _apHandler(nullptr), _apCloseHandler(nullptr) { +#elif defined(ESP32) +PersWiFiManager::PersWiFiManager(WebServer& s, DNSServer& d): _connectHandler(nullptr), _apHandler(nullptr), _apCloseHandler(nullptr) { +#endif _server = &s; _dnsServer = &d; _apPass = ""; + _freshConnectionAttempt = false; } //PersWiFiManager bool PersWiFiManager::attemptConnection(const String& ssid, const String& pass) { //attempt to connect to wifi - WiFi.mode(WIFI_STA); + wl_status_t status; + if (ssid.length()) { +#if defined(ESP8266) + if(WiFi.status() == WL_CONNECTED && WiFi.SSID() == ssid && WiFi.psk() == pass) return true; // Already connected to this network +#elif defined(ESP32) + wifi_config_t conf; + esp_wifi_get_config(WIFI_IF_STA, &conf); // load wifi settings to struct conf + const char *SSID = reinterpret_cast(conf.sta.ssid); + const char *psk = reinterpret_cast(conf.sta.password); +if(WiFi.status() == WL_CONNECTED && String(SSID) == ssid && String(psk) == pass) Serial.println("ALREADY CON"); + if(WiFi.status() == WL_CONNECTED && String(SSID) == ssid && String(psk) == pass) return true; // Already connected to this network +#endif + + if(!(WiFi.getMode() & WIFI_STA)) WiFi.mode(wifi_mode_t(WiFi.getMode() | WIFI_STA)); // Add Station Mode to connect to router + resetSettings(); // To avoid issues (experience from WiFiManager) if (pass.length()) WiFi.begin(ssid.c_str(), pass.c_str()); else WiFi.begin(ssid.c_str()); + time_t start = millis(); + while(millis() - start < 5000) { + if((status = WiFi.status()) == WL_CONNECTED) break; + delay(100); + } + if(status != WL_CONNECTED) { + wifi_mode_t m = WiFi.getMode(); + WiFi.mode(WIFI_AP); // Remove Station Mode if connecting to router failed + if(!(m & WIFI_AP) && _apHandler) _apHandler(); + } } else { - WiFi.begin(); + if(getSsid() == "") { // No saved credentials, so skip trying to connect + _connectStartTime = millis(); + _freshConnectionAttempt = true; + wifi_mode_t m = WiFi.getMode(); + WiFi.mode(WIFI_AP); // Set AP Mode because connecting to router is not attempted + if(!(m & WIFI_AP) && _apHandler) _apHandler(); + return false; + } else { + if(!(WiFi.getMode() & WIFI_STA)) WiFi.mode(wifi_mode_t(WiFi.getMode() | WIFI_STA)); // Add Station Mode to connect to router + WiFi.begin(); + time_t start = millis(); + while(millis() - start < 5000) { + if((status = WiFi.status()) == WL_CONNECTED) break; + delay(100); + } + if(status != WL_CONNECTED) { + wifi_mode_t m = WiFi.getMode(); + WiFi.mode(WIFI_AP); // Remove Station Mode if connecting to router failed + if(!(m & WIFI_AP) && _apHandler) _apHandler(); + } + } } + if(status == WL_CONNECTED) _connectStartTime = millis(); //if in nonblock mode, skip this loop - _connectStartTime = millis();// + 1; while (!_connectNonBlock && _connectStartTime) { handleWiFi(); delay(10); } - return (WiFi.status() == WL_CONNECTED); - + return (status == WL_CONNECTED); } //attemptConnection void PersWiFiManager::handleWiFi() { + // If AP mode and no client connected, close AP mode if the ESP has connected to the router or if time is up + if((WiFi.getMode() & WIFI_AP) && (WiFi.softAPgetStationNum() == 0)) { + if((WiFi.status() == WL_CONNECTED) || (millis() - _apModeStartMillis > _apModeTimeoutMillis)) closeAp(); + } + if (!_connectStartTime) return; if (WiFi.status() == WL_CONNECTED) { _connectStartTime = 0; if (_connectHandler) _connectHandler(); + return; } - - //if failed or not connected and time is up - if ((WiFi.status() == WL_CONNECT_FAILED) || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { +/* + //if failed or no saved SSID or no WiFi credentials were found or not connected and time is up + if ((WiFi.status() == WL_CONNECT_FAILED) || _freshConnectionAttempt || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT)))) { startApMode(); _connectStartTime = 0; //reset connect start time + _freshConnectionAttempt = false; } - +*/ } //handleWiFi void PersWiFiManager::startApMode(){ - //start AP mode - IPAddress apIP(192, 168, 1, 1); - WiFi.mode(WIFI_AP); + IPAddress apIP(192, 168, 4, 1); + WiFi.mode(wifi_mode_t(WiFi.getMode() | WIFI_AP)); // Add AP Mode WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); _apPass.length() ? WiFi.softAP(getApSsid().c_str(), _apPass.c_str()) : WiFi.softAP(getApSsid().c_str()); if (_apHandler) _apHandler(); }//startApMode +void PersWiFiManager::closeAp(){ + wifi_mode_t m = WiFi.getMode(); + if(m & WIFI_AP) { + delay(100); + WiFi.mode(wifi_mode_t(m & ~WIFI_AP)); + if (_apCloseHandler) _apCloseHandler(); + } +}// closeAp + void PersWiFiManager::setConnectNonBlock(bool b) { _connectNonBlock = b; } //setConnectNonBlock +String PersWiFiManager::buildReport() { + String report; + report += "{\"connected\":"; + wl_status_t status = WiFi.status(); + IPAddress routerIP = WiFi.localIP(); // Sometimes the router IP is 0.0.0.0 even if it is connected + report += (status == WL_CONNECTED) ? "true" : "false"; + String SSID = getSsid(); + if(SSID != "") report += ",\"SSID\":\"" + SSID + '"'; + if(WiFi.getMode() & WIFI_AP) { + String ApSSID = getApSsid(); + if(ApSSID != "") report += ",\"APSSID\":\"" + ApSSID + '"'; + } + if((WiFi.getMode() & WIFI_STA) && (status == WL_CONNECTED)) report += ",\"IP\":\"" + routerIP.toString() + '"'; + report += "}"; + return report; +} //buildReport + +void PersWiFiManager::sendNoCacheHeaders() { + _server->sendHeader("Cache-Control", " no-cache, no-store, must-revalidate"); + _server->sendHeader("Expires", " 0"); +} //sendNoacheHeaders + void PersWiFiManager::setupWiFiHandlers() { - IPAddress apIP(192, 168, 1, 1); + IPAddress apIP(192, 168, 4, 1); _dnsServer->setErrorReplyCode(DNSReplyCode::NoError); _dnsServer->start((byte)53, "*", apIP); //used for captive portal in AP mode @@ -90,8 +300,13 @@ void PersWiFiManager::setupWiFiHandlers() { s.reserve(2050); for (int i = 0; i < n && s.length() < 2000; i++) { //check s.length to limit memory usage if (ix[i] != -1) { +#if defined(ESP8266) s += String(i ? "\n" : "") + ((constrain(WiFi.RSSI(ix[i]), -100, -50) + 100) * 2) + "," + ((WiFi.encryptionType(ix[i]) == ENC_TYPE_NONE) ? 0 : 1) + "," + WiFi.SSID(ix[i]); +#elif defined(ESP32) + s += String(i ? "\n" : "") + ((constrain(WiFi.RSSI(ix[i]), -100, -50) + 100) * 2) + "," + + ((WiFi.encryptionType(ix[i]) == WIFI_AUTH_OPEN) ? 0 : 1) + "," + WiFi.SSID(ix[i]); +#endif } } @@ -100,8 +315,18 @@ void PersWiFiManager::setupWiFiHandlers() { }); //_server->on /wifi/list _server->on("/wifi/connect", [&]() { - _server->send(200, "text/html", "connecting..."); +// dilemma: +// if attemptConnection() is called before send() then the client may send retries that messes things up +// if send() is called before attemptConnection() then the connection is not yet established, so a 'not connected' is reported +// Because now attemptConnection() is called first now, attemptConnection() first tests if it is already connected to the requested network. If it is, it returns inmediately, not causing retries +// I think, although I did not test it, that if a connection is made to one WiFi network, and with a client on that +// network it is tried to connect to another network, that still might mess things up +// The work around is: just don't do that :) attemptConnection(_server->arg("n"), _server->arg("p")); + String report = buildReport(); + sendNoCacheHeaders(); + _server->sendHeader("Content-Type", " application/json"); + _server->send(200, "text/html", report); }); //_server->on /wifi/connect _server->on("/wifi/ap", [&](){ @@ -109,33 +334,87 @@ void PersWiFiManager::setupWiFiHandlers() { startApMode(); }); //_server->on /wifi/ap + // Define an endpoint to close AP mode from the browser + _server->on("/wifi/closeap", [&](){ + _server->send(200, "text/htm", "OK"); + closeAp(); + }); //_server->on /wifi/closeap + _server->on("/wifi/rst", [&]() { _server->send(200, "text/html", "Rebooting..."); delay(100); //ESP.restart(); - // Adding Safer Restart method - ESP.wdtDisable(); - ESP.reset(); + // Adding Safer Restart method (for ESP8266) +#if defined(ESP8266) + ESP.wdtDisable(); + ESP.reset(); +#elif defined(ESP32) + ESP.restart(); +#endif delay(2000); }); + _server->on("/wifi/report", [&]() { + String report = buildReport(); + sendNoCacheHeaders(); + _server->sendHeader("Content-Type", " application/json"); + _server->send(200, "text/html", report); + }); + #ifdef WIFI_HTM_PROGMEM _server->on("/wifi.htm", [&]() { - _server->send(200, "text/html", wifi_htm); + sendNoCacheHeaders(); + _server->send(200, "text/html", wifi_htm); }); #endif + _server->begin(); }//setupWiFiHandlers -bool PersWiFiManager::begin(const String& ssid, const String& pass) { +bool PersWiFiManager::begin(const String& ssid, const String& pass, time_t apModeTimeoutSeconds) { + _apModeTimeoutMillis = 1000 * apModeTimeoutSeconds; + _apModeStartMillis = millis(); + WiFi.mode(WIFI_STA); setupWiFiHandlers(); return attemptConnection(ssid, pass); //switched order of these two for return } //begin +void PersWiFiManager::stop() { + _server->stop(); + _dnsServer->stop(); +}// close + +// Remove the WiFi credentials (e.g. for testing purposes) +void PersWiFiManager::resetSettings() { +#if defined(ESP8266) + WiFi.disconnect(); +#elif defined(ESP32) + wifi_mode_t m = WiFi.getMode(); + if(!(m & WIFI_MODE_STA)) WiFi.mode(WIFI_STA); + WiFi.disconnect(false, true); + if(!(m & WIFI_MODE_STA)) WiFi.mode(m); +#endif +} // resetSettings + String PersWiFiManager::getApSsid() { +#if defined(ESP8266) return _apSsid.length() ? _apSsid : "ESP8266"; +#elif defined(ESP32) + return _apSsid.length() ? _apSsid : "ESP32"; +#endif } //getApSsid +String PersWiFiManager::getSsid() { +#if defined(ESP8266) + return WiFi.SSID(); +#elif defined(ESP32) + wifi_config_t conf; + esp_wifi_get_config(WIFI_IF_STA, &conf); // load wifi settings to struct conf + const char *SSID = reinterpret_cast(conf.sta.ssid); + return String(SSID); +#endif +} //getSsid + void PersWiFiManager::setApCredentials(const String& apSsid, const String& apPass) { if (apSsid.length()) _apSsid = apSsid; if (apPass.length() >= 8) _apPass = apPass; @@ -149,4 +428,7 @@ void PersWiFiManager::onAp(WiFiChangeHandlerFunction fn) { _apHandler = fn; } +void PersWiFiManager::onApClose(WiFiChangeHandlerFunction fn) { + _apCloseHandler = fn; +} diff --git a/PersWiFiManager.h b/PersWiFiManager.h index 80fcf03..b3b9b83 100644 --- a/PersWiFiManager.h +++ b/PersWiFiManager.h @@ -1,11 +1,18 @@ #ifndef PERSWIFIMANAGER_H #define PERSWIFIMANAGER_H +#if defined(ESP8266) #include #include +#elif defined(ESP32) +#include +#include +#else +#error "Unknown board class" +#endif #include -#define WIFI_CONNECT_TIMEOUT 30 +//#define WIFI_CONNECT_TIMEOUT 30 class PersWiFiManager { @@ -13,16 +20,26 @@ class PersWiFiManager { typedef std::function WiFiChangeHandlerFunction; +#if defined(ESP8266) PersWiFiManager(ESP8266WebServer& s, DNSServer& d); +#elif defined(ESP32) + PersWiFiManager(WebServer& s, DNSServer& d); +#endif bool attemptConnection(const String& ssid = "", const String& pass = ""); void setupWiFiHandlers(); - bool begin(const String& ssid = "", const String& pass = ""); + bool begin(const String& ssid = "", const String& pass = "", time_t apModeTimeoutSeconds = 300); + + void stop(); + + void resetSettings(); String getApSsid(); + String getSsid(); + void setApCredentials(const String& apSsid, const String& apPass = ""); void setConnectNonBlock(bool b); @@ -31,21 +48,37 @@ class PersWiFiManager { void startApMode(); + void closeAp(); + void onConnect(WiFiChangeHandlerFunction fn); void onAp(WiFiChangeHandlerFunction fn); + void onApClose(WiFiChangeHandlerFunction fn); + private: +#if defined(ESP8266) ESP8266WebServer * _server; +#elif defined(ESP32) + WebServer * _server; +#endif DNSServer * _dnsServer; String _apSsid, _apPass; bool _connectNonBlock; unsigned long _connectStartTime; + bool _freshConnectionAttempt; WiFiChangeHandlerFunction _connectHandler; WiFiChangeHandlerFunction _apHandler; + WiFiChangeHandlerFunction _apCloseHandler; + + String buildReport(); + + void sendNoCacheHeaders(); + time_t _apModeTimeoutMillis; + time_t _apModeStartMillis; };//class #endif diff --git a/data/wifi.htm b/data/wifi.htm index 5d08372..eedf71d 100644 --- a/data/wifi.htm +++ b/data/wifi.htm @@ -1,108 +1,120 @@ - - - - ESP WiFi - - - - - -
- -

-
- -
- -
-
- -
-
-
- -
- -
- -
- Back | - Home -
- - + + + + + ESP WiFi + + + + +
+ +

+
+ +
+ +

+
+ +

+ +
+

+
+ Back |Home +
+ + diff --git a/data/wifi.min.htm b/data/wifi.min.htm index 49a4a55..12240bc 100644 --- a/data/wifi.min.htm +++ b/data/wifi.min.htm @@ -1 +1 @@ -ESP WiFi









Back | Home
\ No newline at end of file + ESP WiFi








Back |Home
diff --git a/examples/basic_rest_api/basic_rest_api.ino b/examples/basic_rest_api/basic_rest_api.ino index b2f22dc..90d0a6c 100644 --- a/examples/basic_rest_api/basic_rest_api.ino +++ b/examples/basic_rest_api/basic_rest_api.ino @@ -10,20 +10,37 @@ //includes #include -#include +// #include // ArduinoJson is a bit of an overkill for this use case +#if defined(ESP8266) #include #include #include -#include #include +#elif defined(ESP32) +#include +#include +#include +#include +#else +#error "Unsupported board class" +#endif +#include +#if defined(ESP8266) #define DEVICE_NAME "ESP8266 DEVICE" +#elif defined(ESP32) +#define DEVICE_NAME "ESP32 DEVICE" +#endif //const char *metaRefreshStr = "redirecting..."; const char *metaRefreshStr = "redirecting..."; //server objects +#if defined(ESP8266) ESP8266WebServer server(80); +#elif defined(ESP32) +WebServer server(80); +#endif DNSServer dnsServer; PersWiFiManager persWM(server, dnsServer); @@ -95,6 +112,10 @@ void setup() { //allows serving of files from SPIFFS SPIFFS.begin(); + + //reset saved settings, clears WiFi credentials e.g. for testing + //persWM.resetSettings(); + persWM.begin(); //serve files from SPIFFS @@ -117,6 +138,7 @@ void setup() { DEBUG_PRINT("y: "+y); } //if +/* //build json object of program data StaticJsonBuffer<200> jsonBuffer; JsonObject &json = jsonBuffer.createObject(); @@ -125,6 +147,9 @@ void setup() { char jsonchar[200]; json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece +*/ + char jsonchar[200]; + sprintf(jsonchar, "{\"x\":%d,\"y\":\"%s\"}", x, y.c_str()); // Easy alternative for ArduinoJson server.send(200, "application/json", jsonchar); }); //server.on api @@ -138,10 +163,9 @@ void setup() { SSDP.setHTTPPort(80); SSDP.setName(DEVICE_NAME); SSDP.setURL("/"); - SSDP.begin(); SSDP.setDeviceType("upnp:rootdevice"); + SSDP.begin(); - server.begin(); DEBUG_PRINT("setup complete."); } //void setup diff --git a/examples/basic_rest_api/data/wifi.htm b/examples/basic_rest_api/data/wifi.htm new file mode 100644 index 0000000..eedf71d --- /dev/null +++ b/examples/basic_rest_api/data/wifi.htm @@ -0,0 +1,120 @@ + + + + + + + ESP WiFi + + + + +
+ +

+
+ +
+ +

+
+ +

+ +
+

+
+ Back |Home +
+ + + diff --git a/examples/basic_rest_api/data/wifi.min.htm b/examples/basic_rest_api/data/wifi.min.htm new file mode 100644 index 0000000..12240bc --- /dev/null +++ b/examples/basic_rest_api/data/wifi.min.htm @@ -0,0 +1 @@ + ESP WiFi








Back |Home
diff --git a/examples/basic_rest_api/data/wifi.min.htm.gz b/examples/basic_rest_api/data/wifi.min.htm.gz deleted file mode 100644 index c8b0d2f..0000000 Binary files a/examples/basic_rest_api/data/wifi.min.htm.gz and /dev/null differ diff --git a/examples/basic_rest_api_reporter/basic_rest_api_reporter.ino b/examples/basic_rest_api_reporter/basic_rest_api_reporter.ino new file mode 100644 index 0000000..9435667 --- /dev/null +++ b/examples/basic_rest_api_reporter/basic_rest_api_reporter.ino @@ -0,0 +1,191 @@ +#define DEBUG_SERIAL //uncomment for Serial debugging statements + +#ifdef DEBUG_SERIAL +#define DEBUG_BEGIN Serial.begin(115200) +#define DEBUG_PRINT(x) Serial.println(x) +#else +#define DEBUG_PRINT(x) +#define DEBUG_BEGIN +#endif + +//includes +#include +#include +#if defined(ESP8266) +#include +#include +#include +#include +#elif defined(ESP32) +#include +#include +#include +#include +#else +#error "Unsupported board class" +#endif +#include + +#if defined(ESP8266) +#define DEVICE_NAME "ESP8266 DEVICE" +#elif defined(ESP32) +#define DEVICE_NAME "ESP32 DEVICE" +#endif + +//const char *metaRefreshStr = "redirecting..."; +const char *metaRefreshStr = "redirecting..."; + +//server objects +#if defined(ESP8266) +ESP8266WebServer server(80); +#elif defined(ESP32) +WebServer server(80); +#endif +DNSServer dnsServer; +PersWiFiManager persWM(server, dnsServer); + +////// Sample program data +int x; +String y; + + +//code from fsbrowser example, consolidated. +bool handleFileRead(String path) { + DEBUG_PRINT("handlefileread" + path); +// if (path.endsWith("/")) path += "index.htm"; + if (path.endsWith("/")) path += "wifi.htm"; + String contentType; + if (path.endsWith(".htm") || path.endsWith(".html")) contentType = "text/html"; + else if (path.endsWith(".css")) contentType = "text/css"; + else if (path.endsWith(".js")) contentType = "application/javascript"; + else if (path.endsWith(".png")) contentType = "image/png"; + else if (path.endsWith(".gif")) contentType = "image/gif"; + else if (path.endsWith(".jpg")) contentType = "image/jpeg"; + else if (path.endsWith(".ico")) contentType = "image/x-icon"; + else if (path.endsWith(".xml")) contentType = "text/xml"; + else if (path.endsWith(".pdf")) contentType = "application/x-pdf"; + else if (path.endsWith(".zip")) contentType = "application/x-zip"; + else if (path.endsWith(".gz")) contentType = "application/x-gzip"; + else if (path.endsWith(".json")) contentType = "application/json"; + else contentType = "text/plain"; + + //split filepath and extension + String prefix = path, ext = ""; + int lastPeriod = path.lastIndexOf('.'); + if (lastPeriod >= 0) { + prefix = path.substring(0, lastPeriod); + ext = path.substring(lastPeriod); + } + + //look for smaller versions of file + //minified file, good (myscript.min.js) + if (SPIFFS.exists(prefix + ".min" + ext)) path = prefix + ".min" + ext; + //gzipped file, better (myscript.js.gz) + if (SPIFFS.exists(prefix + ext + ".gz")) path = prefix + ext + ".gz"; + //min and gzipped file, best (myscript.min.js.gz) + if (SPIFFS.exists(prefix + ".min" + ext + ".gz")) path = prefix + ".min" + ext + ".gz"; + + if (SPIFFS.exists(path)) { + DEBUG_PRINT("sending file " + path); + File file = SPIFFS.open(path, "r"); + if (server.hasArg("download")) + server.sendHeader("Content-Disposition", " attachment;"); + if (server.uri().indexOf("nocache") < 0) + server.sendHeader("Cache-Control", " max-age=172800"); + + //optional alt arg (encoded url), server sends redirect to file on the web + if (WiFi.status() == WL_CONNECTED && server.hasArg("alt")) { + server.sendHeader("Location", server.arg("alt"), true); + server.send ( 302, "text/plain", ""); + } else { + //server sends file + size_t sent = server.streamFile(file, contentType); + } + file.close(); + return true; + } //if SPIFFS.exists + return false; +} //bool handleFileRead + +void setup() { + DEBUG_BEGIN; //for terminal debugging + DEBUG_PRINT(); + + //allows serving of files from SPIFFS + SPIFFS.begin(); + + //reset saved settings + persWM.resetSettings(); + + //make connecting/disconnecting non-blocking + persWM.setConnectNonBlock(true); + + persWM.begin(); + + persWM.onConnect([]() { + DEBUG_PRINT("wifi connected"); + DEBUG_PRINT(WiFi.localIP()); + }); + + persWM.onAp([](){ + DEBUG_PRINT("AP MODE"); + DEBUG_PRINT(persWM.getApSsid()); + DEBUG_PRINT(WiFi.softAPIP()); + }); + + //serve files from SPIFFS + server.onNotFound([]() { + if (!handleFileRead(server.uri())) { + server.sendHeader("Cache-Control", " max-age=172800"); + server.send(302, "text/html", metaRefreshStr); + } + }); //server.onNotFound + + //handles commands from webpage, sends live data in JSON format + server.on("/api", []() { + DEBUG_PRINT("server.on /api"); + if (server.hasArg("x")) { + x = server.arg("x").toInt(); + DEBUG_PRINT(String("x: ")+x); + } //if + if (server.hasArg("y")) { + y = server.arg("y"); + DEBUG_PRINT("y: "+y); + } //if + + //build json object of program data + StaticJsonBuffer<200> jsonBuffer; + JsonObject &json = jsonBuffer.createObject(); + json["x"] = x; + json["y"] = y; + + char jsonchar[200]; + json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece + server.send(200, "application/json", jsonchar); + + }); //server.on api + + + //SSDP makes device visible on windows network + server.on("/description.xml", HTTP_GET, []() { + SSDP.schema(server.client()); + }); + SSDP.setSchemaURL("description.xml"); + SSDP.setHTTPPort(80); + SSDP.setName(DEVICE_NAME); + SSDP.setURL("/"); + SSDP.setDeviceType("upnp:rootdevice"); + SSDP.begin(); + + server.begin(); // Inconsistency: the DNS server is started from the library and the web server directly from setup() + DEBUG_PRINT("setup complete."); +} //void setup + +void loop() { + persWM.handleWiFi(); + dnsServer.processNextRequest(); + server.handleClient(); + + // do stuff with x and y + +} //void loop diff --git a/examples/basic_rest_api_reporter/data/favicon.ico b/examples/basic_rest_api_reporter/data/favicon.ico new file mode 100644 index 0000000..1cd2a8b Binary files /dev/null and b/examples/basic_rest_api_reporter/data/favicon.ico differ diff --git a/examples/basic_rest_api_reporter/data/index.htm b/examples/basic_rest_api_reporter/data/index.htm new file mode 100644 index 0000000..c6f2be1 --- /dev/null +++ b/examples/basic_rest_api_reporter/data/index.htm @@ -0,0 +1,52 @@ +

Current Data

+x: + +
y: + + +
Last updated + seconds ago. + +
+ +

Update Data

+
+ x: + + +
+
+ y: + + +
+ +WiFi settings + + diff --git a/data/wifi.min.htm.gz b/examples/basic_rest_api_reporter/data/wifi.min.htm.gz similarity index 100% rename from data/wifi.min.htm.gz rename to examples/basic_rest_api_reporter/data/wifi.min.htm.gz diff --git a/library.properties b/library.properties index 520b6d1..296d4bf 100644 --- a/library.properties +++ b/library.properties @@ -6,4 +6,4 @@ sentence=Persistent WiFi Manager paragraph=A non-blocking, persistant wifi manager for ESP8266 that allows network changes at any time category=Communication url=http://ryandowning.net/PersWiFiManager/ -architectures=esp8266 +architectures=esp8266,esp32