mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/
Dependencies: EthernetNetIf SDHCFileSystem I2CLEDDisp Agentbed NTPClient_NetServices mbed BMP085 HTTPClient ConfigFile I2CLCD
func.cpp@0:d7b4484099bf, 2011-05-18 (annotated)
- Committer:
- okini3939
- Date:
- Wed May 18 15:01:56 2011 +0000
- Revision:
- 0:d7b4484099bf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:d7b4484099bf | 1 | #include "mbed.h" |
okini3939 | 0:d7b4484099bf | 2 | #include "ConfigFile.h" |
okini3939 | 0:d7b4484099bf | 3 | #include "SDHCFileSystem.h" |
okini3939 | 0:d7b4484099bf | 4 | #include "EthernetNetIf.h" |
okini3939 | 0:d7b4484099bf | 5 | #include "NTPClient.h" |
okini3939 | 0:d7b4484099bf | 6 | #include "HTTPClient.h" |
okini3939 | 0:d7b4484099bf | 7 | #include "conf.h" |
okini3939 | 0:d7b4484099bf | 8 | |
okini3939 | 0:d7b4484099bf | 9 | #define TIMEZONE 9 |
okini3939 | 0:d7b4484099bf | 10 | //#define NONBLOCKING |
okini3939 | 0:d7b4484099bf | 11 | /** @file |
okini3939 | 0:d7b4484099bf | 12 | * @brief mbed Weather Platform |
okini3939 | 0:d7b4484099bf | 13 | */ |
okini3939 | 0:d7b4484099bf | 14 | |
okini3939 | 0:d7b4484099bf | 15 | #define STATION_URL "http://weather.sugakoubou.com/p" |
okini3939 | 0:d7b4484099bf | 16 | #define TWITTER_URL "http://api.supertweet.net/1/statuses/update.xml" |
okini3939 | 0:d7b4484099bf | 17 | |
okini3939 | 0:d7b4484099bf | 18 | extern Serial pc; |
okini3939 | 0:d7b4484099bf | 19 | extern int seq; |
okini3939 | 0:d7b4484099bf | 20 | extern char filename[]; |
okini3939 | 0:d7b4484099bf | 21 | extern Config conf; |
okini3939 | 0:d7b4484099bf | 22 | extern DigitalOut led_y, led2; |
okini3939 | 0:d7b4484099bf | 23 | |
okini3939 | 0:d7b4484099bf | 24 | extern EthernetNetIf *eth; |
okini3939 | 0:d7b4484099bf | 25 | extern NTPClient *ntp; |
okini3939 | 0:d7b4484099bf | 26 | extern HTTPClient *clientP, *clientT; |
okini3939 | 0:d7b4484099bf | 27 | extern Sensor sensor; |
okini3939 | 0:d7b4484099bf | 28 | |
okini3939 | 0:d7b4484099bf | 29 | void writefile (char *buf) { |
okini3939 | 0:d7b4484099bf | 30 | FILE *fp; |
okini3939 | 0:d7b4484099bf | 31 | |
okini3939 | 0:d7b4484099bf | 32 | fp = fopen(filename, "a"); |
okini3939 | 0:d7b4484099bf | 33 | if (fp) { |
okini3939 | 0:d7b4484099bf | 34 | fprintf(fp, buf); |
okini3939 | 0:d7b4484099bf | 35 | fclose(fp); |
okini3939 | 0:d7b4484099bf | 36 | } else { |
okini3939 | 0:d7b4484099bf | 37 | led2 = 0; |
okini3939 | 0:d7b4484099bf | 38 | conf.filetype = 0; |
okini3939 | 0:d7b4484099bf | 39 | } |
okini3939 | 0:d7b4484099bf | 40 | } |
okini3939 | 0:d7b4484099bf | 41 | |
okini3939 | 0:d7b4484099bf | 42 | void weatherstations () { |
okini3939 | 0:d7b4484099bf | 43 | char post_data[200]; |
okini3939 | 0:d7b4484099bf | 44 | HTTPResult ret; |
okini3939 | 0:d7b4484099bf | 45 | HTTPText postContent("application/x-www-form-urlencoded"); |
okini3939 | 0:d7b4484099bf | 46 | |
okini3939 | 0:d7b4484099bf | 47 | led_y = 0; |
okini3939 | 0:d7b4484099bf | 48 | strcpy(post_data, "fcd="); |
okini3939 | 0:d7b4484099bf | 49 | strcat(post_data, conf.stations_id); |
okini3939 | 0:d7b4484099bf | 50 | strcat(post_data, "&pin="); |
okini3939 | 0:d7b4484099bf | 51 | strcat(post_data, conf.stations_pin); |
okini3939 | 0:d7b4484099bf | 52 | sprintf(&post_data[strlen(post_data)], "&d0=%.2f&d1=%.2f&d2=%.2f&d3=%.2f&d4=%.2f&d5=%.2f&d6=%.2f&d7=%.2f&d8=%.2f&d9=%.2f", |
okini3939 | 0:d7b4484099bf | 53 | sensor.pres, sensor.temp, sensor.humi, sensor.anemo, sensor.vane, sensor.rain, sensor.light, sensor.uv, sensor.moist, sensor.temp2); |
okini3939 | 0:d7b4484099bf | 54 | postContent.puts(post_data); |
okini3939 | 0:d7b4484099bf | 55 | |
okini3939 | 0:d7b4484099bf | 56 | #ifdef NONBLOCKING |
okini3939 | 0:d7b4484099bf | 57 | Net::poll(); |
okini3939 | 0:d7b4484099bf | 58 | ret = clientP->post(STATION_URL, postContent, NULL, &cb_clientP); |
okini3939 | 0:d7b4484099bf | 59 | Net::poll(); |
okini3939 | 0:d7b4484099bf | 60 | #else |
okini3939 | 0:d7b4484099bf | 61 | ret = clientP->post(STATION_URL, postContent, NULL); |
okini3939 | 0:d7b4484099bf | 62 | #endif |
okini3939 | 0:d7b4484099bf | 63 | |
okini3939 | 0:d7b4484099bf | 64 | if (ret != HTTP_OK && ret != HTTP_PROCESSING) { |
okini3939 | 0:d7b4484099bf | 65 | pc.printf("error: Weather Statuons %d\r\n", ret); |
okini3939 | 0:d7b4484099bf | 66 | } |
okini3939 | 0:d7b4484099bf | 67 | } |
okini3939 | 0:d7b4484099bf | 68 | |
okini3939 | 0:d7b4484099bf | 69 | void pachube (char *buf) { |
okini3939 | 0:d7b4484099bf | 70 | char uri[100]; |
okini3939 | 0:d7b4484099bf | 71 | HTTPResult ret; |
okini3939 | 0:d7b4484099bf | 72 | HTTPText csvContent("text/csv"); |
okini3939 | 0:d7b4484099bf | 73 | |
okini3939 | 0:d7b4484099bf | 74 | led_y = 0; |
okini3939 | 0:d7b4484099bf | 75 | clientP->setRequestHeader("X-PachubeApiKey", conf.pachube_apikey); |
okini3939 | 0:d7b4484099bf | 76 | csvContent.set(buf); |
okini3939 | 0:d7b4484099bf | 77 | strcpy(uri, "http://api.pachube.com/v1/feeds/"); |
okini3939 | 0:d7b4484099bf | 78 | strcat(uri, conf.pachube_feedid); |
okini3939 | 0:d7b4484099bf | 79 | strcat(uri, ".csv?_method=put"); |
okini3939 | 0:d7b4484099bf | 80 | #ifdef NONBLOCKING |
okini3939 | 0:d7b4484099bf | 81 | Net::poll(); |
okini3939 | 0:d7b4484099bf | 82 | ret = clientP->post(uri, csvContent, NULL, &cb_clientP); |
okini3939 | 0:d7b4484099bf | 83 | Net::poll(); |
okini3939 | 0:d7b4484099bf | 84 | #else |
okini3939 | 0:d7b4484099bf | 85 | ret = clientP->post(uri, csvContent, NULL); |
okini3939 | 0:d7b4484099bf | 86 | #endif |
okini3939 | 0:d7b4484099bf | 87 | |
okini3939 | 0:d7b4484099bf | 88 | if (ret != HTTP_OK && ret != HTTP_PROCESSING) { |
okini3939 | 0:d7b4484099bf | 89 | pc.printf("error: Pachube %d\r\n", ret); |
okini3939 | 0:d7b4484099bf | 90 | } |
okini3939 | 0:d7b4484099bf | 91 | } |
okini3939 | 0:d7b4484099bf | 92 | |
okini3939 | 0:d7b4484099bf | 93 | void cb_clientP (HTTPResult status) { |
okini3939 | 0:d7b4484099bf | 94 | if (status != HTTP_OK) { |
okini3939 | 0:d7b4484099bf | 95 | pc.printf("Pachube failure (%d)\r\n", status); |
okini3939 | 0:d7b4484099bf | 96 | // pc.printf("Pachube failure (%d, %d)\r\n", status, clientP->getHTTPResponseCode()); |
okini3939 | 0:d7b4484099bf | 97 | } |
okini3939 | 0:d7b4484099bf | 98 | } |
okini3939 | 0:d7b4484099bf | 99 | |
okini3939 | 0:d7b4484099bf | 100 | char *fmtstr (char *fmt, char *buf, int len) { |
okini3939 | 0:d7b4484099bf | 101 | int i, j, flg; |
okini3939 | 0:d7b4484099bf | 102 | char c; |
okini3939 | 0:d7b4484099bf | 103 | float value; |
okini3939 | 0:d7b4484099bf | 104 | time_t sec = time(NULL); |
okini3939 | 0:d7b4484099bf | 105 | struct tm *tim = localtime(&sec); |
okini3939 | 0:d7b4484099bf | 106 | |
okini3939 | 0:d7b4484099bf | 107 | j = 0; |
okini3939 | 0:d7b4484099bf | 108 | for (i = 0; i < strlen(fmt) && j < len; i ++) { |
okini3939 | 0:d7b4484099bf | 109 | c = fmt[i]; |
okini3939 | 0:d7b4484099bf | 110 | if (c == '%') { |
okini3939 | 0:d7b4484099bf | 111 | flg = 0; |
okini3939 | 0:d7b4484099bf | 112 | i ++; |
okini3939 | 0:d7b4484099bf | 113 | c = fmt[i]; |
okini3939 | 0:d7b4484099bf | 114 | |
okini3939 | 0:d7b4484099bf | 115 | if (c == '.') { |
okini3939 | 0:d7b4484099bf | 116 | // float format |
okini3939 | 0:d7b4484099bf | 117 | if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { |
okini3939 | 0:d7b4484099bf | 118 | flg = fmt[i + 1] - '0'; |
okini3939 | 0:d7b4484099bf | 119 | i ++; |
okini3939 | 0:d7b4484099bf | 120 | |
okini3939 | 0:d7b4484099bf | 121 | c = fmt[i + 1]; |
okini3939 | 0:d7b4484099bf | 122 | i ++; |
okini3939 | 0:d7b4484099bf | 123 | } |
okini3939 | 0:d7b4484099bf | 124 | } |
okini3939 | 0:d7b4484099bf | 125 | |
okini3939 | 0:d7b4484099bf | 126 | switch (c) { |
okini3939 | 0:d7b4484099bf | 127 | case 'P': |
okini3939 | 0:d7b4484099bf | 128 | value = sensor.pres; |
okini3939 | 0:d7b4484099bf | 129 | break; |
okini3939 | 0:d7b4484099bf | 130 | case 'T': |
okini3939 | 0:d7b4484099bf | 131 | value = sensor.temp; |
okini3939 | 0:d7b4484099bf | 132 | break; |
okini3939 | 0:d7b4484099bf | 133 | case 'H': |
okini3939 | 0:d7b4484099bf | 134 | value = sensor.humi; |
okini3939 | 0:d7b4484099bf | 135 | break; |
okini3939 | 0:d7b4484099bf | 136 | case 'A': |
okini3939 | 0:d7b4484099bf | 137 | value = sensor.anemo; |
okini3939 | 0:d7b4484099bf | 138 | break; |
okini3939 | 0:d7b4484099bf | 139 | case 'V': |
okini3939 | 0:d7b4484099bf | 140 | value = sensor.vane; |
okini3939 | 0:d7b4484099bf | 141 | break; |
okini3939 | 0:d7b4484099bf | 142 | case 'R': |
okini3939 | 0:d7b4484099bf | 143 | value = sensor.rain; |
okini3939 | 0:d7b4484099bf | 144 | break; |
okini3939 | 0:d7b4484099bf | 145 | case 'L': |
okini3939 | 0:d7b4484099bf | 146 | value = sensor.light; |
okini3939 | 0:d7b4484099bf | 147 | break; |
okini3939 | 0:d7b4484099bf | 148 | case 'U': |
okini3939 | 0:d7b4484099bf | 149 | value = sensor.uv; |
okini3939 | 0:d7b4484099bf | 150 | break; |
okini3939 | 0:d7b4484099bf | 151 | case 'M': |
okini3939 | 0:d7b4484099bf | 152 | value = sensor.moist; |
okini3939 | 0:d7b4484099bf | 153 | break; |
okini3939 | 0:d7b4484099bf | 154 | |
okini3939 | 0:d7b4484099bf | 155 | case 'y': |
okini3939 | 0:d7b4484099bf | 156 | value = tim->tm_year + 1900; |
okini3939 | 0:d7b4484099bf | 157 | break; |
okini3939 | 0:d7b4484099bf | 158 | case 'm': |
okini3939 | 0:d7b4484099bf | 159 | value = tim->tm_mon; |
okini3939 | 0:d7b4484099bf | 160 | break; |
okini3939 | 0:d7b4484099bf | 161 | case 'd': |
okini3939 | 0:d7b4484099bf | 162 | value = tim->tm_mday; |
okini3939 | 0:d7b4484099bf | 163 | break; |
okini3939 | 0:d7b4484099bf | 164 | case 'h': |
okini3939 | 0:d7b4484099bf | 165 | value = tim->tm_hour; |
okini3939 | 0:d7b4484099bf | 166 | break; |
okini3939 | 0:d7b4484099bf | 167 | case 'i': |
okini3939 | 0:d7b4484099bf | 168 | value = tim->tm_min; |
okini3939 | 0:d7b4484099bf | 169 | break; |
okini3939 | 0:d7b4484099bf | 170 | case 's': |
okini3939 | 0:d7b4484099bf | 171 | value = tim->tm_sec; |
okini3939 | 0:d7b4484099bf | 172 | break; |
okini3939 | 0:d7b4484099bf | 173 | |
okini3939 | 0:d7b4484099bf | 174 | case '0': |
okini3939 | 0:d7b4484099bf | 175 | case '1': |
okini3939 | 0:d7b4484099bf | 176 | case '2': |
okini3939 | 0:d7b4484099bf | 177 | case '3': |
okini3939 | 0:d7b4484099bf | 178 | case '4': |
okini3939 | 0:d7b4484099bf | 179 | case '5': |
okini3939 | 0:d7b4484099bf | 180 | case '6': |
okini3939 | 0:d7b4484099bf | 181 | case '7': |
okini3939 | 0:d7b4484099bf | 182 | case '8': |
okini3939 | 0:d7b4484099bf | 183 | case '9': |
okini3939 | 0:d7b4484099bf | 184 | // Ascii |
okini3939 | 0:d7b4484099bf | 185 | c = c - '0'; |
okini3939 | 0:d7b4484099bf | 186 | if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { |
okini3939 | 0:d7b4484099bf | 187 | c = (c << 3) | (fmt[i + 1] - '0'); |
okini3939 | 0:d7b4484099bf | 188 | i ++; |
okini3939 | 0:d7b4484099bf | 189 | if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { |
okini3939 | 0:d7b4484099bf | 190 | c = (c << 3) | (fmt[i + 1] - '0'); |
okini3939 | 0:d7b4484099bf | 191 | i ++; |
okini3939 | 0:d7b4484099bf | 192 | } |
okini3939 | 0:d7b4484099bf | 193 | } |
okini3939 | 0:d7b4484099bf | 194 | buf[j] = c; |
okini3939 | 0:d7b4484099bf | 195 | j ++; |
okini3939 | 0:d7b4484099bf | 196 | continue; |
okini3939 | 0:d7b4484099bf | 197 | |
okini3939 | 0:d7b4484099bf | 198 | default: |
okini3939 | 0:d7b4484099bf | 199 | buf[j] = c; |
okini3939 | 0:d7b4484099bf | 200 | j ++; |
okini3939 | 0:d7b4484099bf | 201 | continue; |
okini3939 | 0:d7b4484099bf | 202 | } |
okini3939 | 0:d7b4484099bf | 203 | |
okini3939 | 0:d7b4484099bf | 204 | switch (flg) { |
okini3939 | 0:d7b4484099bf | 205 | case 1: |
okini3939 | 0:d7b4484099bf | 206 | sprintf(&buf[j], "%.1f", value); |
okini3939 | 0:d7b4484099bf | 207 | break; |
okini3939 | 0:d7b4484099bf | 208 | case 2: |
okini3939 | 0:d7b4484099bf | 209 | sprintf(&buf[j], "%.2f", value); |
okini3939 | 0:d7b4484099bf | 210 | break; |
okini3939 | 0:d7b4484099bf | 211 | default: |
okini3939 | 0:d7b4484099bf | 212 | sprintf(&buf[j], "%02d", (int)value); |
okini3939 | 0:d7b4484099bf | 213 | break; |
okini3939 | 0:d7b4484099bf | 214 | } |
okini3939 | 0:d7b4484099bf | 215 | j = strlen(buf); |
okini3939 | 0:d7b4484099bf | 216 | } else { |
okini3939 | 0:d7b4484099bf | 217 | buf[j] = c; |
okini3939 | 0:d7b4484099bf | 218 | j ++; |
okini3939 | 0:d7b4484099bf | 219 | } |
okini3939 | 0:d7b4484099bf | 220 | } |
okini3939 | 0:d7b4484099bf | 221 | buf[j] = 0; |
okini3939 | 0:d7b4484099bf | 222 | return buf; |
okini3939 | 0:d7b4484099bf | 223 | } |
okini3939 | 0:d7b4484099bf | 224 | |
okini3939 | 0:d7b4484099bf | 225 | void twitter (int num) { |
okini3939 | 0:d7b4484099bf | 226 | HTTPMap msg; |
okini3939 | 0:d7b4484099bf | 227 | HTTPResult ret; |
okini3939 | 0:d7b4484099bf | 228 | char buf[170]; |
okini3939 | 0:d7b4484099bf | 229 | |
okini3939 | 0:d7b4484099bf | 230 | led_y = 0; |
okini3939 | 0:d7b4484099bf | 231 | fmtstr(conf.twitter_mesg[num], buf, sizeof(buf)); |
okini3939 | 0:d7b4484099bf | 232 | msg["status"] = buf; |
okini3939 | 0:d7b4484099bf | 233 | |
okini3939 | 0:d7b4484099bf | 234 | clientT->basicAuth(conf.twitter_user, conf.twitter_pwd); |
okini3939 | 0:d7b4484099bf | 235 | #ifdef NONBLOCKING |
okini3939 | 0:d7b4484099bf | 236 | Net::poll(); |
okini3939 | 0:d7b4484099bf | 237 | ret = clientT->post(TWITTER_URL, msg, NULL, &cb_clientT); |
okini3939 | 0:d7b4484099bf | 238 | Net::poll(); |
okini3939 | 0:d7b4484099bf | 239 | #else |
okini3939 | 0:d7b4484099bf | 240 | ret = clientT->post(TWITTER_URL, msg, NULL); |
okini3939 | 0:d7b4484099bf | 241 | #endif |
okini3939 | 0:d7b4484099bf | 242 | |
okini3939 | 0:d7b4484099bf | 243 | if (ret != HTTP_OK && ret != HTTP_PROCESSING) { |
okini3939 | 0:d7b4484099bf | 244 | pc.printf("error: Twitter %d\r\n", ret); |
okini3939 | 0:d7b4484099bf | 245 | } |
okini3939 | 0:d7b4484099bf | 246 | } |
okini3939 | 0:d7b4484099bf | 247 | |
okini3939 | 0:d7b4484099bf | 248 | void ntpdate () { |
okini3939 | 0:d7b4484099bf | 249 | ntp = new NTPClient; |
okini3939 | 0:d7b4484099bf | 250 | Host ntpserver(IpAddr(), 123, conf.ntpserver); |
okini3939 | 0:d7b4484099bf | 251 | |
okini3939 | 0:d7b4484099bf | 252 | led_y = 0; |
okini3939 | 0:d7b4484099bf | 253 | #ifdef NONBLOCKING |
okini3939 | 0:d7b4484099bf | 254 | Net::poll(); |
okini3939 | 0:d7b4484099bf | 255 | ntp->setTime(ntpserver, &cb_settime); |
okini3939 | 0:d7b4484099bf | 256 | Net::poll(); |
okini3939 | 0:d7b4484099bf | 257 | #else |
okini3939 | 0:d7b4484099bf | 258 | ntp->setTime(ntpserver); |
okini3939 | 0:d7b4484099bf | 259 | time_t sec = time(NULL) + (60 * 60 * TIMEZONE); |
okini3939 | 0:d7b4484099bf | 260 | set_time(sec); |
okini3939 | 0:d7b4484099bf | 261 | #endif |
okini3939 | 0:d7b4484099bf | 262 | } |
okini3939 | 0:d7b4484099bf | 263 | |
okini3939 | 0:d7b4484099bf | 264 | void cb_settime (NTPResult status) { |
okini3939 | 0:d7b4484099bf | 265 | if (status == NTP_OK) { |
okini3939 | 0:d7b4484099bf | 266 | led_y = 0; |
okini3939 | 0:d7b4484099bf | 267 | time_t sec = time(NULL) + (60 * 60 * TIMEZONE); |
okini3939 | 0:d7b4484099bf | 268 | set_time(sec); |
okini3939 | 0:d7b4484099bf | 269 | pc.printf("Ntp success: %s\r\n", ctime(&sec)); |
okini3939 | 0:d7b4484099bf | 270 | } else { |
okini3939 | 0:d7b4484099bf | 271 | pc.printf("Ntp failure (%d)\r\n", status); |
okini3939 | 0:d7b4484099bf | 272 | } |
okini3939 | 0:d7b4484099bf | 273 | // ntp->close(); |
okini3939 | 0:d7b4484099bf | 274 | } |