diff --git a/examples/mqttRSSI_FS/data/mqtt_save.json b/examples/mqttRSSI_FS/data/mqtt_save.json index 35954ed..6c8c687 100644 --- a/examples/mqttRSSI_FS/data/mqtt_save.json +++ b/examples/mqttRSSI_FS/data/mqtt_save.json @@ -1,19 +1,23 @@ { "title": "MQTT Setting", "uri": "/mqtt_save", - "name": "mqttsave", "menu": false, "element": [ { "name": "caption", "type": "ACText", - "value": "

Parameter saved as:

", + "value": "

Parameters saved as:

", "style": "text-align:center;color:#2f4f4f;padding:10px;" }, { - "name": "validation", - "type": "ACText", - "value": "" + "name": "parameters", + "type": "ACText" + }, + { + "name": "clear", + "type": "ACSubmit", + "value": "Clear channel", + "uri": "/mqtt_clear" } ] } diff --git a/examples/mqttRSSI_FS/data/mqtt_setting.json b/examples/mqttRSSI_FS/data/mqtt_setting.json index 319335d..2778428 100644 --- a/examples/mqttRSSI_FS/data/mqtt_setting.json +++ b/examples/mqttRSSI_FS/data/mqtt_setting.json @@ -18,26 +18,26 @@ { "name": "mqttserver", "type": "ACInput", - "value": "", + "value": "mqtt.thingspeak.com", "placeholder": "MQTT broker server", "label": "Server" }, { "name": "channelid", "type": "ACInput", - "value": "", + "value": "454951", "label": "Channel ID" }, { "name": "userkey", "type": "ACInput", - "value": "", + "value": "NRTFYGJ6TJFGX4RC", "label": "User Key" }, { "name": "apikey", "type": "ACInput", - "value": "", + "value": "HBVQ2XV6VYBI4582", "label": "API Key" }, { @@ -58,8 +58,9 @@ "value": "
" }, { - "name": "apid", + "name": "uniqueid", "type": "ACCheckbox", + "value": "unique", "label": "Use APID unique", "checked": true }, @@ -72,7 +73,7 @@ { "name": "save", "type": "ACSubmit", - "value": "SAVE", + "value": "Save&Start", "uri": "/mqtt_save" }, { diff --git a/examples/mqttRSSI_FS/mqttRSSI_FS.ino b/examples/mqttRSSI_FS/mqttRSSI_FS.ino index 7900e2a..4a5b1bf 100644 --- a/examples/mqttRSSI_FS/mqttRSSI_FS.ino +++ b/examples/mqttRSSI_FS/mqttRSSI_FS.ino @@ -20,6 +20,7 @@ #if defined(ARDUINO_ARCH_ESP8266) #include +#include #elif defined(ARDUINO_ARCH_ESP32) #include #include @@ -31,6 +32,7 @@ #define PARAM_FILE "/param.json" #define AUX_MQTTSETTING "/mqtt_setting" #define AUX_MQTTSAVE "/mqtt_save" +#define AUX_MQTTCLEAR "/mqtt_clear" AutoConnect portal; AutoConnectConfig config; @@ -118,41 +120,45 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) { } String saveParams(AutoConnectAux& aux, PageArgument& args) { - String echo; - serverName = args.arg("mqttserver"); serverName.trim(); - echo = "mqttserver: " + serverName + "
"; channelId = args.arg("channelid"); channelId.trim(); - echo += "channelid: " + channelId + "
"; userKey = args.arg("userkey"); userKey.trim(); - echo += "userkey: " + userKey + "
"; apiKey = args.arg("apikey"); apiKey.trim(); - echo += "apikey: " + apiKey + "
"; String upd = args.arg("period"); updateInterval = upd.substring(0, 2).toInt() * 1000; - echo += "period: " + String(updateInterval) + "
"; String uniqueid = args.arg("uniqueid"); - echo += "uniqueid: " + uniqueid + "
"; hostName = args.arg("hostname"); hostName.trim(); - echo += "hostname: " + hostName + "
"; + // In order to retrieve the elements of /mqtt_setting, + // 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" }); param.close(); SPIFFS.end(); - return echo; + + // Echo back saved parameters to AutoConnectAux page. + AutoConnectText& echo = aux.getElement("parameters"); + echo.value = "Server: " + serverName + "
"; + echo.value += "Channel ID: " + channelId + "
"; + echo.value += "User Key: " + userKey + "
"; + echo.value += "API Key: " + apiKey + "
"; + echo.value += "Update period: " + String(updateInterval / 1000) + " sec.
"; + echo.value += "Use APID unique: " + uniqueid + "
"; + echo.value += "ESP host name: " + hostName + "
"; + + return ""; } void handleRoot() { @@ -175,6 +181,32 @@ void handleRoot() { server.send(200, "text/html", content); } +// 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(); + + // 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 + server.sendHeader("Location", String("http://") + server.client().localIP().toString() + String("/")); + server.send(302, "text/plain", ""); + server.client().flush(); + server.client().stop(); +} + +// Load AutoConnectAux JSON from SPIFFS. bool loadAux(const String auxName) { bool rc = false; String fn = auxName + ".json"; @@ -208,10 +240,12 @@ void setup() { if (hostnameElm.value.length()) { config.hostName = hostnameElm.value; } + config.bootUri = AC_ONBOOTURI_HOME; + config.homeUri = "/"; + portal.config(config); portal.on(AUX_MQTTSETTING, loadParams); - portal.on(AUX_MQTTSAVE, saveParams, AC_EXIT_LATER); - portal.config(config); + portal.on(AUX_MQTTSAVE, saveParams); } else Serial.println("aux. load error"); @@ -234,6 +268,7 @@ void setup() { WebServer& server = portal.host(); #endif server.on("/", handleRoot); + server.on(AUX_MQTTCLEAR, handleClearChannel); } void loop() {