MQTT connection timeout on Arduino UNO Wi-fi Rev2

My main goal is to have a continuous stream of accelerometer and gyroscope data sent to the MQTT broker set up on my main computer (Mosquitto 1.6.9) on a local network (old TP-Link router, TL-WR841ND). I measure data from the IMU 100 times a second and I would like to send the data 10 times per second (becuase I want to smoothen the data with MattFryer/Smoothed library). Also, I use this MQTT client on my board and the popular ArduinoJSON for my JSON document creation.

The problem I'm facing right now that after about 4 seconds the data flow stops and after keep alive expires, I get the error message: 'Client Arduino has exceeded timeout, disconnecting.'

I tried including client.loop() to ping the broker, doc.clear() for the JSON document that I'm trying to send. None of them helped. Do you have any suggestions? Should I use other libraries or is there something in my implementation that I'm missing?

my code:


#include <ArduinoJson.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include <Arduino_LSM6DS3.h>
#include <Smoothed.h>
 
#include "secrets_data.h"
 
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
 
const size_t CAPACITY = JSON_OBJECT_SIZE(6);
StaticJsonDocument<CAPACITY> doc;
JsonObject object = doc.to<JsonObject>();
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "192.168.0.100";
int port = 1883;
const char topic[] = "arduino_uno_wifi_rev2_data";
const long interval = 10000;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int count = 0;
float ax, ay, az, vX0, vY0, vZ0, vX, vY, vZ, gx, gy, gz;
Smoothed <float> acceleroX;
Smoothed <float> acceleroY;
Smoothed <float> acceleroZ;
Smoothed <float> gyroX;
Smoothed <float> gyroY;
Smoothed <float> gyroZ;
 
void setup()
{
  mqttClient.poll();
  acceleroX.begin(SMOOTHED_AVERAGE, 1);
  acceleroY.begin(SMOOTHED_AVERAGE, 1);
  acceleroZ.begin(SMOOTHED_AVERAGE, 1);
  gyroX.begin(SMOOTHED_AVERAGE, 1);
  gyroY.begin(SMOOTHED_AVERAGE, 1);
  gyroZ.begin(SMOOTHED_AVERAGE, 1);
  acceleroX.clear();
  acceleroY.clear();
  acceleroZ.clear();
  gyroX.clear();
  gyroY.clear();
  gyroZ.clear();
  vX0 = 0;
  vY0 = 0;
  vZ0 = 0;
  Serial.begin(9600);
  while (!Serial);
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
 
    while (1);
  }
  Serial.println("Attempting to connect to WPA SSID: ");
  Serial.print(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    //failure, retry
    Serial.println(".");
    delay(5000);
  }
  Serial.println("Coonected to network: ");
  Serial.print(ssid);
 
  Serial.println("Attempting to connect to MQTT Broker:");
  Serial.print(broker);
  if (!mqttClient.connect(broker, port)) {
    Serial.println("MQTT connection failed! Error code: ");
    Serial.print(mqttClient.connectError());
    while (1);
  }
  Serial.println("You're connected to the broker!");
  Serial.println("Subscribing to topic: ");
  Serial.print(topic);
}
 
void loop()
{
  for (int i = 0; i < 10; i++) {
    currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      if (IMU.accelerationAvailable()) {
        IMU.readAcceleration(ax, ay, az);
      }
      if (IMU.gyroscopeAvailable()) {
        IMU.readGyroscope(gx, gy, gz);
      }
      acceleroX.add(ax);
      acceleroY.add(ay);
      acceleroZ.add(az);
      gyroX.add(gx);
      gyroY.add(gy);
      gyroZ.add(gz);
    }
  }
  IMU.readAcceleration(ax, ay, az);
  IMU.readGyroscope(gx, gy, gz);
   acceleroX.add(ax);
      acceleroY.add(ay);
      acceleroZ.add(az);
      gyroX.add(gx);
      gyroY.add(gy);
      gyroZ.add(gz);
  ax = acceleroX.get();
  ay = acceleroY.get();
  az = acceleroZ.get();
  ax = ax * 0.1;
  ay = ay * 0.1;
  az = az * 0.1;
  gx = gx * 0.1;
  gy = gy * 0.1;
  gz = gz * 0.1;
  vX = vX0 + ax;
  vY = vY0 + ay;
  vZ = vZ0 + az;
  doc["ax"] = vX;
  doc["ay"] = vY;
  doc["az"] = vZ;
  doc["gx"] = gx;
  doc["gy"] = gy;
  doc["gz"] = gz;
 
  int qos = 2;
  bool retain = false;
  bool dup = 0;
  mqttClient.beginMessage(topic, retain, qos, dup);
  serializeJsonPretty(doc, mqttClient);
  mqttClient.endMessage();
  acceleroX.clear();
  acceleroY.clear();
  acceleroZ.clear();
  gyroX.clear();
  gyroY.clear();
  gyroZ.clear();
  vX0 = vX;
  vY0 = vY;
  vZ0 = vZ;
  bool cleanSess = true;
  mqttClient.setCleanSession(cleanSess);
}

Post the code to the forum!

1 Like