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!