Get weather Information
WeatherHacks.cpp@3:6618936f930a, 2015-11-22 (annotated)
- Committer:
- takashikojo
- Date:
- Sun Nov 22 08:50:49 2015 +0000
- Revision:
- 3:6618936f930a
- Parent:
- 2:76b812680942
Serial terminal macro
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:6d53d6d34ac3 | 1 | #include <mbed.h> |
takashikojo | 0:6d53d6d34ac3 | 2 | #include "WeatherHacks.h" |
takashikojo | 0:6d53d6d34ac3 | 3 | |
takashikojo | 0:6d53d6d34ac3 | 4 | #include "EthernetInterface.h" |
takashikojo | 0:6d53d6d34ac3 | 5 | #include "HTTPClient.h" |
takashikojo | 0:6d53d6d34ac3 | 6 | #include "XMLaide.h" |
takashikojo | 0:6d53d6d34ac3 | 7 | |
takashikojo | 0:6d53d6d34ac3 | 8 | #define RSS_URL "http://weather.livedoor.com/forecast/rss/area/130010.xml" |
takashikojo | 0:6d53d6d34ac3 | 9 | |
takashikojo | 0:6d53d6d34ac3 | 10 | #if 0 |
takashikojo | 0:6d53d6d34ac3 | 11 | //Enable debug |
takashikojo | 0:6d53d6d34ac3 | 12 | #define DBG(x, ...) std::printf("[WeatherHacks : DBG]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 0:6d53d6d34ac3 | 13 | #define WARN(x, ...) std::printf("[WeatherHacks : WARN]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 0:6d53d6d34ac3 | 14 | #else |
takashikojo | 0:6d53d6d34ac3 | 15 | //Disable debug |
takashikojo | 0:6d53d6d34ac3 | 16 | #define DBG(x, ...) |
takashikojo | 0:6d53d6d34ac3 | 17 | #define WARN(x, ...) |
takashikojo | 0:6d53d6d34ac3 | 18 | #endif |
takashikojo | 0:6d53d6d34ac3 | 19 | |
takashikojo | 0:6d53d6d34ac3 | 20 | #define ERR(x, ...) std::printf("[WeatherHacks : ERR]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 0:6d53d6d34ac3 | 21 | |
takashikojo | 3:6618936f930a | 22 | #if 0 |
takashikojo | 3:6618936f930a | 23 | #define TERMINAL(x, ...) std::printf("[WeatherHacks :]"x"\r\n", ##__VA_ARGS__); |
takashikojo | 3:6618936f930a | 24 | #else |
takashikojo | 3:6618936f930a | 25 | #define TERMINAL(x, ...) |
takashikojo | 3:6618936f930a | 26 | #endif |
takashikojo | 3:6618936f930a | 27 | |
takashikojo | 1:8655f1720450 | 28 | extern HTTPClient httpClient; |
takashikojo | 0:6d53d6d34ac3 | 29 | |
takashikojo | 0:6d53d6d34ac3 | 30 | void WH_init(void) |
takashikojo | 0:6d53d6d34ac3 | 31 | { |
takashikojo | 0:6d53d6d34ac3 | 32 | |
takashikojo | 0:6d53d6d34ac3 | 33 | } |
takashikojo | 0:6d53d6d34ac3 | 34 | |
takashikojo | 2:76b812680942 | 35 | #define LINK_SIZE 200 |
takashikojo | 0:6d53d6d34ac3 | 36 | static void getLink(const char *buff, char *link) |
takashikojo | 0:6d53d6d34ac3 | 37 | { |
takashikojo | 0:6d53d6d34ac3 | 38 | const char *p = buff ; |
takashikojo | 0:6d53d6d34ac3 | 39 | p = XML_getTag(p, "item") ; p = XML_getTag(p, "item") ; p = XML_getTag(p, "link") ; |
takashikojo | 0:6d53d6d34ac3 | 40 | p = XML_getElement(p, link, LINK_SIZE) ; |
takashikojo | 0:6d53d6d34ac3 | 41 | } |
takashikojo | 0:6d53d6d34ac3 | 42 | |
takashikojo | 0:6d53d6d34ac3 | 43 | static const char *getTodayP(const char *p) { |
takashikojo | 0:6d53d6d34ac3 | 44 | return XML_getTag(p, "table") ; |
takashikojo | 0:6d53d6d34ac3 | 45 | } |
takashikojo | 0:6d53d6d34ac3 | 46 | |
takashikojo | 0:6d53d6d34ac3 | 47 | static const char *getTomorrowP(const char *p) { |
takashikojo | 0:6d53d6d34ac3 | 48 | p = XML_getTag(p, "table") ; p = XML_getTag(p, "table") ; |
takashikojo | 0:6d53d6d34ac3 | 49 | return XML_getTag(p, "table") ; |
takashikojo | 0:6d53d6d34ac3 | 50 | } |
takashikojo | 0:6d53d6d34ac3 | 51 | |
takashikojo | 0:6d53d6d34ac3 | 52 | #define TEMP_SIZE 5 |
takashikojo | 0:6d53d6d34ac3 | 53 | static const char *getHiTemp(const char *p, char *hi) |
takashikojo | 0:6d53d6d34ac3 | 54 | { |
takashikojo | 0:6d53d6d34ac3 | 55 | p = XML_getTag(p, "table") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; |
takashikojo | 0:6d53d6d34ac3 | 56 | p = XML_getTag(p, "span") ; |
takashikojo | 0:6d53d6d34ac3 | 57 | p = XML_getElement(p, hi, 2) ; |
takashikojo | 0:6d53d6d34ac3 | 58 | return p ; |
takashikojo | 0:6d53d6d34ac3 | 59 | } |
takashikojo | 0:6d53d6d34ac3 | 60 | |
takashikojo | 0:6d53d6d34ac3 | 61 | static const char *getLoTemp(const char *p, char *lo) |
takashikojo | 0:6d53d6d34ac3 | 62 | { |
takashikojo | 0:6d53d6d34ac3 | 63 | p = XML_getTag(p, "tr") ; p = XML_getTag(p, "tr") ; |
takashikojo | 0:6d53d6d34ac3 | 64 | p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; |
takashikojo | 0:6d53d6d34ac3 | 65 | p = XML_getTag(p, "span") ; |
takashikojo | 0:6d53d6d34ac3 | 66 | p = XML_getElement(p, lo, 2) ; |
takashikojo | 0:6d53d6d34ac3 | 67 | return p ; |
takashikojo | 0:6d53d6d34ac3 | 68 | } |
takashikojo | 0:6d53d6d34ac3 | 69 | |
takashikojo | 0:6d53d6d34ac3 | 70 | static const char *getPrec(const char *p, char *prec) |
takashikojo | 0:6d53d6d34ac3 | 71 | { |
takashikojo | 0:6d53d6d34ac3 | 72 | p = XML_getTag(p, "th") ; p = XML_getTag(p, "th") ; |
takashikojo | 0:6d53d6d34ac3 | 73 | p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; p = XML_getTag(p, "td") ; |
takashikojo | 0:6d53d6d34ac3 | 74 | p = XML_getElement(p, prec, 2) ; |
takashikojo | 1:8655f1720450 | 75 | if(strcmp(prec, "0%") == 0)strcpy(prec, " 0") ; |
takashikojo | 0:6d53d6d34ac3 | 76 | return p ; |
takashikojo | 0:6d53d6d34ac3 | 77 | } |
takashikojo | 0:6d53d6d34ac3 | 78 | |
takashikojo | 0:6d53d6d34ac3 | 79 | static void getInfo(const char *buff, char *hi, char *lo, char *prec) |
takashikojo | 0:6d53d6d34ac3 | 80 | { |
takashikojo | 0:6d53d6d34ac3 | 81 | #define JST (9*60*60) |
takashikojo | 0:6d53d6d34ac3 | 82 | struct tm t; |
takashikojo | 0:6d53d6d34ac3 | 83 | time_t ctTime; |
takashikojo | 0:6d53d6d34ac3 | 84 | ctTime = time(NULL) + JST ; |
takashikojo | 0:6d53d6d34ac3 | 85 | t = *localtime(&ctTime); |
takashikojo | 3:6618936f930a | 86 | TERMINAL("Time: % 2d:%02d => ", t.tm_hour, t.tm_min) ; |
takashikojo | 0:6d53d6d34ac3 | 87 | const char *p ; |
takashikojo | 0:6d53d6d34ac3 | 88 | |
takashikojo | 0:6d53d6d34ac3 | 89 | if(t.tm_hour >= 12) { |
takashikojo | 0:6d53d6d34ac3 | 90 | p = getTomorrowP(buff) ; |
takashikojo | 0:6d53d6d34ac3 | 91 | } else { |
takashikojo | 0:6d53d6d34ac3 | 92 | p = getTodayP(buff) ; |
takashikojo | 0:6d53d6d34ac3 | 93 | } |
takashikojo | 0:6d53d6d34ac3 | 94 | p = getHiTemp(p, hi) ; |
takashikojo | 0:6d53d6d34ac3 | 95 | p = getLoTemp(p, lo) ; |
takashikojo | 0:6d53d6d34ac3 | 96 | p = getPrec(p, prec) ; |
takashikojo | 3:6618936f930a | 97 | TERMINAL("Hi=%s, Lo=%s, Prec=%s\n", hi, lo, prec) ; |
takashikojo | 0:6d53d6d34ac3 | 98 | } |
takashikojo | 0:6d53d6d34ac3 | 99 | |
takashikojo | 0:6d53d6d34ac3 | 100 | #define FREE(p) if(p)free(p) |
takashikojo | 0:6d53d6d34ac3 | 101 | #define BUFF_SIZE 1024*12 |
takashikojo | 0:6d53d6d34ac3 | 102 | |
takashikojo | 2:76b812680942 | 103 | void WH_getInfo(char *buff, int size, char *hiTemp, char *loTemp, char *prec) |
takashikojo | 0:6d53d6d34ac3 | 104 | { |
takashikojo | 0:6d53d6d34ac3 | 105 | int ret ; |
takashikojo | 1:8655f1720450 | 106 | char link[LINK_SIZE] ; |
takashikojo | 2:76b812680942 | 107 | |
takashikojo | 1:8655f1720450 | 108 | ret = httpClient.get(RSS_URL, buff, size); |
takashikojo | 0:6d53d6d34ac3 | 109 | if (!ret) { |
takashikojo | 3:6618936f930a | 110 | DBG("Result: %s\n", buff); |
takashikojo | 0:6d53d6d34ac3 | 111 | } else { |
takashikojo | 2:76b812680942 | 112 | ERR("Error %s\n- ret = %d - HTTP return code = %d\n", RSS_URL, ret, httpClient.getHTTPResponseCode()); |
takashikojo | 0:6d53d6d34ac3 | 113 | } |
takashikojo | 1:8655f1720450 | 114 | |
takashikojo | 3:6618936f930a | 115 | getLink(buff, link) ; DBG("link=%s\n", link) ; |
takashikojo | 1:8655f1720450 | 116 | ret = httpClient.get(link, buff, size) ; |
takashikojo | 2:76b812680942 | 117 | if (!ret) { |
takashikojo | 3:6618936f930a | 118 | DBG("Result: %s\n", buff) ; |
takashikojo | 2:76b812680942 | 119 | } else { |
takashikojo | 2:76b812680942 | 120 | ERR("Error %s\n- ret = %d - HTTP return code = %d\n", link, ret, httpClient.getHTTPResponseCode()); |
takashikojo | 2:76b812680942 | 121 | } |
takashikojo | 1:8655f1720450 | 122 | getInfo(buff, hiTemp, loTemp, prec); |
takashikojo | 2:76b812680942 | 123 | |
takashikojo | 0:6d53d6d34ac3 | 124 | } |