简体   繁体   中英

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. 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.

The virtual sensor is publishing at v1/devices/me/telemetry and the subscriber is subscribed at the same topic 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" .

However, you are not subscribing to the same topic using the subscriber client.

Change the following line in your Subscriber client program:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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