From ef582028ba288748f0bf9300ff9d636f86b30f4a Mon Sep 17 00:00:00 2001 From: couin3 <35873376+couin3@users.noreply.github.com> Date: Mon, 27 Apr 2020 19:10:44 +0200 Subject: [PATCH] MQTT merged complement --- RFLink/6_WiFi_MQTT.cpp | 25 +++++++++---------------- RFLink/9_AutoConnect.cpp | 2 ++ RFLink/RFLink.h | 6 +++--- RFLink/RFLink.ino | 22 +++++++++++++--------- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/RFLink/6_WiFi_MQTT.cpp b/RFLink/6_WiFi_MQTT.cpp index b314324c..15e23fa4 100644 --- a/RFLink/6_WiFi_MQTT.cpp +++ b/RFLink/6_WiFi_MQTT.cpp @@ -39,11 +39,10 @@ WiFiClient WIFIClient; PubSubClient MQTTClient; // MQTTClient(WIFIClient); +void callback(char *, byte *, unsigned int); + #ifndef AUTOCONNECT_ENABLED static String WIFI_PWR = String(WIFI_PWR_0); -#endif - -void callback(char *, byte *, unsigned int); void setup_WIFI() { @@ -109,18 +108,18 @@ void reconnect() // Once connected, resubscribe MQTTClient.subscribe(MQTT_TOPIC_IN.c_str()); Serial.print(F("MQTT connection Established : ")); - Serial.println(clientId.c_str()); + Serial.println(MQTT_ID.c_str()); MQTTClient.subscribe(MQTT_TOPIC_IN.c_str()); } else { - Serial.print(F("Connection mqttserver : ")); + Serial.print(F("Connection MQTT_Server : ")); Serial.println(MQTT_SERVER.c_str()); - Serial.print(F("Connection Mqtt_ID : ")); + Serial.print(F("Connection MQTT_ID : ")); Serial.println(MQTT_ID.c_str()); - Serial.print(F("Connection Mqtt_Username : ")); + Serial.print(F("Connection MQTT_Username : ")); Serial.println(MQTT_USER.c_str()); - Serial.print(F("Connection Mqtt_Password : ********")); + Serial.print(F("Connection MQTT_Password : ********")); Serial.print(F("Connection failed : ")); Serial.println(MQTTClient.state()); if (!--retry) @@ -135,18 +134,15 @@ void reconnect() { // Loop until we're reconnected // delay(1); - uint8_t retry = 3; - - Serial.print(F("test")); while (!MQTTClient.connected()) { Serial.print(F("Attempting MQTT connection...")); // Attempt to connect - if (MQTTClient.connect(ac_MQTT_ID.c_str(), ac_MQTT_USER.c_str(), ac_MQTT_PSWD.c_str())) + if (MQTTClient.connect(MQTT_ID.c_str(), MQTT_USER.c_str(), MQTT_PSWD.c_str())) { Serial.println(F("Connected")); // Once connected, resubscribe - // MQTTClient.subscribe(ac_MQTT_TOPIC_IN.c_str()); + MQTTClient.subscribe(MQTT_TOPIC_IN.c_str()); } else { @@ -156,9 +152,6 @@ void reconnect() // Wait 5 seconds before retrying for (byte i = 0; i < 10; i++) delay(500); // delay(5000) may cause hang - if (!--retry) - break; - delay(500); } } } diff --git a/RFLink/9_AutoConnect.cpp b/RFLink/9_AutoConnect.cpp index c0416ec8..6767f9b1 100644 --- a/RFLink/9_AutoConnect.cpp +++ b/RFLink/9_AutoConnect.cpp @@ -451,7 +451,9 @@ String saveParams(AutoConnectAux &aux, PageArgument &args) echo.value += Adv_Power; echo.value += F("
"); +#ifdef MQTT_ENABLED setup_MQTT(); // Reload settings +#endif return String(""); } diff --git a/RFLink/RFLink.h b/RFLink/RFLink.h index 047ee954..3caeddaa 100644 --- a/RFLink/RFLink.h +++ b/RFLink/RFLink.h @@ -106,9 +106,9 @@ // MQTT messages #define SERIAL_ENABLED // Send RFLink messages over Serial #if (defined(ESP32) || defined(ESP8266)) -#define MQTT_ENABLED // Send RFLink messages over MQTT -#define MQTT_LOOP_MS 1000 // MQTTClient.loop(); call period (in mSec) -#define MQTT_RETAINED false // Retained option +#define MQTT_ENABLED // Send RFLink messages over MQTT +#define MQTT_LOOP_MS 1000 // MQTTClient.loop(); call period (in mSec) +#define MQTT_RETAINED_0 false // Retained option #endif // Debug default diff --git a/RFLink/RFLink.ino b/RFLink/RFLink.ino index 6701dedf..f2a852b5 100644 --- a/RFLink/RFLink.ino +++ b/RFLink/RFLink.ino @@ -109,19 +109,23 @@ void loop() { #ifdef AUTOCONNECT_ENABLED loop_AutoConnect(); -#endif -#ifdef MQTT_ENABLED if (WiFi.status() == WL_CONNECTED) { - checkMQTTloop(); - sendMsg(); #endif - - if (CheckSerial()) +#ifdef MQTT_ENABLED + checkMQTTloop(); sendMsg(); +#endif - if (ScanEvent()) - sendMsg(); + if (CheckSerial()) + sendMsg(); + + if (ScanEvent()) + sendMsg(); + +#ifdef AUTOCONNECT_ENABLED + } +#endif } void sendMsg() @@ -135,7 +139,7 @@ void sendMsg() publishMsg(); #endif #ifdef AUTOCONNECT_ENABLED - LastMsg = pbuffer; + LastMsg = pbuffer; #endif #ifdef OLED_ENABLED print_OLED();