From 023f9c750dc032c0614e3a66ccca79fe06103e59 Mon Sep 17 00:00:00 2001 From: agr Date: Tue, 26 May 2020 16:33:29 +0200 Subject: [PATCH] added reconnect to loop --- Klappe_Arduino.ino | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/Klappe_Arduino.ino b/Klappe_Arduino.ino index 60f3582..54b89a8 100644 --- a/Klappe_Arduino.ino +++ b/Klappe_Arduino.ino @@ -60,6 +60,25 @@ void callback(char *topic, byte *payload, unsigned int length) { } } +void mqttreconnect() { + if (mqttClient.connect("arduino-1", "loxberry", "OSVL0AMqISFXgr5g")) { + // connection succeeded + Serial.println("Connected "); + boolean r = mqttClient.subscribe("loxone/hof/Gansetorauf"); + Serial.println("loxone/hof/Gansetorauf subscribe "); + Serial.println(r); + r = mqttClient.subscribe("loxone/hof/Gansetorzu"); + Serial.println("loxone/hof/Gansetorzu subscribe "); + Serial.println(r); + } else { + // connection failed + // mqttClient.state() will provide more information + // on why it failed. + Serial.println("Connection failed "); + Serial.println(mqttClient.state()); + } +} + void setup() { // Open serial communications and wait for port to open: @@ -74,21 +93,8 @@ void setup() { mqttClient.setServer("192.168.11.35", 1883); mqttClient.setCallback(callback); - if (mqttClient.connect("arduino-1", "loxberry", "OSVL0AMqISFXgr5g")) { - // connection succeeded - Serial.println("Connected "); - boolean r = mqttClient.subscribe("loxone/hof/Gansetorauf"); - Serial.println("loxone/hof/Gansetorauf subscribe "); - Serial.println(r); - r = mqttClient.subscribe("loxone/hof/Gansetorzu"); - Serial.println("loxone/hof/Gansetorzu subscribe "); - Serial.println(r); - } else { - // connection failed - // mqttClient.state() will provide more information - // on why it failed. - Serial.println("Connection failed "); - } + mqttreconnect(); + // Relais pinMode(relais1pin, OUTPUT); pinMode(relais2pin, OUTPUT); @@ -100,9 +106,11 @@ void loop() { // while (true) { - + if (!mqttClient.loop()) { + mqttreconnect(); + } delay(1000); - mqttClient.loop(); + } }