Eslam Ali
Published © GPL3+

Display Traffic Status Using Arduino & 1Sheeld

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.

IntermediateFull instructions provided4,261
Display Traffic Status Using Arduino & 1Sheeld

Things used in this project

Hardware components

Transparent wall clock
Got it from IKEA arround 3$
×1
Arduino UNO
Arduino UNO
×1
1Sheeld
1Sheeld
×1
RGB leds
×14
9V battery (generic)
9V battery (generic)
×1

Software apps and online services

Google Maps
Google Maps

Hand tools and fabrication machines

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

Story

Read more

Schematics

LED Connection

Code

Traffic_Indicator

C/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;
  }
}

Credits

Eslam Ali

Eslam Ali

2 projects • 17 followers

Comments