Sending data to Thingsboard using Python
On thingsboard , device is the place where your actual data comes. Thingsboard uses MQTT protocol to receive data from users . Here we will see how to send data to thingsboard using Python. Assuming you have created a device on thingsboard. If you have not created a device on thingsboard, please click on the following link.
Creating a device on thingsboard
1)Here we will send data on Python device . If you don’t know how to create a device on thingsboard please click on above mentioned link.
2)When we create a device on thingsboard, access token gets assign to created device.Access token is required when we send data. Access token helps users to identify a device. Users identify particular device on thingsboard while sending data using access tokens of device not by their actual name. To see your device access token please click on Manage credentials.
3)Data comes to LATEST TELEMETRY field of Device. To see data, click anywhere on the device and then click on the LATEST TELEMETRY field of device. If you see below , there is no data at the moment.
4)In order to send data to thingsboard using python, you need to install paho mqtt library. Please click on following link to know , how to install paho mqtt library on windows as well as linux machine.
Installing paho mqtt library on windows as well as linux machin
5)Assuming you have successfully installed paho mqtt library.
6)Download and Extract the following code to send data to thingsboard using python and modify following fields:
I)TOKEN:Please replace token with token of your device on which you want receive data .
II)If you want to modify data please modify Payload .
Download Thingsboard data post code using python (1160 downloads)import paho.mqtt.client as paho #mqtt library import os import json import time from datetime import datetime ACCESS_TOKEN='NN7QEiWaX6mxPRnVdJsQ' #Token of your device broker="demo.thingsboard.io" #host name port=1883 #data listening port def on_publish(client,userdata,result): #create function for callback print("data published to thingsboard \n") pass client1= paho.Client("control1") #create client object client1.on_publish = on_publish #assign function to callback client1.username_pw_set(ACCESS_TOKEN) #access token from thingsboard device client1.connect(broker,port,keepalive=60) #establish connection while True: payload="{" payload+="\"Humidity\":60,"; payload+="\"Temperature\":25"; payload+="}" ret= client1.publish("v1/devices/me/telemetry",payload) #topic-v1/devices/me/telemetry print("Please check LATEST TELEMETRY field of your device") print(payload); time.sleep(5)
7)Run the above code in python with your device Access token.
8)Now check LATEST TELEMETRY field of your device to see received data.