Live Traffic Update Nokia LCD Display

This project utilizes mbed's networking features to display live traffic updates on the Nokia LCD using the MapQuest API's Traffic Web Service.

Parts

Wiring

Ethernet Magjack Connector

/media/uploads/rrajan8/_scaled_ethernet.jpg

MbedSparkfun Ethernet Breakout
TD+P1
TD-P2
RD+P7
RD-P8

Sparkfun Nokia LCD Breakout

/media/uploads/rrajan8/_scaled_20130306_132415.jpg

MbedSparkfun Nokia LCD pins
GndGnd
p5(mosi)DIO
p7 (sclk)SCK
p8CS
p9Reset
Vout (3.3V)3.3V
Vout (3.3V)Vbat

/media/uploads/rrajan8/20130306_132332.jpg Wiring Arrangement.

Project Code

Import programLiveTrafficDisplayFinal

This project utilizes mbed's networking features to display live traffic updates on the Nokia LCD using the MapQuest API's Traffic Web Service.

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "NokiaLCD.h"
//Mapquest traffic RSS Feed - get web page with XML
// displays Road diversions/closures on LCD  from XML "<fullDesc>.... text...</fullDesc>"
NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
EthernetNetIf eth;
HTTPClient http;
HTTPResult result;
bool completed = false;

void request_callback(HTTPResult r) {
    result = r;
    completed = true;
}

int main() {
    char *tstartXML = "<fullDesc>"; //RSS XML start title
    char *tendXML = "</fullDesc>";  //RSS XML end title
    char *tsptr;                    //Start Pointer
    char *teptr;                    //End Pointer
    int i=0,j=0;
    lcd.cls();                      //Clear LCD Screen
    lcd.locate(0,1);
    lcd.printf("net setup");
    EthernetErr ethErr = eth.setup();
    if (ethErr) {
        lcd.printf("net error");
        return -1;
    }
    lcd.locate(0,2);
    lcd.printf("net ok");
    wait(1);
    lcd.cls();                      //Clear LCD Screen
    HTTPStream stream;
    char BigBuf[2048 + 1] = {0};
    stream.readNext((byte*)BigBuf, 2048); //Point to buffer for the first read
    //Get web page with XML
    HTTPResult r = http.get("http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd%7Cluub2l6z2u%2C75%3Do5-96twuy&callback=handleIncidentsResponse&boundingBox=39.950960,-105.259451,39.528562,-104.710135&filters=construction,incidents&inFormat=kvp&outFormat=xml", &stream, request_callback);
    while (!completed) {
        Net::poll();                // Polls the Networking stack
        if (stream.readable()) {    // check for end of file
            BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
            tsptr = BigBuf;
            // displays titles on LCD  from XML "<title>....title text...</title>"
            do {
                tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not
                teptr = strstr(tsptr,tendXML);   // find <\title> in string - NULL if not
                if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<title>"
                if ((tsptr!=NULL)&&(teptr!=NULL)) {
                    i=0;
                    // loop to display lines on LCD
                    for (j=0; (j)<(strlen(tsptr)-strlen(teptr)); j=j+16) {
  //                      lcd.cls(); // clear screen before writing a new line
                        lcd.locate(0,(2+(j/16)));
                        // loop to output a line on the LCD
                        for (i=0; ((i<16)&&(tsptr[i+j] != '<')); i++) {
                            lcd.putc(tsptr[i+j]);
                        }
                    }
                    wait(5);
                    lcd.cls(); //clear LCD between traffic updates
                    lcd.locate(0,2);
                }
            } while (tsptr!=NULL); // No more "<fullDesc>"s in BigBuf to display
            stream.readNext((byte*)BigBuf, 2048); //Buffer read, now we can put more data in it
        }
    }
    lcd.cls();
    if (result == HTTP_OK) {
        lcd.cls();
        lcd.locate(0,1);
        lcd.printf(" Read complete");
    } else {
        lcd.printf(" Error %d\", result");
        return -1;
    }
}

Description

The data displayed on the LCD is the Full description of the incident that has closed or diverted a road. The format is "<fullDesc>....text.....</fullDesc>". The code uses parsing with strstr (i.e., this searches a larger string for a substring) to find the "<fullDesc>" and "</fullDesc>" strings in the read buffer and prints on the Nokia LCD all information between the two strings. The data on the web page is in most cases too large to read into a memory buffer of the given mbed's RAM size, so it is split into several smaller buffers for each read and processed as the data streams in.

A part of the XML code that is being parsed is shown below.

/media/uploads/rrajan8/xmlfile.jpg

The current code uses the Latitude and Longitude of Atlanta, Georgia to get Traffic Feeds. In order to view traffic feeds for a different location enter the required location into the <quote>Location</quote> of the mapquest website and change the code to match the URL for the traffic incidents of the new location and the individual key provided when registering for the API.

An example is shown below where the coordinates of Denver are highlighted.

/media/uploads/rrajan8/denver.jpg

Video Demo

Future Work

  • Building on this project, it is possible to use a more accurate and faster updating API like those provided by Google or Yahoo.
  • Another improvement of this project would be to display a traffic overlay of the region on a map on the LCD.
  • It is also possible to connect a GPS device to the mbed and use it to retrieve the coordinates of the mbed device and use these coordinates to get detailed traffic updates and overlays using a better API.


Please log in to post comments.