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
- Committer:
- okini3939
- Date:
- 2011-03-19
- Revision:
- 13:20c0f845df68
- Parent:
- 10:848a2205aed1
- Child:
- 14:ee6cc1632166
File content as of revision 13:20c0f845df68:
#include "mbed.h" #include "ConfigFile.h" #include "SDFileSystem.h" #include "EthernetNetIf.h" #include "NTPClient.h" #include "HTTPClient.h" #include "conf.h" #define TIMEZONE 9 //#define NONBLOCKING /** @file * @brief mbed Weather Platform */ #define STATION_URL "http://weather.sugakoubou.com/p" #define TWITTER_URL "http://api.supertweet.net/1/statuses/update.xml" extern Serial pc; extern int seq; extern char filename[]; extern Config conf; extern DigitalOut led_y, led2; extern EthernetNetIf *eth; extern NTPClient *ntp; extern HTTPClient *clientP, *clientT; extern float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2; void writefile (char *buf) { FILE *fp; fp = fopen(filename, "a"); if (fp) { fprintf(fp, buf); fclose(fp); } else { led2 = 0; conf.filetype = 0; } } void weatherstations () { char post_data[200]; HTTPResult ret; HTTPText postContent("application/x-www-form-urlencoded"); led_y = 0; strcpy(post_data, "fcd="); strcat(post_data, conf.stations_id); strcat(post_data, "&pin="); strcat(post_data, conf.stations_pin); 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", pres, temp, humi, anemo, vane, rain, light, uv, moist, temp2); postContent.puts(post_data); #ifdef NONBLOCKING Net::poll(); ret = clientP->post(STATION_URL, postContent, NULL, &cb_clientP); Net::poll(); #else ret = clientP->post(STATION_URL, postContent, NULL); #endif if (ret != HTTP_OK && ret != HTTP_PROCESSING) { pc.printf("error: Weather Statuons %d\r\n", ret); } } void pachube (char *buf) { char uri[100]; HTTPResult ret; HTTPText csvContent("text/csv"); led_y = 0; clientP->setRequestHeader("X-PachubeApiKey", conf.pachube_apikey); csvContent.set(buf); strcpy(uri, "http://api.pachube.com/v1/feeds/"); strcat(uri, conf.pachube_feedid); strcat(uri, ".csv?_method=put"); #ifdef NONBLOCKING Net::poll(); ret = clientP->post(uri, csvContent, NULL, &cb_clientP); Net::poll(); #else ret = clientP->post(uri, csvContent, NULL); #endif if (ret != HTTP_OK && ret != HTTP_PROCESSING) { pc.printf("error: Pachube %d\r\n", ret); } } void cb_clientP (HTTPResult status) { if (status != HTTP_OK) { pc.printf("Pachube failure (%d)\r\n", status); // pc.printf("Pachube failure (%d, %d)\r\n", status, clientP->getHTTPResponseCode()); } } char *fmtstr (char *fmt, char *buf, int len) { int i, j, flg; char c; float value; time_t sec = time(NULL); struct tm *tim = localtime(&sec); flg = 0; j = 0; for (i = 0; i < strlen(fmt) && j < len; i ++) { c = fmt[i]; if (c == '%') { i ++; c = fmt[i]; switch (c) { case 'P': value = pres; break; case 'T': value = temp; break; case 'H': value = humi; break; case 'A': value = anemo; break; case 'V': value = vane; break; case 'R': value = rain; break; case 'L': value = light; break; case 'U': value = uv; break; case 'y': value = tim->tm_year + 1900; break; case 'm': value = tim->tm_mon; break; case 'd': value = tim->tm_mday; break; case 'h': value = tim->tm_hour; break; case 'i': value = tim->tm_min; break; case 's': value = tim->tm_sec; break; case '%': buf[j] = c; j ++; continue; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': c = c - '0'; if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { c = (c << 3) | (fmt[i + 1] - '0'); i ++; if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { c = (c << 3) | (fmt[i + 1] - '0'); i ++; } } buf[j] = c; j ++; continue; case '.': if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { flg = fmt[i + 1] - '0'; i ++; } continue; default: continue; } if (c < 'a' && flg == 1) { sprintf(&buf[j], "%.1f", value); } else if (c < 'a' && flg == 2) { sprintf(&buf[j], "%.2f", value); } else { sprintf(&buf[j], "%02d", (int)value); } j = strlen(buf); } else { buf[j] = c; j ++; } } buf[j] = 0; return buf; } void twitter () { HTTPMap msg; HTTPResult ret; char buf[170]; led_y = 0; fmtstr(conf.twitter_mesg, buf, sizeof(buf)); msg["status"] = buf; clientT->basicAuth(conf.twitter_user, conf.twitter_pwd); #ifdef NONBLOCKING Net::poll(); ret = clientT->post(TWITTER_URL, msg, NULL, &cb_clientT); Net::poll(); #else ret = clientT->post(TWITTER_URL, msg, NULL); #endif if (ret != HTTP_OK && ret != HTTP_PROCESSING) { pc.printf("error: Pachube %d\r\n", ret); } } void ntpdate () { ntp = new NTPClient; Host ntpserver(IpAddr(), 123, conf.ntpserver); led_y = 0; #ifdef NONBLOCKING Net::poll(); ntp->setTime(ntpserver, &cb_settime); Net::poll(); #else ntp->setTime(ntpserver); time_t sec = time(NULL) + (60 * 60 * TIMEZONE); set_time(sec); #endif } void cb_settime (NTPResult status) { if (status == NTP_OK) { led_y = 0; time_t sec = time(NULL) + (60 * 60 * TIMEZONE); set_time(sec); pc.printf("Ntp success: %s\r\n", ctime(&sec)); } else { pc.printf("Ntp failure (%d)\r\n", status); } // ntp->close(); }