I attended an Internet of Things with Arduino @MadLabUK course last year and thought it would be cool to use an Arduino (in this case a Mega) to display some of the data that is accessible through the HERE maps APIs a.k.a the HERE platform (used to be known as the Nokia Location Platform prior to rebranding in 2013).
I work at HERE (a Nokia mapping business which most people knew as NAVTEQ back in the day) and my role is focused on helping HERE customers make better sense of the map (and related) data (and services) we provide. My job lets me get my mitts on tonnes of map data.
In the office we have a glass partitioning wall that I thought could do with livening up – whilst on the MadLab course I got the idea of showing real time traffic information, which HERE provides, using LED’s, Arduino and the EthernetShield.
The HERE platform publishes real time average speeds for roads belonging to the TMC (Traffic Messaging Channel) network – when these real time speeds are compared with expected free flow speeds a colour value can be calculated which indicates how freely the traffic is flowing. This technique is used by most mapping sites for displaying traffic conditions (e.g. here.com, google maps, bing maps). Here’s here.com showing traffic on the M60.
My aim was to replicate this in a physical form using LED’s – I chose the M60 because our office is located in Manchester.
WS2801 RGB LEDs
At the MadLab course I was introduced to RGB LEDs which we used during one of the exercises – using a single RGB LED to show readings from a temperature sensor. I think this is what sparked the idea to use these LEDs to show traffic.
I wondered whether it was possible to get a string of these LED’s – of course it is! After a bit of research I discovered first the self adhesive LED strips but wondered whether these would be diffuse enough for the colours to be seen from all angles. I then came across these bullet WS2801 RGB LEDs from adafruit which looked ideal. I bought 4 strands of 25 and a suitable power supply. The LED’s have to be powered seperately and cannot be powered from the 5V line of an Arduino. Adafruit supply a library and tutorial which makes getting up and running with the LED’s and Arduino really easy. Boom! I loved it when the Arduino started making these pretty colours – I stared at them like a rabbit caught in some oh so colourful headlights.
I’m building a frame…
After getting the LEDs I had to figure out a way of hanging them. Luckily the rubber casing of the bullet LED’s had rubber flanges above and below a 1mm section of the case. This would allow an LED to be pushed into a 11mm hole and the LED would sit there.
Deciding on how to make the shape of the M60 took a while – at one point I thought about laser cutting something but eventually I came across some 1000mm long 20mm wide steel strips in Wickes. Using some chipboard and nails I could bend these to the shape of the M60 (this was my Dad’s suggestion).
The next step was to produce a plot of the M60 carriageways showing the TMC codes between junctions (so I could assign LEDs to the TMC code). Using ArcGIS and HERE map data I selected polylines representing the M60 carriageways and digitized the position of the central reservation. Using ArcGIS I then applied a buffer to the central reservation polyline which gave me the size and form of the frame for the LEDs. Here’s the masterplan:
With the tracks for each carriageway made and fixed into postion on the chipboard I could position each LED (making a note of the TMC code that it would be assigned to) and drill a hole for it. Some TMC’s where represented by more than 1 LED.
To finish the frame I put braces between the metal tracks at appropriate points to give it strength. Loaded with the LEDs it looked like this:
Breathing life into the map – hooking the map up to the HERE platform.
With the Arduino connected to the internet via an Ethernet Shield I wrote some companion php (living_map.php – if you want to look at the code the github details are below) which would connect to the HERE platform and retrieve the speed information of the TMC codes which form the M60.
The php basically hits the HERE GetLinkInfo service (part of the routing REST API) with the list of TMC codes for the M60. The xml in the response provides an average speed and free flow speed for each TMC code in the request. The php then parses this xml, calculates the percentage of current speed vs free flow for each TMC and then writes out the percentage that applies to each of the 100 LEDs. Something like this.
The Arduino code wasn’t much different to the cycling indicator. The Arduino code (living_map.ino) calls the php page to save the hassle of parsing xml in Arduino. The Arduino then decides which colour to display for each LED based on the percentage. The LED’s glow green (free flow), yellow (still travelling but not as fast as you’d like) and red (nuts…i’m gonna be late).
When a section of the M60 slows down enough for an LED to glow red all of the map’s LED’s are set to fade to red for a few seconds to represent brake lights. The map then reverts back to showing the traffic conditions on the whole of the M60.
The map that does more…..
After hooking up the Arduino to the traffic feeds I decided it’d be a waste if the map just displayed traffic conditions. I started scratching round for other feeds I could display information from. DEFRA has a feed which gives pollution levels for various monitoring stations around the country – http://uk-air.defra.gov.uk/rss/current_site_levels.xml. One of these stations is local to the office (Eccles, Salford) so I had the LED’s fade to green (fading the colour in and out) when the pollution index reaches a certain level.
I also thought it’d be nice to show some animations when certain accounts got mentioned on twitter. I knocked together some php (twitter_mentions.php) which receives the name of a twitter account as a parameter and returns the last tweet mentioning this account (by using the twitter search api and searching for tweets containing @accountname). The Arduino code requests this php every 15mins or so and checks to see if a more recent tweet contains this string. If it does it lights the LED’s in an appropriate manner. For instance if the local police station’s twitter account (@GMPSalfordCen) is mentioned the LED’s flash blue and red like blues and twos.
The twitter php I used expanded upon the Twitter voting thingy I’d put together a while ago. However since them Twitter have changed the API so that search requests need to be authenticated. Luckily I found some php code which made this pretty easy – a simple php wrapper for Twitter 1.1 API calls.
In order to animate the LEDs I switched to the FastLED library (rather than the library provided by Adafruit) and found some great code to do some nice animations here http://funkboxing.com/wordpress.
The sound, the sound, the sound……
Showing pretty lights is one thing but as the project progressed I thought it would be nice to have some sounds play at the same time as the map made nice with the lights. For instance, to accompany the LED’s glowing red, representing slowing traffic, the map could play something appropriate like e.g. Crosstown Traffic by Jimi or Cars by Gary Numan.
To play sound from the Arduino I bought a Sound Shield kit from Oomlout (in fact I ended up buying 2 because I made such a lousy soldering job of the first ;)). Like the LEDs from Adafruit, the Sound Shield came with libraries and examples which allow you to get up and running really quickly.
I had intended the Sound Shield to sit underneath the Ethernet Shield on top of the Mega – and initially started with this configuration but this plan changed (see below).
The SoundShield basically streams WAV files off an SD card (it comes with an SD card reader that you mount to the circuit board). For each event the map reacts to I made a directory on the SD card into which I copied sound clips into with a numeric name. I then had the Arduino pick a tune at random number for each event (so the same tune wasn’t played again and again driving the office insane).
But I encountered a problem – I couldn’t play music and animate the LEDs properly at the same time. I posted in the FastLED user group and it turned out the FastLED library makes assumptions that no other interupts are running. Unfortinately in this case, the SoundShield plays audio on an interupt. This caused the LEDs to reset and flicker when I tried to play music and control the LEDs.
To solve this I enlisted the help of a second Arduino. Using the EasyTransfer library I got the 2 Arduino’s communicating over their serial ports. The master Arduino (the Mega with Ethernet Shield running living_map.ino) sends a message to the slave Arduino (an Uno with the Sound Shield running audio_slave.ino). The message consists of 2 integers representing the filename and directory. The slave Arduino receives the message and then plays the audio. This can all be done whilst the master Arduino animates the LEDs.
BTW the Sound Shield has an audio out which takes a 3.5mm headphone plug. In order to broadcast the sound I came across this cool, and cheap, vibration speaker which wobbles glass to produce sound.
Silly String – Out of memory – the final frontier…
With the Living Map up and running, showing traffic conditions on the M60 and breaking off every now and then to perform animations and play music I ran into another problem. After a while the Mega would just hang. It was then that I realised (well read) that using Strings is a bad thing and that eventually you’ll get memory problems. I was using the String class all over the place :). Anyway, the String class is ok if you want to try some code out really quickly but generally it should be avoided (which is a shame for lazy people like me ;)). The out of memory problem went away after switching the code over to use character arrays instead of the Arduino String class.
So in the dead of night and in order to give my work colleagues a surprise my Dad and I snuck into the office and set everything up. I hung the map using picture wire from the struts which support the office’s false ceiling. My Dad did a great job of painting a slogan to accompany the map using window paint.
Here’s my Dad hard at work 😉
The map that does even more…..
Here’s a list of what the map currently does / reacts to:
- At the start of each day the map comes alive at 8am with the LEDs fading up from northing to bright yellow via red – like a sunrise accompanied by e.g. Dolly Parton’s Working 9 to 5 and other such ditties.
- A reminder that it’s lunchtime at 12pm accompanied by something like Weird Al Jankovic’s Eat It, CBeebies lunchtime song.
- Likewise at the end of the working day the LEDs fade down, from yellow to red to nothing, like a sunset accompanied by The End by The Doors (or something similarly final).
- Every day in December at 3pm it played a Christmas song and performed a festive animation. Here it is playing a version of Frosty the Snowman by Man Or Astroman.
- When the pollution level at a nearby monitoring station reaches a certain level the LEDs throb green and an appropriate song is played e.g. The Pogues’ Dirty Old Town, Beck’s New Pollution.
- When traffic starts to slow the LEDs throb red accompanied by e.g. Kraftwerk’s Autobahn, Gary Numan’s Cars.
- When the local police station gets mentioned on Twitter a red/blue police light LED sequence is list accompanied by e.g. Blind Boy’s of Alabama’s Way Down in the Hole (big fans of the Wire in our office).
- At 11am if it’s someone’s birthday the LEDs go into party mode. The birthday boy/girl’s name is announced and a party song is played e.g. Altered Image’s Happy Birthday.
- more to come…..
Here’s some more pictures of the map in action:
The LEDs are bright and can be seen for some distance around 🙂 if the office blinds are open.
Mike Roberts at Slipstream Design for helping me solder the 2nd SoundShield.
MadLabUK for the excellent courses.
Daniel Garcia at the FastLED user group helped me out with my question re: sound and LEDs.
Bill Porter for his EasyTransfer Arduino library.
J7mbo for his excellent php wrapper for Twitter API calls.
teldredge for his great LED animation code.
My Dad for the idea of the bending the metal frame using nails hammered into chip board and for helping me install everything.
Source code is here if you want to check it out – https://github.com/thunderhoss/LivingMap