As we discuss the code found in chapter04/mqtt_led.py, pay particular attention to how the code connects to the MQTT broker and manages the connection life cycle. Furthermore, as we cover how the code receives and processes messages, try to relate the code workflow back to the command-line examples that we used to publish the message in the previous subsection, Running the LED MQTT example.
Once you have an understanding of our Python code and how it integrates with our MQTT broker, you'll have an end-to-end working reference solution built around MQTT messaging that you can adapt for your own needs and projects.
We will start by looking at the imports. As usual, we will skip over any common code that we have already covered in previous chapters, including logging setup and GPIOZero-related code.