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 Sep 01 04:12:37 2012 +0000
Revision:
4:ab3092d15121
Parent:
3:07562878d3c3
HTTPFile was changed to follow the latest HTTPClient library.; BlinkLed was isolated as a library.; Change LED port from LED1,2 to LED3,4 for the specification of pwmout.

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 4:ab3092d15121 16 BlinkLed led3(LED3, 0.02);
togayan 4:ab3092d15121 17 BlinkLed led4(LED4, 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
togayan 0:1855a008f28e 22 const char* rssUrl = "http://www3.nhk.or.jp/rj/podcast/rss/english.xml";
togayan 0:1855a008f28e 23 const char* rssPath = "/usb/english.xml";
togayan 0:1855a008f28e 24 const char* mp3Path = "/usb/english.mp3";
togayan 0:1855a008f28e 25
togayan 0:1855a008f28e 26 int main()
togayan 0:1855a008f28e 27 {
togayan 0:1855a008f28e 28 printf("\n\n================================\n");
togayan 0:1855a008f28e 29 printf("mpod NHK English news Downloader\n");
togayan 0:1855a008f28e 30 printf("================================\n\n");
togayan 0:1855a008f28e 31
togayan 4:ab3092d15121 32 // Indicate downloading
togayan 4:ab3092d15121 33 led4.startBlink();
togayan 4:ab3092d15121 34 ethYellow.startBlink();
togayan 4:ab3092d15121 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 3:07562878d3c3 46 char lastBuildDateOriginal[128] = {0};
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 3:07562878d3c3 51 strcpy(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 3:07562878d3c3 58 strcpy(lastBuildDateOriginal, "No \"lastBuildDate\" element in original RSS");
togayan 2:0da3a4508b46 59 }
togayan 2:0da3a4508b46 60 else
togayan 2:0da3a4508b46 61 {
togayan 3:07562878d3c3 62 strcpy(lastBuildDateOriginal, lastBuildDateOriginalElement->GetText());
togayan 2:0da3a4508b46 63 }
togayan 2:0da3a4508b46 64 }
togayan 0:1855a008f28e 65 }
togayan 3:07562878d3c3 66 printf("\nlastBuildDate (original): %s\n", lastBuildDateOriginal);
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 3:07562878d3c3 72 char lastBuildDateCurrent[128] = {0};
togayan 3:07562878d3c3 73 char mp3Url[256] = {0};
togayan 3:07562878d3c3 74 char mp3Length[32] = {0};
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 3:07562878d3c3 89 strcpy(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 3:07562878d3c3 97 strcpy(mp3Url, enclosureElement->Attribute("url"));
togayan 3:07562878d3c3 98 strcpy(mp3Length, enclosureElement->Attribute("length"));
togayan 0:1855a008f28e 99 }
togayan 3:07562878d3c3 100 printf("\nlastBuildDate (current) : %s\n", lastBuildDateCurrent);
togayan 0:1855a008f28e 101
togayan 2:0da3a4508b46 102 // Determine the necessity of downloading new MP3.
togayan 2:0da3a4508b46 103 bool flgDownloadMp3 = false;
togayan 3:07562878d3c3 104 if ( strcmp(lastBuildDateOriginal, lastBuildDateCurrent) == 0 )
togayan 0:1855a008f28e 105 {
togayan 3:07562878d3c3 106 printf("lastBuildDate (original) == lastBuildDate (current)\n");
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 3:07562878d3c3 111 if (ftell(mp3fp) != atol(mp3Length))
togayan 2:0da3a4508b46 112 {
togayan 3:07562878d3c3 113 printf("MP3 file size is invalid.\n");
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 3:07562878d3c3 120 printf("However, no enlish.mp3 in USB memory\n");
togayan 2:0da3a4508b46 121 flgDownloadMp3 = true;
togayan 0:1855a008f28e 122 }
togayan 0:1855a008f28e 123 }
togayan 0:1855a008f28e 124 else
togayan 0:1855a008f28e 125 {
togayan 3:07562878d3c3 126 printf("lastBuildDate (original) != lastBuildDate (current)\n");
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 3:07562878d3c3 133 GetFile(mp3Path, mp3Url);
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 3:07562878d3c3 140 printf("\nUSB host was switched to HSD2(External Device).\n");
togayan 1:1637e625b21b 141 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 142
togayan 4:ab3092d15121 143 // Indicate finish downloading
togayan 4:ab3092d15121 144 led4.finishBlink();
togayan 4:ab3092d15121 145 ethYellow.finishBlink();
togayan 4:ab3092d15121 146 led3.startBlink();
togayan 3:07562878d3c3 147 ethGreen.startBlink();
togayan 0:1855a008f28e 148
togayan 0:1855a008f28e 149 while(true){}
togayan 0:1855a008f28e 150 }
togayan 0:1855a008f28e 151
togayan 0:1855a008f28e 152 int GetFile(const char *path, const char *url)
togayan 0:1855a008f28e 153 {
togayan 4:ab3092d15121 154 printf("Getting %s -> %s\n", url, path);
togayan 0:1855a008f28e 155
togayan 2:0da3a4508b46 156 HTTPFile file(path);
togayan 0:1855a008f28e 157 HTTPResult retGet = http.get(url, &file);
togayan 2:0da3a4508b46 158 if (retGet != HTTP_OK)
togayan 2:0da3a4508b46 159 {
togayan 1:1637e625b21b 160 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 161 error("Error in http.get in GetFile(): %d\n", retGet);
togayan 0:1855a008f28e 162 }
togayan 0:1855a008f28e 163 file.clear();
togayan 0:1855a008f28e 164
togayan 0:1855a008f28e 165 return (0);
togayan 0:1855a008f28e 166 }