Hello
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++) {
ArSensor1.add(var);
}
}
WriteBufferingStream bufferedWifiClient(wifiSSLClient, 64);
serializeJson(doc, Serial);
client.beginPublish(topic, measureJson(doc), 1);
serializeJson(doc, bufferedWifiClient);
bufferedWifiClient.flush();
client.endPublish();
doc.clear();
Serial.println();
delay(10);
client.loop();```
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.
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"
#ifdef ARDUINO_ARCH_SAMD
#define ser SerialUSB
#else
#define ser Serial
#endif
///////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[] = "#######.s2.eu.hivemq.cloud";
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:
Serial.begin(9600);
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: "));
Serial.println(F(ssid));
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(3000);
}
Serial.println(F("You're connected to the network"));
Serial.println();
// You can provide a username and password for authentication
mqttClient.setUsernamePassword("USER1", "####");
Serial.print(F("Attempting to connect to the MQTT broker: "));
Serial.println(F(broker));
if (!mqttClient.connect(broker, port)) {
Serial.print(F("MQTT connection failed! Error code = "));
Serial.println(mqttClient.connectError());
while (1)
;
}
Serial.println(F("You're connected to the MQTT broker!"));
Serial.println();
}
void loop() {
unsigned long currentMillis = millis();
doc.clear();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
mqttClient.poll();
//************ send message, the PLecSens_1rint interface can be used to set the message contents
mqttClient.beginMessage(topic);
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++) {
ArSensor1.add(result);
}
}
serializeJson(doc, Serial);
serializeJson(doc, mqttClient);
mqttClient.endMessage();
doc.clear();
Serial.println();
Serial.println(mqttClient.connected());
delay(10);
}
}
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.