mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
wifi reconnection bug fixed
This commit is contained in:
@@ -43,7 +43,7 @@
|
||||
},
|
||||
{
|
||||
"type": "h4",
|
||||
"title": "LittleFS version: 245"
|
||||
"title": "LittleFS version: 246"
|
||||
},
|
||||
{
|
||||
"type": "hr"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
//=================Firmeare=================
|
||||
#define FIRMWARE_NAME "esp8266-iotm"
|
||||
#define FIRMWARE_VERSION 245
|
||||
#define FIRMWARE_VERSION 246
|
||||
#define FLASH_4MB true
|
||||
|
||||
//=================System===================
|
||||
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace MqttClient {
|
||||
|
||||
void init();
|
||||
boolean connect();
|
||||
void reconnect();
|
||||
void loop();
|
||||
|
||||
void subscribe();
|
||||
void mqttInit();
|
||||
boolean mqttConnect();
|
||||
void mqttReconnect();
|
||||
void mqttLoop();
|
||||
void mqttSubscribe();
|
||||
|
||||
boolean publish(const String& topic, const String& data);
|
||||
boolean publishData(const String& topic, const String& data);
|
||||
@@ -21,7 +19,5 @@ boolean publishStatus(const String& topic, const String& data);
|
||||
void publishWidgets();
|
||||
void publishState();
|
||||
|
||||
void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length);
|
||||
void mqttCallback(char* topic, uint8_t* payload, size_t length);
|
||||
const String getStateStr();
|
||||
|
||||
} // namespace MqttClient
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
boolean isNetworkActive();
|
||||
|
||||
void startSTAMode();
|
||||
void routerConnect();
|
||||
|
||||
bool startAPMode();
|
||||
|
||||
boolean scanWiFi(String ssid);
|
||||
boolean RouterFind(String ssid);
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ class ButtonInClass : public LineParsing {
|
||||
void switchChangeVirtual(String key, String state) {
|
||||
eventGen(key, "");
|
||||
jsonWriteInt(configLiveJson, key, state.toInt());
|
||||
MqttClient::publishStatus(key, state);
|
||||
publishStatus(key, state);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ class ButtonOutClass : public LineParsing {
|
||||
}
|
||||
eventGen(key, "");
|
||||
jsonWriteInt(configLiveJson, key, state.toInt());
|
||||
MqttClient::publishStatus(key, state);
|
||||
publishStatus(key, state);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -18,13 +18,13 @@ class InputClass : public LineParsing {
|
||||
void inputSetFloat(String key, String state) {
|
||||
eventGen(key, "");
|
||||
jsonWriteFloat(configLiveJson, key, state.toFloat());
|
||||
MqttClient::publishStatus(key, state);
|
||||
publishStatus(key, state);
|
||||
}
|
||||
|
||||
void inputSetStr(String key, String state) {
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, state);
|
||||
MqttClient::publishStatus(key, state);
|
||||
publishStatus(key, state);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class OutputTextClass : public LineParsing {
|
||||
state.replace("#", " ");
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, state);
|
||||
MqttClient::publishStatus(key, state);
|
||||
publishStatus(key, state);
|
||||
}
|
||||
};
|
||||
extern OutputTextClass myOutputText;
|
||||
@@ -24,7 +24,7 @@ class PwmOutClass : public LineParsing {
|
||||
analogWrite(pinInt, state.toInt());
|
||||
eventGen(key, "");
|
||||
jsonWriteInt(configLiveJson, key, state.toInt());
|
||||
MqttClient::publishStatus(key, state);
|
||||
publishStatus(key, state);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ class SensorAnalogClass : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class SensorBme280Class : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class SensorBme280Class : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ class SensorBme280Class : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -28,7 +28,7 @@ class SensorBmp280Class : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ class SensorBmp280Class : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -36,7 +36,7 @@ class SensorDallasClass : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ class SensorDhtClass : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
} else {
|
||||
Serial.println("[E] sensor '" + key);
|
||||
@@ -67,7 +67,7 @@ class SensorDhtClass : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
} else {
|
||||
Serial.println("[E] sensor '" + key);
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
// int valueFl = this->correction(key, value);
|
||||
// eventGen(key, "");
|
||||
// jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
// MqttClient::publishStatus(key, String(valueFl));
|
||||
// publishStatus(key, String(valueFl));
|
||||
// Serial.println("I sensor '" + key + "' data: " + String(valueFl) + ", Slave dev addr: " + String(slaveAddress) + ", Register: " + String(regAddress));
|
||||
// }
|
||||
//
|
||||
|
||||
@@ -34,7 +34,7 @@ class SensorUltrasonic : public SensorConvertingClass {
|
||||
float valueFl = this->correction(key, value);
|
||||
eventGen(key, "");
|
||||
jsonWriteStr(configLiveJson, key, String(valueFl));
|
||||
MqttClient::publishStatus(key, String(valueFl));
|
||||
publishStatus(key, String(valueFl));
|
||||
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -122,7 +122,7 @@ void cmd_init() {
|
||||
// }
|
||||
//
|
||||
// jsonWriteStr(configLiveJson, "text" + number, text);
|
||||
// MqttClient::publishStatus("text" + number, text);
|
||||
// publishStatus("text" + number, text);
|
||||
//}
|
||||
//=====================================================================================================================================
|
||||
////=========================================Модуль шагового мотора======================================================================
|
||||
@@ -243,7 +243,7 @@ void cmd_init() {
|
||||
//
|
||||
// eventGen("servo", number);
|
||||
// jsonWriteInt(configLiveJson, "servo" + number, value);
|
||||
// MqttClient::publishStatus("servo" + number, String(value, DEC));
|
||||
// publishStatus("servo" + number, String(value, DEC));
|
||||
//}
|
||||
//#endif
|
||||
////====================================================================================================================================================
|
||||
|
||||
@@ -137,7 +137,7 @@ void sendLogData(String file, String topic) {
|
||||
json_array = "{\"status\":[" + json_array + "]}";
|
||||
SerialPrint("I","module",json_array);
|
||||
|
||||
MqttClient::publishChart(topic, json_array);
|
||||
publishChart(topic, json_array);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,75 +6,58 @@
|
||||
#include "Global.h"
|
||||
#include "Init.h"
|
||||
|
||||
|
||||
|
||||
namespace MqttClient {
|
||||
|
||||
// Errors
|
||||
int wifi_lost_error = 0;
|
||||
int mqtt_lost_error = 0;
|
||||
bool connected = false;
|
||||
|
||||
// Session params
|
||||
String mqttPrefix;
|
||||
String mqttRootDevice;
|
||||
|
||||
void init() {
|
||||
|
||||
void mqttInit() {
|
||||
myNotAsincActions->add(
|
||||
do_MQTTPARAMSCHANGED, [&](void*) {
|
||||
reconnect();
|
||||
mqttReconnect();
|
||||
},
|
||||
nullptr);
|
||||
|
||||
mqtt.setCallback(handleSubscribedUpdates);
|
||||
mqtt.setCallback(mqttCallback);
|
||||
|
||||
ts.add(
|
||||
WIFI_MQTT_CONNECTION_CHECK, MQTT_RECONNECT_INTERVAL,
|
||||
[&](void*) {
|
||||
if (isNetworkActive()) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
SerialPrint("I", "WIFI", "OK");
|
||||
if (mqtt.connected()) {
|
||||
if (!connected) {
|
||||
SerialPrint("I","MQTT","OK");
|
||||
setLedStatus(LED_OFF);
|
||||
connected = true;
|
||||
}
|
||||
SerialPrint("I", "MQTT", "OK");
|
||||
setLedStatus(LED_OFF);
|
||||
} else {
|
||||
connect();
|
||||
if (!just_load) mqtt_lost_error++;
|
||||
SerialPrint("E", "MQTT", "lost connection");
|
||||
mqttConnect();
|
||||
}
|
||||
} else {
|
||||
if (connected) {
|
||||
SerialPrint("[E]","MQTT","connection lost");
|
||||
connected = false;
|
||||
}
|
||||
SerialPrint("E", "WIFI", "Lost WiFi connection");
|
||||
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
|
||||
wifi_lost_error++;
|
||||
startAPMode();
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
SerialPrint("I","MQTT","disconnect");
|
||||
void mqttDisconnect() {
|
||||
SerialPrint("I", "MQTT", "disconnect");
|
||||
mqtt.disconnect();
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
disconnect();
|
||||
connect();
|
||||
void mqttReconnect() {
|
||||
mqttDisconnect();
|
||||
mqttConnect();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
void mqttLoop() {
|
||||
if (!isNetworkActive() || !mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
void subscribe() {
|
||||
SerialPrint("I","MQTT","subscribe");
|
||||
void mqttSubscribe() {
|
||||
SerialPrint("I", "MQTT", "subscribe");
|
||||
mqtt.subscribe(mqttPrefix.c_str());
|
||||
mqtt.subscribe((mqttRootDevice + "/+/control").c_str());
|
||||
mqtt.subscribe((mqttRootDevice + "/order").c_str());
|
||||
@@ -83,59 +66,50 @@ void subscribe() {
|
||||
mqtt.subscribe((mqttRootDevice + "/devs").c_str());
|
||||
}
|
||||
|
||||
boolean connect() {
|
||||
SerialPrint("I","MQTT","connect");
|
||||
|
||||
boolean mqttConnect() {
|
||||
SerialPrint("I", "MQTT", "start connection");
|
||||
String addr = jsonReadStr(configSetupJson, "mqttServer");
|
||||
if (!addr) {
|
||||
SerialPrint("[E]","MQTT","no broker address");
|
||||
SerialPrint("E", "MQTT", "no broker address");
|
||||
return false;
|
||||
}
|
||||
|
||||
int port = jsonReadInt(configSetupJson, "mqttPort");
|
||||
String user = jsonReadStr(configSetupJson, "mqttUser");
|
||||
String pass = jsonReadStr(configSetupJson, "mqttPass");
|
||||
|
||||
//Session params
|
||||
mqttPrefix = jsonReadStr(configSetupJson, "mqttPrefix");
|
||||
mqttRootDevice = mqttPrefix + "/" + chipId;
|
||||
|
||||
SerialPrint("I","MQTT","broker " + addr + ":" + String(port, DEC));
|
||||
SerialPrint("I","MQTT","topic " + mqttRootDevice);
|
||||
|
||||
SerialPrint("I", "MQTT", "broker " + addr + ":" + String(port, DEC));
|
||||
SerialPrint("I", "MQTT", "topic " + mqttRootDevice);
|
||||
setLedStatus(LED_FAST);
|
||||
mqtt.setServer(addr.c_str(), port);
|
||||
bool res = false;
|
||||
if (!mqtt.connected()) {
|
||||
if (mqtt.connect(chipId.c_str(), user.c_str(), pass.c_str())) {
|
||||
SerialPrint("I","MQTT","connected");
|
||||
SerialPrint("I", "MQTT", "connected");
|
||||
setLedStatus(LED_OFF);
|
||||
subscribe();
|
||||
mqttSubscribe();
|
||||
res = true;
|
||||
} else {
|
||||
SerialPrint("[E]","MQTT","could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s");
|
||||
SerialPrint("E", "MQTT", "could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s");
|
||||
setLedStatus(LED_FAST);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) {
|
||||
void mqttCallback(char* topic, uint8_t* payload, size_t length) {
|
||||
String topicStr = String(topic);
|
||||
|
||||
SerialPrint("I","MQTT",topicStr);
|
||||
|
||||
SerialPrint("I", "MQTT", topicStr);
|
||||
String payloadStr;
|
||||
|
||||
payloadStr.reserve(length + 1);
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
payloadStr += (char)payload[i];
|
||||
}
|
||||
|
||||
SerialPrint("I","MQTT",payloadStr);
|
||||
SerialPrint("I", "MQTT", payloadStr);
|
||||
|
||||
if (payloadStr.startsWith("HELLO")) {
|
||||
SerialPrint("I","MQTT","Full update");
|
||||
SerialPrint("I", "MQTT", "Full update");
|
||||
publishWidgets();
|
||||
publishState();
|
||||
#ifdef LOGGING_ENABLED
|
||||
@@ -143,7 +117,6 @@ void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) {
|
||||
#endif
|
||||
|
||||
} else if (topicStr.indexOf("control")) {
|
||||
|
||||
//iotTeam/12882830-1458415/light 1
|
||||
|
||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
@@ -184,7 +157,7 @@ boolean publish(const String& topic, const String& data) {
|
||||
boolean publishData(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic;
|
||||
if (!publish(path, data)) {
|
||||
SerialPrint("[E]","MQTT","on publish data");
|
||||
SerialPrint("[E]", "MQTT", "on publish data");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -193,7 +166,7 @@ boolean publishData(const String& topic, const String& data) {
|
||||
boolean publishChart(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic + "/status";
|
||||
if (!publish(path, data)) {
|
||||
SerialPrint("[E]","MQTT","on publish chart");
|
||||
SerialPrint("[E]", "MQTT", "on publish chart");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -244,12 +217,12 @@ void publishWidgets() {
|
||||
void publishWidgets() {
|
||||
auto file = seekFile("layout.txt");
|
||||
if (!file) {
|
||||
SerialPrint("[E]","MQTT","no file layout.txt");
|
||||
SerialPrint("[E]", "MQTT", "no file layout.txt");
|
||||
return;
|
||||
}
|
||||
while (file.available()) {
|
||||
String payload = file.readStringUntil('\n');
|
||||
SerialPrint("I","MQTT","widgets: " + payload);
|
||||
SerialPrint("I", "MQTT", "widgets: " + payload);
|
||||
publishData("config", payload);
|
||||
}
|
||||
file.close();
|
||||
@@ -319,5 +292,3 @@ const String getStateStr() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace MqttClient
|
||||
@@ -48,7 +48,7 @@
|
||||
// float value = 0;
|
||||
// static int counter;
|
||||
// if (dht.getStatus() != 0 && counter < 5) {
|
||||
// //MqttClient::publishStatus(dhtT_value_name, String(dht.getStatusString()));
|
||||
// // publishStatus(dhtT_value_name, String(dht.getStatusString()));
|
||||
// counter++;
|
||||
// } else {
|
||||
// counter = 0;
|
||||
@@ -56,7 +56,7 @@
|
||||
// if (String(value) != "nan") {
|
||||
// //eventGen(dhtT_value_name, "");
|
||||
// //jsonWriteStr(configLiveJson, dhtT_value_name, String(value));
|
||||
// //MqttClient::publishStatus(dhtT_value_name, String(value));
|
||||
// // publishStatus(dhtT_value_name, String(value));
|
||||
// //Serial.println("I sensor '" + dhtT_value_name + "' data: " + String(value));
|
||||
// }
|
||||
// }
|
||||
@@ -86,7 +86,7 @@
|
||||
// float value = 0;
|
||||
// static int counter;
|
||||
// if (dht.getStatus() != 0 && counter < 5) {
|
||||
// //MqttClient::publishStatus(dhtH_value_name, String(dht.getStatusString()));
|
||||
// // publishStatus(dhtH_value_name, String(dht.getStatusString()));
|
||||
// counter++;
|
||||
// } else {
|
||||
// counter = 0;
|
||||
@@ -94,7 +94,7 @@
|
||||
// if (String(value) != "nan") {
|
||||
// //eventGen(dhtH_value_name, "");
|
||||
// //jsonWriteStr(configLiveJson, dhtH_value_name, String(value));
|
||||
// //MqttClient::publishStatus(dhtH_value_name, String(value));
|
||||
// // publishStatus(dhtH_value_name, String(value));
|
||||
// //Serial.println("I sensor '" + dhtH_value_name + "' data: " + String(value));
|
||||
// }
|
||||
// }
|
||||
@@ -112,13 +112,13 @@
|
||||
//void dhtP_reading() {
|
||||
// byte value;
|
||||
// if (dht.getStatus() != 0) {
|
||||
// MqttClient::publishStatus("dhtPerception", String(dht.getStatusString()));
|
||||
// publishStatus("dhtPerception", String(dht.getStatusString()));
|
||||
// } else {
|
||||
// //value = dht.computePerception(jsonReadStr(configLiveJson, dhtT_value_name).toFloat(), jsonReadStr(configLiveJson, dhtH_value_name).toFloat(), false);
|
||||
// String final_line = perceptionStr(value);
|
||||
// jsonWriteStr(configLiveJson, "dhtPerception", final_line);
|
||||
// eventGen("dhtPerception", "");
|
||||
// MqttClient::publishStatus("dhtPerception", final_line);
|
||||
// publishStatus("dhtPerception", final_line);
|
||||
// if (mqtt.connected()) {
|
||||
// Serial.println("I sensor 'dhtPerception' data: " + final_line);
|
||||
// }
|
||||
@@ -137,13 +137,13 @@
|
||||
//void dhtC_reading() {
|
||||
// ComfortState cf;
|
||||
// if (dht.getStatus() != 0) {
|
||||
// MqttClient::publishStatus("dhtComfort", String(dht.getStatusString()));
|
||||
// publishStatus("dhtComfort", String(dht.getStatusString()));
|
||||
// } else {
|
||||
// //dht.getComfortRatio(cf, jsonReadStr(configLiveJson, dhtT_value_name).toFloat(), jsonReadStr(configLiveJson, dhtH_value_name).toFloat(), false);
|
||||
// String final_line = comfortStr(cf);
|
||||
// jsonWriteStr(configLiveJson, "dhtComfort", final_line);
|
||||
// eventGen("dhtComfort", "");
|
||||
// MqttClient::publishStatus("dhtComfort", final_line);
|
||||
// publishStatus("dhtComfort", final_line);
|
||||
// Serial.println("I sensor 'dhtComfort' send date " + final_line);
|
||||
// }
|
||||
//}
|
||||
@@ -230,12 +230,12 @@
|
||||
//void dhtD_reading() {
|
||||
// float value;
|
||||
// if (dht.getStatus() != 0) {
|
||||
// MqttClient::publishStatus("dhtDewpoint", String(dht.getStatusString()));
|
||||
// publishStatus("dhtDewpoint", String(dht.getStatusString()));
|
||||
// } else {
|
||||
// //value = dht.computeDewPoint(jsonReadStr(configLiveJson, dhtT_value_name).toFloat(), jsonReadStr(configLiveJson, dhtH_value_name).toFloat(), false);
|
||||
// jsonWriteInt(configLiveJson, "dhtDewpoint", value);
|
||||
// eventGen("dhtDewpoint", "");
|
||||
// MqttClient::publishStatus("dhtDewpoint", String(value));
|
||||
// publishStatus("dhtDewpoint", String(value));
|
||||
// Serial.println("I sensor 'dhtDewpoint' data: " + String(value));
|
||||
// }
|
||||
//}
|
||||
@@ -271,7 +271,7 @@
|
||||
// value = temp_event.temperature;
|
||||
// //jsonWriteStr(configLiveJson, bmp280T_value_name, String(value));
|
||||
// //eventGen(bmp280T_value_name, "");
|
||||
// //MqttClient::publishStatus(bmp280T_value_name, String(value));
|
||||
// // publishStatus(bmp280T_value_name, String(value));
|
||||
// //Serial.println("I sensor '" + bmp280T_value_name + "' data: " + String(value));
|
||||
//}
|
||||
//
|
||||
@@ -303,7 +303,7 @@
|
||||
// value = value / 1.333224;
|
||||
// //jsonWriteStr(configLiveJson, bmp280P_value_name, String(value));
|
||||
// //eventGen(bmp280P_value_name, "");
|
||||
// //MqttClient::publishStatus(bmp280P_value_name, String(value));
|
||||
// // publishStatus(bmp280P_value_name, String(value));
|
||||
// //Serial.println("I sensor '" + bmp280P_value_name + "' data: " + String(value));
|
||||
//}
|
||||
//
|
||||
@@ -328,7 +328,7 @@
|
||||
// value = bme.readTemperature();
|
||||
// //jsonWriteStr(configLiveJson, bme280T_value_name, String(value));
|
||||
// //eventGen(bme280T_value_name, "");
|
||||
// //MqttClient::publishStatus(bme280T_value_name, String(value));
|
||||
// // publishStatus(bme280T_value_name, String(value));
|
||||
// //Serial.println("I sensor '" + bme280T_value_name + "' data: " + String(value));
|
||||
//}
|
||||
//
|
||||
@@ -352,7 +352,7 @@
|
||||
// value = value / 1.333224 / 100;
|
||||
// //jsonWriteStr(configLiveJson, bme280P_value_name, String(value));
|
||||
// //eventGen(bme280P_value_name, "");
|
||||
// //MqttClient::publishStatus(bme280P_value_name, String(value));
|
||||
// // publishStatus(bme280P_value_name, String(value));
|
||||
// //Serial.println("I sensor '" + bme280P_value_name + "' data: " + String(value));
|
||||
//}
|
||||
//
|
||||
@@ -375,7 +375,7 @@
|
||||
// value = bme.readHumidity();
|
||||
// //jsonWriteStr(configLiveJson, bme280H_value_name, String(value));
|
||||
// //eventGen(bme280H_value_name, "");
|
||||
// //MqttClient::publishStatus(bme280H_value_name, String(value));
|
||||
// // publishStatus(bme280H_value_name, String(value));
|
||||
// //Serial.println("I sensor '" + bme280H_value_name + "' data: " + String(value));
|
||||
//}
|
||||
//
|
||||
@@ -399,7 +399,7 @@
|
||||
//
|
||||
// //eventGen(bme280A_value_name, "");
|
||||
//
|
||||
// //MqttClient::publishStatus(bme280A_value_name, String(value));
|
||||
// // publishStatus(bme280A_value_name, String(value));
|
||||
//
|
||||
// //Serial.println("I sensor '" + bme280A_value_name + "' data: " + String(value));
|
||||
//}
|
||||
|
||||
@@ -1,115 +1,106 @@
|
||||
#include "Utils/WiFiUtils.h"
|
||||
|
||||
|
||||
|
||||
void startSTAMode() {
|
||||
void routerConnect() {
|
||||
setLedStatus(LED_SLOW);
|
||||
SerialPrint("I","WIFI","STA Mode");
|
||||
|
||||
String ssid = jsonReadStr(configSetupJson, "routerssid");
|
||||
String passwd = jsonReadStr(configSetupJson, "routerpass");
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
if (ssid == "" && passwd == "") {
|
||||
byte tries = 20;
|
||||
|
||||
String _ssid = jsonReadStr(configSetupJson, "routerssid");
|
||||
String _password = jsonReadStr(configSetupJson, "routerpass");
|
||||
//WiFi.persistent(false);
|
||||
|
||||
if (_ssid == "" && _password == "") {
|
||||
WiFi.begin();
|
||||
} else {
|
||||
if (WiFi.begin(ssid.c_str(), passwd.c_str()) == WL_CONNECT_FAILED) {
|
||||
SerialPrint("[E]","WIFI","failed on start");
|
||||
}
|
||||
WiFi.begin(_ssid.c_str(), _password.c_str());
|
||||
SerialPrint("I", "WIFI", "ssid: " + _ssid);
|
||||
}
|
||||
|
||||
bool keepConnecting = true;
|
||||
uint8_t tries = 20;
|
||||
sint8_t connRes;
|
||||
do {
|
||||
#ifdef ESP8266
|
||||
connRes = WiFi.waitForConnectResult(1000);
|
||||
#else
|
||||
byte connRes = WiFi.waitForConnectResult();
|
||||
#endif
|
||||
switch (connRes) {
|
||||
case WL_NO_SSID_AVAIL: {
|
||||
SerialPrint("[E]","WIFI","no network");
|
||||
keepConnecting = false;
|
||||
} break;
|
||||
case WL_CONNECTED: {
|
||||
String hostIpStr = WiFi.localIP().toString();
|
||||
SerialPrint("I","WIFI","http://" + hostIpStr);
|
||||
jsonWriteStr(configSetupJson, "ip", hostIpStr);
|
||||
keepConnecting = false;
|
||||
} break;
|
||||
case WL_CONNECT_FAILED: {
|
||||
SerialPrint("[E]","WIFI","check credentials");
|
||||
jsonWriteInt(configOptionJson, "pass_status", 1);
|
||||
keepConnecting = false;
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
while (--tries && WiFi.status() != WL_CONNECTED) {
|
||||
if (WiFi.status() == WL_CONNECT_FAILED) {
|
||||
SerialPrint("E", "WIFI", "password is not correct");
|
||||
tries = 1;
|
||||
jsonWriteInt(configOptionJson, "pass_status", 1);
|
||||
}
|
||||
} while (keepConnecting && tries--);
|
||||
Serial.print(".");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
if (isNetworkActive()) {
|
||||
MqttClient::init();
|
||||
setLedStatus(LED_OFF);
|
||||
} else {
|
||||
SerialPrint("[E]","WIFI","failed: " + String(connRes, DEC));
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("");
|
||||
startAPMode();
|
||||
};
|
||||
} else {
|
||||
Serial.println("");
|
||||
SerialPrint("I", "WIFI", "http://" + WiFi.localIP().toString());
|
||||
jsonWriteStr(configSetupJson, "ip", WiFi.localIP().toString());
|
||||
setLedStatus(LED_OFF);
|
||||
mqttInit();
|
||||
}
|
||||
}
|
||||
|
||||
bool startAPMode() {
|
||||
setLedStatus(LED_ON);
|
||||
SerialPrint("I","WIFI","AP Mode");
|
||||
|
||||
String ssid = jsonReadStr(configSetupJson, "apssid");
|
||||
String passwd = jsonReadStr(configSetupJson, "appass");
|
||||
SerialPrint("I", "WIFI", "AP Mode");
|
||||
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
WiFi.softAP(ssid.c_str(), passwd.c_str());
|
||||
String hostIpStr = WiFi.softAPIP().toString();
|
||||
SerialPrint("I","WIFI","Host IP: " + hostIpStr);
|
||||
jsonWriteStr(configSetupJson, "ip", hostIpStr);
|
||||
String _ssidAP = jsonReadStr(configSetupJson, "apssid");
|
||||
String _passwordAP = jsonReadStr(configSetupJson, "appass");
|
||||
|
||||
WiFi.softAP(_ssidAP.c_str(), _passwordAP.c_str());
|
||||
IPAddress myIP = WiFi.softAPIP();
|
||||
|
||||
SerialPrint("I", "WIFI", "AP IP: " + myIP.toString());
|
||||
jsonWriteStr(configSetupJson, "ip", myIP.toString());
|
||||
|
||||
//if (jsonReadInt(configOptionJson, "pass_status") != 1) {
|
||||
ts.add(
|
||||
WIFI_SCAN, 10 * 1000,
|
||||
[&](void*) {
|
||||
WIFI_SCAN, 10 * 1000, [&](void*) {
|
||||
String sta_ssid = jsonReadStr(configSetupJson, "routerssid");
|
||||
SerialPrint("I","WIFI","scanning for " + sta_ssid);
|
||||
if (scanWiFi(sta_ssid)) {
|
||||
|
||||
SerialPrint("I", "WIFI", "scanning for " + sta_ssid);
|
||||
|
||||
if (RouterFind(sta_ssid)) {
|
||||
ts.remove(WIFI_SCAN);
|
||||
startSTAMode();
|
||||
WiFi.scanDelete();
|
||||
routerConnect();
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
|
||||
//}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean scanWiFi(String ssid) {
|
||||
|
||||
boolean RouterFind(String ssid) {
|
||||
bool res = false;
|
||||
int8_t n = WiFi.scanComplete();
|
||||
SerialPrint("I","WIFI","scan result: " + String(n, DEC));
|
||||
if (n == -2) {
|
||||
// не было запущено, запускаем
|
||||
SerialPrint("I","WIFI","start scanning");
|
||||
// async, show_hidden
|
||||
int n = WiFi.scanComplete();
|
||||
SerialPrint("I", "WIFI", "scan result: " + String(n, DEC));
|
||||
|
||||
if (n == -2) { //Сканирование не было запущено, запускаем
|
||||
SerialPrint("I", "WIFI", "start scanning");
|
||||
WiFi.scanNetworks(true, false); //async, show_hidden
|
||||
}
|
||||
|
||||
else if (n == -1) { //Сканирование все еще выполняется
|
||||
SerialPrint("I", "WIFI", "scanning in progress");
|
||||
}
|
||||
|
||||
else if (n == 0) { //ни одна сеть не найдена
|
||||
SerialPrint("I", "WIFI", "no networks found");
|
||||
WiFi.scanNetworks(true, false);
|
||||
} else if (n == -1) {
|
||||
// все еще выполняется
|
||||
SerialPrint("I","WIFI","scanning in progress");
|
||||
} else if (n == 0) {
|
||||
// не найдена ни одна сеть
|
||||
SerialPrint("I","WIFI","no networks found");
|
||||
WiFi.scanNetworks(true, false);
|
||||
} else if (n > 0) {
|
||||
}
|
||||
|
||||
else if (n > 0) {
|
||||
for (int8_t i = 0; i < n; i++) {
|
||||
if (WiFi.SSID(i) == ssid) {
|
||||
res = true;
|
||||
}
|
||||
SerialPrint("I","WIFI",(res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
|
||||
SerialPrint("I", "WIFI", (res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
|
||||
}
|
||||
}
|
||||
WiFi.scanDelete();
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@@ -209,7 +209,7 @@ void web_init() {
|
||||
}
|
||||
|
||||
if (request->hasArg("mqttcheck")) {
|
||||
String buf = "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + MqttClient::getStateStr();
|
||||
String buf = "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + getStateStr();
|
||||
|
||||
String payload = "{}";
|
||||
jsonWriteStr(payload, "title", buf);
|
||||
|
||||
@@ -55,7 +55,7 @@ void setup() {
|
||||
all_init();
|
||||
|
||||
SerialPrint("I","WIFI","Network Init");
|
||||
startSTAMode();
|
||||
routerConnect();
|
||||
|
||||
SerialPrint("I","Uptime","Uptime Init");
|
||||
uptime_init();
|
||||
@@ -105,7 +105,7 @@ void loop() {
|
||||
ws.cleanupClients();
|
||||
#endif
|
||||
timeNow->loop();
|
||||
MqttClient::loop();
|
||||
mqttLoop();
|
||||
myButtonIn.loop();
|
||||
myScenario->loop();
|
||||
loopCmdExecute();
|
||||
|
||||
Reference in New Issue
Block a user