Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqttClient.connected() always 0 despite being connected #93

Open
Andreasjkoch opened this issue Sep 24, 2023 · 3 comments
Open

mqttClient.connected() always 0 despite being connected #93

Andreasjkoch opened this issue Sep 24, 2023 · 3 comments

Comments

@Andreasjkoch
Copy link

Hi.

I am using the "ArduinoMqttClient" library (version 0.1.7) to connect to an MQTT broker. However, I have encountered an issue where the mqttClient.connected() function always returns 0, even when the connection to the MQTT broker is active and data is being sent.

Environment:

  • Arduino board: MKR1010

Code:

#include "wifi.h"
#include "mqtt.h"

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const long interval = 15000;
unsigned long previousMillis = 0;

void setup() {
    Serial.begin(9600);
    while (!Serial)
    
    connectWifiClient();
    connectMqttClient(mqttClient);
    postHomeassistantDiscoveryObjects(mqttClient);
}

void loop() {
    Serial.println(mqttClient.connected()); // This always returns 0, despite generateTestData() that actually does send data, so the connection clearly works
    mqttClient.poll();
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        generateTestData(mqttClient);
    }
    checkWifiConnection(); 
}
@Andreasjkoch
Copy link
Author

Andreasjkoch commented Sep 24, 2023

Okay so I attempted to go ahead and move the clientConnected() function from private to the public space in the "MqttClient.h" file from the library instead, and that functions works just fine...

@richardsstafford
Copy link

@Andreasjkoch did that solve the problem for you? I'm seeing a similar issue using Arduino Portenta H7 board in Arduino Portenta Machine Control device, connecting to Azure IOT Hub. mqttClient.connected() always returns 0

@Andreasjkoch
Copy link
Author

@Andreasjkoch did that solve the problem for you?

Yes it did. For some reason the public function just doesn't work. So just move the line defining clientConnected() in MqttClient.h so that it is inside of public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants