Minor fixed for APIs

pull/41/head
Hieromon Ikasamo 6 years ago
parent 314545bc02
commit a2000cb957
  1. 23
      examples/mqttRSSI/mqttRSSI.ino
  2. 28
      examples/mqttRSSI_FS/mqttRSSI_FS.ino

@ -210,15 +210,6 @@ bool mqttConnect() {
void mqttPublish(String msg) { void mqttPublish(String msg) {
String path = String("channels/") + channelId + String("/publish/") + apiKey; 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()); mqttClient.publish(path.c_str(), msg.c_str());
} }
@ -237,7 +228,6 @@ int getStrength(uint8_t points) {
// elements defined in /mqtt_setting JSON. // elements defined in /mqtt_setting JSON.
String loadParams(AutoConnectAux& aux, PageArgument& args) { String loadParams(AutoConnectAux& aux, PageArgument& args) {
(void)(args); (void)(args);
SPIFFS.begin();
File param = SPIFFS.open(PARAM_FILE, "r"); File param = SPIFFS.open(PARAM_FILE, "r");
if (param) { if (param) {
if (aux.loadElement(param)) if (aux.loadElement(param))
@ -248,10 +238,11 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) {
} }
else { else {
Serial.println(PARAM_FILE " open failed"); 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)'."); 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 // 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. // The entered value is owned by AutoConnectAux of /mqtt_setting.
// To retrieve the elements of /mqtt_setting, it is necessary to get // To retrieve the elements of /mqtt_setting, it is necessary to get
// the AutoConnectAux object of /mqtt_setting. // the AutoConnectAux object of /mqtt_setting.
SPIFFS.begin();
File param = SPIFFS.open(PARAM_FILE, "w"); File param = SPIFFS.open(PARAM_FILE, "w");
mqtt_setting->saveElement(param, { "mqttserver", "channelid", "userkey", "apikey", "uniqueid", "hostname" }); mqtt_setting->saveElement(param, { "mqttserver", "channelid", "userkey", "apikey", "uniqueid", "hostname" });
param.close(); param.close();
SPIFFS.end();
// Echo back saved parameters to AutoConnectAux page. // Echo back saved parameters to AutoConnectAux page.
AutoConnectText& echo = aux.getElement<AutoConnectText>("parameters"); 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 += "Use APID unique: " + String(uniqueid == true ? "true" : "false") + "<br>";
echo.value += "ESP host name: " + hostName + "<br>"; echo.value += "ESP host name: " + hostName + "<br>";
return String(); return String("");
} }
void handleRoot() { void handleRoot() {
@ -330,12 +319,13 @@ void handleRoot() {
// Clear channel using ThingSpeak's API. // Clear channel using ThingSpeak's API.
void handleClearChannel() { void handleClearChannel() {
HTTPClient httpClient; HTTPClient httpClient;
WiFiClient client;
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.print("DELETE " + delUrl); Serial.print("DELETE " + delUrl);
if (httpClient.begin(delUrl)) { if (httpClient.begin(client, delUrl)) {
Serial.print(":"); Serial.print(":");
int resCode = httpClient.sendRequest("DELETE"); int resCode = httpClient.sendRequest("DELETE");
String res = httpClient.getString(); String res = httpClient.getString();
@ -357,6 +347,7 @@ void setup() {
delay(1000); delay(1000);
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
SPIFFS.begin();
if (portal.load(FPSTR(AUX_mqtt_setting))) { if (portal.load(FPSTR(AUX_mqtt_setting))) {
AutoConnectAux* mqtt_setting = portal.aux(AUX_SETTING_URI); AutoConnectAux* mqtt_setting = portal.aux(AUX_SETTING_URI);

@ -94,15 +94,7 @@ bool mqttConnect() {
void mqttPublish(String msg) { void mqttPublish(String msg) {
String path = String("channels/") + channelId + String("/publish/") + apiKey; String path = String("channels/") + channelId + String("/publish/") + apiKey;
int tLen = path.length(); mqttClient.publish(path.c_str(), msg.c_str());
char topic[tLen];
path.toCharArray(topic, tLen + 1);
int mLen = msg.length();
char payload[mLen];
msg.toCharArray(payload, mLen + 1);
mqttClient.publish(topic, payload);
} }
int getStrength(uint8_t points) { int getStrength(uint8_t points) {
@ -118,18 +110,14 @@ int getStrength(uint8_t points) {
String loadParams(AutoConnectAux& aux, PageArgument& args) { String loadParams(AutoConnectAux& aux, PageArgument& args) {
(void)(args); (void)(args);
SPIFFS.begin();
File param = SPIFFS.open(PARAM_FILE, "r"); File param = SPIFFS.open(PARAM_FILE, "r");
if (param) { if (param) {
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)'."); return String("");
}
SPIFFS.end();
return "";
} }
String saveParams(AutoConnectAux& aux, PageArgument& args) { String saveParams(AutoConnectAux& aux, PageArgument& args) {
@ -156,11 +144,9 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) {
// The entered value is owned by AutoConnectAux of /mqtt_setting. // The entered value is owned by AutoConnectAux of /mqtt_setting.
// To retrieve the elements of /mqtt_setting, it is necessary to get // To retrieve the elements of /mqtt_setting, it is necessary to get
// the AutoConnectAux object of /mqtt_setting. // the AutoConnectAux object of /mqtt_setting.
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", "period", "uniqueid", "hostname" });
param.close(); param.close();
SPIFFS.end();
// Echo back saved parameters to AutoConnectAux page. // Echo back saved parameters to AutoConnectAux page.
AutoConnectText& echo = aux.getElement<AutoConnectText>("parameters"); AutoConnectText& echo = aux.getElement<AutoConnectText>("parameters");
@ -172,7 +158,7 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) {
echo.value += "Use APID unique: " + uniqueid + "<br>"; echo.value += "Use APID unique: " + uniqueid + "<br>";
echo.value += "ESP host name: " + hostName + "<br>"; echo.value += "ESP host name: " + hostName + "<br>";
return ""; return String("");
} }
void handleRoot() { void handleRoot() {
@ -194,12 +180,13 @@ void handleRoot() {
// Clear channel using Thingspeak's API. // Clear channel using Thingspeak's API.
void handleClearChannel() { void handleClearChannel() {
HTTPClient httpClient; HTTPClient httpClient;
WiFiClient client;
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.print("DELETE " + delUrl); Serial.print("DELETE " + delUrl);
if (httpClient.begin(delUrl)) { if (httpClient.begin(client, delUrl)) {
Serial.print(":"); Serial.print(":");
int resCode = httpClient.sendRequest("DELETE"); int resCode = httpClient.sendRequest("DELETE");
String res = httpClient.getString(); String res = httpClient.getString();
@ -236,11 +223,10 @@ void setup() {
delay(1000); delay(1000);
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
SPIFFS.begin(); SPIFFS.begin();
loadAux(AUX_MQTTSETTING); loadAux(AUX_MQTTSETTING);
loadAux(AUX_MQTTSAVE); loadAux(AUX_MQTTSAVE);
SPIFFS.end();
AutoConnectAux* setting = portal.aux(AUX_MQTTSETTING); AutoConnectAux* setting = portal.aux(AUX_MQTTSETTING);
if (setting) { if (setting) {

Loading…
Cancel
Save