mqtt problem

Hi! I'm getting data from a Firebase database and sending it via mqtt with the IBM broker through node-red (attachment).

But, when connecting to ESP32 and subscribing to the topic, I can't get this data in the Arduino IDE, the device is connected and immediately disconnected in the IBM IoT, I don't know what can be ...

void setup() {

  Serial.begin(115200);
  Serial.println();
  Serial.print("Connecting...: ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  Serial.print("WiFi ok, IP: ");
  Serial.println(WiFi.localIP());
  Serial.println(WiFi.SSID());
  conectarMQTT();
}

void loop() {

  client.loop();

}

void conectarMQTT()
{
  Serial.println("Connecting MQTT Broker...");

  if (client.connect(clientId, user, senha))
  {
    Serial.println("Connected MQTT!");
    client.setCallback(callback);
    client.subscribe(topicoLocker);
  }
  else
  {
    Serial.println("Erro MQTT");
    Serial.println(client.state());
    conectarMQTT();
  }
}


void callback(char* topic, unsigned char* payload, unsigned int length) {
  Serial.println("topic: ");
  Serial.println(topic);
  StaticJsonBuffer<100> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(payload);

  if (!root.success())
  {
    Serial.println("Json Parse Error");
    return;
  }

  if (strcmp(topic, topicLocker) == 0)
  {
    String name = (const char*)root["data1"]["name"];
    String name2 = (const char*)root["data2"]["name"];
  }
}

Apparently, client.setCallback(callback) doesn't work.
Can someone help me?

Thanks!

I realized that when I comment the topic subscription line in the code, it is connected to the IBM IoT and the data arrives there, now I don't really understand, I don't know how to get this information if I don't subscribe to the topic ...

:frowning:

Post all of the code.

Read the sticky at the top of the forum "Read this before posting a programming question".

If you post your code as described in the how to use this forum sticky, more forum members will read it.

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