Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » How to connect to a digitransit broker using paho mqtt client(Receive public transport data using python paho mqtt client)
How to connect to a digitransit broker using paho mqtt client [message #1853526] Wed, 06 July 2022 14:04 Go to next message
Daniel Seliger is currently offline Daniel SeligerFriend
Messages: 1
Registered: July 2022
Junior Member
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.

I am not sure if something is missing. Please not that the script is executed via browser based jupyter notebook.

- 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 in a browser based jupyter notebook 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?
Re: How to connect to a digitransit broker using paho mqtt client [message #1855585 is a reply to message #1853526] Fri, 21 October 2022 14:38 Go to previous message
Gambit Support is currently offline Gambit SupportFriend
Messages: 46
Registered: March 2019
Member
Since you are going over TLS to port 8883, you need to at least set TLS parameters for the client, eg. lines 269 and 270 in our mqtt-stats.py at

https://github.com/gambitcomminc/mqtt-stats/blob/master/mqtt-stats.py

which is also published in the Eclipse Marketplace at

https://marketplace.eclipse.org/content/mqtt-stats
Previous Topic:RabbitMQ Server shutdown when connecting and disconnecting via org.eclipse.paho.client.mqttv3 broker
Next Topic:MQTT over Websockets - status code 302 redirect ?
Goto Forum:
  


Current Time: Fri Apr 26 09:46:06 GMT 2024

Powered by FUDForum. Page generated in 0.03741 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top