简体   繁体   English

python 中 ThingsBoard 代理的 MQTT 订阅者

[英]MQTT Subscriber to ThingsBoard broker in python

Situation: I have a python virtual sensor (a python program) that submits data via MQTT protocol to my device in ThingsBoard.情况:我有一个 python 虚拟传感器(一个 python 程序),它通过 MQTT 协议将数据提交到我在 ThingsBoard 中的设备。 I can visualize data on the dashboard, so I'm sure that the data are received.我可以在仪表板上可视化数据,因此我确定已收到数据。

Problem: When I try to connect a python subscriber to the thingsboard broker (demo.thingsboard.io) using paho I obtain that the connection code is 0, so the connection is OK, however I see that the dashboard stops visualising the data from the virtual sensor but the subscriber does not receive anything.问题:当我尝试使用 paho 将 python 订阅者连接到 thingsboard 代理 (demo.thingsboard.io) 时,我发现连接代码为 0,因此连接正常,但是我看到仪表板停止显示来自虚拟传感器,但订阅者没有收到任何东西。

The virtual sensor is publishing at v1/devices/me/telemetry and the subscriber is subscribed at the same topic v1/devices/me/telemetry .虚拟传感器在v1/devices/me/telemetry发布,订阅者在同一主题v1/devices/me/telemetry订阅。

How to show the data published by the virtual sensor on my subscriber client?如何在我的订阅者客户端上显示虚拟传感器发布的数据?

VIRTUAL SENSOR CODE:虚拟传感器代码:

import paho.mqtt.client as paho                     #mqtt library
import os
import json
import time
from datetime import datetime

ACCESS_TOKEN="vgFztmvT6bps7JCeOEZq"                 #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)

CLIENT SUBSCRIBER CODE:客户订阅者代码:

import paho.mqtt.client as mqtt
import time

token = "vgFztmvT6bps7JCeOEZq"
broker="demo.thingsboard.io"                        # host name
port=1883
topic = "v1/devices/me/telemetry"

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc) :
    if (rc==0) :
        print("connected OK Returned code = ", rc)
    else :
        print("Bad connection Returned code = ", rc)
    
def on_message(client, userdata, msg) :
    print (msg.topic + " " + str(msg.payload))    


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set(token)
client.connect(broker , port, 60)
client.subscribe(topic)

client.loop_forever()

In you publisher client , you are using a topic named: "v1/devices/me/telemetry" .在您的发布者客户端中,您正在使用名为"v1/devices/me/telemetry"

However, you are not subscribing to the same topic using the subscriber client.但是,您不会使用订阅者客户端订阅同一主题。

Change the following line in your Subscriber client program:在您的 Subscriber 客户端程序中更改以下行:

client.subscribe(token)

to

client.subscribe(topic)

This should solve the problem.这应该可以解决问题。

我相信 TB 只允许每个客户端有一个连接,因此您应该为发布者和订阅者使用不同的 access_keys。

Try to use tcp://51.159.155.114 but not demo.thingsboard.io尝试使用tcp://51.159.155.114但不要使用demo.thingsboard.io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM