Let's work through the steps to send (that is, publish) and receive (that is, subscribe to) messages using MQTT:
- In a Terminal, run the following command. mosquitto_sub (Mosquitto subscribe) is a command-line tool to subscribe to messages:
# Terminal #1 (Subscriber)
$ mosquitto_sub -v -h localhost -t 'pyiot'
The options are as follows:
-
- -v (--verbose): verbose is so we get both the message topic and message payload printed on the Terminal.
- -h (--host): localhost is the host of the broker we want to connect to; here it's the one we just installed. The default port used is 1883.
- -t (--topic): pyiot is the topic we want to subscribe to and listen to.
In this chapter, we will require two and sometimes three Terminal sessions for the examples. The first line of a code block will indicate which Terminal you need to run a command in; for example, Terminal #1 in the preceding code block...