Adjusted to common code between ESP8266 and ESP32

pull/41/head
Hieromon Ikasamo 6 years ago
parent 91741fb104
commit b0fbfc3558
  1. 54
      examples/mqttRSSI/mqttRSSI.ino
  2. 48
      examples/mqttRSSI_FS/mqttRSSI_FS.ino
  3. 4
      src/AutoConnect.cpp

@ -16,9 +16,12 @@ https://opensource.org/licenses/MIT
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#define GET_CHIPID() (ESP.getChipId())
#elif defined(ARDUINO_ARCH_ESP32) #elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h> #include <WiFi.h>
#include <SPIFFS.h> #include <SPIFFS.h>
#include <HTTPClient.h>
#define GET_CHIPID() ((uint16_t)(ESP.getEfuseMac()>>32))
#endif #endif
#include <FS.h> #include <FS.h>
#include <PubSubClient.h> #include <PubSubClient.h>
@ -86,7 +89,7 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw(
"type": "ACCheckbox", "type": "ACCheckbox",
"value": "unique", "value": "unique",
"label": "Use APID unique", "label": "Use APID unique",
"checked": false "checked": true
}, },
{ {
"name": "period", "name": "period",
@ -151,6 +154,13 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw(
] ]
)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; AutoConnect portal;
AutoConnectConfig config; AutoConnectConfig config;
WiFiClient wifiClient; WiFiClient wifiClient;
@ -236,8 +246,10 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) {
Serial.println(PARAM_FILE " failed to load"); Serial.println(PARAM_FILE " failed to load");
param.close(); param.close();
} }
else else {
Serial.println(PARAM_FILE " open failed"); 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(); SPIFFS.end();
return ""; return "";
} }
@ -275,7 +287,7 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) {
// it is necessary to get the AutoConnectAux object of /mqtt_setting. // it is necessary to get the AutoConnectAux object of /mqtt_setting.
SPIFFS.begin(); SPIFFS.begin();
File param = SPIFFS.open(PARAM_FILE, "w"); 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(); param.close();
SPIFFS.end(); SPIFFS.end();
@ -304,33 +316,31 @@ void handleRoot() {
"</body>" "</body>"
"</html>"; "</html>";
#if defined(ARDUINO_ARCH_ESP8266) WiFiWebServer& server = portal.host();
ESP8266WebServer& server = portal.host();
#elif defined(ARDUINO_ARCH_ESP32)
WebServer& server = portal.host();
#endif
server.send(200, "text/html", content); server.send(200, "text/html", content);
} }
// Clear channel using Thingspeak's API. // Clear channel using ThingSpeak's API.
void handleClearChannel() { void handleClearChannel() {
HTTPClient httpClient; HTTPClient httpClient;
String endpoint = serverName; String endpoint = serverName;
endpoint.replace("mqtt", "api"); endpoint.replace("mqtt", "api");
String delUrl = "http://" + endpoint + "/channels/" + channelId + "/feeds.json?api_key=" + userKey; String delUrl = "http://" + endpoint + "/channels/" + channelId + "/feeds.json?api_key=" + userKey;
Serial.println(delUrl + ":" + String(httpClient.begin(delUrl))); Serial.print("DELETE " + delUrl);
Serial.println("res:" + String(httpClient.sendRequest("DELETE"))); if (httpClient.begin(delUrl)) {
String res = httpClient.getString(); Serial.print(":");
httpClient.end(); 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 // Returns the redirect response. The page is reloaded and its contents
// are updated to the state after deletion. // are updated to the state after deletion.
#if defined(ARDUINO_ARCH_ESP8266) WiFiWebServer& server = portal.host();
ESP8266WebServer& server = portal.host();
#elif defined(ARDUINO_ARCH_ESP32)
WebServer& server = portal.host();
#endif
server.sendHeader("Location", String("http://") + server.client().localIP().toString() + String("/")); server.sendHeader("Location", String("http://") + server.client().localIP().toString() + String("/"));
server.send(302, "text/plain", ""); server.send(302, "text/plain", "");
server.client().flush(); server.client().flush();
@ -347,7 +357,7 @@ void setup() {
AutoConnectCheckbox& uniqueidElm = setting->getElement<AutoConnectCheckbox>("uniqueid"); AutoConnectCheckbox& uniqueidElm = setting->getElement<AutoConnectCheckbox>("uniqueid");
AutoConnectInput& hostnameElm = setting->getElement<AutoConnectInput>("hostname"); AutoConnectInput& hostnameElm = setting->getElement<AutoConnectInput>("hostname");
if (uniqueidElm.checked) { 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); Serial.println("apid set to " + config.apid);
} }
if (hostnameElm.value.length()) { if (hostnameElm.value.length()) {
@ -377,11 +387,7 @@ void setup() {
} }
} }
#if defined(ARDUINO_ARCH_ESP8266) WiFiWebServer& server = portal.host();
ESP8266WebServer& server = portal.host();
#elif defined(ARDUINO_ARCH_ESP32)
WebServer& server = portal.host();
#endif
server.on("/", handleRoot); server.on("/", handleRoot);
server.on(AUX_CLEAR_URI, handleClearChannel); server.on(AUX_CLEAR_URI, handleClearChannel);
} }

@ -21,9 +21,12 @@
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#define GET_CHIPID() (ESP.getChipId())
#elif defined(ARDUINO_ARCH_ESP32) #elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h> #include <WiFi.h>
#include <SPIFFS.h> #include <SPIFFS.h>
#include <HTTPClient.h>
#define GET_CHIPID() ((uint16_t)(ESP.getEfuseMac()>>32))
#endif #endif
#include <FS.h> #include <FS.h>
#include <PubSubClient.h> #include <PubSubClient.h>
@ -34,6 +37,13 @@
#define AUX_MQTTSAVE "/mqtt_save" #define AUX_MQTTSAVE "/mqtt_save"
#define AUX_MQTTCLEAR "/mqtt_clear" #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; AutoConnect portal;
AutoConnectConfig config; AutoConnectConfig config;
WiFiClient wifiClient; WiFiClient wifiClient;
@ -113,8 +123,10 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) {
aux.loadElement(param); aux.loadElement(param);
param.close(); param.close();
} }
else else {
Serial.println(PARAM_FILE " open failed"); 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(); SPIFFS.end();
return ""; return "";
} }
@ -173,11 +185,7 @@ void handleRoot() {
"</body>" "</body>"
"</html>"; "</html>";
#if defined(ARDUINO_ARCH_ESP8266) WiFiWebServer& server = portal.host();
ESP8266WebServer& server = portal.host();
#elif defined(ARDUINO_ARCH_ESP32)
WebServer& server = portal.host();
#endif
server.send(200, "text/html", content); server.send(200, "text/html", content);
} }
@ -188,18 +196,20 @@ void handleClearChannel() {
endpoint.replace("mqtt", "api"); endpoint.replace("mqtt", "api");
String delUrl = "http://" + endpoint + "/channels/" + channelId + "/feeds.json?api_key=" + userKey; String delUrl = "http://" + endpoint + "/channels/" + channelId + "/feeds.json?api_key=" + userKey;
Serial.println(delUrl + ":" + String(httpClient.begin(delUrl))); Serial.print("DELETE " + delUrl);
Serial.println("res:" + String(httpClient.sendRequest("DELETE"))); if (httpClient.begin(delUrl)) {
String res = httpClient.getString(); Serial.print(":");
httpClient.end(); 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 // Returns the redirect response. The page is reloaded and its contents
// are updated to the state after deletion. // are updated to the state after deletion.
#if defined(ARDUINO_ARCH_ESP8266) WiFiWebServer& server = portal.host();
ESP8266WebServer& server = portal.host();
#elif defined(ARDUINO_ARCH_ESP32)
WebServer& server = portal.host();
#endif
server.sendHeader("Location", String("http://") + server.client().localIP().toString() + String("/")); server.sendHeader("Location", String("http://") + server.client().localIP().toString() + String("/"));
server.send(302, "text/plain", ""); server.send(302, "text/plain", "");
server.client().flush(); server.client().flush();
@ -235,7 +245,7 @@ void setup() {
AutoConnectCheckbox& uniqueidElm = setting->getElement<AutoConnectCheckbox>("uniqueid"); AutoConnectCheckbox& uniqueidElm = setting->getElement<AutoConnectCheckbox>("uniqueid");
AutoConnectInput& hostnameElm = setting->getElement<AutoConnectInput>("hostname"); AutoConnectInput& hostnameElm = setting->getElement<AutoConnectInput>("hostname");
if (uniqueidElm.checked) { 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); Serial.println("apid set to " + config.apid);
} }
if (hostnameElm.value.length()) { if (hostnameElm.value.length()) {
@ -264,11 +274,7 @@ void setup() {
} }
} }
#if defined(ARDUINO_ARCH_ESP8266) WiFiWebServer& server = portal.host();
ESP8266WebServer& server = portal.host();
#elif defined(ARDUINO_ARCH_ESP32)
WebServer& server = portal.host();
#endif
server.on("/", handleRoot); server.on("/", handleRoot);
server.on(AUX_MQTTCLEAR, handleClearChannel); server.on(AUX_MQTTCLEAR, handleClearChannel);
} }

@ -17,8 +17,10 @@
*/ */
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
#define SOFT_RESET() ESP.reset() #define SOFT_RESET() ESP.reset()
#define SET_HOSTNAME(x) do { WiFi.hostname(x); } while(0)
#elif defined(ARDUINO_ARCH_ESP32) #elif defined(ARDUINO_ARCH_ESP32)
#define SOFT_RESET() ESP.restart() #define SOFT_RESET() ESP.restart()
#define SET_HOSTNAME(x) do { WiFi.setHostname(x); } while(0)
#endif #endif
/** /**
@ -113,7 +115,7 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long
// Set host name // Set host name
if (_apConfig.hostName.length()) 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 // If the portal is requested promptly skip the first WiFi.begin and
// immediately start the portal. // immediately start the portal.

Loading…
Cancel
Save