ハイパー・マイコン mbedでインターネット 電子工作 5章 リスト5-3 SPXmlWeather
Dependencies: EthernetInterface HTTPClient TextLCD mbed-rtos mbed spxml
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 #include "TextLCD.h" 00005 00006 #include "EthernetInterface.h" 00007 #include "HTTPClient.h" 00008 00009 #include "spdomparser.hpp" 00010 #include "spxmlnode.hpp" 00011 #include "spxmlhandle.hpp" 00012 00013 TextLCD lcd(p24, p26, p27, p28, p29, p30); 00014 char buf[3000]; 00015 00016 int main() { 00017 EthernetInterface eth; 00018 int retEth; 00019 HTTPClient http; 00020 int retHttp; 00021 SP_XmlDomParser parser; 00022 00023 printf("Setting up ...\r\n"); 00024 lcd.cls(); 00025 lcd.locate(0,0); 00026 lcd.printf("Setting Up..."); 00027 00028 eth.init(); 00029 retEth = eth.connect(); 00030 00031 lcd.locate(0,0); 00032 if (!retEth) { 00033 printf("Network Setup OK\r\n"); 00034 lcd.printf("Network Setup OK"); 00035 } else { 00036 printf("Network Error %d\r\n", retEth); 00037 lcd.printf("Network Error %d"); 00038 return -1; 00039 } 00040 00041 while(true) 00042 { 00043 retHttp = http.get("http://weather.yahooapis.com/forecastrss?w=28426187&u=c", buf, sizeof(buf)); 00044 00045 lcd.locate(0,1); 00046 switch(retHttp){ 00047 case HTTP_OK: 00048 printf("Read completely\r\n"); 00049 lcd.printf("Read completely "); 00050 break; 00051 case HTTP_TIMEOUT: 00052 printf("Connection Timeout\r\n"); 00053 lcd.printf("Timeout "); 00054 break; 00055 case HTTP_CONN: 00056 printf("Connection Error\r\n"); 00057 lcd.printf("Connection Error"); 00058 break; 00059 default: 00060 printf("Error\r\n"); 00061 lcd.printf("Error "); 00062 } 00063 00064 parser.append( buf, strlen(buf)); // stream current buffer data to the XML parser 00065 wait(5.0); 00066 printf("\r\n----------%s----------\r\n",buf); 00067 00068 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); 00069 00070 /* 00071 SP_XmlCDataNode * title = rootHandle.getChild( "channel" ).getChild("title").getChild(0).toCData(); 00072 printf("\r\n === Title:%s === \r\n",title->getText()); 00073 SP_XmlCDataNode * title1 = rootHandle.getChild( "channel" ).getChild("image").getChild("title").getChild(0).toCData(); 00074 printf("\r\n === Title:%s === \r\n",title1->getText()); 00075 SP_XmlCDataNode * title2 = rootHandle.getChild( "channel" ).getChild("item").getChild("title").getChild(0).toCData(); 00076 printf("\r\n === Title:%s === \r\n",title2->getText()); 00077 */ 00078 00079 SP_XmlElementNode * location = rootHandle.getChild( "channel" ).getChild( "yweather:location" ).toElement(); 00080 if (location) { 00081 printf("\r\n === Location:%s === \r\n",location->getAttrValue("city")); 00082 } 00083 00084 SP_XmlElementNode * forecast; 00085 00086 forecast = rootHandle.getChild( "channel" ).getChild("item").getChild( "yweather:forecast",0).toElement(); 00087 if (forecast) { 00088 printf("\r\n ----- Date:%s(%s) ----- \r\n",forecast->getAttrValue("date"),forecast->getAttrValue("day")); 00089 printf("Condition:%s \n",forecast->getAttrValue("text")); 00090 printf("Temp:Low%sC High%sC\n",forecast->getAttrValue("low"),forecast->getAttrValue("high")); 00091 } 00092 00093 forecast = rootHandle.getChild( "channel" ).getChild("item").getChild( "yweather:forecast",1).toElement(); 00094 if (forecast) { 00095 printf("\r\n ----- Date:%s(%s) ----- \r\n",forecast->getAttrValue("date"),forecast->getAttrValue("day")); 00096 printf("Condition:%s \n",forecast->getAttrValue("text")); 00097 printf("Temp:Low%sC High%sC\n",forecast->getAttrValue("low"),forecast->getAttrValue("high")); 00098 } 00099 00100 wait(120.0); 00101 } 00102 }
Generated on Tue Jul 12 2022 20:39:11 by 1.7.2