'Sending Commands with MQTT - is there a pattern?

I'm new to MQTT: but I have got some basic Python programs working where sensor readings can be published to a particular topic: and other clients can then subscribe to get the temperature on a event-driven basis.

But when it comes to sending commands; I'm a little stuck on the best to do this.

So for example: take a 'countdown timer' connected to mqtt.

This timer has two states: 'stopped' and 'started'. It will initialize itself into the 'stopped' state and wait for a 'start' command; and then will count down; publishing the current countdown to a topic. When the countdown reaches zero; it will switch its state to 'stopped' again, and wait for another 'start' command.

If it receives a 'stop' command (over mqtt); it should also go into the 'stopped' state.

So perhaps I could create topics something like:

countdown_timer/command
countdown_timer/state
countdown_timer/value

And the countdown device could subscribe to 'command' and react by publishing to 'state'. ('stopped' or 'started'?)

But should the client somehow 'consume' the 'command' topic value once it has processed it ?

Or would it better to have something like:

countdown_timer/send_command
countdown_timer/command_result

Where the controller would send a command, the subscribed-device would carry-out the command and put 'ok' or 'error' on the 'command_result' topic ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source