chong wang / Mbed 2 deprecated lab3-News_Reader1

Dependencies:   FatFileSystem HTTPClient NetServices RPG SDFileSystem TextLCD mbed spxml

Fork of Arduino_MP3_Shield_MP3Player_UI by Matthew Petersen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fetch.cpp Source File

fetch.cpp

00001 #include "mbed.h"
00002 #include "VS1002.h"
00003 #include "TextLCD.h"
00004 #include "RPG.h"
00005 #include "EthernetNetIf.h"
00006 #include "HTTPClient.h"
00007 #include "SDFileSystem.h"
00008 
00009 #include <string>
00010 #include <iostream>
00011 
00012 TextLCD lcd2(p10, p18, p24, p23, p22, p21, TextLCD::LCD16x2 ); // rs, e, d0-d3
00013 
00014 HTTPClient http;
00015 HTTPResult result;
00016 bool completed = false;
00017 
00018 void request_callback(HTTPResult r)
00019 {
00020     result = r;
00021     completed = true;
00022 }
00023 
00024 
00025 
00026 void  fetch ()
00027 {
00028     string voiceurl;
00029     string httpurl = "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=";
00030     Serial pc(USBTX, USBRX);
00031     pc.baud(9600);
00032     char* title[10];
00033     int counter=0;
00034     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"};
00035     char *tstartXML = "<title>"; //RSS XML start title
00036     char *tendXML ="</title>"; //RSS XML end title
00037     char *tsptr;
00038     char *teptr;
00039     int i=0,j=0;
00040     // the eth and HTTP code has be taken directly from the HTTPStream documentation page
00041     // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
00042    
00043     HTTPStream stream;
00044     char BigBuf[1024 + 1] = {0};
00045     stream.readNext((byte*)BigBuf, 1024); //Point to buffer for the first read
00046     //CNN Tech News RSS Feed - get web page with XML
00047     HTTPResult r = http.get("HTTP://rss.cnn.com/rss/cnn_tech.rss", &stream, request_callback);
00048 //fetch new and print
00049     while (!completed) {
00050         Net::poll(); // Polls the Networking stack
00051         if (stream.readable()) { // check for end of file
00052             BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
00053             tsptr = BigBuf;
00054             // displays titles on LCD  from XML "<title>....title text...</title>"
00055             do {
00056                 tsptr = strstr(tsptr,tstartXML); // find <title> in string - NULL if not
00057                 teptr = strstr(tsptr,tendXML); // find <\title> in string - NULL if not
00058 
00059                 if (tsptr!=NULL) {
00060                     tsptr = tsptr + strlen(tstartXML);// move to char after "<title>"
00061 
00062                     voiceurl.erase();
00063                     voiceurl=tsptr;
00064                     voiceurl.erase(voiceurl.begin()+(strlen(tsptr)-strlen(teptr)),voiceurl.end());
00065 
00066                     printf("title: _____ %s\n",voiceurl);
00067                 }
00068                 if ((tsptr!=NULL)&&(teptr!=NULL)) {
00069 
00070                     voiceurl=httpurl+voiceurl;
00071                     for(i=0; (i<voiceurl.length()); i++) {
00072                         if(voiceurl[i]==' '||voiceurl[i]=='\'') {
00073                             voiceurl[i]='+';
00074                         }
00075                     }
00076                     voiceurl=voiceurl+"!!";
00077                     printf("%s\n",voiceurl.c_str());
00078                     if(counter==10) {
00079                         counter=0;
00080                     }
00081                     voiceurl=voiceurl.c_str();
00082                     char temp[voiceurl.length()];
00083 
00084                     for(i=0; (i<=voiceurl.length()); i++) {
00085  
00086                         temp[i]=voiceurl[i];
00087 
00088                     }
00089 
00090 
00091                     FILE *fp = fopen(ct[counter], "w");
00092                     if(fp == NULL) {
00093                         error("Could not open file for write\n");
00094                     }
00095                     fprintf(fp, temp);
00096                     fclose(fp);
00097                     printf("stored: _____ %s\n\n",temp);
00098                     printf("Goodbye World!\n");
00099                     counter++;
00100 
00101                     wait(.2);
00102                 }
00103             } while (tsptr!=NULL); // No more "<title>"s in BigBuf to display
00104             stream.readNext((byte*)BigBuf, 1024); //Buffer has been read, now we can put more data in it
00105         }
00106     }
00107     lcd2.cls();
00108     if (result == HTTP_OK) {
00109         lcd2.cls();
00110         lcd2.printf(" Read complete\n\r");
00111     } else {
00112         lcd2. printf(" Error %d\n", result);
00113 
00114     }
00115 
00116 free (BigBuf);
00117 }