Live RSS news feeds are displayed on a basic text LCD See http://mbed.org/users/4180_1/notebook/news-lcd-display/

Dependencies:   NetServices TextLCD mbed spxml HTTPClient_ToBeRemoved

Committer:
4180_1
Date:
Tue Apr 24 02:27:43 2012 +0000
Revision:
0:582c6436e759

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:582c6436e759 1 #include "mbed.h"
4180_1 0:582c6436e759 2 #include "EthernetNetIf.h"
4180_1 0:582c6436e759 3 #include "HTTPClient.h"
4180_1 0:582c6436e759 4 #include "TextLCD.h"
4180_1 0:582c6436e759 5 //CNN Tech News RSS Feed - get web page with XML
4180_1 0:582c6436e759 6 // displays titles on LCD from XML "<title>....title text...</title>"
4180_1 0:582c6436e759 7 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d0-d3
4180_1 0:582c6436e759 8 EthernetNetIf eth;
4180_1 0:582c6436e759 9 HTTPClient http;
4180_1 0:582c6436e759 10 HTTPResult result;
4180_1 0:582c6436e759 11 bool completed = false;
4180_1 0:582c6436e759 12 void request_callback(HTTPResult r) {
4180_1 0:582c6436e759 13 result = r;
4180_1 0:582c6436e759 14 completed = true;
4180_1 0:582c6436e759 15 }
4180_1 0:582c6436e759 16
4180_1 0:582c6436e759 17 int main() {
4180_1 0:582c6436e759 18 char *tstartXML = "<title>"; //RSS XML start title
4180_1 0:582c6436e759 19 char *tendXML ="</title>"; //RSS XML end title
4180_1 0:582c6436e759 20 char *tsptr;
4180_1 0:582c6436e759 21 char *teptr;
4180_1 0:582c6436e759 22 int i=0,j=0;
4180_1 0:582c6436e759 23 // the eth and HTTP code has be taken directly from the HTTPStream documentation page
4180_1 0:582c6436e759 24 // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
4180_1 0:582c6436e759 25 lcd.cls();
4180_1 0:582c6436e759 26 lcd.printf("net setup");
4180_1 0:582c6436e759 27 EthernetErr ethErr = eth.setup();
4180_1 0:582c6436e759 28 if (ethErr) {
4180_1 0:582c6436e759 29 lcd.printf("\n\r net error");
4180_1 0:582c6436e759 30 return -1;
4180_1 0:582c6436e759 31 }
4180_1 0:582c6436e759 32 lcd.printf("\n\r net ok");
4180_1 0:582c6436e759 33 HTTPStream stream;
4180_1 0:582c6436e759 34 char BigBuf[2048 + 1] = {0};
4180_1 0:582c6436e759 35 stream.readNext((byte*)BigBuf, 2048); //Point to buffer for the first read
4180_1 0:582c6436e759 36 //CNN Tech News RSS Feed - get web page with XML
4180_1 0:582c6436e759 37 HTTPResult r = http.get("HTTP://rss.cnn.com/rss/cnn_tech.rss", &stream, request_callback);
4180_1 0:582c6436e759 38 while (!completed) {
4180_1 0:582c6436e759 39 Net::poll(); // Polls the Networking stack
4180_1 0:582c6436e759 40 if (stream.readable()) { // check for end of file
4180_1 0:582c6436e759 41 BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
4180_1 0:582c6436e759 42 tsptr = BigBuf;
4180_1 0:582c6436e759 43 // displays titles on LCD from XML "<title>....title text...</title>"
4180_1 0:582c6436e759 44 do {
4180_1 0:582c6436e759 45 tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not
4180_1 0:582c6436e759 46 teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not
4180_1 0:582c6436e759 47 if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<title>"
4180_1 0:582c6436e759 48 if ((tsptr!=NULL)&&(teptr!=NULL)) {
4180_1 0:582c6436e759 49 i=0;
4180_1 0:582c6436e759 50 // loop to scroll characters slowly across LCD
4180_1 0:582c6436e759 51 for (j=0; (j+15)<(strlen(tsptr)-strlen(teptr)); j++) {
4180_1 0:582c6436e759 52 lcd.cls(); // clear screen before writing a new line
4180_1 0:582c6436e759 53 // loop to output a line on the LCD
4180_1 0:582c6436e759 54 for (i=0; ((i<16)&&(tsptr[i+j-1] != '<')); i++) {
4180_1 0:582c6436e759 55 lcd.putc(tsptr[i+j]);
4180_1 0:582c6436e759 56 }
4180_1 0:582c6436e759 57 if (j==0) wait(1.2); //add first line delays for scroll timing
4180_1 0:582c6436e759 58 wait(.2); //delay for charcter scroll timing
4180_1 0:582c6436e759 59 }
4180_1 0:582c6436e759 60 wait(.4);
4180_1 0:582c6436e759 61 lcd.cls(); //clear LCD between news items
4180_1 0:582c6436e759 62 wait(.2);
4180_1 0:582c6436e759 63 }
4180_1 0:582c6436e759 64 } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display
4180_1 0:582c6436e759 65 stream.readNext((byte*)BigBuf, 2048); //Buffer has been read, now we can put more data in it
4180_1 0:582c6436e759 66 }
4180_1 0:582c6436e759 67 }
4180_1 0:582c6436e759 68 lcd.cls();
4180_1 0:582c6436e759 69 if (result == HTTP_OK) {
4180_1 0:582c6436e759 70 lcd.cls();
4180_1 0:582c6436e759 71 lcd.printf(" Read complete\n\r");
4180_1 0:582c6436e759 72 } else {
4180_1 0:582c6436e759 73 lcd. printf(" Error %d\n", result);
4180_1 0:582c6436e759 74 return -1;
4180_1 0:582c6436e759 75 }
4180_1 0:582c6436e759 76 }