This project utilizes mbed's networking features to display live traffic updates on the Nokia LCD using the MapQuest API's Traffic Web Service.
Dependencies: NetServices-Traffic mbed spxml
main.cpp@0:88e082c58797, 2013-03-06 (annotated)
- Committer:
- rrajan8
- Date:
- Wed Mar 06 19:15:22 2013 +0000
- Revision:
- 0:88e082c58797
This project utilizes mbed's networking features to display live traffic updates on the Nokia LCD using the MapQuest API's Traffic Web Service.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rrajan8 | 0:88e082c58797 | 1 | #include "mbed.h" |
rrajan8 | 0:88e082c58797 | 2 | #include "EthernetNetIf.h" |
rrajan8 | 0:88e082c58797 | 3 | #include "HTTPClient.h" |
rrajan8 | 0:88e082c58797 | 4 | #include "NokiaLCD.h" |
rrajan8 | 0:88e082c58797 | 5 | //Mapquest traffic RSS Feed - get web page with XML |
rrajan8 | 0:88e082c58797 | 6 | // displays Road diversions/closures on LCD from XML "<fullDesc>.... text...</fullDesc>" |
rrajan8 | 0:88e082c58797 | 7 | NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type |
rrajan8 | 0:88e082c58797 | 8 | EthernetNetIf eth; |
rrajan8 | 0:88e082c58797 | 9 | HTTPClient http; |
rrajan8 | 0:88e082c58797 | 10 | HTTPResult result; |
rrajan8 | 0:88e082c58797 | 11 | bool completed = false; |
rrajan8 | 0:88e082c58797 | 12 | |
rrajan8 | 0:88e082c58797 | 13 | void request_callback(HTTPResult r) { |
rrajan8 | 0:88e082c58797 | 14 | result = r; |
rrajan8 | 0:88e082c58797 | 15 | completed = true; |
rrajan8 | 0:88e082c58797 | 16 | } |
rrajan8 | 0:88e082c58797 | 17 | |
rrajan8 | 0:88e082c58797 | 18 | int main() { |
rrajan8 | 0:88e082c58797 | 19 | char *tstartXML = "<fullDesc>"; //RSS XML start title |
rrajan8 | 0:88e082c58797 | 20 | char *tendXML = "</fullDesc>"; //RSS XML end title |
rrajan8 | 0:88e082c58797 | 21 | char *tsptr; //Start Pointer |
rrajan8 | 0:88e082c58797 | 22 | char *teptr; //End Pointer |
rrajan8 | 0:88e082c58797 | 23 | int i=0,j=0; |
rrajan8 | 0:88e082c58797 | 24 | lcd.cls(); //Clear LCD Screen |
rrajan8 | 0:88e082c58797 | 25 | lcd.locate(0,1); |
rrajan8 | 0:88e082c58797 | 26 | lcd.printf("net setup"); |
rrajan8 | 0:88e082c58797 | 27 | EthernetErr ethErr = eth.setup(); |
rrajan8 | 0:88e082c58797 | 28 | if (ethErr) { |
rrajan8 | 0:88e082c58797 | 29 | lcd.printf("net error"); |
rrajan8 | 0:88e082c58797 | 30 | return -1; |
rrajan8 | 0:88e082c58797 | 31 | } |
rrajan8 | 0:88e082c58797 | 32 | lcd.locate(0,2); |
rrajan8 | 0:88e082c58797 | 33 | lcd.printf("net ok"); |
rrajan8 | 0:88e082c58797 | 34 | wait(1); |
rrajan8 | 0:88e082c58797 | 35 | lcd.cls(); //Clear LCD Screen |
rrajan8 | 0:88e082c58797 | 36 | HTTPStream stream; |
rrajan8 | 0:88e082c58797 | 37 | char BigBuf[2048 + 1] = {0}; |
rrajan8 | 0:88e082c58797 | 38 | stream.readNext((byte*)BigBuf, 2048); //Point to buffer for the first read |
rrajan8 | 0:88e082c58797 | 39 | //Get web page with XML |
rrajan8 | 0:88e082c58797 | 40 | HTTPResult r = http.get("http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd%7Cluub2l6z2u%2C75%3Do5-96twuy&callback=handleIncidentsResponse&boundingBox=33.977698,-84.662833,33.520957,-84.113516&filters=construction,incidents&inFormat=kvp&outFormat=xml", &stream, request_callback); |
rrajan8 | 0:88e082c58797 | 41 | while (!completed) { |
rrajan8 | 0:88e082c58797 | 42 | Net::poll(); // Polls the Networking stack |
rrajan8 | 0:88e082c58797 | 43 | if (stream.readable()) { // check for end of file |
rrajan8 | 0:88e082c58797 | 44 | BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string |
rrajan8 | 0:88e082c58797 | 45 | tsptr = BigBuf; |
rrajan8 | 0:88e082c58797 | 46 | // displays titles on LCD from XML "<title>....title text...</title>" |
rrajan8 | 0:88e082c58797 | 47 | do { |
rrajan8 | 0:88e082c58797 | 48 | tsptr = strstr(tsptr,tstartXML); // find <fullDesc> in string - NULL if not |
rrajan8 | 0:88e082c58797 | 49 | teptr = strstr(tsptr,tendXML); // find <\fullDesc> in string - NULL if not |
rrajan8 | 0:88e082c58797 | 50 | if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<fullDesc>" |
rrajan8 | 0:88e082c58797 | 51 | if ((tsptr!=NULL)&&(teptr!=NULL)) { |
rrajan8 | 0:88e082c58797 | 52 | i=0; |
rrajan8 | 0:88e082c58797 | 53 | // loop to display lines on LCD |
rrajan8 | 0:88e082c58797 | 54 | for (j=0; (j)<(strlen(tsptr)-strlen(teptr)); j=j+16) { |
rrajan8 | 0:88e082c58797 | 55 | // lcd.cls(); // clear screen before writing a new line |
rrajan8 | 0:88e082c58797 | 56 | lcd.locate(0,(2+(j/16))); |
rrajan8 | 0:88e082c58797 | 57 | // loop to output a line on the LCD |
rrajan8 | 0:88e082c58797 | 58 | for (i=0; ((i<16)&&(tsptr[i+j] != '<')); i++) { |
rrajan8 | 0:88e082c58797 | 59 | lcd.putc(tsptr[i+j]); |
rrajan8 | 0:88e082c58797 | 60 | } |
rrajan8 | 0:88e082c58797 | 61 | } |
rrajan8 | 0:88e082c58797 | 62 | wait(5); |
rrajan8 | 0:88e082c58797 | 63 | lcd.cls(); //clear LCD between traffic updates |
rrajan8 | 0:88e082c58797 | 64 | lcd.locate(0,2); |
rrajan8 | 0:88e082c58797 | 65 | } |
rrajan8 | 0:88e082c58797 | 66 | } while (tsptr!=NULL); // No more "<fullDesc>"s in BigBuf to display |
rrajan8 | 0:88e082c58797 | 67 | stream.readNext((byte*)BigBuf, 2048); //Buffer read, now we can put more data in it |
rrajan8 | 0:88e082c58797 | 68 | } |
rrajan8 | 0:88e082c58797 | 69 | } |
rrajan8 | 0:88e082c58797 | 70 | lcd.cls(); |
rrajan8 | 0:88e082c58797 | 71 | if (result == HTTP_OK) { |
rrajan8 | 0:88e082c58797 | 72 | lcd.cls(); |
rrajan8 | 0:88e082c58797 | 73 | lcd.locate(0,1); |
rrajan8 | 0:88e082c58797 | 74 | lcd.printf(" Read complete"); |
rrajan8 | 0:88e082c58797 | 75 | } else { |
rrajan8 | 0:88e082c58797 | 76 | lcd.printf(" Error %d\", result"); |
rrajan8 | 0:88e082c58797 | 77 | return -1; |
rrajan8 | 0:88e082c58797 | 78 | } |
rrajan8 | 0:88e082c58797 | 79 | } |