Bus timetables for the smart home with Alexa, node.js and AWS Lambda

For my sins, I use public transport for commuting, specifically First Buses in Leeds. I don’t work to a strict regiment, so on a morning I’ll catch the next bus into the city centre, whichever that may be, and from whichever stop that might be (I have the luxury of being served by two different bus stops).

Whilst First Group have made good efforts to bring bus timetables into the 21st century with the release of their native apps, they leave somewhat to be desired in terms of usability and convenience. What I really want on a morning is to know when the next bus is, and I want to do it as I’m walking out of the door. It’s possible with the existing apps, but they are slow and clunky - especially when you need to request information about multiple bus stops.

Without further ado, I’d like to introduce the Alexa skill I’ve been developing recently - “NextBus UK”.

“Alexa, next bus.”

As crudely demonstrated in the video above, now on a morning as I’m rushing out of the door I can utter “Alexa, next bus.” and my echo dot device will tell me when the next three buses are due from a selection of two bus stops. Armed with this information I can quickly determine if I have time to get to the bus stop or not. That’s it.

It’s a real pleasure to not have to muck around on my smartphone. Here’s what the worst case scenario looks like in the “traditional” experience:

  • Grab phone and unlock.
  • Find the native bus app.
  • Open app and wait for it to load.
  • Use “next bus” feature.
  • Find bus stop on map
  • Open bus stop and load timetable.
  • Find second bus stop on map
  • Open bus stop and load timetable.
  • Lock phone and put away.

I’ve now been using the Alexa skill for a week and I’m really happy with the results. It’s laughable to suggest I’m reclaiming hours per week, but cutting down nine actions to one every day is a win for me.

How it works

The skill is integrated with an AWS Lambda function. The function itself is small application that makes requests to the TransportAPI for live timetables for the configured bus stops (using ATCO codes).

The response is then processed, sorted and spliced to churn out the three nearest buses. The output is formatted so Alexa can audibly give the bus times in human terms and also via a card presented within the Alexa app - just in case you forget/mishear the information.

Tech stack

I’m still contemplating whether to open source the project which would restrict the benefit to other techies, or turn it into a commercial service with the hope of helping Joe Bloggs.

Until then, for those interested in the technologies used, here’s a summary:

  • [TransportAPI

]( Really cool API that let’s you get to a lot of transport data - even beyond buses. Usage restrictions look reasonably fair too. Check it out. * [AWS Lambda

]( My first dive into serverless technology. Really impressed and it’s made me think about how I might use this more outside of this project. One thing I would say is next time I’d use the serverless framework. Deploys without it have been too manual for my liking. * [node.js

]( Dusting off the old JavaScript hat. Enjoyed getting back into it. * [Alexa SDK

]( Started off with some example code using the hello world skill projects, but ultimately found this package. Will start new projects with this library from the beginning. * [Request promise

]( Thought about giving async/await a whirl but later realised this is in node.js v7+. AWS Lambda runs on 4.3.2 at time of writing, so I stuck with promises.[

]( * [Moment

]( Needed to do date/time processing and Moment does everything you’d ever need. Very effective in avoiding transient tests and mocking the clock. * [Mocha

]( My usual go to testing framework in JS land is Jasmine. Was a good excuse to try another one out. * [Nock

]( Great for stubbing out HTTP responses.

What’s next?

I’m going to sit on this one for a bit longer whilst I make my mind up. It’s not currently in the public skills store. If nothing else, I’ll be using this practically every day. If you would like to discuss the project more please get in touch.