|
|
@ -1,16 +1,16 @@ |
|
|
|
/*
|
|
|
|
/*
|
|
|
|
ESP8266/ESP32 publish the RSSI as the WiFi signal strength to ThingSpeak channel. |
|
|
|
ESP8266/ESP32 publish the RSSI as the WiFi signal strength to ThingSpeak channel. |
|
|
|
This example is for explaining how to use the AutoConnect library. |
|
|
|
This example is for explaining how to use the AutoConnect library. |
|
|
|
|
|
|
|
|
|
|
|
In order to execute this example, the ThingSpeak account is needed. Sing up |
|
|
|
In order to execute this example, the ThingSpeak account is needed. Sing up |
|
|
|
for New User Account and create a New Channel via My Channels. |
|
|
|
for New User Account and create a New Channel via My Channels. |
|
|
|
For details, please refer to the project page. |
|
|
|
For details, please refer to the project page. |
|
|
|
https://hieromon.github.io/AutoConnect/examples/index.html#used-with-mqtt-as-a-client-application
|
|
|
|
https://hieromon.github.io/AutoConnect/examples/index.html#used-with-mqtt-as-a-client-application
|
|
|
|
|
|
|
|
|
|
|
|
This example is based on the environment as of March 20, 2018. |
|
|
|
This example is based on the environment as of March 20, 2018. |
|
|
|
Copyright (c) 2018 Hieromon Ikasamo. |
|
|
|
Copyright (c) 2018 Hieromon Ikasamo. |
|
|
|
This software is released under the MIT License. |
|
|
|
This software is released under the MIT License. |
|
|
|
https://opensource.org/licenses/MIT
|
|
|
|
https://opensource.org/licenses/MIT
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINO_ARCH_ESP8266) |
|
|
|
#if defined(ARDUINO_ARCH_ESP8266) |
|
|
@ -57,26 +57,22 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw( |
|
|
|
{ |
|
|
|
{ |
|
|
|
"name": "mqttserver", |
|
|
|
"name": "mqttserver", |
|
|
|
"type": "ACInput", |
|
|
|
"type": "ACInput", |
|
|
|
"value": "mqtt.thingspeak.com", |
|
|
|
|
|
|
|
"placeholder": "MQTT broker server", |
|
|
|
"placeholder": "MQTT broker server", |
|
|
|
"label": "Server" |
|
|
|
"label": "Server" |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
"name": "channelid", |
|
|
|
"name": "channelid", |
|
|
|
"type": "ACInput", |
|
|
|
"type": "ACInput", |
|
|
|
"value": "454951", |
|
|
|
|
|
|
|
"label": "Channel ID" |
|
|
|
"label": "Channel ID" |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
"name": "userkey", |
|
|
|
"name": "userkey", |
|
|
|
"type": "ACInput", |
|
|
|
"type": "ACInput", |
|
|
|
"value": "NRTFYGJ6TJFGX4RC", |
|
|
|
|
|
|
|
"label": "User Key" |
|
|
|
"label": "User Key" |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
"name": "apikey", |
|
|
|
"name": "apikey", |
|
|
|
"type": "ACInput", |
|
|
|
"type": "ACInput", |
|
|
|
"value": "HBVQ2XV6VYBI4582", |
|
|
|
|
|
|
|
"label": "API Key" |
|
|
|
"label": "API Key" |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
@ -85,8 +81,7 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw( |
|
|
|
"label": "Update period", |
|
|
|
"label": "Update period", |
|
|
|
"value": [ |
|
|
|
"value": [ |
|
|
|
"30 sec.", |
|
|
|
"30 sec.", |
|
|
|
"60 sec.", |
|
|
|
"60 sec." |
|
|
|
"180 sec." |
|
|
|
|
|
|
|
], |
|
|
|
], |
|
|
|
"arrange": "vertical", |
|
|
|
"arrange": "vertical", |
|
|
|
"checked": 1 |
|
|
|
"checked": 1 |
|
|
@ -112,7 +107,7 @@ static const char AUX_mqtt_setting[] PROGMEM = R"raw( |
|
|
|
{ |
|
|
|
{ |
|
|
|
"name": "save", |
|
|
|
"name": "save", |
|
|
|
"type": "ACSubmit", |
|
|
|
"type": "ACSubmit", |
|
|
|
"value": "Save&Start", |
|
|
|
"value": "Save&Start", |
|
|
|
"uri": "/mqtt_save" |
|
|
|
"uri": "/mqtt_save" |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
@ -166,8 +161,8 @@ unsigned long lastPub = 0; |
|
|
|
|
|
|
|
|
|
|
|
bool mqttConnect() { |
|
|
|
bool mqttConnect() { |
|
|
|
static const char alphanum[] = "0123456789" |
|
|
|
static const char alphanum[] = "0123456789" |
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|
|
|
"abcdefghijklmnopqrstuvwxyz"; // For random generation of client ID.
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"; // For random generation of client ID.
|
|
|
|
char clientId[9]; |
|
|
|
char clientId[9]; |
|
|
|
|
|
|
|
|
|
|
|
uint8_t retry = 10; |
|
|
|
uint8_t retry = 10; |
|
|
@ -186,7 +181,8 @@ bool mqttConnect() { |
|
|
|
if (mqttClient.connect(clientId, MQTT_USER_ID, userKey.c_str())) { |
|
|
|
if (mqttClient.connect(clientId, MQTT_USER_ID, userKey.c_str())) { |
|
|
|
Serial.println("Established:" + String(clientId)); |
|
|
|
Serial.println("Established:" + String(clientId)); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
Serial.println("Connection failed:" + String(mqttClient.state())); |
|
|
|
Serial.println("Connection failed:" + String(mqttClient.state())); |
|
|
|
if (!--retry) |
|
|
|
if (!--retry) |
|
|
|
break; |
|
|
|
break; |
|
|
@ -227,7 +223,10 @@ String loadParams(AutoConnectAux& aux, PageArgument& args) { |
|
|
|
SPIFFS.begin(); |
|
|
|
SPIFFS.begin(); |
|
|
|
File param = SPIFFS.open(PARAM_FILE, "r"); |
|
|
|
File param = SPIFFS.open(PARAM_FILE, "r"); |
|
|
|
if (param) { |
|
|
|
if (param) { |
|
|
|
aux.loadElement(param); |
|
|
|
// if (aux.loadElement(param))
|
|
|
|
|
|
|
|
// Serial.println(PARAM_FILE " loaded");
|
|
|
|
|
|
|
|
// else
|
|
|
|
|
|
|
|
// Serial.println(PARAM_FILE " failed to load");
|
|
|
|
param.close(); |
|
|
|
param.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
@ -360,7 +359,8 @@ void setup() { |
|
|
|
if (portal.begin()) { |
|
|
|
if (portal.begin()) { |
|
|
|
Serial.println("connected:" + WiFi.SSID()); |
|
|
|
Serial.println("connected:" + WiFi.SSID()); |
|
|
|
Serial.println("IP:" + WiFi.localIP().toString()); |
|
|
|
Serial.println("IP:" + WiFi.localIP().toString()); |
|
|
|
} else { |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
Serial.println("connection failed:" + String(WiFi.status())); |
|
|
|
Serial.println("connection failed:" + String(WiFi.status())); |
|
|
|
while (1) { |
|
|
|
while (1) { |
|
|
|
delay(100); |
|
|
|
delay(100); |
|
|
|