Project tutorial
Traffic Monitor- Monitors traffic on the Go

Traffic Monitor- Monitors traffic on the Go © GPL3+

The device uses color indication to display traffic on the road you are driving or you have selected.

  • 4,723 views
  • 0 comments
  • 10 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

In India, traffic is getting a higher and higher terribly. At peak hours you can't even think for a ride. The Commute Indicator presented here will monitor traffic and time distance between the destinations using Google API Integration. The Indication will be done on Sparkfun Neopixel LEDs. If LED is green, it means your are ready to go and the traffic will be displayed as Green>Yellow>Red. You can keep this device in your car and monitor traffic on the go as it has inbuilt wifi and it is also easy to power. You can also hang it on Home Wall to see traffic on your road. It can also keep you tracked on the go. It will be updated in certain time limit such as 5 minutes.

ESP8266 has inbuilt wifi so we can turn on router at home and get updates and in car we have to turn on our mobile hotspots.

To build a commute indicator we will run python program to integrate with the google maps matrix API and create an indication with the help of Sparkfun Niopixel Strip of LEDs.

If LEDs Go Green it means you are ready for smooth Ride. If it goes yellow, it means moderate traffic. If turns red it will be bad traffic outside. It will be updated in certain time limit.

ESP8266 will need Sparkfun Logic Level Converter. I will use Arduino Uno to talk with 3.3V Sparkfun ESP8266.

It will be enclosed in nice box and then you can hang it on wall of home to monitor your road traffic or hang it in car.

Step 1: Power Supply

Here we will use 9v Battery for power supply. We will need 7805 Voltage Regulator IC to get 9V. Then from 7805 IC, feed it to arduino.

Step 2: Connecting ESP266 and powering it

Connect the Arduino’s 3v3 (3.3V) output to ESP8266. The ESP8266 works with 3.3V and not 5V, so this is necessary.

Step 3: Connect RX and TX

Connect the RXD pin of the Arduino to the RX pin of the ESP8266.Connect the TXD pin of the Arduino to the TX pin of the ESP. When we want two things to talk to each other over serial, we connect the TX pin of one to the RX of the other (send goes to receive and the opposite). Here we do not have the Arduino talk to the ESP8266 though, our computer is talking to it via the Arduino.

Step 4: GND and RST

Connect the RES or RESET pin, When you ground the reset pin, the Arduino works as a dumb USB to serial connector, which is what we want to talk to the ESP8266.

Step 5: Setting Board

  • Connect to that network.
  • This should pull up the uno's setup page, the default password is "arduino"
  • Click configure, set a new password.
  • Under the wireless parameters find your home network and enter in the network details. Finally click "Configure and Restart"
  • At this point if all goes well the Arduino will automatically switch to station mode and connect to your network. To make sure it is connected wait a couple minutes then go back to your browser and enter http://arduino.localand it should bring up the same login screen. If it doesn't then it most likely didn't connect correctly. If this happens press and hold the wifi rst button on the uno esp for over 5 sec and it will kick back into AP mode.
  • Now you are connected to the internet and ready to start coding!

Google Maps Integration

First you will need to get a key from Google in order to access their google maps API. In order to do this you will need to create a project, enable the API, and get a key. It is pretty straight forward once you are there and click around a bit:https://developers.google.com/maps/documentation/d...

We will specifically be using the Distance Matrix API to determine the current travel times. We are specifically going to be looking to capture the duration_in_traffic value. If you are unfamiliar with assembling the URL for the request I highly suggest downloading a software called Postmanhttps://www.getpostman.com/ with will allow you to copy over one the examples from this page: https://developers.google.com/maps/documentation/d... run it with your key, and change parameters until you get the correct response. This software also gives you a chance to quickly see what google returns.

Here is an example of the url I am using for my request (add your key to this):

<p><a href="https://maps.googleapis.com/m" rel="nofollow">https://maps.googleapis.com/m</a>aps/api/distancematrix/json?units=imperial&origins=10+1st+St+Austin+Texas+7874&destinations=20+University+Pkwy+Round+Rock+Texas+78681&traffic_model=best_guess&key="ADD YOUR KEY HERE, DELETE THE QUOTES"&departure_time=now</p>

So now we need to put this into a python function that we will run on the Linux portion of the UNO. This part is actually very simple because this type of API call requires no authorization.

import urllib2<br>import json import sys response = urllib2.urlopen('ADD THE URL FROM ABOVE HERE WITH YOUR PARAMETERS AND KEY') data = json.load(response) ttime = data['rows'][0]['elements'][0]['duration_in_traffic']['value'] print (ttime) sys.exit()

Now you need to get this python file uploaded into the UNO. The best way to do this on windows is using WinSCP. https://winscp.net/eng/download.php Once you have WinSCP installed, connect to the Yun with SCP protocol, host-name: arduino.local, and port 22. You should be prompted to log in using root and the password you setup prior.

Once logged in navigate to /mnt/sda1/arduino/www/python and then upload your python file to this folder. This assumes that you have an SD card mounted. If not, then you can put the file in a different folder (ex. root) and change the code in the next step to reflect that different path.

Code

Untitled fileC/C++
#include <Bridge.h>
#include <Process.h>
#include <Adafruit_NeoPixel.h>
#define PIN            7   //neopixel gpio pin
#define NUMPIXELS      24  // number of pixels in ring
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

float functionoutput;  //output from the checktraffic() function
int delayval;   //delay between assigning value to next LED
int inputrng[2]; //used to establish traffic value ranges
int r;
int g;
int b;
long previousrun = 0;  //intiate previousrun
const long timebetweenruns = 300000;  //300000 = 5 min
unsigned long currentrun; //set variable to time store

void setup() {
  // Initialize Bridge
  delay(60000); // delay to give the linux processor time to boot
  pixels.begin();
  Bridge.begin();
  // Initialize Serial
  SerialUSB.begin(9600);
  currentrun = millis();
  loading_colors();  // fun loading light sequence
  traffic_check();  // run the traffic check at start up, if no lights come on then most likely the delay above needs to be increased
  clearcolors();  // clear colors before setting new values
  delay(1000);
  fullcolors_dial();   // sets the neopixels according to the value returned from the trafficcheck function. Sets it like a dial counting from best to worst commute time
}

void loop() {
    currentrun = millis(); //sets the counter
    // re-runs program according to the value set for timebetweenruns
    if (currentrun - previousrun >= timebetweenruns){
      previousrun = currentrun; 
      SerialUSB.print("timing trigger"); //for reference
      loading_colors();
      traffic_check();
      clearcolors();
      delay(1000);
      fullcolors_dial();
      }
}
  
///////////////////// sets neopixel colors full with dial //////////////////////////////////

void fullcolors_dial(){
  delayval = 150;  // how quick the neopixel fills LEDs
  SerialUSB.print(functionoutput);
  int startpixel = 0;    //input for each instance
  int endpixel = 24;       //input for each instance
  float totalpixelcount = abs(endpixel - startpixel); //established total possibly pixel count for this instance 
  float currentpixel = ((functionoutput / ((float)inputrng[1])) * totalpixelcount) + startpixel;
  for(float i = startpixel; i < currentpixel; i++) 
  {   
      pixels.setPixelColor(i, pixels.Color(r,g,b)); 
      pixels.show();
      delay(delayval);
      }
}

///////////////////// clear neopixel //////////////////////////////////

void clearcolors(){
  for(int i=0;i<NUMPIXELS;i++){  
  // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
  pixels.setPixelColor(i, pixels.Color(0,0,0)); // set rgb values
  
  pixels.show(); // This sends the updated pixel color to the hardware.
  }
}

///////////////////// loading neopixel //////////////////////////////////

uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
 return (pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0) * 3);
} else if(WheelPos < 170) {
 WheelPos -= 85;
 return (pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3) * .3);
} else {
 WheelPos -= 170;
 return (pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3) * .3);
  }
}

void loading_colors(){
  uint16_t i, j;
  for(j=0; j<256; j++) {
    for(i=0; i<NUMPIXELS; i++) {
      pixels.setPixelColor(i, Wheel((i+j) & 200));
    }
    pixels.show();
    delay(10);
  }
}

///////////////////// Traffic Mode //////////////////////////////////

void traffic_check() {
  SerialUSB.print("beginning traffic check");
  delay(1000);
  char inData[20]; // Allocate some space for the string
  char inChar=-1; // Where to store the character read
  byte index = 0; // Index into array; where to store the character
  Process p;		// Create a process and call it "p"
  p.begin("python");	// Process that launch the python code
  p.addParameter("/mnt/sda1/arduino/www/python/transittime_check.py"); // initiated python code make sure to update this file path if you change the name or directory
  p.run();		// Run the process and wait for its termination
  while (p.available() > 0) {
     inChar = p.read(); // Read results being transmitted onto the serial communication between processors
     inData[index] = inChar; // Store it
     index++; // Increment where to write next
     inData[index] = '\0'; // Null terminate the string
  }
  // Ensure the last bit of data is sent.
  SerialUSB.flush();
  int traveltime = atoi(inData);
  functionoutput = traveltime; //main purpose is used when adding additional modes
  SerialUSB.print(traveltime);  //for reference

////Change these values///

  inputrng[0] = 1080;   // the quickest time for your commute in sec
  inputrng[1] = 3000;  // the longest time for your commute in sec
  int dif = (inputrng[1] - inputrng[0])/3;  //sets ranges
  int rng1 = inputrng[0] + dif;  
  int rng2 = rng1 + dif;

  // set RGB colors for the quickest commute and before
  if (traveltime < inputrng[0]){   
    r = 0;
    g = 120;
    b = 0;
  } // set RGB colors for the quickest - 1/3 range
   else if (inputrng[0] < traveltime && traveltime < rng1 ){
    r = 50;
    g = 120;
    b = 0;
  } // set RGB colors for the 1/3 - 2/3 range
   else if (rng1 < traveltime && traveltime < rng2 ){
    r = 100;
    g = 80;
    b = 0;
  } // set RGB colors for the 2/3 - longest range
  else if (rng2 < traveltime && traveltime < inputrng[1]) {
    r = 100;
    g = 25;
    b = 0;
  } // set RGB colors for the longest commute and beyond
  else {
    r = 120;
    g = 0;
    b = 0;
  }

 for ( uint8_t i = 0; i < 20; i++ )  //clear inData
 inData[i] = 0;
}

Schematics

capture2_QFp0g2oqtp.JPG
Capture2 qfp0g2oqtp

Comments

Similar projects you might like

Safe City: A Device to Keep You Safe

Project tutorial by Patel Darshil

  • 7,865 views
  • 0 comments
  • 24 respects

Easy IOT Water Leakage Indicator

Project tutorial by Patel Darshil

  • 9,186 views
  • 2 comments
  • 13 respects

Safe Home Smart Home: Safety Device For Every Family Member

Project tutorial by Patel Darshil

  • 4,488 views
  • 0 comments
  • 11 respects

IoT arduino ESP Garage Door opener (UD)

Project in progress by David Smerkous

  • 10,855 views
  • 6 comments
  • 16 respects

Hot Air Balloon or Weather Satellite Navigation System

Project tutorial by Patel Darshil

  • 2,106 views
  • 0 comments
  • 15 respects
Add projectSign up / Login