knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.78k stars 1.46k forks source link

publish include clientID #998

Open hpservertech opened 1 year ago

hpservertech commented 1 year ago

Using the mqtt_8266 example it creates a clientid based on the chip. Perfect. This way every device can have the exact same code but connect to the broker with a unique ID.

When publishing though, the clientid is not part of the topic so when more then 1 device is connecting to the broker & doing a publish they all overwrite each other.

How can the clientID be part of the topic? There really should be a way to add a prefix to the topic being published. Such as: client.publish(prefix, "/temperature", msg) client.publish(prefix, "/humidity", msg)

Hundreds of examples of publishing sensor info but every one uses static topic information. How has anyone solved using multiple sensors to the same broker without hard coding unique information in the topic?

The issue isn't on the broker side, the logs show each clientid being passed through on the connection but since the topic is always the same.

esphome solves this with a prefix that appends the clientid(or friendly name if put in) to the beginning of the topic.

hpservertech commented 1 year ago

Here is what 2 devices running the same code look like from mqtt explorer. Mosquitto sees the publish of each client but they are use the same topic.

pubsubhumidity

coreydaley commented 1 year ago

If you want want to use a separate topic for each client, then you need to include that when you publish:

client.publish(prefix, "/temperature/<clientid>", msg)
client.publish(prefix, "/humidity/<clientid>", msg)

You could also just include the clientid in the payload and whatever subscribers you have can parse it out. Each subscriber should get a copy of each message that is published, even though what you see is only the most current message. If you want a queue of messages that you can work through you may be more invested in using something like RabbitMQ.

hpservertech commented 1 year ago

My above adding prefix is an example of what the code should add. It doesnt exist right now. Open to ideas but the work around is not fun when you have a couple dozen topics you want to publish. Had to do the following to get this to do the same results like ESPHome uses. 8 lines just to publish 1 topic so that the client id is part of the topic. Can't really see how anyone has deployed more then 1 device with identical code and used it successfully. The topic should include the client ID, topic & payload. Just a very dirty way of doing it when the code should either allow for a string, or allow for client ID to be added like the example I gave above.

String dewpoint_str; char mqttdewpoint[50]; char dewpointtopic[40]; char dp1[] = {"/Dewpoint"}; strcpy(dewpointtopic, mqttapssid); strcat(dewpointtopic, dp1); dewpoint_str = String(DewPoint); dewpoint_str.toCharArray(mqttdewpoint, hum_str.length() +1);

And finally the publish: client.publish(dewpointtopic, mqttdewpoint);