Reader small image

You're reading from  Practical Python Programming for IoT

Product typeBook
Published inNov 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838982461
Edition1st Edition
Languages
Right arrow

Publishing and subscribing MQTT messages

Let's work through the steps to send (that is, publish) and receive (that is, subscribe to) messages using MQTT:

  1. 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...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Practical Python Programming for IoT
Published in: Nov 2020Publisher: PacktISBN-13: 9781838982461