Python Tutorial: Introduction to Data streams

Learn to subscribe to IoT sensor data using MQTT and Python's Paho client. The video covers two approaches: subscribesimple for single messages and subscribecallback for continuous streams with a callback function. See a complete example that receives temperature and humidity data as JSON.
Chapters
Welcome back. In this lesson, we'll learn how to subscribe to data streams using mqtt. Data streams are a constant stream of data. Examples of data streams include, but are not limited, Twitter messages, online news articles, video streams such as this video, sensor data or IoT, and financial market orders. This course will focus on sensor data from IoT devices. We mentioned MQTT a couple of times already, but what is it exactly? Mqtt, or Message Queuing Telemetry Transport is a publish subscribed messaging protocol designed to have a small footprint, making it ideal for IoT use cases an MQTT system consists of clients connecting to a server, also called a broker. These clients can publish or produce data to a topic and subscribe or consume data from one or multiple topics. A topic is a string that the broker uses to filter messages for each connected client. Each topic can have multiple clients producing or consuming the same topic. During this course, we'll only act as a client subscribing to predefined topics. Assuming a producer is available and producing data, we'll be using the Eclipse PAHO MQTT Byton client to connect to MQTT brokers. The Python modules are available in the package PAHO mqtt. More information and the documentation for the library is available under the project's GitHub page. In the simplest case, we only want one message from the broker. This could be the case for a webpage showing the latest value available. We can use subscribe simple for this, which takes the topic as the first argument and the broker's hostname as the second argument and will return a message object. We can then print the message topic as well as the message payload. More often we'll want to receive a constant stream of data. We are going to use a concept named callback to subscribe to a topic. This is a very efficient way to wait for things to happen on the other end. We do this by defining a callback function which will be called whenever a new message is available. We are free to select any function name. A recommended name is onmessage and we'll stick to that. The header for a function used as the callback is defined by the BAHO MQTT library and needs to have the arguments client the client instance for this callback User data Private user data set when creating the instance message, an instance of mqtt message which is a class with the properties topic payload, QoS and retain all arguments are mandatory even if we don't use them. Since the library calls the function with exactly these arguments within the function. We'll print the topic of the message and the message payload separated by a colon. We now need to connect our function to an MQTT topic. We'll import the module paho mqtt subscribe and use subscribe callback, with the function to be called as the first argument, the topic to subscribe datacamproomdemp as the second argument, and the server or broker as the keyword argument hostname to connect the function to the topic. The library will then connect to the mqtt broker and call our function each time a new message is available. Let's have a final look at the complete code. First, we import Baho MQTT SubscribeASubscribe. Next, we define our function onmessage and then pass this function as the first argument to subscribe, callback the topic name datacamproomtemp as the second argument and the hostname for the MQTT server as keyboard argument hostname. Once we run our program, we will see the following the topic on the left of the column as well as the message payload on the right, which in this case is a JSON object containing the time, payload, temperature and humidity from an environmental sensor. And now let's try this out.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







