![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ハイパー・マイコン mbedでインターネット 電子工作 5章 リスト5-6 spxml_WeatherLCD
Dependencies: EthernetInterface HTTPClient NTPClient NokiaLCD_With_JapaneseFont SDFileSystem TextLCD mbed-rtos mbed spxml
main.cpp@0:420af60927fd, 2014-07-13 (annotated)
- Committer:
- sunifu
- Date:
- Sun Jul 13 12:26:08 2014 +0000
- Revision:
- 0:420af60927fd
2014.07.13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunifu | 0:420af60927fd | 1 | #include "mbed.h" |
sunifu | 0:420af60927fd | 2 | #include "TextLCD.h" |
sunifu | 0:420af60927fd | 3 | #include "NokiaLCD.h" |
sunifu | 0:420af60927fd | 4 | #include "SDFileSystem.h" |
sunifu | 0:420af60927fd | 5 | |
sunifu | 0:420af60927fd | 6 | #include "EthernetInterface.h" |
sunifu | 0:420af60927fd | 7 | #include "HTTPClient.h" |
sunifu | 0:420af60927fd | 8 | #include "NTPClient.h" |
sunifu | 0:420af60927fd | 9 | |
sunifu | 0:420af60927fd | 10 | #include "spdomparser.hpp" |
sunifu | 0:420af60927fd | 11 | #include "spxmlnode.hpp" |
sunifu | 0:420af60927fd | 12 | #include "spxmlhandle.hpp" |
sunifu | 0:420af60927fd | 13 | |
sunifu | 0:420af60927fd | 14 | TextLCD lcd(p24, p26, p27, p28, p29, p30); |
sunifu | 0:420af60927fd | 15 | NokiaLCD lcd1(p11, p13, p14, p15, NokiaLCD::PCF8833); |
sunifu | 0:420af60927fd | 16 | //NokiaLCD lcd1(p11, p13, p14, p15, NokiaLCD::LCD3300); |
sunifu | 0:420af60927fd | 17 | |
sunifu | 0:420af60927fd | 18 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
sunifu | 0:420af60927fd | 19 | //LocalFileSystem local("sd"); |
sunifu | 0:420af60927fd | 20 | |
sunifu | 0:420af60927fd | 21 | char filename[20]; |
sunifu | 0:420af60927fd | 22 | char buf[3000]; |
sunifu | 0:420af60927fd | 23 | char lcdMsg[16]; |
sunifu | 0:420af60927fd | 24 | int daySW = 0; |
sunifu | 0:420af60927fd | 25 | |
sunifu | 0:420af60927fd | 26 | void parseWeather(SP_XmlElementNode *node) { |
sunifu | 0:420af60927fd | 27 | lcd1.cls(); |
sunifu | 0:420af60927fd | 28 | SP_XmlHandle handle(node); |
sunifu | 0:420af60927fd | 29 | |
sunifu | 0:420af60927fd | 30 | SP_XmlElementNode * location = handle.getChild( "yweather:location" ).toElement(); |
sunifu | 0:420af60927fd | 31 | if (location) { |
sunifu | 0:420af60927fd | 32 | lcd1.printf("Location:%s\n",location->getAttrValue("city")); |
sunifu | 0:420af60927fd | 33 | } |
sunifu | 0:420af60927fd | 34 | |
sunifu | 0:420af60927fd | 35 | SP_XmlElementNode * forecast; |
sunifu | 0:420af60927fd | 36 | if( daySW == 0 ) |
sunifu | 0:420af60927fd | 37 | forecast = handle.getChild("item").getChild( "yweather:forecast",0).toElement(); |
sunifu | 0:420af60927fd | 38 | else |
sunifu | 0:420af60927fd | 39 | forecast = handle.getChild("item").getChild( "yweather:forecast",1).toElement(); |
sunifu | 0:420af60927fd | 40 | |
sunifu | 0:420af60927fd | 41 | if (forecast) { |
sunifu | 0:420af60927fd | 42 | lcd1.printf("Condition:%s \n",forecast->getAttrValue("text")); |
sunifu | 0:420af60927fd | 43 | sprintf(filename,"/sd/%s.bmp",forecast->getAttrValue("code")); |
sunifu | 0:420af60927fd | 44 | lcd1.printf("Temp:Low%sC High%sC\n",forecast->getAttrValue("low"),forecast->getAttrValue("high")); |
sunifu | 0:420af60927fd | 45 | lcd1.printf("Date:%s(%s)",forecast->getAttrValue("date"),forecast->getAttrValue("day")); |
sunifu | 0:420af60927fd | 46 | } |
sunifu | 0:420af60927fd | 47 | } |
sunifu | 0:420af60927fd | 48 | |
sunifu | 0:420af60927fd | 49 | int PrintIcon(void) |
sunifu | 0:420af60927fd | 50 | { |
sunifu | 0:420af60927fd | 51 | FILE *fs; |
sunifu | 0:420af60927fd | 52 | int i; |
sunifu | 0:420af60927fd | 53 | char header[54]; |
sunifu | 0:420af60927fd | 54 | int rgb; |
sunifu | 0:420af60927fd | 55 | unsigned char datr,datg,datb; |
sunifu | 0:420af60927fd | 56 | |
sunifu | 0:420af60927fd | 57 | printf( "Weather icons access [%s]\r\n",filename); |
sunifu | 0:420af60927fd | 58 | if ( NULL == (fs = fopen(filename, "rb" )) ) { |
sunifu | 0:420af60927fd | 59 | printf( "file open error when oening file \r\n"); |
sunifu | 0:420af60927fd | 60 | return -1; |
sunifu | 0:420af60927fd | 61 | } |
sunifu | 0:420af60927fd | 62 | |
sunifu | 0:420af60927fd | 63 | //bitmap headder throw |
sunifu | 0:420af60927fd | 64 | for (i=0;i<0x36;i++) |
sunifu | 0:420af60927fd | 65 | fread(&header, sizeof(unsigned char), 1, fs); |
sunifu | 0:420af60927fd | 66 | |
sunifu | 0:420af60927fd | 67 | |
sunifu | 0:420af60927fd | 68 | for(int y=50;y>0;y--){ |
sunifu | 0:420af60927fd | 69 | for(int x=0;x<60;x++){ |
sunifu | 0:420af60927fd | 70 | fread(&datb, sizeof(unsigned char), 1, fs); |
sunifu | 0:420af60927fd | 71 | fread(&datg, sizeof(unsigned char), 1, fs); |
sunifu | 0:420af60927fd | 72 | fread(&datr, sizeof(unsigned char), 1, fs); |
sunifu | 0:420af60927fd | 73 | |
sunifu | 0:420af60927fd | 74 | datb = (0xF0&datb)>>4; |
sunifu | 0:420af60927fd | 75 | datg = (0xF0&datg)>>4; |
sunifu | 0:420af60927fd | 76 | datr = (0xF0&datr)>>4; |
sunifu | 0:420af60927fd | 77 | |
sunifu | 0:420af60927fd | 78 | rgb = (datr <<20) | (datg<<12) | (datb<<4); |
sunifu | 0:420af60927fd | 79 | lcd1.pixel(x+30,y+70,rgb); |
sunifu | 0:420af60927fd | 80 | } |
sunifu | 0:420af60927fd | 81 | } |
sunifu | 0:420af60927fd | 82 | |
sunifu | 0:420af60927fd | 83 | fclose(fs); |
sunifu | 0:420af60927fd | 84 | |
sunifu | 0:420af60927fd | 85 | return 0; |
sunifu | 0:420af60927fd | 86 | } |
sunifu | 0:420af60927fd | 87 | |
sunifu | 0:420af60927fd | 88 | void lcd_Update(void const *arg) |
sunifu | 0:420af60927fd | 89 | { |
sunifu | 0:420af60927fd | 90 | while(true){ |
sunifu | 0:420af60927fd | 91 | Thread::wait(15000); |
sunifu | 0:420af60927fd | 92 | time_t ctTime = time(NULL)+32400; // JST |
sunifu | 0:420af60927fd | 93 | strftime(lcdMsg,16,"%y/%m/%d %H:%M",localtime(&ctTime)); |
sunifu | 0:420af60927fd | 94 | lcd.locate(0,0); |
sunifu | 0:420af60927fd | 95 | lcd.printf("[%s]",lcdMsg); |
sunifu | 0:420af60927fd | 96 | printf("lcd_Update(%s)\r\n",lcdMsg); |
sunifu | 0:420af60927fd | 97 | } |
sunifu | 0:420af60927fd | 98 | } |
sunifu | 0:420af60927fd | 99 | |
sunifu | 0:420af60927fd | 100 | int main() { |
sunifu | 0:420af60927fd | 101 | // the eth and HTTP code has be taken directly from the HTTPStream documentation page |
sunifu | 0:420af60927fd | 102 | // see http://mbed.org/cookbook/HTTP-Client-Data-Containers |
sunifu | 0:420af60927fd | 103 | EthernetInterface eth; |
sunifu | 0:420af60927fd | 104 | NTPClient ntp; |
sunifu | 0:420af60927fd | 105 | int retEth; |
sunifu | 0:420af60927fd | 106 | HTTPClient http; |
sunifu | 0:420af60927fd | 107 | int retHttp; |
sunifu | 0:420af60927fd | 108 | SP_XmlDomParser parser; |
sunifu | 0:420af60927fd | 109 | |
sunifu | 0:420af60927fd | 110 | printf("Setting up ...\r\n"); |
sunifu | 0:420af60927fd | 111 | lcd.cls(); |
sunifu | 0:420af60927fd | 112 | lcd.locate(0,0); |
sunifu | 0:420af60927fd | 113 | lcd.printf("Setting Up..."); |
sunifu | 0:420af60927fd | 114 | |
sunifu | 0:420af60927fd | 115 | eth.init(); |
sunifu | 0:420af60927fd | 116 | retEth = eth.connect(); |
sunifu | 0:420af60927fd | 117 | |
sunifu | 0:420af60927fd | 118 | lcd.locate(0,0); |
sunifu | 0:420af60927fd | 119 | if (!retEth) { |
sunifu | 0:420af60927fd | 120 | printf("Network Setup ok\r\n"); |
sunifu | 0:420af60927fd | 121 | lcd.printf("Network Setup OK"); |
sunifu | 0:420af60927fd | 122 | } else { |
sunifu | 0:420af60927fd | 123 | printf("Network Error %d\r\n", retEth); |
sunifu | 0:420af60927fd | 124 | lcd.printf("Network Error %d"); |
sunifu | 0:420af60927fd | 125 | return -1; |
sunifu | 0:420af60927fd | 126 | } |
sunifu | 0:420af60927fd | 127 | |
sunifu | 0:420af60927fd | 128 | printf("Trying to update time...\r\n"); |
sunifu | 0:420af60927fd | 129 | if (ntp.setTime("ntp.nict.jp") == 0) |
sunifu | 0:420af60927fd | 130 | { |
sunifu | 0:420af60927fd | 131 | printf("Set time successfully\r\n"); |
sunifu | 0:420af60927fd | 132 | } |
sunifu | 0:420af60927fd | 133 | else |
sunifu | 0:420af60927fd | 134 | { |
sunifu | 0:420af60927fd | 135 | printf("NTP Set Error\r\n"); |
sunifu | 0:420af60927fd | 136 | } |
sunifu | 0:420af60927fd | 137 | |
sunifu | 0:420af60927fd | 138 | Thread thread0(lcd_Update); |
sunifu | 0:420af60927fd | 139 | |
sunifu | 0:420af60927fd | 140 | while(true) |
sunifu | 0:420af60927fd | 141 | { |
sunifu | 0:420af60927fd | 142 | retHttp = http.get("http://weather.yahooapis.com/forecastrss?w=28426187&u=c", buf, sizeof(buf)); |
sunifu | 0:420af60927fd | 143 | |
sunifu | 0:420af60927fd | 144 | lcd.locate(0,1); |
sunifu | 0:420af60927fd | 145 | switch(retHttp){ |
sunifu | 0:420af60927fd | 146 | case HTTP_OK: |
sunifu | 0:420af60927fd | 147 | printf("Read completely\r\n"); |
sunifu | 0:420af60927fd | 148 | lcd.printf("Read completely "); |
sunifu | 0:420af60927fd | 149 | break; |
sunifu | 0:420af60927fd | 150 | case HTTP_TIMEOUT: |
sunifu | 0:420af60927fd | 151 | printf("Connection Timeout\r\n"); |
sunifu | 0:420af60927fd | 152 | lcd.printf("Timeout "); |
sunifu | 0:420af60927fd | 153 | break; |
sunifu | 0:420af60927fd | 154 | case HTTP_CONN: |
sunifu | 0:420af60927fd | 155 | printf("Connection Error\r\n"); |
sunifu | 0:420af60927fd | 156 | lcd.printf("Connection Error"); |
sunifu | 0:420af60927fd | 157 | break;; |
sunifu | 0:420af60927fd | 158 | default: |
sunifu | 0:420af60927fd | 159 | printf("Error\r\n"); |
sunifu | 0:420af60927fd | 160 | lcd.printf("Error "); |
sunifu | 0:420af60927fd | 161 | } |
sunifu | 0:420af60927fd | 162 | printf("\r\n-----%s-----\r\n%s\r\n",lcdMsg,buf); |
sunifu | 0:420af60927fd | 163 | |
sunifu | 0:420af60927fd | 164 | parser.append( buf, strlen(buf)); // stream current buffer data to the XML parser |
sunifu | 0:420af60927fd | 165 | |
sunifu | 0:420af60927fd | 166 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
sunifu | 0:420af60927fd | 167 | SP_XmlElementNode * child = rootHandle.getChild( "channel" ).toElement(); |
sunifu | 0:420af60927fd | 168 | |
sunifu | 0:420af60927fd | 169 | if ( child ) { |
sunifu | 0:420af60927fd | 170 | parseWeather(child); |
sunifu | 0:420af60927fd | 171 | } |
sunifu | 0:420af60927fd | 172 | |
sunifu | 0:420af60927fd | 173 | if ( NULL != parser.getError() ) { |
sunifu | 0:420af60927fd | 174 | printf( "\nerror: %s\n", parser.getError() ); |
sunifu | 0:420af60927fd | 175 | } |
sunifu | 0:420af60927fd | 176 | |
sunifu | 0:420af60927fd | 177 | PrintIcon(); |
sunifu | 0:420af60927fd | 178 | |
sunifu | 0:420af60927fd | 179 | if ( daySW == 0 ) |
sunifu | 0:420af60927fd | 180 | daySW = 1; |
sunifu | 0:420af60927fd | 181 | else |
sunifu | 0:420af60927fd | 182 | daySW = 0 ; |
sunifu | 0:420af60927fd | 183 | printf("\r\nUpdateIcon%s\r\n",lcdMsg); |
sunifu | 0:420af60927fd | 184 | |
sunifu | 0:420af60927fd | 185 | wait(120.0) ; |
sunifu | 0:420af60927fd | 186 | } |
sunifu | 0:420af60927fd | 187 | } |