You might be curios what "semafor" means, and where it came from? In Czech it means traffic light, and this is what my project is about.
I wanted to come up with some simple project which can help someone with Electric Imp platform, and turn it into a little and funny project, which is simple as blinking LED (The "Hello, World" of hardware), but much more useful in a real life.
The idea is pretty simple, we'll have three LED: green, yellow and red, but we'll turn on one or another based on a traffic information.
So you can use it to monitor traffic from your home to your work or other way around, which is very helpful if you have flexible schedule and don't need to leave home or work at the same time every day.
We'll need following:
- Three color LEDs (green, yellow, red)
- Three 330Om resistors
- Electric imp dev kit: Contains Imp001 and April board
Where to get traffic information?
Google have a great way to get you travel time if you are using google maps. You can type following URL into your browser to see what is travel time from San Francisco to Palo Alto: https://www.google.com/maps/dir/San+Francisco,+CA/Palo+Alto,+CA/ and it will show you different routes and time:
So I thought that it would be a simple just to use Google Maps Directions API to get current traffic information, but it turns out that it allows to only get route information and travel time without current traffic situation! Functionality we need available only for Google Maps API for Work, which costs money and the only information I was able to find about it in internet that subscription coasts about $10000. Doesn't work for us for such a simple project.
What we can do here is to find other way to get information we need: use other service or API, parse google maps page using different tools or write our own service.
Second way sounds like a funny project, so, let's take a closer look on google maps page. There tons of javascript and this is not really easy to parse it all to get our traffic data. What we can do in a simple way is to use phantomjs/casperjs to load page and get what we need. I wrote simple script using caperjs which is a perfect tool for web scrapping.
You can find script here on a github.
To run it, execute following command:
casperjs --ignore-ssl-errors=true traffic-status-casper.js --start="start point" --finish="finish point"
for example:
casperjs --ignore-ssl-errors=true traffic-status-casper.js --start="San Francisco, CA" --finish="Palo Alto, CA"
which produce output:
[{"color":"red","currentTime":66,"defaultTime":"38 min without traffic","distance":"33.0 miles","name":"Via US-101 S"}]
If google find more then one route, all these routes will be sorted by travel time, this is exactly what we need.
To make our life even more easier I wrote simple nodejs server which uses the same script, and even created a docker container, so you can host it anywhere you want.
All these files available on a github:
To request the same information from our simple service we'll need to send request:
http://directions.my-secret-domain.com:8080/api/route/San+Francisco,+CA/Palo+Alto,+CA/
The output will be the same as above.
We'll use this URL in our Electric IMP program later to get traffic information.
Hacking hardware.
You can use just three color LED or even use RGB. I decided that it would be more fun to make my project look like a real traffic lights, and this is what I found:
It costs just around $12 on ebay in this is exactly what I need. If you want to get the same toy just go to ebay or amazon and search for traffic lights toy.
First thing I want to do is to disassemble it to find out where I can put Imp with April board. And this is how disassembled toy look like. What a mess!
April board perfectly feet into battery storage, plenty of space there, but it needs some modification using rotary tool:
Whatever you do, the main idea is you need to connect three LED to April board to turn them on when we need it. Here is a simple schematic you can use as a guideline:
And this is what I've got when I connected all three LED from the toy to the Imp's April board running my code. Testing time!
So now we are ready to a fun part: program all the things!
Connecting real things to Internet
This is what IoT is about. Right? So our code will be slitted into two parts: one is agent code and another one is a device code.
Algorithm is a pretty simple:
Every N minutes (let it be 5 by default) device wake up and ask agent to check traffic. Agent send request to the URL where you host your mini nodejs app and get response with traffic information back. Agent code parse response to get "color" of minimal travel time and pass it back to device. Device code will check if desired "color" is already turned on and do nothing if it is true, otherwise it just turn on desired LED, all other LEDs will be turned off.
You can find agent code and device code on a github.
Comments
Please log in or sign up to comment.