This page explains how to use ArduinoJson with ArduinoMqttClient, the official MQTT client library for Arduino. It shows how to use the JSON format in MQTT messages, but you can quickly adapt the examples to use MessagePack.

ArduinoMqttClient is quite new; if you have trouble, you should consider the more mature PubSubClient library.

Deserializing a JSON document in MQTT message

Once your program has received an MQTT message, you can call deserializeJson() and pass the MqttClient instance:

int messageSize = mqttClient.parseMessage();
if (messageSize) {
  StaticJsonDocument<256> doc;
  deserializeJson(doc, mqttClient);
  // use the JsonDocument as usual...
}

This code leverages the Stream interface implemented by MqttClient; that’s why you don’t need to use a temporary buffer.

You can also use the same technique if you use a callback with MqttClient:

void onMqttMessage(int messageSize) {
  StaticJsonDocument<256> doc;
  deserializeJson(doc, mqttClient);
  // use the JsonDocument as usual...
}

Serializing a JSON document into an MQTT message

Because MqttClient implements the Stream interface, you can pass the instance to serializeJson(), like so:

mqttClient.beginMessage(topic);
serializeJson(doc, mqttClient);
mqttClient.endMessage();

Unfortunately, this syntax limits you to a message of 256 bytes, because it goes through the internal buffer of MqttClient. To bypass the buffer, you must specify the size of the message to beginMessage(), like so:

mqttClient.beginMessage(topic, (unsigned long)measureJson(doc));
serializeJson(doc, mqttClient);
mqttClient.endMessage();

The cast to unsigned long prevents the error “call of overloaded beginMessage() is ambiguous”.

See also