|
|
|
@ -210,15 +210,6 @@ bool mqttConnect() { |
|
|
|
|
|
|
|
|
|
void mqttPublish(String msg) { |
|
|
|
|
String path = String("channels/") + channelId + String("/publish/") + apiKey; |
|
|
|
|
// int tLen = path.length();
|
|
|
|
|
// char topic[tLen];
|
|
|
|
|
// path.toCharArray(topic, tLen + 1);
|
|
|
|
|
|
|
|
|
|
// int mLen = msg.length();
|
|
|
|
|
// char payload[mLen];
|
|
|
|
|
// msg.toCharArray(payload, mLen + 1);
|
|
|
|
|
|
|
|
|
|
// mqttClient.publish(topic, payload);
|
|
|
|
|
mqttClient.publish(path.c_str(), msg.c_str()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -237,7 +228,6 @@ int getStrength(uint8_t points) { |
|
|
|
|
// elements defined in /mqtt_setting JSON.
|
|
|
|
|
String loadParams(AutoConnectAux& aux, PageArgument& args) { |
|
|
|
|
(void)(args); |
|
|
|
|
SPIFFS.begin(); |
|
|
|
|
File param = SPIFFS.open(PARAM_FILE, "r"); |
|
|
|
|
if (param) { |
|
|
|
|
if (aux.loadElement(param)) |
|
|
|
@ -248,10 +238,11 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) { |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
Serial.println(PARAM_FILE " open failed"); |
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32 |
|
|
|
|
Serial.println("If you get error as 'SPIFFS: mount failed, -10025', Please modify with 'SPIFFS.begin(true)'."); |
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
SPIFFS.end(); |
|
|
|
|
return String(); |
|
|
|
|
return String(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Save the value of each element entered by '/mqtt_setting' to the
|
|
|
|
@ -292,11 +283,9 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) { |
|
|
|
|
// The entered value is owned by AutoConnectAux of /mqtt_setting.
|
|
|
|
|
// 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"); |
|
|
|
|
mqtt_setting->saveElement(param, { "mqttserver", "channelid", "userkey", "apikey", "uniqueid", "hostname" }); |
|
|
|
|
param.close(); |
|
|
|
|
SPIFFS.end(); |
|
|
|
|
|
|
|
|
|
// Echo back saved parameters to AutoConnectAux page.
|
|
|
|
|
AutoConnectText& echo = aux.getElement<AutoConnectText>("parameters"); |
|
|
|
@ -308,7 +297,7 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) { |
|
|
|
|
echo.value += "Use APID unique: " + String(uniqueid == true ? "true" : "false") + "<br>"; |
|
|
|
|
echo.value += "ESP host name: " + hostName + "<br>"; |
|
|
|
|
|
|
|
|
|
return String(); |
|
|
|
|
return String(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void handleRoot() { |
|
|
|
@ -330,12 +319,13 @@ void handleRoot() { |
|
|
|
|
// Clear channel using ThingSpeak's API.
|
|
|
|
|
void handleClearChannel() { |
|
|
|
|
HTTPClient httpClient; |
|
|
|
|
WiFiClient client; |
|
|
|
|
String endpoint = serverName; |
|
|
|
|
endpoint.replace("mqtt", "api"); |
|
|
|
|
String delUrl = "http://" + endpoint + "/channels/" + channelId + "/feeds.json?api_key=" + userKey; |
|
|
|
|
|
|
|
|
|
Serial.print("DELETE " + delUrl); |
|
|
|
|
if (httpClient.begin(delUrl)) { |
|
|
|
|
if (httpClient.begin(client, delUrl)) { |
|
|
|
|
Serial.print(":"); |
|
|
|
|
int resCode = httpClient.sendRequest("DELETE"); |
|
|
|
|
String res = httpClient.getString(); |
|
|
|
@ -357,6 +347,7 @@ void setup() { |
|
|
|
|
delay(1000); |
|
|
|
|
Serial.begin(115200); |
|
|
|
|
Serial.println(); |
|
|
|
|
SPIFFS.begin(); |
|
|
|
|
|
|
|
|
|
if (portal.load(FPSTR(AUX_mqtt_setting))) { |
|
|
|
|
AutoConnectAux* mqtt_setting = portal.aux(AUX_SETTING_URI); |
|
|
|
|