knolleary / pubsubclient

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

Multiple Subcribers #428

Open kermit943 opened 6 years ago

kermit943 commented 6 years ago

Hey there I use the Basic ESP8266 MQTT example on esp12 Wemos mini clon and nodered and mosquitto server on a RaspberryPi 2. There is no problem with switching on and of the LED if I have only one subscriber, but when I use two subscribers I have to send the payload two times to switch both LEDs off/on one LED per message. I thought every subscriber get the payload if I send it once. Any one knows how I can fix this? Is it a problem of the mqtt server or node red? or is this normal?

Thank you for your help

Paul-Reed commented 6 years ago

You need to make these changes - https://github.com/knolleary/pubsubclient/pull/169/files to create a unique subscriber ID on each device. I've already asked in #416 if a new version could be made, so these changes would be added to the arduino library.

kermit943 commented 6 years ago

ahh so there need to be different ID´s wich connect to the server? That makes sense for me

Thanks you =) will try this later today

kermit943 commented 6 years ago

did not try it yet but wouldn´t it be better to have one name per ESP8266? Other wise the server thinks there are a lot more subscribers because every reconect there is a new one?! just asking

EDIT: What about using the Mac-adress?

Paul-Reed commented 6 years ago

If you prefer, you could hardcode the ID into the sketch. You can see in the link that I gave above which lines need to be changed. It's personal choice which option you make, so long as each device has a different subscribe ID.

kermit943 commented 6 years ago

I already changed the example code with your link. Thanks again

kermit943 commented 6 years ago

So I tried your code and it worked well it took me some tries to notice that I´m more stupid than I thought forgot if (!client.connected()) { reconnect(); } client.loop(); in the main loop ....

Then I changed one part of your code

String clientId = "ESP8266Client-"; clientId += String( ESP.getChipId());

@Paul-Reed Thanks again for your great help

Paul-Reed commented 6 years ago

Using the chipId is a nice solution for ESP chips, but I suspect the repo owner wants some consistency across all of the platforms that this library supports. Great that you are sorted.

OK to close?