Severe weather alert using NOAA RSS feed

Dependencies:   NetServices TextLCD mbed spxml

Fork of News_LCD_display by jim hamblen

Committer:
ipapp3
Date:
Wed Mar 06 01:16:25 2013 +0000
Revision:
1:182ff6aef029
Parent:
0:582c6436e759
final

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;
ipapp3 1:182ff6aef029 11 PwmOut speaker(p21);
4180_1 0:582c6436e759 12 bool completed = false;
ipapp3 1:182ff6aef029 13 void request_callback(HTTPResult r)
ipapp3 1:182ff6aef029 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
ipapp3 1:182ff6aef029 19 int main()
ipapp3 1:182ff6aef029 20 {
ipapp3 1:182ff6aef029 21 //while(1) {
4180_1 0:582c6436e759 22 char *tstartXML = "<title>"; //RSS XML start title
4180_1 0:582c6436e759 23 char *tendXML ="</title>"; //RSS XML end title
4180_1 0:582c6436e759 24 char *tsptr;
4180_1 0:582c6436e759 25 char *teptr;
ipapp3 1:182ff6aef029 26 int i=0,j=0,t=0,c=0,d=0;
4180_1 0:582c6436e759 27 lcd.cls();
4180_1 0:582c6436e759 28 lcd.printf("net setup");
4180_1 0:582c6436e759 29 EthernetErr ethErr = eth.setup();
4180_1 0:582c6436e759 30 if (ethErr) {
4180_1 0:582c6436e759 31 lcd.printf("\n\r net error");
4180_1 0:582c6436e759 32 return -1;
4180_1 0:582c6436e759 33 }
4180_1 0:582c6436e759 34 lcd.printf("\n\r net ok");
4180_1 0:582c6436e759 35 HTTPStream stream;
4180_1 0:582c6436e759 36 char BigBuf[2048 + 1] = {0};
4180_1 0:582c6436e759 37 stream.readNext((byte*)BigBuf, 2048); //Point to buffer for the first read
4180_1 0:582c6436e759 38 //CNN Tech News RSS Feed - get web page with XML
ipapp3 1:182ff6aef029 39 HTTPResult r = http.get("http://alerts.weather.gov/cap/wwaatmget.php?x=GAC121&y=0", &stream, request_callback);
4180_1 0:582c6436e759 40 while (!completed) {
ipapp3 1:182ff6aef029 41 c=0;
4180_1 0:582c6436e759 42 Net::poll(); // Polls the Networking stack
4180_1 0:582c6436e759 43 if (stream.readable()) { // check for end of file
4180_1 0:582c6436e759 44 BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
4180_1 0:582c6436e759 45 tsptr = BigBuf;
4180_1 0:582c6436e759 46 // displays titles on LCD from XML "<title>....title text...</title>"
4180_1 0:582c6436e759 47 do {
4180_1 0:582c6436e759 48 tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not
4180_1 0:582c6436e759 49 teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not
4180_1 0:582c6436e759 50 if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<title>"
4180_1 0:582c6436e759 51 if ((tsptr!=NULL)&&(teptr!=NULL)) {
4180_1 0:582c6436e759 52 i=0;
4180_1 0:582c6436e759 53 // loop to scroll characters slowly across LCD
4180_1 0:582c6436e759 54 for (j=0; (j+15)<(strlen(tsptr)-strlen(teptr)); j++) {
4180_1 0:582c6436e759 55 lcd.cls(); // clear screen before writing a new line
4180_1 0:582c6436e759 56 // loop to output a line on the LCD
4180_1 0:582c6436e759 57 for (i=0; ((i<16)&&(tsptr[i+j-1] != '<')); i++) {
ipapp3 1:182ff6aef029 58 c++;
ipapp3 1:182ff6aef029 59 }
ipapp3 1:182ff6aef029 60 i=0;
ipapp3 1:182ff6aef029 61 if (t>0 && c> 51 && d==0 && c!=108) {
ipapp3 1:182ff6aef029 62 speaker.period(1.0/500.0); // 500hz period
ipapp3 1:182ff6aef029 63 speaker =0.5; //50% duty cycle - max volume
ipapp3 1:182ff6aef029 64 wait(2.5);
ipapp3 1:182ff6aef029 65 speaker=0.0; // turn off audio
ipapp3 1:182ff6aef029 66 wait(1);
ipapp3 1:182ff6aef029 67 speaker =0.5; //50% duty cycle - max volume
ipapp3 1:182ff6aef029 68 wait(2.5);
ipapp3 1:182ff6aef029 69 speaker=0.0; // turn off audio
ipapp3 1:182ff6aef029 70 wait(1);
ipapp3 1:182ff6aef029 71 i=0;
ipapp3 1:182ff6aef029 72 d=1;
ipapp3 1:182ff6aef029 73 }
ipapp3 1:182ff6aef029 74 for (i=0; ((i<16)&&(tsptr[i+j-1] != '<')); i++) {
4180_1 0:582c6436e759 75 lcd.putc(tsptr[i+j]);
4180_1 0:582c6436e759 76 }
4180_1 0:582c6436e759 77 if (j==0) wait(1.2); //add first line delays for scroll timing
ipapp3 1:182ff6aef029 78 wait(.25); //delay for charcter scroll timing
4180_1 0:582c6436e759 79 }
4180_1 0:582c6436e759 80 wait(.4);
4180_1 0:582c6436e759 81 lcd.cls(); //clear LCD between news items
4180_1 0:582c6436e759 82 wait(.2);
ipapp3 1:182ff6aef029 83
4180_1 0:582c6436e759 84 }
ipapp3 1:182ff6aef029 85
4180_1 0:582c6436e759 86 } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display
4180_1 0:582c6436e759 87 stream.readNext((byte*)BigBuf, 2048); //Buffer has been read, now we can put more data in it
ipapp3 1:182ff6aef029 88 c=0;
ipapp3 1:182ff6aef029 89 t++;
4180_1 0:582c6436e759 90 }
4180_1 0:582c6436e759 91 }
4180_1 0:582c6436e759 92 lcd.cls();
4180_1 0:582c6436e759 93 if (result == HTTP_OK) {
4180_1 0:582c6436e759 94 lcd.cls();
4180_1 0:582c6436e759 95 lcd.printf(" Read complete\n\r");
ipapp3 1:182ff6aef029 96
4180_1 0:582c6436e759 97 } else {
4180_1 0:582c6436e759 98 lcd. printf(" Error %d\n", result);
4180_1 0:582c6436e759 99 return -1;
4180_1 0:582c6436e759 100 }
ipapp3 1:182ff6aef029 101 // }
4180_1 0:582c6436e759 102 }