Publishing Data Using Python library
In this tutorial, we'll demonstrate how to send random data to the IIOT Platform using Python scratch code and the IIOTPlatform library. By following this tutorial, you will be able to send random data to the IIOT Platform with ease and efficiency. The Python scratch code and IIOTPlatform library make the process straightforward and accessible. So, let's get started and begin sending random data to the IIOT Platform!
Step 1:
Download the IIOTPlatform Library
First, you need to download the IIOTPlatform library. For this, you can download this library using this link ( Download Library. )
Step 2:
Retrieve MQTT Credentials
Log in to the IIOT Platform and go to the project tab. From there, you can copy your MQTT username and token inside the setting section and your MQTT topic inside the assets section that is needed in the program.
Fig 1: Settings Section
Step 3:
Implement MQTT Connection and Data Publishing to IIOT Platform
Now, let's dive into the Python code that establishes the connection with the IIOT Platform and send the random data on the platform.
Explanation:
Including the necessary modules: We include the required modules, such as IIOTPlatform, random and time.
Defining the constant: The code defines the credentials of MQTT details such as server, username, password, and topic.
Create necessary objects: The code creates a variable called Object that handles the MQTT server.
Connecting to the MQTT server: The Object is responsible for connecting to the MQTT server and publishing the sensor data. It establishes a connection to the MQTT server using the provided credentials.
Publishing the data on the platform: We create a loop in which the random data is uploaded every 3 seconds. In this loop, the variable called Object reads the random data and publishes it to the appropriate topic. The random data (temperature and humidity) is sent in string format.
Programs:
import random
import time
from IOTPlatform_functions import IOTPlatform
mqtt_username = "sampleProject_1683791983321" # Your MQTT username
mqtt_password = "EcLA8nzJ4izURJ0vPHNru_7UUm_T-08-" # your MQTT passwrod
mqtt_publish_topic = "a/sampleAsset_1683791983321" # The MQTT topic
if __name__ == '__main__':
Object = IOTPlatform()
Object.debugMode(condition=True)
Object.setCredentials(mqtt_username, mqtt_password)
Object.setTopic(mqtt_publish_topic)
while True:
temperature = random.randint(25, 30)
humidity = random.randint(65, 70)
Object.addParameter("temperature", temperature)
Object.addParameter("humidity", humidity)
Object.publish()
time.sleep(3)
Step 4:
Execute the Python Script
After inserting your MQTT credentials and MQTT topic in the code, Run the code on any Python software (in my case I am using VS code).
Step 5:
Monitor Data on the IIOT Platform
Allow the programme to run for a while so that you can view some useful data on the website. A sample set of readings stored on the website is shown below in graphical format and tabular data:
Fig 2: Graphical Data Representation
Conclusion
In this tutorial, we looked at how to use the IIOTPlatform library to send random data to the IIOT Platform. By following the steps outlined in this tutorial, you should now have a basic understanding of how to use the IIOTPlatform library to send data to the IIOT Platform.