News_Reader

Dependencies:   FatFileSystem HTTPClient NetServices RPG SDFileSystem TextLCD mbed spxml

Fork of Arduino_MP3_Shield_MP3Player_UI by Matthew Petersen

Committer:
cwang365
Date:
Wed Mar 06 00:37:29 2013 +0000
Revision:
2:447591cbba27
Parent:
1:0014879fa94f
lab3_news_reader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cwang365 1:0014879fa94f 1 #include "mbed.h"
cwang365 1:0014879fa94f 2 #include "VS1002.h"
cwang365 1:0014879fa94f 3 #include "TextLCD.h"
cwang365 1:0014879fa94f 4 #include "RPG.h"
cwang365 1:0014879fa94f 5 #include "EthernetNetIf.h"
cwang365 1:0014879fa94f 6 #include "HTTPClient.h"
cwang365 1:0014879fa94f 7 #include "SDFileSystem.h"
cwang365 1:0014879fa94f 8
cwang365 1:0014879fa94f 9 #include <string>
cwang365 1:0014879fa94f 10 #include <iostream>
cwang365 1:0014879fa94f 11
cwang365 1:0014879fa94f 12 TextLCD lcd2(p10, p18, p24, p23, p22, p21, TextLCD::LCD16x2 ); // rs, e, d0-d3
cwang365 1:0014879fa94f 13
cwang365 1:0014879fa94f 14 HTTPClient http;
cwang365 1:0014879fa94f 15 HTTPResult result;
cwang365 1:0014879fa94f 16 bool completed = false;
cwang365 1:0014879fa94f 17
cwang365 1:0014879fa94f 18 void request_callback(HTTPResult r)
cwang365 1:0014879fa94f 19 {
cwang365 1:0014879fa94f 20 result = r;
cwang365 1:0014879fa94f 21 completed = true;
cwang365 1:0014879fa94f 22 }
cwang365 1:0014879fa94f 23
cwang365 1:0014879fa94f 24
cwang365 1:0014879fa94f 25
cwang365 1:0014879fa94f 26 void fetch ()
cwang365 1:0014879fa94f 27 {
cwang365 1:0014879fa94f 28 string voiceurl;
cwang365 1:0014879fa94f 29 string httpurl = "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=";
cwang365 1:0014879fa94f 30 Serial pc(USBTX, USBRX);
cwang365 1:0014879fa94f 31 pc.baud(9600);
cwang365 1:0014879fa94f 32 char* title[10];
cwang365 1:0014879fa94f 33 int counter=0;
cwang365 1:0014879fa94f 34 char *ct[10]= {"/sd/0.txt","/sd/1.txt","/sd/2.txt","/sd/3.txt","/sd/4.txt","/sd/5.txt","/sd/6.txt","/sd/7.txt","/sd/8.txt","/sd/9.txt"};
cwang365 1:0014879fa94f 35 char *tstartXML = "<title>"; //RSS XML start title
cwang365 1:0014879fa94f 36 char *tendXML ="</title>"; //RSS XML end title
cwang365 1:0014879fa94f 37 char *tsptr;
cwang365 1:0014879fa94f 38 char *teptr;
cwang365 1:0014879fa94f 39 int i=0,j=0;
cwang365 1:0014879fa94f 40 // the eth and HTTP code has be taken directly from the HTTPStream documentation page
cwang365 1:0014879fa94f 41 // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
cwang365 1:0014879fa94f 42
cwang365 1:0014879fa94f 43 HTTPStream stream;
cwang365 1:0014879fa94f 44 char BigBuf[1024 + 1] = {0};
cwang365 1:0014879fa94f 45 stream.readNext((byte*)BigBuf, 1024); //Point to buffer for the first read
cwang365 1:0014879fa94f 46 //CNN Tech News RSS Feed - get web page with XML
cwang365 1:0014879fa94f 47 HTTPResult r = http.get("HTTP://rss.cnn.com/rss/cnn_tech.rss", &stream, request_callback);
cwang365 1:0014879fa94f 48 //fetch new and print
cwang365 1:0014879fa94f 49 while (!completed) {
cwang365 1:0014879fa94f 50 Net::poll(); // Polls the Networking stack
cwang365 1:0014879fa94f 51 if (stream.readable()) { // check for end of file
cwang365 1:0014879fa94f 52 BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
cwang365 1:0014879fa94f 53 tsptr = BigBuf;
cwang365 1:0014879fa94f 54 // displays titles on LCD from XML "<title>....title text...</title>"
cwang365 1:0014879fa94f 55 do {
cwang365 1:0014879fa94f 56 tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not
cwang365 1:0014879fa94f 57 teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not
cwang365 1:0014879fa94f 58
cwang365 1:0014879fa94f 59 if (tsptr!=NULL) {
cwang365 1:0014879fa94f 60 tsptr = tsptr + strlen(tstartXML);// move to char after "<title>"
cwang365 1:0014879fa94f 61
cwang365 1:0014879fa94f 62 voiceurl.erase();
cwang365 1:0014879fa94f 63 voiceurl=tsptr;
cwang365 1:0014879fa94f 64 voiceurl.erase(voiceurl.begin()+(strlen(tsptr)-strlen(teptr)),voiceurl.end());
cwang365 1:0014879fa94f 65
cwang365 1:0014879fa94f 66 printf("title: _____ %s\n",voiceurl);
cwang365 1:0014879fa94f 67 }
cwang365 1:0014879fa94f 68 if ((tsptr!=NULL)&&(teptr!=NULL)) {
cwang365 1:0014879fa94f 69
cwang365 1:0014879fa94f 70 voiceurl=httpurl+voiceurl;
cwang365 1:0014879fa94f 71 for(i=0; (i<voiceurl.length()); i++) {
cwang365 1:0014879fa94f 72 if(voiceurl[i]==' '||voiceurl[i]=='\'') {
cwang365 1:0014879fa94f 73 voiceurl[i]='+';
cwang365 1:0014879fa94f 74 }
cwang365 1:0014879fa94f 75 }
cwang365 1:0014879fa94f 76 voiceurl=voiceurl+"!!";
cwang365 1:0014879fa94f 77 printf("%s\n",voiceurl.c_str());
cwang365 1:0014879fa94f 78 if(counter==10) {
cwang365 1:0014879fa94f 79 counter=0;
cwang365 1:0014879fa94f 80 }
cwang365 1:0014879fa94f 81 voiceurl=voiceurl.c_str();
cwang365 1:0014879fa94f 82 char temp[voiceurl.length()];
cwang365 1:0014879fa94f 83
cwang365 1:0014879fa94f 84 for(i=0; (i<=voiceurl.length()); i++) {
cwang365 1:0014879fa94f 85
cwang365 1:0014879fa94f 86 temp[i]=voiceurl[i];
cwang365 1:0014879fa94f 87
cwang365 1:0014879fa94f 88 }
cwang365 1:0014879fa94f 89
cwang365 1:0014879fa94f 90
cwang365 1:0014879fa94f 91 FILE *fp = fopen(ct[counter], "w");
cwang365 1:0014879fa94f 92 if(fp == NULL) {
cwang365 1:0014879fa94f 93 error("Could not open file for write\n");
cwang365 1:0014879fa94f 94 }
cwang365 1:0014879fa94f 95 fprintf(fp, temp);
cwang365 1:0014879fa94f 96 fclose(fp);
cwang365 1:0014879fa94f 97 printf("stored: _____ %s\n\n",temp);
cwang365 1:0014879fa94f 98 printf("Goodbye World!\n");
cwang365 1:0014879fa94f 99 counter++;
cwang365 1:0014879fa94f 100
cwang365 1:0014879fa94f 101 wait(.2);
cwang365 1:0014879fa94f 102 }
cwang365 1:0014879fa94f 103 } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display
cwang365 1:0014879fa94f 104 stream.readNext((byte*)BigBuf, 1024); //Buffer has been read, now we can put more data in it
cwang365 1:0014879fa94f 105 }
cwang365 1:0014879fa94f 106 }
cwang365 1:0014879fa94f 107 lcd2.cls();
cwang365 1:0014879fa94f 108 if (result == HTTP_OK) {
cwang365 1:0014879fa94f 109 lcd2.cls();
cwang365 1:0014879fa94f 110 lcd2.printf(" Read complete\n\r");
cwang365 1:0014879fa94f 111 } else {
cwang365 1:0014879fa94f 112 lcd2. printf(" Error %d\n", result);
cwang365 1:0014879fa94f 113
cwang365 1:0014879fa94f 114 }
cwang365 1:0014879fa94f 115
cwang365 1:0014879fa94f 116 free (BigBuf);
cwang365 1:0014879fa94f 117 }