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:
Fri Aug 17 12:49:12 2012 +0000
Revision:
1:1637e625b21b
Parent:
0:1855a008f28e
Child:
2:0da3a4508b46
Refactor main.cpp

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 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 1:1637e625b21b 31 printf("USB host was switched to HSD1(mbed).\n\n");
togayan 1:1637e625b21b 32 fsusb30s = 0; // HSD1
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 1:1637e625b21b 39 // Obtain original lastBuildDate
togayan 0:1855a008f28e 40 const char* lastBuildDateOriginal;
togayan 0:1855a008f28e 41 XMLDocument docOriginal;
togayan 0:1855a008f28e 42 if(XML_SUCCESS != docOriginal.LoadFile(rssPath))
togayan 0:1855a008f28e 43 {
togayan 0:1855a008f28e 44 lastBuildDateOriginal = "No english.xml in USB memory";
togayan 0:1855a008f28e 45 }
togayan 0:1855a008f28e 46 XMLElement* lastBuildDateOriginalElement = docOriginal.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
togayan 0:1855a008f28e 47 if(NULL == lastBuildDateOriginalElement)
togayan 0:1855a008f28e 48 {
togayan 0:1855a008f28e 49 lastBuildDateOriginal = "No \"lastBuildDate\" element in RSS";
togayan 0:1855a008f28e 50 }
togayan 0:1855a008f28e 51 else
togayan 0:1855a008f28e 52 {
togayan 0:1855a008f28e 53 lastBuildDateOriginal = lastBuildDateOriginalElement->GetText();
togayan 0:1855a008f28e 54 }
togayan 0:1855a008f28e 55
togayan 1:1637e625b21b 56 // Download RSS
togayan 0:1855a008f28e 57 GetFile(rssPath, rssUrl);
togayan 1:1637e625b21b 58
togayan 1:1637e625b21b 59 // Obtain current lastBuildDate
togayan 0:1855a008f28e 60 const char* lastBuildDateCurrent;
togayan 0:1855a008f28e 61 XMLDocument docCurrent;
togayan 0:1855a008f28e 62 if(XML_SUCCESS != docCurrent.LoadFile(rssPath))
togayan 0:1855a008f28e 63 {
togayan 0:1855a008f28e 64 lastBuildDateCurrent = "No english.xml in USB memory";
togayan 0:1855a008f28e 65 }
togayan 0:1855a008f28e 66 XMLElement* lastBuildDateCurrentElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("lastBuildDate");
togayan 0:1855a008f28e 67 if(NULL == lastBuildDateCurrentElement)
togayan 0:1855a008f28e 68 {
togayan 0:1855a008f28e 69 lastBuildDateCurrent = "No \"lastBuildDate\" element in RSS";
togayan 0:1855a008f28e 70 }
togayan 0:1855a008f28e 71 else
togayan 0:1855a008f28e 72 {
togayan 0:1855a008f28e 73 lastBuildDateCurrent = lastBuildDateCurrentElement->GetText();
togayan 0:1855a008f28e 74 }
togayan 0:1855a008f28e 75
togayan 1:1637e625b21b 76 printf("\n");
togayan 1:1637e625b21b 77 printf("lastBuildDate (original): %s\n", lastBuildDateOriginal);
togayan 1:1637e625b21b 78 printf("lastBuildDate (current) : %s\n", lastBuildDateCurrent);
togayan 0:1855a008f28e 79
togayan 1:1637e625b21b 80 // Download new MP3
togayan 0:1855a008f28e 81 if (strcmp(lastBuildDateOriginal, lastBuildDateCurrent) == 0)
togayan 0:1855a008f28e 82 {
togayan 0:1855a008f28e 83 printf("lastBuildDate (original) == lastBuildDate (current)\n");
togayan 0:1855a008f28e 84 FILE* mp3fp = fopen(mp3Path, "r"); // check an existance of english.mp3
togayan 0:1855a008f28e 85 if (mp3fp != NULL)
togayan 0:1855a008f28e 86 {
togayan 0:1855a008f28e 87 fclose(mp3fp);
togayan 0:1855a008f28e 88 }
togayan 0:1855a008f28e 89 else
togayan 0:1855a008f28e 90 {
togayan 0:1855a008f28e 91 printf("However, no enlish.mp3 in USB memory\n");
togayan 0:1855a008f28e 92 }
togayan 0:1855a008f28e 93 }
togayan 0:1855a008f28e 94 else
togayan 0:1855a008f28e 95 {
togayan 1:1637e625b21b 96 printf("lastBuildDate (original) != lastBuildDate (current)\n");
togayan 0:1855a008f28e 97 XMLElement* enclosureElement = docCurrent.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item")->FirstChildElement("enclosure");
togayan 0:1855a008f28e 98 if(NULL == enclosureElement)
togayan 0:1855a008f28e 99 {
togayan 1:1637e625b21b 100 printf("No \"enclosure\" element in RSS");
togayan 0:1855a008f28e 101 }
togayan 0:1855a008f28e 102 else
togayan 0:1855a008f28e 103 {
togayan 1:1637e625b21b 104 const char* mp3Url = enclosureElement->Attribute( "url" );
togayan 1:1637e625b21b 105 GetFile(mp3Path, mp3Url);
togayan 0:1855a008f28e 106 }
togayan 0:1855a008f28e 107 }
togayan 0:1855a008f28e 108
togayan 1:1637e625b21b 109 // FSUSB30 switches to HSD2 (External Device)
togayan 1:1637e625b21b 110 printf("\nUSB host was switched to HSD2(External Device).\n");
togayan 1:1637e625b21b 111 fsusb30s = 1; // HSD2
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 1:1637e625b21b 122 printf("\nGetting %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 1:1637e625b21b 130 // FSUSB30 switches to HSD2 (External Device)
togayan 1:1637e625b21b 131 printf("USB host was switched to HSD2(External Device).\n");
togayan 1:1637e625b21b 132 fsusb30s = 1; // HSD2
togayan 0:1855a008f28e 133 error("Error in http.get in GetFile(): %d\n", retGet);
togayan 0:1855a008f28e 134 }
togayan 0:1855a008f28e 135 file.clear();
togayan 0:1855a008f28e 136
togayan 0:1855a008f28e 137 timer.stop();
togayan 0:1855a008f28e 138 printf("timer.read_ms(): %d\n", timer.read_ms());
togayan 0:1855a008f28e 139
togayan 0:1855a008f28e 140 return (0);
togayan 0:1855a008f28e 141 }