Introduction: Arduino Traffic Display Using Google Maps API

Hello Everyone!

In this instructable I'm going to show you how to make a Arduino device that gets traffic data from the Google Maps API and displays it to a screen and will also set the colour of a RGB LED.

The project runs on an Wemos D1 Mini (ESP8266) programmed via the Arduino IDE and costs roughly $10 in total to make.

The Arduino Traffic Display in this instructable will have the following features:

  • Gets the estimated travel time between two locations from Google Maps
  • Is configured dynamically using Telegram messenger
  • Configurations are persistent across resets (Uses SPIFFS, internal flash memory on the ESP8266 board)
  • Displays to an OLED screen and a RBG LED

The code should run on any ESP8266 board, but the small one with only a couple of GPIO pins will not have enough pins for the display and RGB led.

Step 1: Hardware

First thing we need to do is put together the hardware for the project You'll need the following

You then need to assemble it as shown in the wiring diagram.

Step 2: Google Maps API Key

In order to get the traffic data from the Google Maps API you need use an API key, these are free and very easy to get.

  1. Click on this link
  2. Scroll down to Quick start steps and click the Get a Key option
  3. Type in the name of your project and click the checkbox to agree to the terms and conditions
  4. You will then be given your key that you need to use later in the sketch

I recommend trying out your key using the following URL to make sure it's working correctly (Make sure to change the key at the end!)

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Galway,+Ireland&destinations=Dublin,Ireland&departure_time=now&traffic_model=best_guess&key=PutYourNewlyGeneratedKeyHere

Step 3: Telegram Bot Token and Chat ID

Telegram is an instant messenger very like whatsapp.One advantage it has over whatsapp is it allows you to create bots, which are programs that you can interact with in the chat application. We'll be using the Universal Arduino Telegram Library.

To use the library the first thing you need to get is a Bot Token, to get this inside telegram search for "botfather" and type the "/newbot" command. Then follow the onscreen instructions for naming the bot. Botfather will return you a bot token that you will use with the library.

Bots can only message you if you've first started a conversation with the bot first, so click on the link to your bot in the message that botfather sent and click the "Start" button

So the bot knows where to send the message you are going to need to add your chat id to the sketch, this is your unique identifier on Telegram. The easiest way to get this is search for a bot called "myIdBot", press the start button and send the "/getid" command.

You should now have everything you need to use Telegram to control the Arduino Traffic Display.

Step 4: The Code

You now have everything you need to program you board with the example sketch which you can get off the project's Github page.

  • Open the traffic-notifier sketch in the Arduino IDE.
  • There is a list of libraries up the top of the sketch, they will all need to be installed if you do not have them already (All are available on the Arduino Library Manager)
  • If you scroll down a small bit, you will see a section with the comment Replace the following, update everything in here

You should now be able to program your board with the sketch.

Step 5: Controlling the Device Using Telegram

When the device powers on and connects to the WiFi it will send you a message via Telegram.

You can reply back to the device to send commands to configure it. Here is a list of the commands and what you can do with them

  • /start
    Lists out the available commands to the bot
  • /values
    Lists the currently configured values that the bot has
  • /originlocation
    Sets the place you want your journey to start from. See comment below the list
  • /destinationlocation
    Sets the destination of your journey. See comment below the list
  • /limitvalue
    Sets the limit of when the LED should turn red based on percentage difference. Value is a float (a negative percentage difference would mean the traffic is slower, so this would normally be a minus value (-10.0) or 0.0)
  • /save
    Saves the configured value to a file (this file will be loaded at start up) and also will force a update from Google Maps

Note on locations, if you want more precise locations go to Google Maps and get the travel time between the two areas. You will see the origin and destination in the URL, you can use those values as they appear in the URL.

And that's it! Hopefully you enjoy this project, please let me know if you have questions or feedback.

Thanks alot

Brian

Comments

author
Greg_Griessel (author)2017-07-27

Some questions regarding the Logic and Displays

Display gives % slower .. slower than what ? is the baseline normal traffic conditons ?

i dont understand the /limit command .. seems from the code that if the % slower is less than the limit then it will light the Red Led ?? doesnt make sense ...

author

The maps API returns two travel times, one "duration" and "duration_in_traffic" (or something like this).

Duration seems to be some sort of average travel time (I actually can't find a proper description of what this is), as during low traffic, such as at night, duration_In_Traffic can be lower than the travel time.

I take the duration and traffic away from duration and that is my difference. I use this difference and compare it to the duration to get the percentage difference.

If duration in traffic is bigger than duration, that percentage difference will be negative. So as a bigger difference means a more negatvie (smaller number) i check is it less than the limit.

Maybe this is the wrong way to do it, feel free to change it around to however you like!

author

Thx Brian I think i understand it better now , let me play around with the options a bit

author
TuananhT2 (author)2017-07-09

i want to use thought bluetooth. How?

author
tmercados (author)2017-06-30

How could I put the letter of the traffic time, bigger?

Thanks

author
witnessmenow (author)tmercados2017-06-30

Hey tmercados

The screen library being used is this one,

https://github.com/squix78/esp8266-oled-ssd1306

There are some instructions on how to use it on the Github page

author
DeepaK18 (author)2017-05-15

Hi,

What if I have to calculate intensity of vehicle in just 100 m, how do I do that ?

author
witnessmenow (author)DeepaK182017-05-16

Hi Deepak,

You dont get to see the travel time of individual parts of the route, but what you could do is make a request for that 100m of road as you origin and destination.

Go to google maps and get the request you want to make, and then you can take the origin and destination from the URL

About This Instructable

2,015views

24favorites

License:

Bio: Hey everyone! My name is Brian and thanks for checking my Instructables. I'm a software developer by trade but I've recently gotten into ... More »
More by witnessmenow:Using Witnessmenow's ESP8266/Arduino LibrariesArduino Commute CheckerSocial Stats Counter
Add instructable to: