Using Node-Red To See Tube Arrivals For Your Station

Now that I can see how much energy I, using in the house before i leave for the day, I thought it would be nice to to show how much time I have before my train is due.

Hello Node-Red, let me introduce you to the TFL unified API 🙂

This API allows you access a lot of TFL data including tube schedules. You do need to register at https://api.tfl.gov.uk/ but it only takes a couple of minutes.

I suggest you follow my previous blog on how to install node red for a raspberry pi

https://wherethingsareborn.wordpress.com/2016/07/16/how-to-install-node-red-on-a-raspberry-pi/

Then access the node red we page on your pi and use the men button on the top right and select import from clipboard

[{“id”:”639bfc15.b36bc4″,”type”:”ui_tab”,”z”:”cd173fdb.ff863″,”name”:”Tube Schedule”,”icon”:”dashboard”,”order”:”1″},{“id”:”f6a22f67.06f0b”,”type”:”http request”,”z”:”cd173fdb.ff863″,”name”:”Arrivals”,”method”:”GET”,”ret”:”txt”,”url”:”https://api.tfl.gov.uk/StopPoint/940GZZLUFYR/arrivals&#8221;,”x”:320,”y”:520,”wires”:[[“d2026b60.d00f78”]]},{“id”:”d2c76c11.630d2″,”type”:”inject”,”z”:”cd173fdb.ff863″,”name”:”Injector”,”topic”:””,”payload”:””,”payloadType”:”date”,”repeat”:”15″,”crontab”:””,”once”:false,”x”:87,”y”:520,”wires”:[[“f6a22f67.06f0b”]]},{“id”:”d2026b60.d00f78″,”type”:”json”,”z”:”cd173fdb.ff863″,”name”:””,”x”:469,”y”:522,”wires”:[[“555568a8.6cec38”]]},{“id”:”555568a8.6cec38″,”type”:”function”,”z”:”cd173fdb.ff863″,”name”:”CreateSchedule”,”func”:”var tfl = msg.payload;\nvar trains = {};\nvar station;\nvar expected;\nvar nextTrains = [];\n\n\n// check for valid data first\nif (msg.statusCode == 200) \n{\n // console.log(\”Got valid message\”)\n //if(tfl[0] !== undefined){\n if(tfl[0])\n {\n // console.log(\”Got Station Name\”)\n station = tfl[0].stationName;\n\n // sort schedule\n tfl.sort(function(a,b){\n \t return (a.timeToStation – b.timeToStation);\n \t});\n \n // iterate through all schedule records and save required data\n for (var i = 0; i < tfl.length; i++) {\n if(tfl[i].direction == ‘inbound’ || tfl[i].direction == ‘outbound’){\n \tnextTrains.push({\n \t ‘expected’: ((tfl[i].timeToStation)/60).toFixed(2),\n \t ‘destination’: tfl[i].towards,\n \t ‘platform’: tfl[i].platformName\n \t});\n //\tconsole.log(\”looping\”);\n } \n }\n }\n else\n {\n console.log(\”No station info returned\”)\n // no schedule received\n station = msg.stationid; // use station id as default\n nextTrains.push({\n ‘expected’: 0,\n \t’destination’: \”Out of Service\”\n })\n }\n \n trains.station = station;\n trains.schedule = nextTrains;\n}\n\n// display and send schedule data\nconsole.log(\”outbound schedule new schema > \”);\nconsole.log(util.inspect(trains, false, null));\nmsg.payload = trains;\nflow.set(‘trainschedule’, msg.payload); // save for flow use\nreturn msg;”,”outputs”:1,”noerr”:0,”x”:650,”y”:523,”wires”:[[“441b73e0.bbe31c”]]},{“id”:”441b73e0.bbe31c”,”type”:”ui_template”,”z”:”cd173fdb.ff863″,”tab”:”639bfc15.b36bc4″,”name”:”Train Indicators”,”group”:”Train Indicators”,”order”:1,”format”:”{{msg.payload.station}}\n<ul>\n <li ng-repeat=\”x in msg.payload.schedule\”>\n {{ x.expected + ‘ min, ‘ + x.destination + ‘ ‘ +x.platform }}</li>\n</ul>”,”storeOutMessages”:true,”fwdInMessages”:true,”x”:847,”y”:522,”wires”:[[]]},{“id”:”33aafa2a.c0fd06″,”type”:”comment”,”z”:”cd173fdb.ff863″,”name”:”Comment”,”info”:”Create an injector that runs on a regular basis. \nI have done this every 15 seconds for the demo but\nI recommend using 60 seconds.”,”x”:89,”y”:451,”wires”:[]},{“id”:”86c2e80d.2bddd8″,”type”:”comment”,”z”:”cd173fdb.ff863″,”name”:”Comment”,”info”:”Register for a TFL api key at\n\nhttps://api.tfl.gov.uk/ and then figure out which station you\nwant to monitor. I chose finchely road.\n\nhttps://api.tfl.gov.uk/StopPoint/940GZZLUFYR/arrivals”,”x”:316,”y”:456,”wires”:[]},{“id”:”9b41a98a.54f578″,”type”:”comment”,”z”:”cd173fdb.ff863″,”name”:”Cpmment”,”info”:”COnvert Data To json”,”x”:473,”y”:454,”wires”:[]},{“id”:”178b59fe.ed2736″,”type”:”comment”,”z”:”cd173fdb.ff863″,”name”:”Comment”,”info”:”Process the data and create a schedule”,”x”:632,”y”:455,”wires”:[]},{“id”:”36a2c277.e3687e”,”type”:”comment”,”z”:”cd173fdb.ff863″,”name”:”Comment”,”info”:”Send the data o a UI page”,”x”:827,”y”:451,”wires”:[]}]

This is what the tube arrival schedule looks like

trainflow

Finally, go to the UI page (which is the same as the node-red page with /ui at the end)

and you should see this

trainschedule

Not super exciting visually but still handy to know.

Oh and of course the video

 

A world of possibilities are starting to bubble up in the recesses of my mind.

WhereThingsAreBorn 🙂

Advertisements

One thought on “Using Node-Red To See Tube Arrivals For Your Station

  1. Hi this looks great but I can’t get the scripts to paste – the import button is greyed. On another Node Red example page I was able to paste a shorter script. I’m very new to this so forgive the dumb question – any ideas?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s