From b0fbfc3558afe01d8725f6f71a675ea97c0342b7 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Sun, 30 Dec 2018 19:47:44 +0900 Subject: [PATCH] Adjusted to common code between ESP8266 and ESP32 --- examples/mqttRSSI/mqttRSSI.ino | 54 +++++++++++++++------------- examples/mqttRSSI_FS/mqttRSSI_FS.ino | 48 ++++++++++++++----------- src/AutoConnect.cpp | 4 ++- 3 files changed, 60 insertions(+), 46 deletions(-) diff --git a/examples/mqttRSSI/mqttRSSI.ino b/examples/mqttRSSI/mqttRSSI.ino index f8bac9e..54ae6bf 100644 --- a/examples/mqttRSSI/mqttRSSI.ino +++ b/examples/mqttRSSI/mqttRSSI.ino @@ -16,9 +16,12 @@ https://opensource.org/licenses/MIT #if defined(ARDUINO_ARCH_ESP8266) #include #include +#define GET_CHIPID() (ESP.getChipId()) #elif defined(ARDUINO_ARCH_ESP32) #include #include +#include +#define GET_CHIPID() ((uint16_t)(ESP.getEfuseMac()>>32)) #endif #include #include @@ -86,7 +89,7 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw( "type": "ACCheckbox", "value": "unique", "label": "Use APID unique", - "checked": false + "checked": true }, { "name": "period", @@ -151,6 +154,13 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw( ] )raw"; +// Adjusting WebServer class with between ESP8266 and ESP32. +#if defined(ARDUINO_ARCH_ESP8266) +typedef ESP8266WebServer WiFiWebServer; +#elif defined(ARDUINO_ARCH_ESP32) +typedef WebServer WiFiWebServer; +#endif + AutoConnect portal; AutoConnectConfig config; WiFiClient wifiClient; @@ -236,8 +246,10 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) { Serial.println(PARAM_FILE " failed to load"); param.close(); } - else + else { Serial.println(PARAM_FILE " open failed"); + Serial.println("If you get error as 'SPIFFS: mount failed, -10025', Please modify with 'SPIFFS.begin(true)'."); + } SPIFFS.end(); return ""; } @@ -275,7 +287,7 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) { // it is necessary to get the AutoConnectAux object of /mqtt_setting. SPIFFS.begin(); File param = SPIFFS.open(PARAM_FILE, "w"); - portal.aux("/mqtt_setting")->saveElement(param, { "mqttserver", "channelid", "userkey", "apikey", "period", "uniqueid", "hostname" }); + portal.aux("/mqtt_setting")->saveElement(param, { "mqttserver", "channelid", "userkey", "apikey", "uniqueid", "hostname" }); param.close(); SPIFFS.end(); @@ -304,33 +316,31 @@ void handleRoot() { "" ""; -#if defined(ARDUINO_ARCH_ESP8266) - ESP8266WebServer& server = portal.host(); -#elif defined(ARDUINO_ARCH_ESP32) - WebServer& server = portal.host(); -#endif + WiFiWebServer& server = portal.host(); server.send(200, "text/html", content); } -// Clear channel using Thingspeak's API. +// Clear channel using ThingSpeak's API. void handleClearChannel() { HTTPClient httpClient; String endpoint = serverName; endpoint.replace("mqtt", "api"); String delUrl = "http://" + endpoint + "/channels/" + channelId + "/feeds.json?api_key=" + userKey; - Serial.println(delUrl + ":" + String(httpClient.begin(delUrl))); - Serial.println("res:" + String(httpClient.sendRequest("DELETE"))); - String res = httpClient.getString(); - httpClient.end(); + Serial.print("DELETE " + delUrl); + if (httpClient.begin(delUrl)) { + Serial.print(":"); + int resCode = httpClient.sendRequest("DELETE"); + String res = httpClient.getString(); + httpClient.end(); + Serial.println(String(resCode) + "," + res); + } + else + Serial.println(" failed"); // Returns the redirect response. The page is reloaded and its contents // are updated to the state after deletion. -#if defined(ARDUINO_ARCH_ESP8266) - ESP8266WebServer& server = portal.host(); -#elif defined(ARDUINO_ARCH_ESP32) - WebServer& server = portal.host(); -#endif + WiFiWebServer& server = portal.host(); server.sendHeader("Location", String("http://") + server.client().localIP().toString() + String("/")); server.send(302, "text/plain", ""); server.client().flush(); @@ -347,7 +357,7 @@ void setup() { AutoConnectCheckbox& uniqueidElm = setting->getElement("uniqueid"); AutoConnectInput& hostnameElm = setting->getElement("hostname"); if (uniqueidElm.checked) { - config.apid = String("ESP") + "_" + String(ESP.getChipId(), HEX); + config.apid = String("ESP") + "_" + String(GET_CHIPID(), HEX); Serial.println("apid set to " + config.apid); } if (hostnameElm.value.length()) { @@ -377,11 +387,7 @@ void setup() { } } -#if defined(ARDUINO_ARCH_ESP8266) - ESP8266WebServer& server = portal.host(); -#elif defined(ARDUINO_ARCH_ESP32) - WebServer& server = portal.host(); -#endif + WiFiWebServer& server = portal.host(); server.on("/", handleRoot); server.on(AUX_CLEAR_URI, handleClearChannel); } diff --git a/examples/mqttRSSI_FS/mqttRSSI_FS.ino b/examples/mqttRSSI_FS/mqttRSSI_FS.ino index c4c6778..64ef690 100644 --- a/examples/mqttRSSI_FS/mqttRSSI_FS.ino +++ b/examples/mqttRSSI_FS/mqttRSSI_FS.ino @@ -21,9 +21,12 @@ #if defined(ARDUINO_ARCH_ESP8266) #include #include +#define GET_CHIPID() (ESP.getChipId()) #elif defined(ARDUINO_ARCH_ESP32) #include #include +#include +#define GET_CHIPID() ((uint16_t)(ESP.getEfuseMac()>>32)) #endif #include #include @@ -34,6 +37,13 @@ #define AUX_MQTTSAVE "/mqtt_save" #define AUX_MQTTCLEAR "/mqtt_clear" +// Adjusting WebServer class with between ESP8266 and ESP32. +#if defined(ARDUINO_ARCH_ESP8266) +typedef ESP8266WebServer WiFiWebServer; +#elif defined(ARDUINO_ARCH_ESP32) +typedef WebServer WiFiWebServer; +#endif + AutoConnect portal; AutoConnectConfig config; WiFiClient wifiClient; @@ -113,8 +123,10 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) { aux.loadElement(param); param.close(); } - else + else { Serial.println(PARAM_FILE " open failed"); + Serial.println("If you get error as 'SPIFFS: mount failed, -10025', Please modify with 'SPIFFS.begin(true)'."); + } SPIFFS.end(); return ""; } @@ -173,11 +185,7 @@ void handleRoot() { "" ""; -#if defined(ARDUINO_ARCH_ESP8266) - ESP8266WebServer& server = portal.host(); -#elif defined(ARDUINO_ARCH_ESP32) - WebServer& server = portal.host(); -#endif + WiFiWebServer& server = portal.host(); server.send(200, "text/html", content); } @@ -188,18 +196,20 @@ void handleClearChannel() { endpoint.replace("mqtt", "api"); String delUrl = "http://" + endpoint + "/channels/" + channelId + "/feeds.json?api_key=" + userKey; - Serial.println(delUrl + ":" + String(httpClient.begin(delUrl))); - Serial.println("res:" + String(httpClient.sendRequest("DELETE"))); - String res = httpClient.getString(); - httpClient.end(); + Serial.print("DELETE " + delUrl); + if (httpClient.begin(delUrl)) { + Serial.print(":"); + int resCode = httpClient.sendRequest("DELETE"); + String res = httpClient.getString(); + httpClient.end(); + Serial.println(String(resCode) + "," + res); + } + else + Serial.println(" failed"); // Returns the redirect response. The page is reloaded and its contents // are updated to the state after deletion. -#if defined(ARDUINO_ARCH_ESP8266) - ESP8266WebServer& server = portal.host(); -#elif defined(ARDUINO_ARCH_ESP32) - WebServer& server = portal.host(); -#endif + WiFiWebServer& server = portal.host(); server.sendHeader("Location", String("http://") + server.client().localIP().toString() + String("/")); server.send(302, "text/plain", ""); server.client().flush(); @@ -235,7 +245,7 @@ void setup() { AutoConnectCheckbox& uniqueidElm = setting->getElement("uniqueid"); AutoConnectInput& hostnameElm = setting->getElement("hostname"); if (uniqueidElm.checked) { - config.apid = String("ESP") + "_" + String(ESP.getChipId(), HEX); + config.apid = String("ESP") + "_" + String(GET_CHIPID(), HEX); Serial.println("apid set to " + config.apid); } if (hostnameElm.value.length()) { @@ -264,11 +274,7 @@ void setup() { } } -#if defined(ARDUINO_ARCH_ESP8266) - ESP8266WebServer& server = portal.host(); -#elif defined(ARDUINO_ARCH_ESP32) - WebServer& server = portal.host(); -#endif + WiFiWebServer& server = portal.host(); server.on("/", handleRoot); server.on(AUX_MQTTCLEAR, handleClearChannel); } diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index f8f08c0..ec3d17a 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -17,8 +17,10 @@ */ #if defined(ARDUINO_ARCH_ESP8266) #define SOFT_RESET() ESP.reset() +#define SET_HOSTNAME(x) do { WiFi.hostname(x); } while(0) #elif defined(ARDUINO_ARCH_ESP32) #define SOFT_RESET() ESP.restart() +#define SET_HOSTNAME(x) do { WiFi.setHostname(x); } while(0) #endif /** @@ -113,7 +115,7 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long // Set host name if (_apConfig.hostName.length()) - WiFi.hostname(_apConfig.hostName.c_str()); + SET_HOSTNAME(_apConfig.hostName.c_str()); // If the portal is requested promptly skip the first WiFi.begin and // immediately start the portal.