Publishing Data Streams from Python
In this tutorial, we look into how to publish Data Stream messages into the IoTFlows console using a simple Python script. If you want to skip instructions and want the answer, head straight to Example Code.
Installing the Paho MQTT Library
You can install the MQTT client using PIP with the following command in your terminal window
NOTE: Depending on the version of Python you are running on your computer will depend on the version of PIP you will require. For older versions check out Intro to MQTT Python Client
Client Methods
Paho MQTT client has several methods, such as
publish()
subscribe()
unsubscribe()
connect()
disconnect()
These methods are associated with callbacks, which we will look into later in our example code.
Importing Client Class
First, we import the client class into our Python code and simplify the naming to just paho
Creating Client Instance
We begin by creating global variables for the client and client id. The client is the client constructor and can take up to 4 optional parameters, but only the client id is required and it must be unique.
The client id will be created from the IoTFlows console as we have seen before. This can be seen in the device page for the specific device you will be streaming to. A specific client can be created for a Python application, similar to our Node-Red Example.
To create a new client you create the object shown below
Connecting to IoTFlows Broker
In order to publish messages to the IoTFlows console, you need to establish a connection with the IoTFlows broker.
To do this use the connect method of the Python MQTT client.
The connect method can be called with 4 parameters. This method declaration is shown below with its default parameters
NOTE: You only need to provide the host (broker name/IP address)
In order to connect to the IoTFlows broker through a secure port, we will create the connection below
Publishing Messages
Once a connection is made, you will use the publish method. This method accepts 4 parameters, which is shown below
NOTE: The only parameters you must supply are the topic and the payload.
The payload is the message you want to publish.
Example Code
Putting everything together, the code below will publish a simulated Temperature value into the IoTFlows console. The parameters that are needed to be changed are
YOUR-DEVICE-CLIENT-ID: This is the Client ID that is displayed on your devices page in the IoTFlows console. See the image below or go to Creating a Device Client for more details.
YOUR-DEVICE-CLIENT-ID-PASSWORD: This is the password for the generated Client ID on the devices page in the IoTFlows console. Go to Creating a Device Client for more details.
YOUR-DEVICE-DATA-STREAM-ID: This is the Data Stream ID that is displayed on the devices page in the IoTFlows console. See the image below or go to Creating a Data Stream for more details.
YOUR-DATA-STREAM-MQTT-TOPIC: This is the Data Stream MQTT Topic that is displayed under each Data Stream. See the image below for more details.
YOUR-DATA: This data is what will be published to the IoTFlows console table.
Running code
Now that you have everything in place, in your terminal run the following command to start your Python script. In our case, our file name is PublishToIoTFlows.py
Below is the example data coming from the Python script
Last updated