如何使用PAHO MQTT客户端连接到Digitransit经纪人
我是MQTT的新手,我想建立与Digitransit MQTT经纪人的连接,以使用Python Paho MQTT客户端接收公共交通数据。要连接到经纪人端口,需要主题。
关于经纪人来源中提供的信息,我想我至少需要两个信息,但我不确定是否缺少某些信息。请不要说脚本是通过基于浏览器的jupyter笔记本执行的。
经纪人来源: https://digitransit.fi/en/开发人员/API/4-REALTIME-API/车辆位置/ paho文档: https://www.eclipse.org /paho/index.php?page=clients/python/index.php
- 端口:8883
- 主题:/hfp/v2/journey/#
- broker:mqtt.hsl.fi
通过命令行运行时可以接收数据,例如,
/hfp/v2/journey/ongoing/vp/bus/0022/01281/1040/2/Elielinaukio/14:29/1140118/0//// {"VP":{"desi":"40","dir":"2","oper":22,"veh":1281,"tst":"2022-07-06T11:56:53.417Z","tsi":1657108613,"spd":null,"hdg":null,"lat":null,"long":null,"acc":null,"dl":-101,"odo":8530,"drst":0,"oday":"2022-07-06","jrn":2646,"line":62,"start":"14:29","loc":"ODO","stop":null,"route":"1040","occu":0}}
您可以使用两个命令尝试一下:
npm install -g mqtt
mqtt subscribe -h mqtt.hsl.fi -p 8883 -l mqtts -v -t "/hfp/v2/journey/#"
在jupyter Notebook或Python上运行时脚本我只看到定义的输出on_connect
方法,但没有显示数据。
我的代码片段是:
# pip install paho-mqtt
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+ str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/hfp/v2/journey/#")
# The callback for when a PUBLISH message is received from the server.
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.tls_set()
client.connect("mqtt.hsl.fi", 8883, 60)
client.loop_forever()
有人知道哪种细节是错误的还是缺少的?
更新:接受的解决方案适用于通过Pycharm或Visual Studio Code执行的Python脚本,但不使用基于浏览器的IDE(例如Jupyter Notebook)。
I am pretty new to MQTT and I would like to establish a connection to a digitransit mqtt broker to receive public transportation data using python paho mqtt client. To connect to a broker the port and the topic is needed.
Regarding the information provided in the broker source I guess I need at least two pieces of information but I am not sure if something is missing. Please not that the script is executed via browser based jupyter notebook.
Broker source: https://digitransit.fi/en/developers/apis/4-realtime-api/vehicle-positions/
Paho documentation: https://www.eclipse.org/paho/index.php?page=clients/python/index.php
- Port: 8883
- Topic: /hfp/v2/journey/#
- Broker: mqtt.hsl.fi
When running via command line the data can be received, e.g.
/hfp/v2/journey/ongoing/vp/bus/0022/01281/1040/2/Elielinaukio/14:29/1140118/0//// {"VP":{"desi":"40","dir":"2","oper":22,"veh":1281,"tst":"2022-07-06T11:56:53.417Z","tsi":1657108613,"spd":null,"hdg":null,"lat":null,"long":null,"acc":null,"dl":-101,"odo":8530,"drst":0,"oday":"2022-07-06","jrn":2646,"line":62,"start":"14:29","loc":"ODO","stop":null,"route":"1040","occu":0}}
You can try this out simply using two commands:
npm install -g mqtt
mqtt subscribe -h mqtt.hsl.fi -p 8883 -l mqtts -v -t "/hfp/v2/journey/#"
When running on a jupyter notebook or python script I only see the output that is defined on_connect
method but no data shows up.
My code snippet is:
# pip install paho-mqtt
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+ str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/hfp/v2/journey/#")
# The callback for when a PUBLISH message is received from the server.
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.tls_set()
client.connect("mqtt.hsl.fi", 8883, 60)
client.loop_forever()
Does anyone know what kind of detail is wrong or missing?
UPDATE: The accepted solution works for python scripts executed via python IDE like Pycharm or Visual Studio Code but not with browser based IDEs like Jupyter Notebook.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您尚未告诉使用TLS连接的Python Paho客户端。
在调用
contry> connect()
之前,添加client.tls_set()
The problem is you have not told the Python Paho Client it needs to connect using TLS.
Add the
client.tls_set()
before the call toconnect()