Beam transit clock - never miss the bus again

 

We're going to build a scrolling LED display for bus schedules with Beam! You may have seen one of these at your subway station or bus stop. You can build one for the home to help you know exactly how much time you have left -- especially if you tend to hit the snooze alarm in the morning like us! Let's get started. 

 

 

Connect Beam to Particle Photon

Follow the hookup guide here

Sign up for Open API

For this example, we're using the Translink Open API that's available for Vancouver. Go to https://developer.translink.ca/ and sign up for a free account. Your city probably will probably have their own API - a popular one for the US is NextBus

Find your bus stop number

The easiest way is to use Google Maps.  In this example, we searched from Fish Counter to BC Place.  When you display the details of the Bus Directions, the bus stop ID will show up underneath the Bus Number and Route. In this case, the bus stop ID is 59837.  

Register your webhook on Particle Cloud

In the Particle CLI, register your webhook with the following code.  

particle webhook GET bus_info "http://api.translink.ca/rttiapi/v1/stops/59837/estimates?apikey=AAAAAAAAAAAAAAAAAA&count=1&routeNo=003"

Make sure to replace the apikey in the string above with the one you received after signing up for the API. 

Click on the image to the right for more details.  For more info on webhooks, click here: Particle Webhooks.

Download Code from Github and Flash!

Download the library and navigate to the /examples/BeamTransit folder. The Beam library is also on our Github, so make sure to copy it over to the Particle IDE. 

 

A few notes on the code:

The following subscribe method registers a callback called gotBusData to our webhook. 

Particle.subscribe("hook-response/bus_info", gotBusData, MY_DEVICES);

In the gotBusData callback function, we try to extract the relevant info and print the string to Beam. 

void gotBusData(const char *name, const char *data) {
String str = String(data);
String leaveStr = tryExtractString(str, "<ExpectedLeaveTime>", "</ExpectedLeaveTime>");
String destinationStr = tryExtractString(str, "<Destination>", "</Destination>");
String countdownStr = tryExtractString(str, "<ExpectedCountdown>", "</ExpectedCountdown>");
String routeStr = tryExtractString(str, "<RouteNo>", "</RouteNo>");

if (routeStr != NULL) {
Serial.println("Route No: " + routeStr);
}
if (destinationStr != NULL) {
Serial.println("Going to: " + destinationStr);
}
if (countdownStr != NULL) {
Serial.println("Leaving in: " + countdownStr + String("Mins"));
}
if (routeStr !=NULL){
String beamString =String(routeStr + " " + destinationStr + " " + countdownStr + " MINS");
beamString.toUpperCase();
Serial.println(beamString);

char buf[1024];
beamString.toCharArray(buf, 1024);

b.begin();
b.print(buf);
b.setSpeed(5);
b.play();
}
}

 

Finally, we call the publish method in our main loop every 60 seconds. This is to ensure we don't reach our limit for the daily API calls allowed.  

Particle.publish("bus_info");
delay(30000);

 

You're all set

Hopefully at this point, your project is working as expecting.  Any easy way to make sure everything is working is to open up your serial monitor and check for debug messages being printed by the Beam library. We highly recommend following the quick start guide here. If it's still not working, double check your wiring or email us at support@hoverlabs.co