IIOT Platform Using Python
In this tutorial, we'll demonstrate how to send random data to the IIOT Platform using Python scratch code and MQTT. This tutorial will enable the user to collect and analyse data from various sources and use it to make informed decisions. Overall, this tutorial provides a comprehensive guide for beginners to get started with the IIOT platform.
Step 1:
Download and Install Required Python Module
You need to download the required Python module (paho-mqtt). The latest stable version is available in the Python Package Index (PyPi) and can be installed using this command:
pip install paho-mqtt
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
Now, let's dive into the Python code that establishes the connection with IIOT Platform and send the random data on the platform.
Explanation:
Including the necessary modules: We include the required modules, such as Paho-mqtt, 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 an object called a client that handles the mqtt server and connects to it.
Connecting to the MQTT server: The client 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. It reads the random data and publishes it to the appropriate topic.
Publishing the data on the platform: We create a loop in which the random data is uploaded every 3 seconds. The random data (temperature and humidity) is sent in string format.
Programs:
import time
import random
import paho.mqtt.client as mqtt
mqtt_host = "iiotif.com" # Your server name
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
mqtt_client_id = "IIOTPlatform_pico"
# Initialize our MQTTClient and connect to the MQTT server
client = mqtt.Client()
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(mqtt_host, 1883)
try:
while True:
temperature = random.randint(25, 30)
humidity = random.randint(65, 70)
print("Humidity: {}% Temperature: {}°C".format(humidity, temperature))
temperature_str = "{:.2f}".format(temperature)
humidity_str = "{:.2f}".format(humidity)
payload = "{{\"temperature\":{},\"humidity\":{}}}".format(temperature_str, humidity_str)
client.publish(mqtt_publish_topic, str(payload))
time.sleep(3)
except Exception as e:
print(f'Failed to publish message: {e}')
finally:
client.disconnect()
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 paho-mqtt to send random data to the IIOT Platform. By using paho-mqtt, we were able to establish a connection with the IIOT Platform and send random data in a structured format. With the knowledge gained from this tutorial, users can now confidently use paho-mqtt to send their own data to the IIOT Platform.