You will find the code in the chapter04/mqtt_led.py file. Please review this file before proceeding to get an overall idea of what it contains and then follow these steps:
- Run the program in a Terminal with the following command:
# Terminal #1
(venv) $ python mqtt_led.py
INFO:main:Listening for messages on topic 'led'. Press Control + C to exit.
INFO:main:Connected to MQTT Broker
- Now, open a second Terminal window and try the following, and the LED should turn on (be careful to make sure the JSON string is formed correctly):
# Terminal #2
$ mosquitto_pub -q 2 -h localhost -t 'led' -r -m '{"level": "100"}'
- Did you notice the -r (--retain) option used in step 2? Terminate and restart mqtt_led.py and watch the log output in Terminal #1 and the LED. You should notice on startup that mqtt_led.py receives the LED's brightness value from the topic's retained...