Search code examples
mqttbrokerhivemq

MQTT topic name design to handle multiple "same" things


In my current IOT project. I will be using multiple ESP8266s (3) to send data and receive actions. Each MCU will be in charge of monitoring different aquariums around the house. I have thought of structuring my topics like the following:

"Data" topics will follow the same structure, for example to retrieve temperature data:

esp8266/aquarium/aquarium_id/temperature/dht11

"Action topics", the topics the MCU subscribes to receive commands, for example:

aquariumcontroller/aquarium_id/action/water

The topic which the aquarium subscribes to to updte the MCU's params:

aquariumcontroller/aquarium/aquarium_id/params

The aquariumcontroller is the MQTT client written in Python. This is the entity that will be sending actions and handling messages received. I have two questions, are my topics correctly named and structured to handle multiple aquariums? Also as I will have a Controller, isn't it better to also have a database which will contain the aquariums info like the topics for this specific aquarium and the params or will i run into problems if i persist topics when i change the aquarium ID?

Thank you


Solution

  • What you mentioned will work. You can also have a common topic that everyone subscribes to and use JSON format to send data. In JSON format you can mention necessary identifiers and actions.

    {
        "aquarium_id": "xxx",
        "operation": "temperature_read",
        "value": "24.5"
    }
    

    For every aquarium_id you can have an individual topic. The advantage of this scheme is that aquarium controller will only receive message that are associated with it however their will be a added complexity where by you have to pre-populate aquarium_id in your server.

    {
        "operation": "temperature_read",
        "value": "24.5"
    }
    

    You should ideally have a database to store aquarium_id and its relevant properties (something like master record). With database you can also store you readings and action to get an historical view of your data. You can use postgres as DBMS.