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

Dependencies:   NetServices TextLCDfor mbed spxml

Fork of News_LCD_display by jim hamblen

Reads the RSS-Fedd of the ZVV and prints the current state of the public transports on a LCD

Committer:
18padx08
Date:
Sat Feb 22 23:26:48 2014 +0000
Revision:
2:6aaaf927e994
Parent:
1:27033a7fe75c
A news ticker for the ZVV-net in Zuerich

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>"
18padx08 1:27033a7fe75c 7 TextLCD lcd(p26,p25,p24,p23,p22,p20,p19,TextLCD::LCD20x4); // 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;
18padx08 1:27033a7fe75c 12 void loadZVVNews();
18padx08 1:27033a7fe75c 13 void request_callback(HTTPResult r)
18padx08 1:27033a7fe75c 14 {
4180_1 0:582c6436e759 15 result = r;
4180_1 0:582c6436e759 16 completed = true;
4180_1 0:582c6436e759 17 }
4180_1 0:582c6436e759 18
18padx08 1:27033a7fe75c 19 int main()
18padx08 1:27033a7fe75c 20 {
18padx08 1:27033a7fe75c 21
4180_1 0:582c6436e759 22 // the eth and HTTP code has be taken directly from the HTTPStream documentation page
4180_1 0:582c6436e759 23 // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
4180_1 0:582c6436e759 24 lcd.cls();
4180_1 0:582c6436e759 25 lcd.printf("net setup");
4180_1 0:582c6436e759 26 EthernetErr ethErr = eth.setup();
4180_1 0:582c6436e759 27 if (ethErr) {
4180_1 0:582c6436e759 28 lcd.printf("\n\r net error");
4180_1 0:582c6436e759 29 return -1;
4180_1 0:582c6436e759 30 }
4180_1 0:582c6436e759 31 lcd.printf("\n\r net ok");
18padx08 1:27033a7fe75c 32 wait(0.5);
18padx08 1:27033a7fe75c 33 lcd.cls();
18padx08 1:27033a7fe75c 34 /* HTTPStream stream;
18padx08 1:27033a7fe75c 35 char BigBuf[2048 + 1] = {0};
18padx08 1:27033a7fe75c 36 stream.readNext((byte*)BigBuf, 2048); //Point to buffer for the first read
18padx08 1:27033a7fe75c 37 //CNN Tech News RSS Feed - get web page with XML
18padx08 1:27033a7fe75c 38 HTTPResult r = http.get("http://online.fahrplan.zvv.ch/bin/help.exe/dn?tpl=rss_feed_VBZ", &stream, request_callback);
18padx08 1:27033a7fe75c 39 while (!completed) {
18padx08 1:27033a7fe75c 40 Net::poll(); // Polls the Networking stack
18padx08 1:27033a7fe75c 41 if (stream.readable()) { // check for end of file
18padx08 1:27033a7fe75c 42 BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
18padx08 1:27033a7fe75c 43 tsptr = BigBuf;
18padx08 1:27033a7fe75c 44 // displays titles on LCD from XML "<title>....title text...</title>"
18padx08 1:27033a7fe75c 45 do {
18padx08 1:27033a7fe75c 46 tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not
18padx08 1:27033a7fe75c 47 teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not
18padx08 1:27033a7fe75c 48 if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<title>"
18padx08 1:27033a7fe75c 49 if ((tsptr!=NULL)&&(teptr!=NULL)) {
18padx08 1:27033a7fe75c 50 //i=0;
18padx08 1:27033a7fe75c 51 // loop to scroll characters slowly across LCD
18padx08 1:27033a7fe75c 52 for (j=0; j<(strlen(tsptr)-strlen(teptr)); j++) {
18padx08 1:27033a7fe75c 53 //lcd.cls(); // clear screen before writing a new line
18padx08 1:27033a7fe75c 54 // loop to output a line on the LCD
18padx08 1:27033a7fe75c 55 for (i=0; ((i<16)&&(tsptr[i+j-1] != '<')); i++) {
18padx08 1:27033a7fe75c 56 lcd.putc(tsptr[i+j]);
18padx08 1:27033a7fe75c 57 }
18padx08 1:27033a7fe75c 58 lcd.putc(tsptr[j]);
18padx08 1:27033a7fe75c 59 //if (j==0) wait(1.2); //add first line delays for scroll timing
18padx08 1:27033a7fe75c 60 wait(.1); //delay for charcter scroll timing
18padx08 1:27033a7fe75c 61 }
18padx08 1:27033a7fe75c 62 wait(.4);
18padx08 1:27033a7fe75c 63 lcd.cls(); //clear LCD between news items
18padx08 1:27033a7fe75c 64 wait(.2);
18padx08 1:27033a7fe75c 65 }
18padx08 1:27033a7fe75c 66 } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display
18padx08 1:27033a7fe75c 67 stream.readNext((byte*)BigBuf, 2048); //Buffer has been read, now we can put more data in it
18padx08 1:27033a7fe75c 68 }
18padx08 1:27033a7fe75c 69 }*/
18padx08 1:27033a7fe75c 70 loadZVVNews();
18padx08 1:27033a7fe75c 71 while(true) {
18padx08 1:27033a7fe75c 72 completed = false;
18padx08 1:27033a7fe75c 73 if (result == HTTP_OK) {
18padx08 2:6aaaf927e994 74 wait(2);
18padx08 1:27033a7fe75c 75 lcd.cls();
18padx08 1:27033a7fe75c 76 loadZVVNews();
18padx08 1:27033a7fe75c 77
18padx08 1:27033a7fe75c 78 //lcd.cls();
18padx08 1:27033a7fe75c 79 //lcd.printf(" Read complete\n\r");
18padx08 1:27033a7fe75c 80 } else {
18padx08 1:27033a7fe75c 81 lcd. printf(" Error %d\n", result);
18padx08 1:27033a7fe75c 82 wait(1.2);
18padx08 1:27033a7fe75c 83 lcd.cls();
18padx08 1:27033a7fe75c 84 loadZVVNews();
18padx08 1:27033a7fe75c 85 }
18padx08 1:27033a7fe75c 86 }
18padx08 1:27033a7fe75c 87 }
18padx08 1:27033a7fe75c 88
18padx08 1:27033a7fe75c 89 void loadZVVNews()
18padx08 1:27033a7fe75c 90 {
18padx08 1:27033a7fe75c 91 char *tstartXML = "<title>"; //RSS XML start title
18padx08 1:27033a7fe75c 92 char *tendXML ="</title>"; //RSS XML end title
18padx08 1:27033a7fe75c 93 char *tsptr;
18padx08 1:27033a7fe75c 94 char *teptr;
18padx08 1:27033a7fe75c 95 int j=0;
4180_1 0:582c6436e759 96 HTTPStream stream;
18padx08 1:27033a7fe75c 97 char BigBuf[4096 + 1] = {0};
18padx08 1:27033a7fe75c 98 stream.readNext((byte*)BigBuf, 4096); //Point to buffer for the first read
4180_1 0:582c6436e759 99 //CNN Tech News RSS Feed - get web page with XML
18padx08 1:27033a7fe75c 100 HTTPResult r = http.get("http://online.fahrplan.zvv.ch/bin/help.exe/dn?tpl=rss_feed_VBZ", &stream, request_callback);
4180_1 0:582c6436e759 101 while (!completed) {
4180_1 0:582c6436e759 102 Net::poll(); // Polls the Networking stack
4180_1 0:582c6436e759 103 if (stream.readable()) { // check for end of file
4180_1 0:582c6436e759 104 BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
4180_1 0:582c6436e759 105 tsptr = BigBuf;
4180_1 0:582c6436e759 106 // displays titles on LCD from XML "<title>....title text...</title>"
4180_1 0:582c6436e759 107 do {
4180_1 0:582c6436e759 108 tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not
4180_1 0:582c6436e759 109 teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not
4180_1 0:582c6436e759 110 if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<title>"
4180_1 0:582c6436e759 111 if ((tsptr!=NULL)&&(teptr!=NULL)) {
18padx08 1:27033a7fe75c 112 //i=0;
4180_1 0:582c6436e759 113 // loop to scroll characters slowly across LCD
18padx08 1:27033a7fe75c 114 for (j=0; j<(strlen(tsptr)-strlen(teptr)); j++) {
18padx08 1:27033a7fe75c 115 //lcd.cls(); // clear screen before writing a new line
4180_1 0:582c6436e759 116 // loop to output a line on the LCD
18padx08 1:27033a7fe75c 117 /*for (i=0; ((i<16)&&(tsptr[i+j-1] != '<')); i++) {
4180_1 0:582c6436e759 118 lcd.putc(tsptr[i+j]);
18padx08 1:27033a7fe75c 119 }*/
18padx08 1:27033a7fe75c 120 lcd.putc(tsptr[j]);
18padx08 1:27033a7fe75c 121 //if (j==0) wait(1.2); //add first line delays for scroll timing
18padx08 1:27033a7fe75c 122 //delay for charcter scroll timing
4180_1 0:582c6436e759 123 }
18padx08 2:6aaaf927e994 124 if(tsptr!=NULL) wait(2.5);
4180_1 0:582c6436e759 125 lcd.cls(); //clear LCD between news items
18padx08 1:27033a7fe75c 126
4180_1 0:582c6436e759 127 }
4180_1 0:582c6436e759 128 } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display
18padx08 1:27033a7fe75c 129 stream.readNext((byte*)BigBuf, 4096); //Buffer has been read, now we can put more data in it
4180_1 0:582c6436e759 130 }
4180_1 0:582c6436e759 131 }
18padx08 1:27033a7fe75c 132 }