Skip to content

Instantly share code, notes, and snippets.

@tjukanovt
Last active September 26, 2019 02:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjukanovt/ce8f84026db01582fbc23bfd67d56f8b to your computer and use it in GitHub Desktop.
Save tjukanovt/ce8f84026db01582fbc23bfd67d56f8b to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 14 16:54:39 2019
@author: topit
MQTT part based on: http://www.steves-internet-guide.com/into-mqtt-python-client/
API docs for HSL high-frequency positioning: https://digitransit.fi/en/developers/apis/4-realtime-api/vehicle-positions/
"""
import paho.mqtt.client as mqtt
import json
import csv
# 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)
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
# This particular topic collects ALL traffic
client.subscribe("/hfp/v1/journey/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
j = msg.payload.decode('utf8').replace("'", '"')
d = json.loads(j)
v = d["VP"]
# I don't want all of the data, so I define here the values I want
values=v['long'],v['lat'],v['desi'],v['oper'],v['jrn'],v['line'],v['spd'], v['dl'], v['hdg'],v['drst'],v['veh'],v['tsi']
# There are some nulls in there, so this is just a stupid way to exclude those
if v['lat'] > 0:
with open(r'hsl_mqtt.csv', 'a') as f:
writer = csv.writer(f, quoting=csv.QUOTE_NONE, lineterminator='\n')
writer.writerow(values)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("mqtt.hsl.fi", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment