I’ve tried searching online but I can’t come up whit the right keywords and I can’t find anything about my problem.
I’m using and ESP8266 to send and receive MQTT messages and mosquitto as a broker, every time I publish a message I also receive it on the device I sent it from.
I guess it makes sense since I’m publishing a message to a topic which I’m subscribed to but it makes debugging a little bit annoying and sometimes it messes whit the functionality of my program.
I'm using the classic function to handle messages:
void callback(char* topic, byte* payload, unsigned int length)
{
//Print message
Serial.print("Message arrived: "); Serial.print(topic); Serial.print(", ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
if (strcmp(topic, "cmd/BLABLA") == 0)
{
if (!strncmp((char *)payload, "ON", length))
{
//DO SOMETHING
}
}
If I try to publish a message client.publish("cmd/BLABLA", "OFF");
In the serial console I read Message arrived: cmd/BLABLA, OFF
Is there a way to resolve this problem? Thanks for the help
For example, if I send a message from device A to device B I would like it to be device B only that receive the message
Or for example if I send a message to my HomeBridge server I would like to not receive the message that I just sent
I don't remember if in the past I have had problems with some functions being called multiple times due to this problem but what interests me most is having a cleaner serial console, very often during debugging I get confused due to the quantity of messages that arrives so I would like to eliminate unnecessary ones or differentiate between the ones I have sent and received.
Since unfortunately it is not my mistake or a setting that I can easily change I don't think I will spend time implementing this feature
What are you controlling? Maybe there's a better way to organize your topics.
MQTT isn't very sophisticated (a part of its appeal) so if you're subscribed to a topic and publish to it, you'll get an echo whether you like it or not.
You could include the publisher name in the message and ignore your own messages when they arrive at the callback, but then all the other subscribers have to be able to cope with the new format. Also, it feels a bit dirty.
It might help if you could give some specifics about what these messages are and what the subscribers/publishers are.
When I program something for my home automation I am forced to use standard messages and I don't think anything can be done about it.
If I use MQTT to exchange data between two ESP8266 I can use the messages I want and maybe somehow it can be done but in my opinion it complicates the code too much and as you say it cannot be done cleanly
I'll guess that there is a shared topic that shouldn't be: turn the frobnicator on and report frobnicator status are using a single topic. Perhaps because of limitations of the Homebridge server (whatever that is).
I was convinced that to publish a message I had to be subscribed to that topic.
Since most of the topics aren't shared I can unsubscribe from the ones I use to send messages and the problem is solved