Skip to content

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

Open
@Andreasjkoch

Description

@Andreasjkoch

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(); 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions