Project tutorial
Display Traffic Status Using Arduino & 1Sheeld

Display Traffic Status Using Arduino & 1Sheeld © GPL3+

Using a transparent IKEA clock and RGB LEDs, I can change the color of the clock according to the traffic status to my way home.

  • 2,990 views
  • 0 comments
  • 13 respects

Components and supplies

Transparent wall clock
Got it from IKEA arround 3$
×1
A000066 iso both
Arduino UNO & Genuino UNO
×1
Screen shot 2019 03 20 at 2 38 27 pm fqndazqcx1
1Sheeld
×1
RGB leds
×14
Tens70
9V battery (generic)
×1

Necessary tools and machines

09507 01
Soldering iron (generic)
Google Matrix API
Double Face glue tape

Apps and online services

About this project

Whenever it’s time to get off the office, I always check the taffic status to choose the best time to go home. But I couldn’t find a way to do that autonomously. So then …. I was up to make a system that shows me the current traffic status with the help of a simple tool that is used probably in all the offices, a wall clock

Using the 1Sheeld and Arduino, I can access the internet and check the traffic time duration (every 5 minutes) to my home and glow the clock green if traffic is clear, or red if is horrible :D, let’s check out the steps to know how to do it yourself!

Code

Traffic_IndicatorC/C++
Make sure to install TImeLib library in your IDE
/*
 
Traffic Indicator

Project that helps you calculate the ETA (estimated time arrival) between to fixed points (origins,destination) given to the Http request 
returing back the time according to the traffic conditions.

Returned ETA is calculated according to the GMT timing so make sure to check your local timing refered by GMT.
*/

#define CUSTOM_SETTINGS
#define INCLUDE_INTERNET_SHIELD
#define INCLUDE_TERMINAL_SHIELD
#define INCLUDE_CLOCK_SHIELD

/* Include 1Sheeld library. */
#include <OneSheeld.h>
/* Include TimeLib library to calculate the epoch (UNIX) time to be parsed by the request. */
#include <TimeLib.h>
/* Create an Http request with discatncematrix api url. */
/* It's important to be created here as a global object. */
HttpRequest request("https://maps.googleapis.com/maps/api/distancematrix/json?origins=30.0678184,31.3313650&destinations=30.0659340,31.4604450&key=YOUR_API_KEY");

/* Set a RGB LED on pin 11,10 and 9. */
int red = 11;
int green = 10;
int blue = 9;
unsigned long epochTime = 0;
char buff [sizeof(unsigned long)*8+1];
bool timeIsSet = false;

void setup() 
{
  /* Start communication. */
  OneSheeld.begin();
  /* Subscribe to success callback for the request. */
  request.setOnSuccess(&onSuccess);
  /* Subscribe to json value replies. */
  request.getResponse().setOnJsonResponse(&onJsonReply);
  /* Subscribe to response errors. */
  request.getResponse().setOnError(&onResponseError);
  Clock.setOnSelected(&getTime);
  /* Subscribe to Internet errors. */
  Internet.setOnError(&onInternetError);  
  /* LED pin modes OUTPUT.*/
  pinMode(red, OUTPUT); 
  pinMode(green, OUTPUT); 
  pinMode(blue, OUTPUT); 
}

void loop()
{
  /* Block the code until selecting Clock shield. */
  if(timeIsSet)
  {
     /* Grab the UNIX "epoch" time. */
    epochTime = now();
    /* Terminal used for debugging. */
    Terminal.println(epochTime);
    /* Convert epoch variable to char string. */
    ultoa(epochTime,buff,10);
    /* Add the UNIX time parameter. */
    request.addParameter("departure_time",buff);
    /* Perform the request get action. */
    Internet.performGet(request);
    /* Wait for 5 minutes. */
    OneSheeld.delay(60000*5); 
  }
}

void getTime()
{
  /* Function to set the current time. */
  /* My local time is post the GMT by 2 hours so subtracted them. */
  setTime((Clock.getHours()-2),Clock.getMinutes(),Clock.getSeconds(),Clock.getDay(),Clock.getMonth(),(int)Clock.getYear());
  timeIsSet = true;
}

void onSuccess(HttpResponse & response)
{
  /* Using the response to query the Json chain till the required value. */
  response["rows"][0]["elements"][0]["duration_in_traffic"]["value"].query();
}

void onJsonReply(JsonKeyChain & hell,char * output)
{
  /* Getting the value and transform it to integer to deal with. */
  int eta = atoi(output);
  int mins = eta/60;

  /* Checking the ETA "estimated time of arrival" to the destination. */
  if(mins>=30) 
  {
    redLightsOn();
  }
  else if(mins<30)
  {
    greenLightsOn();delay(1000);lightsOff();delay(1000);
    greenLightsOn();delay(1000);lightsOff();delay(1000);
    greenLightsOn();delay(1000);lightsOff();delay(1000);
    greenLightsOn();
  }
  /* Terminal line for debugging. */
  Terminal.println(eta/60);
}

void greenLightsOn()
{
  digitalWrite(red,LOW);digitalWrite(blue,LOW);digitalWrite(green,HIGH);
}

void redLightsOn()
{
  digitalWrite(red,HIGH);digitalWrite(blue,LOW);digitalWrite(green,LOW);
}

void yellowLightsOn()
{
  analogWrite(red,200);digitalWrite(blue,LOW);analogWrite(green,40);
}

void lightsOff()
{
  digitalWrite(red,LOW);digitalWrite(blue,LOW);digitalWrite(green,LOW);
}

/* Error handling functions. */
void onResponseError(int errorNumber)
{
  /* Print out error Number.*/
  Terminal.print("Response error:");
  switch(errorNumber)
  {
    case INDEX_OUT_OF_BOUNDS: Terminal.println("INDEX_OUT_OF_BOUNDS");break;
    case RESPONSE_CAN_NOT_BE_FOUND: Terminal.println("RESPONSE_CAN_NOT_BE_FOUND");break;
    case HEADER_CAN_NOT_BE_FOUND: Terminal.println("HEADER_CAN_NOT_BE_FOUND");break;
    case NO_ENOUGH_BYTES: Terminal.println("NO_ENOUGH_BYTES");break;
    case REQUEST_HAS_NO_RESPONSE: Terminal.println("REQUEST_HAS_NO_RESPONSE");break;
    case SIZE_OF_REQUEST_CAN_NOT_BE_ZERO: Terminal.println("SIZE_OF_REQUEST_CAN_NOT_BE_ZERO");break;
    case UNSUPPORTED_HTTP_ENTITY: Terminal.println("UNSUPPORTED_HTTP_ENTITY");break;
    case JSON_KEYCHAIN_IS_WRONG: Terminal.println("JSON_KEYCHAIN_IS_WRONG");break;
  }
}

void onInternetError(int requestId, int errorNumber)
{
  /* Print out error Number.*/
  Terminal.print("Request id:");
  Terminal.println(requestId);
  Terminal.print("Internet error:");
  switch(errorNumber)
  {
    case REQUEST_CAN_NOT_BE_FOUND: Terminal.println("REQUEST_CAN_NOT_BE_FOUND");break;
    case NOT_CONNECTED_TO_NETWORK: Terminal.println("NOT_CONNECTED_TO_NETWORK");break;
    case URL_IS_NOT_FOUND: Terminal.println("URL_IS_NOT_FOUND");break;
    case ALREADY_EXECUTING_REQUEST: Terminal.println("ALREADY_EXECUTING_REQUEST");break;
    case URL_IS_WRONG: Terminal.println("URL_IS_WRONG");break;
  }
}

Schematics

LED Connection
Connection

Comments

Similar projects you might like

Arduino Digital Clock Using 1Sheeld

Project tutorial by amrmostaafaa

  • 2,435 views
  • 0 comments
  • 5 respects

Security System With 1Sheeld

Project showcase by nada abdelnaby mady

  • 2,212 views
  • 0 comments
  • 2 respects

Arduino can hear and understand your voice with 1Sheeld !

Project tutorial by Alessandro Didonna

  • 4,316 views
  • 1 comment
  • 11 respects

Analog Clock using 1Sheeld Graphical LCD

Project tutorial by Ahmed El-Hinidy

  • 2,825 views
  • 0 comments
  • 7 respects

Universal Remote Control using Arduino, 1Sheeld and Android

Project tutorial by Ashraf Nabil

  • 9,525 views
  • 5 comments
  • 15 respects

Traffic Monitor- Monitors traffic on the Go

Project tutorial by Patel Darshil

  • 6,156 views
  • 0 comments
  • 12 respects
Add projectSign up / Login