Hey together,
I'm currently using this example for my MQTT connection, everything works fine so far.
ArduinoMqttClient/examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino at master · arduino-libraries/ArduinoMqttClient (github.com)
Now in this example, the message only gets printed to Serial.
I will receive a value that I will have to work with and set outputs etc.. so currently I'm testing it without any format, just a raw string.
How can I store/save the received message? I'm very very new to Arduino and cpp
Can I use int num =atoi(number_as_string) to convert the message?
Thank you
Presumably you are currently doing something like this
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
If so, then the first step is to store the message in a buffer then convert it to an int. Something like this
char buffer[40];
byte count = 0;
while (mqttClient.available())
{
buffer[count] = mqttClient.read();
count++;
buffer[count] = '\0'; //add C string terminator
}
int value = atoi(buffer); //get int value from the string
cdcdcd
July 9, 2024, 10:59am
3
That wouldve been the solution, but I got the message that we're working with a json file.
I work with an Arduino Nano ESP32:
11:05:38.028 -> [Board] Chip model: ESP32-S3 Rev 0
I've implemented the code part of the ArduinoJson Assistant.. but I get errors:
//------------------------------ VOID MQTT MESSAGE ----------------------//
void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
Serial.print("[MQTT] Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', duplicate = ");
Serial.print(mqttClient.messageDup() ? "true" : "false");
Serial.print(", QoS = ");
Serial.print(mqttClient.messageQoS());
Serial.print(", retained = ");
Serial.print(mqttClient.messageRetain() ? "true" : "false");
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
JsonDocument doc;
deserializeJson(doc, mqttClient);
// use the JsonDocument as usual...
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
int time = doc["time"]; // 10000
const char* direction = doc["direction"]; // "up"
}
Include section at the top looks like this:
//------------------------------ Libraries ------------------------------//
#include <WiFi.h>
#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>
#include "Secrets.h"
//-----------------------------------------------------------------------//
It would have been nice to know that at the start but your example contained no clues that you were using a JSON file
I know little or nothing about the ArduinoJson library, but have you looked at How to use ArduinoJson with ArduinoMqttClient?
As an aside, please do not post screenshots. They are hard to read and the contents cannot be copied for analysis
cdcdcd
July 9, 2024, 11:42am
5
For me too
It was easier for me to blur all the informations than to copy and delete them.
Is it possible that the data gets deleted in/after the while function?
Code looks like this:
//------------------------------ VOID MQTT MESSAGE ----------------------//
void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
Serial.print("[MQTT] Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', duplicate = ");
Serial.print(mqttClient.messageDup() ? "true" : "false");
Serial.print(", QoS = ");
Serial.print(mqttClient.messageQoS());
Serial.print(", retained = ");
Serial.print(mqttClient.messageRetain() ? "true" : "false");
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
Serial.println("[JSON] Deserialize start");
JsonDocument doc;
deserializeJson(doc, mqttClient);
// use the JsonDocument as usual...
int time = doc["time"]; // 10000
const char* direction = doc["direction"]; // "up"
Serial.println(time);
Serial.println(direction);
Serial.println("[JSON] Deserialize finished");
}
//-----------------------------------------------------------------------//
Output like this:
13:40:25.103 -> [MQTT] Received a message with topic 'WorkingHeight/HeightValue', duplicate = false, QoS = 0, retained = false', length 39 bytes:
13:40:25.103 -> {
13:40:25.103 -> "time": 1000,
13:40:25.103 -> "direction": "up"
13:40:25.134 -> }[JSON] Deserialize start
13:40:25.134 -> 0
13:40:25.134 ->
13:40:25.134 -> [JSON] Deserialize finished
cdcdcd
July 9, 2024, 11:50am
6
Update: Works now.. got rid of the serial output of the message..
Code:
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.println("[JSON] Deserialize start");
JsonDocument doc;
deserializeJson(doc, mqttClient);
int time = doc["time"]; // 10000
const char* direction = doc["direction"]; // "up"
Serial.println(time);
Serial.println(direction);
Serial.println("[JSON] Deserialize finished");
}
Output:
13:47:46.272 -> [MQTT] Received a message with topic 'WorkingHeight/HeightValue', duplicate = false, QoS = 0, retained = false', length 39 bytes:
13:47:46.272 -> [JSON] Deserialize start
13:47:46.316 -> 1000
13:47:46.316 -> up
13:47:46.316 -> [JSON] Deserialize finished
You can only read it once, after which it is not available. That is, after all, how the while loop works
I assume that
deserializeJson(doc, mqttClient);
tries to read it again and fails
Either remove the while loop or read the message into a string and deserialise that, which I assume that ArduinoJson can do. If you want/need to see the message then you can print the string
The point is not to make things easier for you , but rather to make things easier for those who are trying to help you ... for free!!!
cdcdcd
July 9, 2024, 12:09pm
9
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.println("[JSON] Deserialize start");
JsonDocument doc;
deserializeJson(doc, mqttClient);
int time = doc["time"]; // 10000
const char* direction = doc["direction"]; // "up"
Serial.print("Output time: ");
Serial.println(time);
Serial.print("Direction: ");
Serial.println(direction);
Serial.println("[JSON] Deserialize finished");
}
13:59:30.879 -> [MQTT] Received a message with topic 'WorkingHeight/HeightValue', duplicate = false, QoS = 0, retained = false', length 37 bytes:
13:59:30.879 -> [JSON] Deserialize start
13:59:30.922 -> Output time: 1000
13:59:30.922 -> Direction: up
13:59:30.922 -> [JSON] Deserialize finished