1.安裝paho-mqtt
pip install paho-mqtt
2.修改網址與金鑰 python 程式
import paho.mqtt.client as paho
import os
import json
import time
from datetime import datetime
ACCESS_TOKEN=‘NN7QEiWaX6mxPRnVdJsQ’ #xxxx
broker=“demo.thingsboard.io” #xxxxx
port=1883
def on_publish(client,userdata,result):
print(“data published to thingsboard \n”)
pass
client1= paho.Client(“control1”)
client1.on_publish = on_publish
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)
3.在 demo.thingsboard.io 產生一個device 然後看數據有無出來