If you live in California, specifically in Los Angeles and the surrounding counties you soon realize that the area has a huge traffic problem, which meant I had to find a more visual way of identifying, what is the ideal time to leave for work, which is normally a 25 to 30 mins commute on a good day. I know you’re going to say we have “google now” and many other service, but nothing beats an visual LED indicator. The idea here is that as part of the final setup we are going to replace one of the mirror panels with a 3D printed part which diffuses the LEDs. Here is a quick video that demo’s the setup and I am using eSpeak to read out the time to work, for more details on eSpeak and how to setup it up check out blog at - Upcycled Clock - Reading out the weather using eSpeak
As part of setup we are going to use the Google Maps Distance Matrix API to capture the time to work, which is basically the “duration_in_traffic” element in the JSON returned by the API once you have followed the setup steps below.
To get started go the documentation link below, and use Get A Key button to create
https://developers.google.com/maps/documentation/distance-matrix/start
- an app
- followed by generating the API key.
Now create the URL, and modify the origin and destination address to suite your needs. And you will also have to add in the API key. In addition, a suggestion here is to use a tool like Postman to check that your URL is well formed, and to check if you get a response from the Maps API which is basically a JSON.
https://maps.googleapis.com/maps/api/distancematrix/json?origins=395+santa+monica+place,+Santa+Monica,+CA&destinations=21821+Oxnard+St,+Woodland+Hills,+CA|&traffic_model=best_guess&key=YourAPIKey&departure_time=now
{gallery} Google Maps - Distance matrix API |
---|
Create the App |
Make a note of the API Key |
Using Postman to test the URL, which returns a JSON |
For the circuit as show in the picture below , I used
Grove button connected to D2 , which is used to trigger the Google Maps API
To power the LED strip , I am using 4x AA batteries
- Data – pin #11
- Clock – pin #13
For a running a basic test with the LED strip check out blog at - Upcycled Clock – Testing the RGB LED strip
And a portable speaker connected to USB sound card, which reads out the time to work.
Before you run the python program below you will also have to setup eSpeak , check out blog at - Upcycled Clock - Reading out the weather using eSpeak
Now Update the python program with you URL that you created and test above and upload and run the python program on your Intel Edison
#!/usr/bin/python # Create by Carmelito to test time it would take to get to work using Google Maps API and LED strip, and this is based on # Google Maps API -Distance Matrix - https://developers.google.com/maps/documentation/distance-matrix/intro#traffic-model # Grove Button https://github.com/intel-iot-devkit/upm/blob/master/examples/python/grovebutton.py # Led Strip https://github.com/intel-iot-devkit/upm/blob/master/examples/python/apa102.py import urllib2, json from upm import pyupm_grove as grove from upm import pyupm_apa102 as mylib import time import subprocess def get_time_intraffic(): #modify the URL below based to add your orgin, destination address and API Key from google response = urllib2.urlopen('https://maps.googleapis.com/maps/api/distancematrix/json?origins=395+santa+monica+place,+Santa+Monica,+CA&destinations=2… ') data = json.load(response) timeInTraffic = data['rows'][0]['elements'][0]['duration']['value'] distanceInTraffic = data['rows'][0]['elements'][0]['distance']['value'] print ('distance :'+str(distanceInTraffic/1000)+ ' km' + ' time to work :' + str(timeInTraffic/60) + ' mins') return timeInTraffic/60 def main(): #Grove button connected to D2 on grove connector shield button = grove.GroveButton(2) # Grove light sensor connected to A2 lightSensor = grove.GroveLight(2) #Instantiate a strip of 30 LEDs on SPI bus 0 connect data-D12 and Clk - D11 ledStrip = mylib.APA102(60, 0, False) ledStrip.setAllLeds(61,0,0,0) time.sleep(1) text = 'Hello from the Intel Edison' while True: print(button.name(), ' value is ', button.value()) print ("Light Value: " + str(lightSensor.raw_value())) if button.value() == 1: #get the time to work from home in mins timeInTraffic = get_time_intraffic() print (timeInTraffic) if timeInTraffic < 30: print("Setting all LEDs to Green- time to leave now") text = 'Leave now, there is no traffic, time to work '+str(timeInTraffic) +' minutes' try: subprocess.check_output(['espeak', text]) except subprocess.CalledProcessError: print ("Something is not working..") ledStrip.setAllLeds(61, 0, 255, 0) time.sleep(2) elif timeInTraffic >= 30 and timeInTraffic <40: print("Setting all LEDs to orange - hmm flip a coin") text = 'Hmm, there is seems to be some traffic, time to work '+str(timeInTraffic) +' minutes' try: subprocess.check_output(['espeak', text]) except subprocess.CalledProcessError: print ("Something is not working..") ledStrip.setAllLeds(61, 200, 100, 100) time.sleep(2) elif timeInTraffic >= 40: print("Setting all LEDs to Red - to much traffic") text = 'It a bad idea to leave now, there is a lot of traffic and the time to work is '+str(timeInTraffic) +' minutes' try: subprocess.check_output(['espeak', text]) except subprocess.CalledProcessError: print ("Something is not working..") ledStrip.setAllLeds(61, 255, 0, 0) time.sleep(2) else: print("Do nothing") if lightSensor.raw_value() < 150: print("Light value below threshold, setting all LEDS off") ledStrip.setAllLeds(61,0,0,0) time.sleep(2) time.sleep(1) # Delete the button object del button if __name__ == '__main__': main()
References for the blog
Google Maps Distance Matrix API - https://developers.google.com/maps/documentation/distance-matrix/start
eSpeak Documentation - http://espeak.sourceforge.net/commands.html
Postman for testing the API- https://www.getpostman.com/docs/postman/launching_postman/sending_the_first_request
Top Comments