![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ハイパー・マイコン mbedでインターネット 電子工作 5章 リスト5-3 SPXmlWeather
Dependencies: EthernetInterface HTTPClient TextLCD mbed-rtos mbed spxml
main.cpp@0:798ebca9033c, 2014-07-13 (annotated)
- Committer:
- sunifu
- Date:
- Sun Jul 13 11:31:43 2014 +0000
- Revision:
- 0:798ebca9033c
2014.7.13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunifu | 0:798ebca9033c | 1 | #include "mbed.h" |
sunifu | 0:798ebca9033c | 2 | #include "rtos.h" |
sunifu | 0:798ebca9033c | 3 | |
sunifu | 0:798ebca9033c | 4 | #include "TextLCD.h" |
sunifu | 0:798ebca9033c | 5 | |
sunifu | 0:798ebca9033c | 6 | #include "EthernetInterface.h" |
sunifu | 0:798ebca9033c | 7 | #include "HTTPClient.h" |
sunifu | 0:798ebca9033c | 8 | |
sunifu | 0:798ebca9033c | 9 | #include "spdomparser.hpp" |
sunifu | 0:798ebca9033c | 10 | #include "spxmlnode.hpp" |
sunifu | 0:798ebca9033c | 11 | #include "spxmlhandle.hpp" |
sunifu | 0:798ebca9033c | 12 | |
sunifu | 0:798ebca9033c | 13 | TextLCD lcd(p24, p26, p27, p28, p29, p30); |
sunifu | 0:798ebca9033c | 14 | char buf[3000]; |
sunifu | 0:798ebca9033c | 15 | |
sunifu | 0:798ebca9033c | 16 | int main() { |
sunifu | 0:798ebca9033c | 17 | EthernetInterface eth; |
sunifu | 0:798ebca9033c | 18 | int retEth; |
sunifu | 0:798ebca9033c | 19 | HTTPClient http; |
sunifu | 0:798ebca9033c | 20 | int retHttp; |
sunifu | 0:798ebca9033c | 21 | SP_XmlDomParser parser; |
sunifu | 0:798ebca9033c | 22 | |
sunifu | 0:798ebca9033c | 23 | printf("Setting up ...\r\n"); |
sunifu | 0:798ebca9033c | 24 | lcd.cls(); |
sunifu | 0:798ebca9033c | 25 | lcd.locate(0,0); |
sunifu | 0:798ebca9033c | 26 | lcd.printf("Setting Up..."); |
sunifu | 0:798ebca9033c | 27 | |
sunifu | 0:798ebca9033c | 28 | eth.init(); |
sunifu | 0:798ebca9033c | 29 | retEth = eth.connect(); |
sunifu | 0:798ebca9033c | 30 | |
sunifu | 0:798ebca9033c | 31 | lcd.locate(0,0); |
sunifu | 0:798ebca9033c | 32 | if (!retEth) { |
sunifu | 0:798ebca9033c | 33 | printf("Network Setup OK\r\n"); |
sunifu | 0:798ebca9033c | 34 | lcd.printf("Network Setup OK"); |
sunifu | 0:798ebca9033c | 35 | } else { |
sunifu | 0:798ebca9033c | 36 | printf("Network Error %d\r\n", retEth); |
sunifu | 0:798ebca9033c | 37 | lcd.printf("Network Error %d"); |
sunifu | 0:798ebca9033c | 38 | return -1; |
sunifu | 0:798ebca9033c | 39 | } |
sunifu | 0:798ebca9033c | 40 | |
sunifu | 0:798ebca9033c | 41 | while(true) |
sunifu | 0:798ebca9033c | 42 | { |
sunifu | 0:798ebca9033c | 43 | retHttp = http.get("http://weather.yahooapis.com/forecastrss?w=28426187&u=c", buf, sizeof(buf)); |
sunifu | 0:798ebca9033c | 44 | |
sunifu | 0:798ebca9033c | 45 | lcd.locate(0,1); |
sunifu | 0:798ebca9033c | 46 | switch(retHttp){ |
sunifu | 0:798ebca9033c | 47 | case HTTP_OK: |
sunifu | 0:798ebca9033c | 48 | printf("Read completely\r\n"); |
sunifu | 0:798ebca9033c | 49 | lcd.printf("Read completely "); |
sunifu | 0:798ebca9033c | 50 | break; |
sunifu | 0:798ebca9033c | 51 | case HTTP_TIMEOUT: |
sunifu | 0:798ebca9033c | 52 | printf("Connection Timeout\r\n"); |
sunifu | 0:798ebca9033c | 53 | lcd.printf("Timeout "); |
sunifu | 0:798ebca9033c | 54 | break; |
sunifu | 0:798ebca9033c | 55 | case HTTP_CONN: |
sunifu | 0:798ebca9033c | 56 | printf("Connection Error\r\n"); |
sunifu | 0:798ebca9033c | 57 | lcd.printf("Connection Error"); |
sunifu | 0:798ebca9033c | 58 | break; |
sunifu | 0:798ebca9033c | 59 | default: |
sunifu | 0:798ebca9033c | 60 | printf("Error\r\n"); |
sunifu | 0:798ebca9033c | 61 | lcd.printf("Error "); |
sunifu | 0:798ebca9033c | 62 | } |
sunifu | 0:798ebca9033c | 63 | |
sunifu | 0:798ebca9033c | 64 | parser.append( buf, strlen(buf)); // stream current buffer data to the XML parser |
sunifu | 0:798ebca9033c | 65 | wait(5.0); |
sunifu | 0:798ebca9033c | 66 | printf("\r\n----------%s----------\r\n",buf); |
sunifu | 0:798ebca9033c | 67 | |
sunifu | 0:798ebca9033c | 68 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
sunifu | 0:798ebca9033c | 69 | |
sunifu | 0:798ebca9033c | 70 | /* |
sunifu | 0:798ebca9033c | 71 | SP_XmlCDataNode * title = rootHandle.getChild( "channel" ).getChild("title").getChild(0).toCData(); |
sunifu | 0:798ebca9033c | 72 | printf("\r\n === Title:%s === \r\n",title->getText()); |
sunifu | 0:798ebca9033c | 73 | SP_XmlCDataNode * title1 = rootHandle.getChild( "channel" ).getChild("image").getChild("title").getChild(0).toCData(); |
sunifu | 0:798ebca9033c | 74 | printf("\r\n === Title:%s === \r\n",title1->getText()); |
sunifu | 0:798ebca9033c | 75 | SP_XmlCDataNode * title2 = rootHandle.getChild( "channel" ).getChild("item").getChild("title").getChild(0).toCData(); |
sunifu | 0:798ebca9033c | 76 | printf("\r\n === Title:%s === \r\n",title2->getText()); |
sunifu | 0:798ebca9033c | 77 | */ |
sunifu | 0:798ebca9033c | 78 | |
sunifu | 0:798ebca9033c | 79 | SP_XmlElementNode * location = rootHandle.getChild( "channel" ).getChild( "yweather:location" ).toElement(); |
sunifu | 0:798ebca9033c | 80 | if (location) { |
sunifu | 0:798ebca9033c | 81 | printf("\r\n === Location:%s === \r\n",location->getAttrValue("city")); |
sunifu | 0:798ebca9033c | 82 | } |
sunifu | 0:798ebca9033c | 83 | |
sunifu | 0:798ebca9033c | 84 | SP_XmlElementNode * forecast; |
sunifu | 0:798ebca9033c | 85 | |
sunifu | 0:798ebca9033c | 86 | forecast = rootHandle.getChild( "channel" ).getChild("item").getChild( "yweather:forecast",0).toElement(); |
sunifu | 0:798ebca9033c | 87 | if (forecast) { |
sunifu | 0:798ebca9033c | 88 | printf("\r\n ----- Date:%s(%s) ----- \r\n",forecast->getAttrValue("date"),forecast->getAttrValue("day")); |
sunifu | 0:798ebca9033c | 89 | printf("Condition:%s \n",forecast->getAttrValue("text")); |
sunifu | 0:798ebca9033c | 90 | printf("Temp:Low%sC High%sC\n",forecast->getAttrValue("low"),forecast->getAttrValue("high")); |
sunifu | 0:798ebca9033c | 91 | } |
sunifu | 0:798ebca9033c | 92 | |
sunifu | 0:798ebca9033c | 93 | forecast = rootHandle.getChild( "channel" ).getChild("item").getChild( "yweather:forecast",1).toElement(); |
sunifu | 0:798ebca9033c | 94 | if (forecast) { |
sunifu | 0:798ebca9033c | 95 | printf("\r\n ----- Date:%s(%s) ----- \r\n",forecast->getAttrValue("date"),forecast->getAttrValue("day")); |
sunifu | 0:798ebca9033c | 96 | printf("Condition:%s \n",forecast->getAttrValue("text")); |
sunifu | 0:798ebca9033c | 97 | printf("Temp:Low%sC High%sC\n",forecast->getAttrValue("low"),forecast->getAttrValue("high")); |
sunifu | 0:798ebca9033c | 98 | } |
sunifu | 0:798ebca9033c | 99 | |
sunifu | 0:798ebca9033c | 100 | wait(120.0); |
sunifu | 0:798ebca9033c | 101 | } |
sunifu | 0:798ebca9033c | 102 | } |