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:
Sun Aug 19 15:57:55 2012 +0000
Revision:
3:07562878d3c3
Parent:
2:0da3a4508b46
Child:
4:ab3092d15121
Remove iostream and Use printf().; Change the BlinkLed class.

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