Search code examples
raspberry-pimqttmosquitto

Get device configuration when device connects to MQTT Broker


Previously I have setup a device to connect to Google Cloud IoT Core and when the device would connect it would get a callback from the server to a topic with the device configuration file.

I am currently moving off of that to a local MQTT broker on a Raspberry Pi with Mosquitto running. What I am trying to do is figure out how to replicate the sending of the configuration file when connected.

Is there a way for other clients to know when a new client connects? If so I could then just have a client running on the Pi that would is responsible for sending messages.

The thought was that the Pi would hold the configuration file of the connected device and once it connects it would be sent back over to it via a topic for that device

Or is there another solution that I dont even know about yet that accomplishes this? I have not set up my own end to end MQTT communication before so I dont exactly have a path forward here


Solution

  • Is there a way for other clients to know when a new client connects?

    Not as part of the protocol; but it's simple enough to publish a message upon connection. I can see a few ways of accomplishing your aim:

    1. Publish all of the configurations as retained messages. You would require a 'config' topic per device for this (say config/uniquedevicename). When the client starts up it will subscribe to the relevant topic and receive its configuration (the 'server' would need to publish a message to each of the config channels with the retain flag set).
    2. Upon starting the device publishes a message requesting its configuration; the server is subscribed to the relevant topic and responds with the configuration. You can use preset topics for the response or pass the topic in as part of the request.

    For one (or a small number of devices) option 1 would be very simple to implement; however option 2 provides more flexibility.