Revised the process

pull/57/head
Hieromon Ikasamo 6 years ago
parent bcfd7e5a9f
commit 1560c48efe
  1. 64
      examples/mqttRSSI/mqttRSSI.ino

@ -173,6 +173,7 @@ String userKey;
String apiKey; String apiKey;
String apid; String apid;
String hostName; String hostName;
bool uniqueid;
unsigned int updateInterval = 0; unsigned int updateInterval = 0;
unsigned long lastPub = 0; unsigned long lastPub = 0;
@ -227,14 +228,32 @@ int getStrength(uint8_t points) {
return points ? static_cast<int>(rssi / points) : 0; return points ? static_cast<int>(rssi / points) : 0;
} }
void getParams(AutoConnectAux& aux) {
serverName = aux["mqttserver"].value;
serverName.trim();
channelId = aux["channelid"].value;
channelId.trim();
userKey = aux["userkey"].value;
userKey.trim();
apiKey = aux["apikey"].value;
apiKey.trim();
AutoConnectRadio& period = aux["period"].as<AutoConnectRadio>();
updateInterval = period.value().substring(0, 2).toInt() * 1000;
uniqueid = aux["uniqueid"].as<AutoConnectCheckbox>().checked;
hostName = aux["hostname"].value;
hostName.trim();
}
// Load parameters saved with saveParams from SPIFFS into the // Load parameters saved with saveParams from SPIFFS into the
// 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);
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)) {
getParams(aux);
Serial.println(PARAM_FILE " loaded"); Serial.println(PARAM_FILE " loaded");
}
else else
Serial.println(PARAM_FILE " failed to load"); Serial.println(PARAM_FILE " failed to load");
param.close(); param.close();
@ -257,33 +276,14 @@ String saveParams(AutoConnectAux& aux, PageArgument& args) {
// The 'where()' function returns the AutoConnectAux that caused // The 'where()' function returns the AutoConnectAux that caused
// the transition to this page. // the transition to this page.
AutoConnectAux& mqtt_setting = portal.where(); AutoConnectAux& mqtt_setting = portal.where();
getParams(mqtt_setting);
AutoConnectInput& mqttserver = mqtt_setting["mqttserver"].as<AutoConnectInput>(); AutoConnectInput& mqttserver = mqtt_setting["mqttserver"].as<AutoConnectInput>();
serverName = mqttserver.value;
serverName.trim();
channelId = mqtt_setting["channelid"].value;
channelId.trim();
userKey = mqtt_setting["userkey"].value;
userKey.trim();
apiKey = mqtt_setting["apikey"].value;
apiKey.trim();
AutoConnectRadio& period = mqtt_setting["period"].as<AutoConnectRadio>();
updateInterval = period.value().substring(0, 2).toInt() * 1000;
bool uniqueid = mqtt_setting["uniqueid"].as<AutoConnectCheckbox>().checked;
hostName = mqtt_setting["hostname"].value;
hostName.trim();
// 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.
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", "period", "hostname" });
param.close(); param.close();
// Echo back saved parameters to AutoConnectAux page. // Echo back saved parameters to AutoConnectAux page.
@ -351,14 +351,14 @@ void setup() {
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);
AutoConnectCheckbox& uniqueidElm = mqtt_setting["uniqueid"].as<AutoConnectCheckbox>(); PageArgument args;
AutoConnectInput& hostnameElm = mqtt_setting["hostname"].as<AutoConnectInput>(); loadParams(mqtt_setting, args);
if (uniqueidElm.checked) { if (uniqueid) {
config.apid = String("ESP") + "-" + String(GET_CHIPID(), 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 (hostName.length()) {
config.hostName = hostnameElm.value; config.hostName = hostName;
Serial.println("hostname set to " + config.hostName); Serial.println("hostname set to " + config.hostName);
} }
config.bootUri = AC_ONBOOTURI_HOME; config.bootUri = AC_ONBOOTURI_HOME;
@ -378,10 +378,7 @@ void setup() {
} }
else { else {
Serial.println("connection failed:" + String(WiFi.status())); Serial.println("connection failed:" + String(WiFi.status()));
while (1) { Serial.println("Needs WiFi connection to start publishing messages");
delay(100);
yield();
}
} }
WiFiWebServer& webServer = portal.host(); WiFiWebServer& webServer = portal.host();
@ -390,7 +387,8 @@ void setup() {
} }
void loop() { void loop() {
portal.handleClient(); if (WiFi.status() == WL_CONNECTED) {
// MQTT publish control
if (updateInterval > 0) { if (updateInterval > 0) {
if (millis() - lastPub > updateInterval) { if (millis() - lastPub > updateInterval) {
if (!mqttClient.connected()) { if (!mqttClient.connected()) {
@ -403,3 +401,5 @@ void loop() {
} }
} }
} }
portal.handleClient();
}

Loading…
Cancel
Save