Update 28 Sep 2015: created a variant that rotates between 2 different destinations. GitHub link in the software section. The flow is largely the same, just 2 different webhooks needs to be created.
As I started working recently on IoT business development in Microsoft, I thought I would learn best by doing a project. And I also wanted something useful. In Seattle area the traffic can be unpredictable. Having a glanceable device that tells me all what traffic to expect on my way home or to the gym is great, I don't need to activate my traffic app on the smartphone.
The challenge is to make this info personally relevant. The idea is similar to a watch or weather station in terms of glanceability. However, the information for those is common to the people in the same place, and there are a number of sites readily providing that info that could be parsed.
With traffic information one needs to submit a route from e.g. work to home, and extract the traffic info from there.
There were two major insights that make this projects feasible
- I used Bing maps API. It is a relatively simple API to get a route. The response- and one can see that in text format just submitting that call through a web browser, comes as JSON. It has the traffic info, including how long it takes with and without the traffic between the two specified points. However, the resulting response is still very long and would be complicated to extract that information directly in the Particle board, potentially facing memory shortage. That's where insight #2 comes to help...
- How about make the cloud to sweat instead of the little device. Particle have introduced a functionality called webhooks. Basically, it can send web request, buffering and processing those. It would be useful in itself, as large responses are cut into 512 bytes, and only that smaller chunks are sent to the board. In case of the Bing route, the response would be broken to app 20 pieces, each one individually less likely to test the capabilities of the board to deal with the data. The beautiful thing though is the ability for a webhook to parse data from a JSON response. In case of this project, from a Bing route response that's over 10 kBytes of data, what's actually sent to the Particle board looks something like this: "None~21.642~1120~1128~"
Let's get started!
Get a Bing app key
- Sign to the Bing Developer portal using your MS account: https://www.bingmapsportal.com/
-
Go to “My account -> Create or view
keys”
-
Create a new key, you can do a basic or
trial
-
Leave
url
blank
-
App
type could be a mobile app
-
I created a trial key that will expire in
October:
AllfFa2mK7DlUH0SYiEyJCin7zWQ1O7bWJeP7kU2kM1iinRriN7_BaEw61MIB1mQ
Bing route
Using Bing route REST API: https://msdn.microsoft.com/en-us/library/ff701717.aspx
This is the call in the project:
Shorten the web call
The Bing route call is too long for our purposes, so using bitly.com to shorten it up
Bing route response
The Bing route call generates a long response- let’s make some sense out of it
Format JSON
Create webhook file
{
"event": “traffic_hackster_io",
"event": “traffic_hackster_io",
"url": "http://bit.ly/1MSkCBe",
"requestType": "POST",
"headers": null,
"query": null,
"responseTemplate": "{{#resourceSets}}{{#resources}}{{trafficCongestion}}~{{travelDistance}}~{{travelDuration}}~{{travelDurationTraffic}}~{{/resources}}{{/resourceSets}}",
"json": null,
"auth": null,
"mydevices": true
}
Create webhook in the Particle CLI
"Particle webhook create traffic_hackster_io.json"
Program
Initialize display and LEDs; the project will work with either one (so can use display and/or LEDs)
Call gotTrafficData on the webhook response
This is just giving a visual cue that something is happening (the LEDs are going on in turn) and the display is initialized
Every minute the webhook is called (triggering the gotTrafficResponse function). Also the display is refreshed with the information when was the last successful traffic update.
On the webhook, the function gotTrafficResponse is called. The code here extracts the traffic info and translates km into miles and seconds into minutes.
The rest is just displaying that info on the display and LEDs
Comments
Please log in or sign up to comment.