InspirationFor about two weeks now I've been working on a small home-automation project.
The inspiration came from few cool YouTube video, and in particular that one:
I'm not particularly fan of Star Trek, but the idea of controlling all the audio and video system from a touch screen using some fancy interface kind of resonated with my inner-geek.
So, what's this home-automation concept anyway?
The home-automation conceptWell, if you push it to the limits, it is about making your house "smarter", which mostly means you can control the power consumption, detect water leaks in the utility room, automatically regulate the temperature depending of the time of the day and your planning, start the coffee machine so it is ready when you wakeup, call the local police station if an intruder is detected, etc, etc...
There's a whole industry1 out there ready to provide wireless cameras, power-grid based transmitters and controllers, security and temperature sensors, smart appliances of all kinds... basically if you can think about something in this domain, it probably exist.
So now, my idea was not to do a full home-automation, if only because it would be overkill for both my small apartment and my wallet. No, what I wanted to do was to have the possibility to replace all my remote controls by one central system connected to a server able to control the whole audio and video equipment, start and stop video streaming from the media center, and then build on that to add small things like gadgets to show the weather forecast, the departure time of the next metro, the latest news headlines, etc... perfectly doable, absolutely pointless, and so by definition something I absolutely needed to give a try at!
So this was the perfect project to motivate me: The control interface had to be practical and nice looking, had to work on computers as well as mobile devices, the real-time nature of the whole thing required some scripting as well.
I'm going to detail another day about how to control the electronic equipment, today I'm only going to show how to display when the next metro is arriving.
Json and the metronautesI guess I started this project at a particularly awesome time.
Not one week had elapsed after the moment I decided I wanted to show on my screen the metro arrival time, that Trafikanten2 decided to provide a web API to access free of charge all the data they use for their main website and stops! Not only that, but they decided in a way that is actually practical and very easy to use.
All the information is available on the Trafikanten Labs page, but since apparently some people have been struggling about how to use it, I'm going to explain you now how to do it. It's very simple, an you can almost copy-paste the code :)
This concept is very simple.
The API is basically just an URL to call, and in return you get some data formatted in JSON format3.
// Parse the data
Then all you have to do is to interpret the data, which is of course dependent on what you requested. In this particular example I requested the real time traffic data for the metro stop Grorud (id 30119404).
The complete codeHere is the complete html code you can use directly to show the results
document.write("Line "+entry['PublishedLineName']+" to "
var aimedTime =convertDate(entry['AimedArrivalTime'])
document.write("Arrival time - Normal:"+aimedTime.toLocaleTimeString())
document.write(" - Probable: "+expectedTime.toLocaleTimeString())
for (var i in array)
Hope this helped!
1. If you are interested, check out products by Z-Wave, X-10, Smarthome's Insteon, Zigbee, Keene, Waveman, Atlona, ...↩
2. The company in charge of the metro, bus and tramway in the Oslo area.↩
3. You can think of JSON as some easier to parse XML↩
4. You can query the id for a particular stop using the API call /Place/FindMatches/grorud↩