1

I'm using aedes as my custom MQTT broker and PubSubClient at the ESP32 side, together with the ArduinoJSON package.

My server test code:

import Aedes from "aedes";
import net from "net";

let port = process.env.MQTT_PORT || 1883; // MQTT Port

const id = "MY_SERVER";

const aedes = new Aedes({
    id: id,
});

aedes.on("client", async (client) => {
    console.log("Client conntected...");
});

aedes.on("subscribe", async (subscriptions, client) => {

    let topic = "cmd/" + client.id;

    let msg = {
        topic: topic,
        type: "test",
        param: "p1"
    };

    console.log("Sending command...")
    console.log(msg);

    this.aedes.publish(msg);

});

let server = net.createServer(aedes.handle, options);

server.listen(port, () => {
    console.log(`Find the server at: mqtt://localhost:${port}/`); 
});

When running I'm getting:

Client connected...
Sending command...
{ topic: 'cmd/4CA9A712B42A', type: 'test', param: 'p1' }

At the client (ESP32) side I have my PubSubClient callback as follows:

void callback(char *topic, byte *payload, unsigned int length)
{
    Serial.println("Message received.....");

    StaticJsonDocument<256> doc;
    deserializeJson(doc, payload, length);

    char *command = doc["type"];
    char *param = doc["param"];

    Serial.println(command);
    Serial.println(param);
}

The callback is being called property after subscription - but I'm not being able to parse the payload:

Message received.....
<empty line>
<empty line>
    

How can I fix the server or client to allow then to send/parse correctly the payload.

5
  • Could you Serial.write(payload, length); in the callback and show us what it prints? Commented Mar 6, 2023 at 21:43
  • Did it - payload has length zero, so nothing is coming from the server... Commented Mar 7, 2023 at 12:37
  • Do you have another way of testing the server? Maybe sniff the connection? Commented Mar 7, 2023 at 12:52
  • No way to sniff.... need to try different approach Commented Mar 7, 2023 at 12:54
  • Found the problem.... Needs to have a payload property when sending message: let msg = {topic: topic, payload: JSON.stringify({type: "test", param: "p1"})} did the trick. Commented Mar 7, 2023 at 14:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.