After initializing our LED and client instances, we get to the program's main entry point.
We are registering a signal handler to capture Ctrl + C key combinations at line (19). The signal_handler method (not shown) simply turns off our LED and gracefully disconnects from the broker:
# Initialise Module
init_led()
init_mqtt()
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler) # (19)
logger.info("Listening for messages on topic '"
+ TOPIC + "'. Press Control + C to exit.")
client.loop_start() # (20)
signal.pause()
At line (20), the call to client.loop_start() is what allows our client to start, connect to the broker, and receive messages.
Did you notice that the LED program is stateless? We are not storing or persisting any LED level in code or to disk. All our program does is subscribe to a topic on the broker and change the LED's brightness...