0

I am working on a project where multiple MQTT sensor devices are connected to the network. MQTT sensor devices are ESP32 devices that collect various sensor data.

The MQTT broker that I am using is Mosquitto and it is running on Ubuntu virtual machine.

Each MQTT sensor device (client) has unique identification ID that allows the MQTT master to parse the messages accordingly and know which device the data is coming from. The identification ID will tell the sensor device which topics it needs to publish.

For example, the identification ID is set to "sensor1". In that case, the sensor will publish to topics that start with : "sensor1/value", "sensor1/config", "sensor1"/ping" and etc.

My main issue:

I must ensure that it would not be possible for 2 devices with the same identification ID to be connected at once. If that happens, sensor device that connected last should be disconnected and unsubscribed to all topics (basically as if it does not exist).

UPDATE

I have recently read about MQTT client_id feature. From what I learned, this client_id allows each client to be identified and allows the broker to identify each client that connects to the network.

If two MQTT clients are connected and they both share matching client_id's, the old device will be automatically disconnected and the new device will take over its place. I have tested this myself and can confirm that it works.

However, this feature only works when auto_reconnect feature is disabled. In my case, auto_reconnect mqtt feature is enabled which means that when the sensor is Disconnected, it will attempt to reconnect every few seconds. For example, someone accidentaly unplugged the ethernet cable and the device got disconnected. When the ethernet cable is plugged back in, the device will reconnect and function normally.

This auto reconnect causes the MQTT clients with matching client_id's to keep connecting/disconnecting kicking each other from the network.

TheBestPlayer
  • 324
  • 2
  • 13
  • 1
    Can you please rewrite the question so it becomes more clear what you're actually asking about? It _sounds_ like you think you already solved your first issue (it's unclear to me who's going to send a disconnected signal if the device is powered off, normally you solve this kinda issue via heartbeat messages, but if you have a solution that works for you that's great), if so why even mention it? If you want to only ask about issue 2, please make sure to make that more prominent in the question body. If you want to ask about both, open 2 questions, one for each. – Cubic May 03 '23 at 09:04
  • @Cubic The MQTT protocol has a concept known as the Last Will & Testament, which the client can register a message to be published if the broker notices the client goes off line without explicitly disconnecting, which can be used to solve part 1. – hardillb May 03 '23 at 10:06
  • Thanks. I have clarified the question to only contain 1 issue. I have removed part 1 and only left the part 2. As @hardillb mentioned above, part1 is easily solved with last will & testament but not sure how to solve the main issue. – TheBestPlayer May 03 '23 at 10:26
  • You need to tell us what MQTT server you are using as that is where the authentication of clients happens, that it where there will be a list of active clients and so that is where the logic to do this has to be. – Pete Kirkham May 03 '23 at 10:41
  • @PeteKirkham Thanks for clarifying. I was not aware that this is too relevant but I have modified my question to include this information. – TheBestPlayer May 03 '23 at 11:03
  • So you have two devices connecting with the same Client ID; how could the server identify which device is "old" and which is "new"? (I don't think it currently has enough info to determine that). You could, potentially, do this via authentication (but as you don't mention this I'm assuming the devices are anonymous). – Brits May 04 '23 at 03:42
  • I think I will just have to use MQTT the way its meant to be used. When new client is connected with matching client_id, I will just kick out the old device. This mechanism is already being handled internally.. – TheBestPlayer May 04 '23 at 05:55

1 Answers1

0

If that happens, sensor device that connected last should be disconnected and unsubscribed to all topics (basically as if it does not exist).

This statement is in direct opposition to the MQTT spec, which states:

If the ClientId represents a Client already connected to the Server then the Server MUST disconnect the existing Client.

http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718032

The solution here is to derive the client id from a physical serial number attached to each device, something that is truly globally unique in your environment for each sensor. E.g. as described in this question/answer ESP32 Arduino-ide how to get unique id

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Yes I am completely aware that what I want to achieve is the opposition to the MQTT spec hence I wonder if it is even possible. I have thought about your suggestion but I do not think it is good for me. It is very important for me to be able to tell which ID device is set to. The sensor device has small LCD display that shows its ID. For example, if I know that there are some issues "sensor4" device, I can just replace it with another device that has ID set to "sensor4" and it will automatically pick up where the previous device left. – TheBestPlayer May 03 '23 at 16:43