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/
main.cpp@0:1855a008f28e, 2012-08-16 (annotated)
- Committer:
- togayan
- Date:
- Thu Aug 16 15:49:19 2012 +0000
- Revision:
- 0:1855a008f28e
- Child:
- 1:1637e625b21b
First revision of mpod_nhk_english.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:1855a008f28e | 16 | BlinkLed led1(LED1, 4); |
togayan | 0:1855a008f28e | 17 | DigitalOut fsusb30s(p9); |
togayan | 0:1855a008f28e | 18 | Timer timer; |
togayan | 0:1855a008f28e | 19 | |
togayan | 0:1855a008f28e | 20 | const char* rssUrl = "http://www3.nhk.or.jp/rj/podcast/rss/english.xml"; |
togayan | 0:1855a008f28e | 21 | const char* rssPath = "/usb/english.xml"; |
togayan | 0:1855a008f28e | 22 | const char* mp3Path = "/usb/english.mp3"; |
togayan | 0:1855a008f28e | 23 | |
togayan | 0:1855a008f28e | 24 | int main() |
togayan | 0:1855a008f28e | 25 | { |
togayan | 0:1855a008f28e | 26 | printf("\n\n================================\n"); |
togayan | 0:1855a008f28e | 27 | printf("mpod NHK English news Downloader\n"); |
togayan | 0:1855a008f28e | 28 | printf("================================\n\n"); |
togayan | 0:1855a008f28e | 29 | |
togayan | 0:1855a008f28e | 30 | // FSUSB30 switches to HSD1 (mbed) |
togayan | 0:1855a008f28e | 31 | printf("USB host was switched to HSD1.\n"); |
togayan | 0:1855a008f28e | 32 | fsusb30s = 0; |
togayan | 0:1855a008f28e | 33 | |
togayan | 0:1855a008f28e | 34 | // Network setup |
togayan | 0:1855a008f28e | 35 | printf("Setup EtherNet with DHCP.\n"); |
togayan | 0:1855a008f28e | 36 | eth.init(); //Use DHCP |
togayan | 0:1855a008f28e | 37 | eth.connect(); |
togayan | 0:1855a008f28e | 38 | |
togayan | 0:1855a008f28e | 39 | |
togayan | 0:1855a008f28e | 40 | // Obtain lastBuildDate |
togayan | 0:1855a008f28e | 41 | const char* lastBuildDateOriginal; |
togayan | 0:1855a008f28e | 42 | XMLDocument docOriginal; |
togayan | 0:1855a008f28e | 43 | if(XML_SUCCESS != docOriginal.LoadFile(rssPath)) |
togayan | 0:1855a008f28e | 44 | { |
togayan | 0:1855a008f28e | 45 | lastBuildDateOriginal = "No english.xml in USB memory"; |
togayan | 0:1855a008f28e | 46 | } |
togayan | 0:1855a008f28e | 47 | XMLElement* lastBuildDateOriginalElement = docOriginal.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate"); |
togayan | 0:1855a008f28e | 48 | if(NULL == lastBuildDateOriginalElement) |
togayan | 0:1855a008f28e | 49 | { |
togayan | 0:1855a008f28e | 50 | lastBuildDateOriginal = "No \"lastBuildDate\" element in RSS"; |
togayan | 0:1855a008f28e | 51 | } |
togayan | 0:1855a008f28e | 52 | else |
togayan | 0:1855a008f28e | 53 | { |
togayan | 0:1855a008f28e | 54 | lastBuildDateOriginal = lastBuildDateOriginalElement->GetText(); |
togayan | 0:1855a008f28e | 55 | } |
togayan | 0:1855a008f28e | 56 | printf("lastBuildDate (original): %s\n", lastBuildDateOriginal); |
togayan | 0:1855a008f28e | 57 | |
togayan | 0:1855a008f28e | 58 | |
togayan | 0:1855a008f28e | 59 | // RSS download and parse |
togayan | 0:1855a008f28e | 60 | GetFile(rssPath, rssUrl); |
togayan | 0:1855a008f28e | 61 | const char* lastBuildDateCurrent; |
togayan | 0:1855a008f28e | 62 | XMLDocument docCurrent; |
togayan | 0:1855a008f28e | 63 | if(XML_SUCCESS != docCurrent.LoadFile(rssPath)) |
togayan | 0:1855a008f28e | 64 | { |
togayan | 0:1855a008f28e | 65 | lastBuildDateCurrent = "No english.xml in USB memory"; |
togayan | 0:1855a008f28e | 66 | } |
togayan | 0:1855a008f28e | 67 | XMLElement* lastBuildDateCurrentElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate"); |
togayan | 0:1855a008f28e | 68 | if(NULL == lastBuildDateCurrentElement) |
togayan | 0:1855a008f28e | 69 | { |
togayan | 0:1855a008f28e | 70 | lastBuildDateCurrent = "No \"lastBuildDate\" element in RSS"; |
togayan | 0:1855a008f28e | 71 | } |
togayan | 0:1855a008f28e | 72 | else |
togayan | 0:1855a008f28e | 73 | { |
togayan | 0:1855a008f28e | 74 | lastBuildDateCurrent = lastBuildDateCurrentElement->GetText(); |
togayan | 0:1855a008f28e | 75 | } |
togayan | 0:1855a008f28e | 76 | printf("lastBuildDate (Current) : %s\n", lastBuildDateCurrent); |
togayan | 0:1855a008f28e | 77 | |
togayan | 0:1855a008f28e | 78 | |
togayan | 0:1855a008f28e | 79 | if (strcmp(lastBuildDateOriginal, lastBuildDateCurrent) == 0) |
togayan | 0:1855a008f28e | 80 | { |
togayan | 0:1855a008f28e | 81 | printf("lastBuildDate (original) == lastBuildDate (current)\n"); |
togayan | 0:1855a008f28e | 82 | FILE* mp3fp = fopen(mp3Path, "r"); // check an existance of english.mp3 |
togayan | 0:1855a008f28e | 83 | if (mp3fp != NULL) |
togayan | 0:1855a008f28e | 84 | { |
togayan | 0:1855a008f28e | 85 | fclose(mp3fp); |
togayan | 0:1855a008f28e | 86 | } |
togayan | 0:1855a008f28e | 87 | else |
togayan | 0:1855a008f28e | 88 | { |
togayan | 0:1855a008f28e | 89 | printf("However, no enlish.mp3 in USB memory\n"); |
togayan | 0:1855a008f28e | 90 | } |
togayan | 0:1855a008f28e | 91 | } |
togayan | 0:1855a008f28e | 92 | else |
togayan | 0:1855a008f28e | 93 | { |
togayan | 0:1855a008f28e | 94 | const char* mp3Url; |
togayan | 0:1855a008f28e | 95 | XMLElement* enclosureElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item")->FirstChildElement("enclosure"); |
togayan | 0:1855a008f28e | 96 | if(NULL == enclosureElement) |
togayan | 0:1855a008f28e | 97 | { |
togayan | 0:1855a008f28e | 98 | while(1){}; |
togayan | 0:1855a008f28e | 99 | } |
togayan | 0:1855a008f28e | 100 | else |
togayan | 0:1855a008f28e | 101 | { |
togayan | 0:1855a008f28e | 102 | mp3Url = enclosureElement->Attribute( "url" ); |
togayan | 0:1855a008f28e | 103 | } |
togayan | 0:1855a008f28e | 104 | printf("mp3Url : %s\n", mp3Url); |
togayan | 0:1855a008f28e | 105 | GetFile(mp3Path, mp3Url); |
togayan | 0:1855a008f28e | 106 | } |
togayan | 0:1855a008f28e | 107 | |
togayan | 0:1855a008f28e | 108 | |
togayan | 0:1855a008f28e | 109 | // Switch USB host from mbed to KWD |
togayan | 0:1855a008f28e | 110 | printf("USB host was switched to HSD2.\n"); |
togayan | 0:1855a008f28e | 111 | fsusb30s = 1; // HSD2 (KWD) |
togayan | 0:1855a008f28e | 112 | |
togayan | 0:1855a008f28e | 113 | // blink LED |
togayan | 0:1855a008f28e | 114 | led1.startBlink(); |
togayan | 0:1855a008f28e | 115 | |
togayan | 0:1855a008f28e | 116 | while(true){} |
togayan | 0:1855a008f28e | 117 | } |
togayan | 0:1855a008f28e | 118 | |
togayan | 0:1855a008f28e | 119 | int GetFile(const char *path, const char *url) |
togayan | 0:1855a008f28e | 120 | { |
togayan | 0:1855a008f28e | 121 | HTTPFile file(path); |
togayan | 0:1855a008f28e | 122 | printf("Getting %s\n", url); |
togayan | 0:1855a008f28e | 123 | |
togayan | 0:1855a008f28e | 124 | timer.stop(); |
togayan | 0:1855a008f28e | 125 | timer.reset(); |
togayan | 0:1855a008f28e | 126 | timer.start(); |
togayan | 0:1855a008f28e | 127 | |
togayan | 0:1855a008f28e | 128 | HTTPResult retGet = http.get(url, &file); |
togayan | 0:1855a008f28e | 129 | if (retGet != HTTP_OK) { |
togayan | 0:1855a008f28e | 130 | error("Error in http.get in GetFile(): %d\n", retGet); |
togayan | 0:1855a008f28e | 131 | } |
togayan | 0:1855a008f28e | 132 | file.clear(); |
togayan | 0:1855a008f28e | 133 | |
togayan | 0:1855a008f28e | 134 | timer.stop(); |
togayan | 0:1855a008f28e | 135 | printf("timer.read_ms(): %d\n", timer.read_ms()); |
togayan | 0:1855a008f28e | 136 | |
togayan | 0:1855a008f28e | 137 | return (0); |
togayan | 0:1855a008f28e | 138 | } |