Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/

Dependencies:   BlinkLed HTTPClient EthernetInterface FatFileSystemCpp MSCFileSystem mbed-rtos mbed

Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/

Committer:
togayan
Date:
Sat Aug 18 16:46:40 2012 +0000
Revision:
2:0da3a4508b46
Parent:
1:1637e625b21b
Child:
3:07562878d3c3
Fixed some bugs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
togayan 0:1855a008f28e 1 #include "mbed.h"
togayan 0:1855a008f28e 2 #include "MSCFileSystem.h"
togayan 0:1855a008f28e 3 #include "EthernetInterface.h"
togayan 0:1855a008f28e 4 #include "HTTPClient.h"
togayan 0:1855a008f28e 5 #include "HTTPFile.h"
togayan 0:1855a008f28e 6 #include "BlinkLed.h"
togayan 0:1855a008f28e 7 #include "tinyxml2.h"
togayan 2:0da3a4508b46 8 #include <string>
togayan 2:0da3a4508b46 9 #include <iostream>
togayan 0:1855a008f28e 10
togayan 2:0da3a4508b46 11 using std::string;
togayan 2:0da3a4508b46 12 using std::cout;
togayan 2:0da3a4508b46 13 using std::endl;
togayan 0:1855a008f28e 14 using namespace tinyxml2;
togayan 0:1855a008f28e 15
togayan 0:1855a008f28e 16 int GetFile(const char *path, const char *url);
togayan 0:1855a008f28e 17
togayan 0:1855a008f28e 18 EthernetInterface eth;
togayan 0:1855a008f28e 19 HTTPClient http;
togayan 0:1855a008f28e 20 MSCFileSystem usb("usb");
togayan 2:0da3a4508b46 21 BlinkLed led1(LED1, 6);
togayan 2:0da3a4508b46 22 BlinkLed led2(LED2, 1);
togayan 0:1855a008f28e 23 DigitalOut fsusb30s(p9);
togayan 0:1855a008f28e 24 Timer timer;
togayan 0:1855a008f28e 25
togayan 0:1855a008f28e 26 const char* rssUrl = "http://www3.nhk.or.jp/rj/podcast/rss/english.xml";
togayan 0:1855a008f28e 27 const char* rssPath = "/usb/english.xml";
togayan 0:1855a008f28e 28 const char* mp3Path = "/usb/english.mp3";
togayan 0:1855a008f28e 29
togayan 0:1855a008f28e 30 int main()
togayan 0:1855a008f28e 31 {
togayan 0:1855a008f28e 32 printf("\n\n================================\n");
togayan 0:1855a008f28e 33 printf("mpod NHK English news Downloader\n");
togayan 0:1855a008f28e 34 printf("================================\n\n");
togayan 0:1855a008f28e 35
togayan 0:1855a008f28e 36 // FSUSB30 switches to HSD1 (mbed)
togayan 1:1637e625b21b 37 printf("USB host was switched to HSD1(mbed).\n\n");
togayan 1:1637e625b21b 38 fsusb30s = 0; // HSD1
togayan 0:1855a008f28e 39
togayan 0:1855a008f28e 40 // Network setup
togayan 0:1855a008f28e 41 printf("Setup EtherNet with DHCP.\n");
togayan 0:1855a008f28e 42 eth.init(); //Use DHCP
togayan 0:1855a008f28e 43 eth.connect();
togayan 0:1855a008f28e 44
togayan 1:1637e625b21b 45 // Obtain original lastBuildDate
togayan 2:0da3a4508b46 46 string lastBuildDateOriginal;
togayan 0:1855a008f28e 47 {
togayan 2:0da3a4508b46 48 XMLDocument docOriginal;
togayan 2:0da3a4508b46 49 if(XML_SUCCESS != docOriginal.LoadFile(rssPath))
togayan 2:0da3a4508b46 50 {
togayan 2:0da3a4508b46 51 lastBuildDateOriginal = "No original english.xml in USB memory";
togayan 2:0da3a4508b46 52 }
togayan 2:0da3a4508b46 53 else
togayan 2:0da3a4508b46 54 {
togayan 2:0da3a4508b46 55 XMLElement* lastBuildDateOriginalElement = docOriginal.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
togayan 2:0da3a4508b46 56 if(NULL == lastBuildDateOriginalElement)
togayan 2:0da3a4508b46 57 {
togayan 2:0da3a4508b46 58 lastBuildDateOriginal = "No \"lastBuildDate\" element in original RSS";
togayan 2:0da3a4508b46 59 }
togayan 2:0da3a4508b46 60 else
togayan 2:0da3a4508b46 61 {
togayan 2:0da3a4508b46 62 lastBuildDateOriginal = lastBuildDateOriginalElement->GetText();
togayan 2:0da3a4508b46 63 }
togayan 2:0da3a4508b46 64 }
togayan 0:1855a008f28e 65 }
togayan 2:0da3a4508b46 66 cout << endl << "lastBuildDate (original): " << lastBuildDateOriginal << endl;
togayan 0:1855a008f28e 67
togayan 1:1637e625b21b 68 // Download RSS
togayan 0:1855a008f28e 69 GetFile(rssPath, rssUrl);
togayan 1:1637e625b21b 70
togayan 1:1637e625b21b 71 // Obtain current lastBuildDate
togayan 2:0da3a4508b46 72 string lastBuildDateCurrent;
togayan 2:0da3a4508b46 73 string mp3Url;
togayan 2:0da3a4508b46 74 string mp3Length;
togayan 0:1855a008f28e 75 {
togayan 2:0da3a4508b46 76 XMLDocument docCurrent;
togayan 2:0da3a4508b46 77 if(XML_SUCCESS != docCurrent.LoadFile(rssPath))
togayan 2:0da3a4508b46 78 {
togayan 2:0da3a4508b46 79 fsusb30s = 1; // HSD2
togayan 2:0da3a4508b46 80 error("No current english.xml in USB memory.\n");
togayan 2:0da3a4508b46 81 }
togayan 2:0da3a4508b46 82
togayan 2:0da3a4508b46 83 XMLElement* lastBuildDateCurrentElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
togayan 2:0da3a4508b46 84 if(NULL == lastBuildDateCurrentElement)
togayan 2:0da3a4508b46 85 {
togayan 2:0da3a4508b46 86 fsusb30s = 1; // HSD2
togayan 2:0da3a4508b46 87 error("No \"lastBuildDate\" element in current RSS.\n");
togayan 2:0da3a4508b46 88 }
togayan 2:0da3a4508b46 89 lastBuildDateCurrent = lastBuildDateCurrentElement->GetText();
togayan 2:0da3a4508b46 90
togayan 2:0da3a4508b46 91 XMLElement* enclosureElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item")->FirstChildElement("enclosure");
togayan 2:0da3a4508b46 92 if(NULL == enclosureElement)
togayan 2:0da3a4508b46 93 {
togayan 2:0da3a4508b46 94 fsusb30s = 1; // HSD2
togayan 2:0da3a4508b46 95 error("No \"enclosure\" element in current RSS.\n");
togayan 2:0da3a4508b46 96 }
togayan 2:0da3a4508b46 97 mp3Url = enclosureElement->Attribute( "url" );
togayan 2:0da3a4508b46 98 mp3Length = enclosureElement->Attribute( "length" );
togayan 0:1855a008f28e 99 }
togayan 2:0da3a4508b46 100 cout << endl << "lastBuildDate (current) : " << lastBuildDateCurrent << endl;
togayan 0:1855a008f28e 101
togayan 2:0da3a4508b46 102 // Determine the necessity of downloading new MP3.
togayan 2:0da3a4508b46 103 bool flgDownloadMp3 = false;
togayan 2:0da3a4508b46 104 if (lastBuildDateOriginal == lastBuildDateCurrent)
togayan 0:1855a008f28e 105 {
togayan 2:0da3a4508b46 106 cout << "lastBuildDate (original) == lastBuildDate (current)" << endl;
togayan 0:1855a008f28e 107 FILE* mp3fp = fopen(mp3Path, "r"); // check an existance of english.mp3
togayan 0:1855a008f28e 108 if (mp3fp != NULL)
togayan 0:1855a008f28e 109 {
togayan 2:0da3a4508b46 110 fseek(mp3fp, 0, SEEK_END); // seek to end of file
togayan 2:0da3a4508b46 111 if (ftell(mp3fp) != atol(mp3Length.c_str()))
togayan 2:0da3a4508b46 112 {
togayan 2:0da3a4508b46 113 cout << "MP3 file size is invalid." << endl;
togayan 2:0da3a4508b46 114 flgDownloadMp3 = true;
togayan 2:0da3a4508b46 115 }
togayan 0:1855a008f28e 116 fclose(mp3fp);
togayan 0:1855a008f28e 117 }
togayan 0:1855a008f28e 118 else
togayan 0:1855a008f28e 119 {
togayan 2:0da3a4508b46 120 cout << "However, no enlish.mp3 in USB memory" << endl;
togayan 2:0da3a4508b46 121 flgDownloadMp3 = true;
togayan 0:1855a008f28e 122 }
togayan 0:1855a008f28e 123 }
togayan 0:1855a008f28e 124 else
togayan 0:1855a008f28e 125 {
togayan 2:0da3a4508b46 126 cout << "lastBuildDate (original) != lastBuildDate (current)" << endl;
togayan 2:0da3a4508b46 127 flgDownloadMp3 = true;
togayan 0:1855a008f28e 128 }
togayan 0:1855a008f28e 129
togayan 2:0da3a4508b46 130 // Download new MP3
togayan 2:0da3a4508b46 131 if(flgDownloadMp3 == true)
togayan 2:0da3a4508b46 132 {
togayan 2:0da3a4508b46 133 GetFile(mp3Path, mp3Url.c_str());
togayan 2:0da3a4508b46 134 }
togayan 2:0da3a4508b46 135
togayan 2:0da3a4508b46 136 // Wait for the completion of writing to USB Mass Storage Device.
togayan 2:0da3a4508b46 137 wait(1);
togayan 2:0da3a4508b46 138
togayan 1:1637e625b21b 139 // FSUSB30 switches to HSD2 (External Device)
togayan 2:0da3a4508b46 140 cout << endl << "USB host was switched to HSD2(External Device)." << endl;
togayan 1:1637e625b21b 141 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 142
togayan 0:1855a008f28e 143 // blink LED
togayan 0:1855a008f28e 144 led1.startBlink();
togayan 0:1855a008f28e 145
togayan 0:1855a008f28e 146 while(true){}
togayan 0:1855a008f28e 147 }
togayan 0:1855a008f28e 148
togayan 0:1855a008f28e 149 int GetFile(const char *path, const char *url)
togayan 0:1855a008f28e 150 {
togayan 2:0da3a4508b46 151 led2.startBlink();
togayan 2:0da3a4508b46 152 cout << endl << "Getting " << url << endl;
togayan 0:1855a008f28e 153
togayan 0:1855a008f28e 154 timer.stop();
togayan 0:1855a008f28e 155 timer.reset();
togayan 0:1855a008f28e 156 timer.start();
togayan 0:1855a008f28e 157
togayan 2:0da3a4508b46 158 HTTPFile file(path);
togayan 0:1855a008f28e 159 HTTPResult retGet = http.get(url, &file);
togayan 2:0da3a4508b46 160 if (retGet != HTTP_OK)
togayan 2:0da3a4508b46 161 {
togayan 1:1637e625b21b 162 // FSUSB30 switches to HSD2 (External Device)
togayan 2:0da3a4508b46 163 cout << "USB host was switched to HSD2(External Device)." << endl;
togayan 1:1637e625b21b 164 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 165 error("Error in http.get in GetFile(): %d\n", retGet);
togayan 0:1855a008f28e 166 }
togayan 0:1855a008f28e 167 file.clear();
togayan 0:1855a008f28e 168
togayan 0:1855a008f28e 169 timer.stop();
togayan 2:0da3a4508b46 170 cout << "timer.read_ms(): " << timer.read_ms() << endl;
togayan 2:0da3a4508b46 171
togayan 2:0da3a4508b46 172 led2.finishBlink();
togayan 0:1855a008f28e 173 return (0);
togayan 0:1855a008f28e 174 }