How to send data from raspberry pi to thingsboard
What you will learn here about sending data from raspberry pi to thingsboard
- How to send data from raspberry pi to thingsboard using Python
- How to send data from raspberry pi to thingsboard using Java
How to send data from raspberry pi to thingsboard using Python
Please follow the following steps to know how to send data to thingsboard using python from raspberry pi
1)Please open the raspberry pi terminal
2)Now please execute the following command to install paho mqtt library on raspberry pi
for python below 3.0 version
for python 3.0 and higher version
3)Now please download the following python code
4)Unzip the downloaded code
5)Now please open downloaded unzip code with python iDE on raspberry pi
I)TOKEN: Please replace the token with a token of your device on which you want to receive data which is shown below.
II)If you want to modify data please modify Payload which is shown below.
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)
6)Now please run the code
7)Once you run the code please check the latest telemetry of your device to see the received data which is shown below
How to send data from raspberry pi to thingsboard using Java
Please follow the following steps to know how to send data from raspberry pi to thingsboard using Java
1)Please visit the following link to know how to send data to thingsboard using Java
Sending data to thingsboard using java
2)Now modify downloaded java code as per your requirement
3)Create the runnable jar file of java program which will send data to thinsgboard
4)Upload the created runnable jar file to your raspberry pi
5)Run the jar file on raspberry pi using following command
6)Once you run jar file please check the latest telemetry field on the thingsboard of your device to see received data.