MQTT reconnect

This commit is contained in:
hg
2015-05-07 08:56:47 +02:00
parent 1a467521e6
commit e581e4e3b1

View File

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