problems receiving mqtt message

I have a problem: I am receiving a .json message from Firebase through Node-red and send it via mqtt to ESP32. I can receive the message when something is changed in Firebase due to the callback function of the "ArduinoMqttClient" library, but I would like to get the data as soon as I load the sketch in ESP32.

Currently, when I upload the sketch, the topic is signed but I only get the message if I change something in Firebase.

I don't have much experience with mqtt, I don't know if I can get the message of the topic when loading the program to the ESP32.

Here is the complete code and attached (for curiosity) the flow in node-red:

#include <ArduinoMqttClient.h>
#include <WiFi.h>
#include <ArduinoJson.h>

char ssid[] = "wifi";
char pass[] = "password123";

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const String ORG = "abcde";
const String DEVICE_TYPE = "ESP32";
const String DEVICE_ID = "008";
#define DEVICE_TOKEN "SD78963214!@#$"

const String CLIENT_ID = "d:" + ORG + ":" + DEVICE_TYPE + ":" + DEVICE_ID;

const char broker[] = "";
int        port     = 1883;
const char topic[]  = "iot-2/cmd/firebaseData/fmt/json";

String json;

void setup() {
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {

  mqttClient.setUsernamePassword("use-token-auth", DEVICE_TOKEN);

  if (!mqttClient.connect(broker, port)) {
    while (1);



  while (mqttClient.available()) {
    char j =;
    json += j;
  Serial.println("JSON: ");


void loop() {

void onMqttMessage(int messageSize)
  json = "";
  Serial.print("', length ");
  Serial.println(" bytes:");
  while (mqttClient.available()) {
    char j =;
    json += j;

Thank you!

Take a look at MQTT retained messages.

Thank you, @wildbill!

The publisher needs to post the message to the topic with a retention flag as true, right?

So, every new subscriber is able to receive the last valid message.

got it right?

I believe so - not that I've ever used that feature.

HiveMQ has a very good description of retained messages. (Watch the video).

Thank you @wildbill and @SteveMann !

It works! :)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.