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

Unreliable behaviour with reconnections #91

Open
magmilo opened this issue Mar 23, 2023 · 0 comments
Open

Unreliable behaviour with reconnections #91

magmilo opened this issue Mar 23, 2023 · 0 comments
Labels
question Further information is requested

Comments

@magmilo
Copy link

magmilo commented Mar 23, 2023

Hi, first of all thank you for creating and sharing this library.

I'm facing a problem using the arduino-mqtt library in combination with SSLClient and WiFi.h WiFiClient to connect to a HiveMQ server.
The goal is to get a reliable reconnection setup of the client going using Arduino's Connection Handler to detect network disconnects.
When starting the application I can publish messages without any issues. The frequency of published messages is once every two seconds, and the MQTT QoS is set to 1. As soon as the WiFi connection is lost, one of four things occur, seemingly random:

  1. The client keeps trying to publish messages, and reports a publish success.
  2. The client keeps trying to publish a message and reports a publish failure.
  3. The board stops responding to input and also doesn't print Serial outputs anymore.
  4. The connection handler recognizes the network disconnect and I stop trying to publish messages until the connection has been reestablished.

The development board in use is a Arduino Portenta H7.

I created a minimal (working) example for the Arduino IDE:

#include <Arduino_ConnectionHandler.h>
#include <WiFi.h>
#include <SSLClient.h>
#include <MQTT.h>
#include "certificates.h"

WiFiClient tcpClient;
SSLClient sslClient = SSLClient(tcpClient, TAs, (size_t)TAs_NUM, A0, SSLClient::SSL_WARN);
MQTTClient* mqttClient = new MQTTClient(192);
WiFiConnectionHandler wifiConnectionHandler(ssid, password);
int lastPublish = 0;

void setup() {
  wifiConnectionHandler.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
  wifiConnectionHandler.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
  wifiConnectionHandler.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
  mqttClient->begin(HiveMQ_URL, 8883, sslClient);
}

void loop() {
  wifiConnectionHandler.check();
  if (mqttClient->connected()) {
    mqttClient->loop();
    int currentMillis = millis();
    if (currentMillis - lastPublish > 10000) {
      mqttClient->publish("topic", "log");
      lastPublish = currentMillis;
    }
  }
}

void onNetworkConnect() {
  Serial.println(">>>> CONNECTED to network");
  mqttClient->connect(clientId, username, password);
}

void onNetworkDisconnect() {
  Serial.println(">>>> DISCONNECTED from network");
}

void onNetworkError() {
  Serial.println(">>>> ERROR");
}

Hope someone can help me figure this out :)

@magmilo magmilo added the question Further information is requested label Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant