Stop receiving information from the broker

I am trying to send a sensor reading in packets of 100 data in Json format. Everything works very well but only for a certain time, suddenly I stopped receiving data from the broker. I am using an Arduino Nano 33 Iot card, for the Mqtt connection I tried the PubSubClient and ArduinoMqttClient libraries and with both I had the same problem.

This is the code how I collect and send the data:

   JsonArray ArSensor1 = doc.createNestedArray("Sensor_1");

    for (int i = 0; i < 99; i++) {
      int var = analogRead(A0);
      for (int x = 0; x <= 1; x++) {
    WriteBufferingStream bufferedWifiClient(wifiSSLClient, 64);
    serializeJson(doc, Serial);
    client.beginPublish(topic, measureJson(doc), 1);
    serializeJson(doc, bufferedWifiClient);


Hello @AlejandroCA

Welcome to HiveMQ Community. Unfortunately I don’t have Arduino Nano 33 IoT card to troubleshoot the issue from my side but if you can share the full sketch code I can take a quick look and see if I can suggest something. Are you using HiveMQ Cloud? Please share more information on which HiveMQ edition you are using.

Best regards,
Diego from HiveMQ Team

hello, here is the complete code, thanks for the help.

#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>
#include <ArduinoJson.hpp>
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiSSLClient.h>
#include "avdweb_AnalogReadFast.h"
#define ser SerialUSB  
#define ser Serial  

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "######";  // your network SSID (name)
char pass[] = "######";             // your network password (use for WPA, or use as key for WEP)

WiFiSSLClient wifiSSLClient;
MqttClient mqttClient(wifiSSLClient);

const char broker[] = "";
int port = 8883;
const char topic[] = "myTopic";
String Cov = "";

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

DynamicJsonDocument doc(16384);  

void setup() {
  //Initialize serial and wait for port to open:
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only

  // attempt to connect to WiFi network:
  Serial.print(F("Attempting to connect to WPA SSID: "));
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry

  Serial.println(F("You're connected to the network"));
  // You can provide a username and password for authentication
  mqttClient.setUsernamePassword("USER1", "####");

  Serial.print(F("Attempting to connect to the MQTT broker: "));

  if (!mqttClient.connect(broker, port)) {
    Serial.print(F("MQTT connection failed! Error code = "));

    while (1)

  Serial.println(F("You're connected to the MQTT broker!"));

void loop() {
   unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time a message was sent
    previousMillis = currentMillis;

    //************ send message, the PLecSens_1rint interface can be used to set the message contents

    JsonArray ArSensor1 = doc.createNestedArray("Sensor_1");

    for (int i = 0; i < 399; i++) {
      // int var = analogRead(A0);
      volatile uint16_t result;
      result = analogReadFast(A0);
      for (int x = 0; x <= 1; x++) {

    serializeJson(doc, Serial);
    serializeJson(doc, mqttClient);



Hello @AlejandroCA

Arduino Nano 33 IoT has limited memory and I don’t know which type of analog sensor is connected to A0 pin and the JSON payload being generated but here are some suggestions.

  • The 16384 buffer size that you have set may be too large. You can experiment with smaller buffer sizes if you only need to send a small JSON message.

  • You can remove the serializeJson(doc, Serial); line if you only need to send the message via MQTT, it may consume less memory.

  • In the line 101 you are calling delay(10); after publishing a message to the MQTT broker. This will pause the execution of your program for 10 milliseconds before the next iteration of the loop begins, I think delay(5000); should be more appropriate in this case.

Best regards,
Diego from HiveMQ Team