Several ESP32/Raspberry Pi devices clients connected to a MQTT broker on the local LAN, e.g (CONNECT packet) and have a response (CONNACK)
Node-Red server
What I am trying to achieve:
Obtain a list of Clients in Node-RED (include details: Client ID, current subscriptions if any)
Send a command from Node-Red to selected clients (as above) to subscribe to a topic
Unicorn goal.
Is to have a file on the client (String) which has details on the client (connected to the MQTT broker)
This would be manually entered when the client came online, the Node-RED could query this file and get information (file format would be text-based, however, have not yet formalised the actual structure at this time)
Along with other info such as the connection timestamp. You use a Last Will & Testament (LWT) setting on the connection to mark the device offline and the initial connection mesage to mark it online. You can add any other info you like to other topics as shown in the example.
The MQTT protocol has no way to provide the information you want about connected clients.
Some brokers may have an out of band management interface, normally a HTTP API that might be able to supply it, but it would be specific to the broker (e.g. emqx) and not related to the MQTT protocol at all.
And there is no way to send a message to a specific MQTT client without either
Publishing to a single topic all clients are subscribed to and including a client identifier in either the message body or if using MQTTv5 a header property and having the clients filter messages intended for them
Have ever client listen on their own specific topic e.g. command/<client-id>
As for retrieving a file from a specific client, again you will need to write this into the client code and have them listen for commands as described above and respond on a topic (this could be in the command request, or a dedicated response topic for each client) with the content of the file
Can't think of any off the top of my head. one of us should probably really do a write up on it.
When your IoT device powers up, it should connect to your broker and send the initial online message. The connection should define the LWT message, this actually is triggered on the broker itself if the client device stops talking to the broker. The initial msg and the LWT should use the same topic, just a different payload.
learning this and it hard to know where to start - I best learn via a project goal and work my way backwards - hence the can it, how to and best approach questions
I use Tasmota, which automatically sends useful messages including birth and lwt.
For example, one device has the mqtt topic set to tasmota/wemos/door
When I tell it to reboot I get the following MQTT messages. It doesn't include the client ID but it does identify the ESP by hostname and IP address:
Edit: the MQTT client ID for this ESP is DVES_C07971. It's possible to infer this from the fallback topic in INFO1 (as long as I don't mess with the defaults)
If you use Arduino firmware of course you get to program all of this by hand
I don't do so, but I could use the GroupTopic to send a single command which all my Tasmota devices would receive, requesting them to reply with (for example) their uptime.
this is heading where I wanted to go - The ip4 Address will be somewhat dynamic, so i was heading name resolution route,
I was hoping to pull; the MAC address to concatenate and UID
Personally, I never got on with the complexity (as I saw it) of Tasmota's topic structures. Probably because I am OCD and a control freak!
I prefer the flexibility of ESPHome.
But the point remains. The trick with MQTT is to get a workable topic structure. I typically de-normalise my topics.
That's to say that I have hardware based structures such as ESP/<device-id>/... where ESP/<device-id> shows online or offline and the sub-topics contain lots of detail.
Then I will have location based structures such as LOCATION/Living Room/Temperature and so on.
I also typically have data-type structures such as ENVIRONMENT/Temperature/<location>.
That way it is easy to subscribe, using wildcards, to whatever data you need for different uses.