MQTT reconnect
This commit is contained in:
@ -24,6 +24,8 @@ WiFiClient wifiClient;
|
|||||||
//byte server[] = { 192, 168, 87, 100 };
|
//byte server[] = { 192, 168, 87, 100 };
|
||||||
byte server[] = { 172, 16, 2, 16 };
|
byte server[] = { 172, 16, 2, 16 };
|
||||||
PubSubClient client(server, 1883, callback, wifiClient);
|
PubSubClient client(server, 1883, callback, wifiClient);
|
||||||
|
uint8_t disconnectState = 0;
|
||||||
|
uint32_t disconnectTime = 0;
|
||||||
|
|
||||||
|
|
||||||
// char ssid[] = "Kinderland"; // your network SSID (name)
|
// char ssid[] = "Kinderland"; // your network SSID (name)
|
||||||
@ -103,21 +105,25 @@ void setup() {
|
|||||||
|
|
||||||
printWifiStatus();
|
printWifiStatus();
|
||||||
|
|
||||||
if (! client.connect("WiFiPowerMeter")) {
|
// if (! client.connect("WiFiPowerMeter")) {
|
||||||
Serial.println("MQTT broker not found");
|
// Serial.println("MQTT broker not found");
|
||||||
lcd.home();
|
// lcd.home();
|
||||||
lcd.clear();
|
// lcd.clear();
|
||||||
lcd.print("MQTT broker not found");
|
// lcd.print("MQTT broker not found");
|
||||||
while (true);
|
// while (true);
|
||||||
} else {
|
// } else {
|
||||||
lcd.print(" *");
|
// lcd.print(" *");
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
delay(1000);
|
delay(1000);
|
||||||
lcd.home();
|
lcd.home();
|
||||||
lcd.clear();
|
lcd.clear();
|
||||||
|
|
||||||
|
disconnectState = 3;
|
||||||
|
disconnectTime = millis();
|
||||||
|
|
||||||
|
|
||||||
// uptime = (uint32_t*) &(Mb.R[121]); // uptime
|
// uptime = (uint32_t*) &(Mb.R[121]); // uptime
|
||||||
uptime = 0;
|
uptime = 0;
|
||||||
|
|
||||||
@ -132,10 +138,12 @@ void setup() {
|
|||||||
|
|
||||||
void updateDisplay() {
|
void updateDisplay() {
|
||||||
static uint32_t lastUptime = 0;
|
static uint32_t lastUptime = 0;
|
||||||
|
static uint8_t lastDisconnectState = 0;
|
||||||
|
|
||||||
|
|
||||||
if (uptime != lastUptime) {
|
if ((disconnectState != lastDisconnectState) || (uptime != lastUptime)) {
|
||||||
lastUptime = uptime;
|
lastUptime = uptime;
|
||||||
|
lastDisconnectState = disconnectState;
|
||||||
lcd.home(); lcd.clear();
|
lcd.home(); lcd.clear();
|
||||||
|
|
||||||
uint32_t days = uptime / 86400;
|
uint32_t days = uptime / 86400;
|
||||||
@ -145,13 +153,14 @@ void updateDisplay() {
|
|||||||
uint32_t minutes = rem / 60;
|
uint32_t minutes = rem / 60;
|
||||||
uint32_t seconds = rem % 60;
|
uint32_t seconds = rem % 60;
|
||||||
lcd.setCursor(0,0);
|
lcd.setCursor(0,0);
|
||||||
lcd.print(days); lcd.print("d "); lcd.print(hours); lcd.print(":");
|
lcd.print(days); lcd.print("/"); lcd.print(hours); lcd.print(":");
|
||||||
lcd.print(minutes); lcd.print(":"); lcd.print(seconds); lcd.print(" ");
|
lcd.print(minutes); lcd.print(":"); lcd.print(seconds); lcd.print(" ");
|
||||||
|
|
||||||
long rssi = WiFi.RSSI();
|
long rssi = WiFi.RSSI();
|
||||||
lcd.print(rssi); lcd.print(" ");
|
lcd.print(rssi); lcd.print(" ");
|
||||||
uint8_t wifiStatus = WiFi.status();
|
uint8_t wifiStatus = WiFi.status();
|
||||||
lcd.print(wifiStatus);
|
lcd.print(wifiStatus);
|
||||||
|
lcd.print(disconnectState);
|
||||||
|
|
||||||
lcd.setCursor(0, 1);
|
lcd.setCursor(0, 1);
|
||||||
lcd.print(getVoltage()); lcd.print("V");
|
lcd.print(getVoltage()); lcd.print("V");
|
||||||
@ -162,16 +171,54 @@ void updateDisplay() {
|
|||||||
lcd.setCursor(10, 2);
|
lcd.setCursor(10, 2);
|
||||||
lcd.print(getPower()); lcd.print("W");
|
lcd.print(getPower()); lcd.print("W");
|
||||||
lcd.setCursor(0, 3);
|
lcd.setCursor(0, 3);
|
||||||
lcd.print(getEnergy()); lcd.print(" "); lcd.print(getNewEnergy());
|
lcd.print(getEnergy()); lcd.print(" "); lcd.print(getNewEnergy()); lcd.print(" ");
|
||||||
lcd.setCursor(17, 3);
|
|
||||||
lcd.print("kWh");
|
lcd.print("kWh");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
updateDisplay();
|
||||||
|
|
||||||
modbusAppExec();
|
modbusAppExec();
|
||||||
// Mb.Run();
|
// Mb.Run();
|
||||||
client.loop();
|
|
||||||
|
if ((disconnectState == 0) && (! client.loop())) {
|
||||||
|
disconnectState = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
switch (disconnectState) {
|
||||||
|
case 0:
|
||||||
|
// Serial.println("discState 0");
|
||||||
|
// everything fine
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
Serial.println("discState 1");
|
||||||
|
client.disconnect();
|
||||||
|
disconnectTime = millis();
|
||||||
|
disconnectState = 2;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
Serial.println("discState 3");
|
||||||
|
if (disconnectTime + 2000 < millis()) {
|
||||||
|
disconnectState = 3;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
Serial.println("discState 3");
|
||||||
|
if (client.connect("WiFiPowerMeter")) {
|
||||||
|
disconnectTime = millis();
|
||||||
|
disconnectState = 0;
|
||||||
|
} else {
|
||||||
|
disconnectState = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
disconnectState = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (second.check() == 1) {
|
if (second.check() == 1) {
|
||||||
@ -191,7 +238,9 @@ void loop() {
|
|||||||
"\"uptime\": " << uptime <<
|
"\"uptime\": " << uptime <<
|
||||||
"}" <<
|
"}" <<
|
||||||
"}" << endl;
|
"}" << endl;
|
||||||
client.publish("IoT", strbuf);
|
if (disconnectState == 0) {
|
||||||
|
client.publish("IoT", strbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -200,7 +249,6 @@ void loop() {
|
|||||||
Serial.println("tick");
|
Serial.println("tick");
|
||||||
}
|
}
|
||||||
|
|
||||||
updateDisplay();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user