mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/
Dependencies: ChaNFSSD EthernetNetIf I2CLEDDisp Agentbed ChaNFSUSB ILinterpreter mbed BMP085 WeatherMeters ConfigFile ChaNFS I2CLCD
Diff: net.cpp
- Revision:
- 3:058292da2cee
- Parent:
- 2:a3e5edf84f74
- Child:
- 7:f7c98d836f81
--- a/net.cpp Wed Aug 24 13:22:32 2011 +0000 +++ b/net.cpp Wed Sep 07 16:03:54 2011 +0000 @@ -12,35 +12,26 @@ #include "weather.h" #include "EthernetNetIf.h" #ifdef USE_NTP -//#include "NTPClient.h" #include "TinySNTP.h" #endif #ifdef USE_HTTP -#include "HTTPClient.h" +#include "TinyHTTP.h" #endif #ifdef USE_EMAIL -#include "SMTPClient.h" +#include "TinySMTP.h" #endif - -#define STATIONS_URL "http://weather.sugakoubou.com/p" -#define TWITTER_URL "http://api.supertweet.net/1/statuses/update.xml" -#define PACHUBE_URL "http://api.pachube.com/v1/feeds/" +#include "TCPSocket.h" EthernetNetIf *eth; DigitalOut led_gayk(p24),led_gkya(p25), led_yk(p26); static DigitalIn eth_link(P1_25), eth_speed(P1_26); static volatile int ethernet_flg = 0; -#ifdef USE_HTTP -//static HTTPClient *http; -#endif -//static NTPClient *ntp; int weatherstations () { #ifdef USE_HTTP - char post_data[150]; - HTTPClient http; - HTTPResult ret; - HTTPText postContent("application/x-www-form-urlencoded"); + Host host; + char buf[FORMAT_STR_SIZE]; + char head[80]; if (! ethernet_flg) return 0; if (! conf.stations_id[0] || ! conf.stations_pin[0]) { @@ -48,39 +39,30 @@ } LED_NET_ACT_ON; - format_str("d0=%.2P&d1=%.2T&d2=%.2H&d3=%.2A&d4=%.2V&d5=%.2R&d6=%.2L&d7=%.2U&d8=%.2M&d9=%.2p", post_data, sizeof_1(post_data)); - strncat(post_data, "&fcd=", sizeof_len(post_data)); - strncat(post_data, conf.stations_id, sizeof_len(post_data)); - strncat(post_data, "&pin=", sizeof_len(post_data)); - strncat(post_data, conf.stations_pin, sizeof_len(post_data)); - postContent.puts(post_data); + + format_str("d0=%.2P&d1=%.2T&d2=%.2H&d3=%.2A&d4=%.2V&d5=%.2R&d6=%.2L&d7=%.2U&d8=%.2M&d9=%.2p", buf, sizeof_1(buf)); + snprintf(&buf[strlen(buf)], sizeof_len(buf), + "&fcd=%s&pin=%s", conf.stations_id, conf.stations_pin); #ifdef DEBUG - pc.printf("S: %s %s\r\n%s\r\n", conf.stations_id, conf.stations_pin, post_data); + pc.printf("S: %s %s\r\n%s\r\n", conf.stations_id, conf.stations_pin, buf); #endif // http->resetRequestHeaders(); // http->basicAuth(NULL, NULL); - http.setTimeout(NET_TIMEOUT); - ret = http.post(STATIONS_URL, postContent, NULL); + + strncpy(head, "Content-type: application/x-www-form-urlencoded\r\n", sizeof_1(head)); - if (ret != HTTP_OK && ret != HTTP_PROCESSING) { - pc.printf("Weather Statuons failure: %d\r\n", ret); - return -1; -#ifdef DEBUG - } else { - pc.printf("Weather Stations success: %d\r\n", ret); + host.setName("weather.sugakoubou.com"); + host.setPort(HTTP_PORT); + return httpRequest(METHOD_POST, &host, "/p", head, buf) == 200 ? 0 : -1; #endif - } -#endif - return 0; } int pachube () { #ifdef USE_HTTP - char buf[100], uri[100]; - HTTPClient http; - HTTPResult ret; - HTTPText csvContent("text/csv"); + Host host; + char buf[FORMAT_STR_SIZE]; + char uri[40], head[160]; if (! ethernet_flg) return 0; if (! conf.pachube_apikey[0] || ! conf.pachube_feedid[0]) { @@ -88,38 +70,28 @@ } LED_NET_ACT_ON; + // body format_str(conf.pachube_mesg, buf, sizeof_1(buf)); - csvContent.set(buf); - - snprintf(uri, sizeof(uri), PACHUBE_URL "%s.csv?_method=put", conf.pachube_feedid); - + // header + snprintf(head, sizeof(head), "Content-type: text/csv\r\nX-PachubeApiKey: %s\r\n", conf.pachube_apikey); + // uri + snprintf(uri, sizeof(uri), "/v1/feeds/%s.csv?_method=put", conf.pachube_feedid); #ifdef DEBUG pc.printf("P: %s %s %s\r\n", conf.pachube_apikey, conf.pachube_feedid, uri); #endif -// http->resetRequestHeaders(); -// http->basicAuth(NULL, NULL); - http.setTimeout(NET_TIMEOUT); - http.setRequestHeader("X-PachubeApiKey", conf.pachube_apikey); - ret = http.post(uri, csvContent, NULL); - if (ret != HTTP_OK && ret != HTTP_PROCESSING) { - pc.printf("Pachube failure: %d\r\n", ret); - return -1; -#ifdef DEBUG - } else { - pc.printf("Pachube success: %d\r\n", ret); + host.setName("api.pachube.com"); + host.setPort(HTTP_PORT); + return httpRequest(METHOD_POST, &host, uri, head, buf) == 200 ? 0 : -1; #endif - } -#endif - return 0; } int twitter (int num) { #ifdef USE_HTTP + Host host; char buf[FORMAT_STR_SIZE]; - HTTPClient http; - HTTPMap msg; - HTTPResult ret; + char msg[FORMAT_STR_SIZE]; + char head[160]; if (! ethernet_flg || num >= CF_TWITTER_NUM) return 0; if (! conf.twitter_user[0] || ! conf.twitter_pwd[0] || ! conf.twitter_mesg[num][0]) { @@ -127,36 +99,27 @@ } LED_NET_ACT_ON; - format_str(conf.twitter_mesg[num], buf, sizeof_1(buf)); - msg["status"] = buf; - + // header + createauth(conf.twitter_user, conf.twitter_pwd, head, sizeof(head)); + strncat(head, "Content-type: application/x-www-form-urlencoded\r\n", sizeof_len(head)); + // post data + format_str(conf.twitter_mesg[num], msg, sizeof_1(msg)); + strcpy(buf, "status="); + urlencode(msg, &buf[strlen(buf)], sizeof_len(buf)); #ifdef DEBUG pc.printf("T: %s %s %s\r\n%s\r\n", conf.twitter_user, conf.twitter_pwd, conf.twitter_mesg[num], buf); #endif -// http->resetRequestHeaders(); - http.setTimeout(NET_TIMEOUT); - http.basicAuth(conf.twitter_user, conf.twitter_pwd); - ret = http.post(TWITTER_URL, msg, NULL); - if (ret != HTTP_OK && ret != HTTP_PROCESSING) { - pc.printf("Twitter failure: %d\r\n", ret); - return -1; -#ifdef DEBUG - } else { - pc.printf("Twitter success: %d\r\n", ret); + host.setName("api.supertweet.net"); + host.setPort(HTTP_PORT); + return httpRequest(METHOD_POST, &host, "/1/statuses/update.xml", head, buf) == 200 ? 0 : -1; #endif - } -#endif - return 0; } int email (int num) { #ifdef USE_EMAIL + Host host; char buf[FORMAT_STR_SIZE]; - static SMTPClient smtp; - Host smtpserver; - EmailMessage msg; - SMTPResult ret; if (! ethernet_flg || num >= CF_MAIL_NUM) return 0; if (! conf.smtphost[0] || ! conf.mailfrom[0] || @@ -165,44 +128,17 @@ } LED_NET_ACT_ON; - smtpserver.setName(conf.smtphost); - smtpserver.setPort(conf.smtpport); - smtp.setServer(smtpserver); - smtp.setHeloDomain("mbed"); - if (conf.smtpuser[0] && conf.smtppwd[0]) { - // SMTP auth - smtp.setAuth(conf.smtpuser, conf.smtppwd); - } - msg.setFrom(conf.mailfrom); - msg.addTo(conf.mailto[num]); -/* - msg.printf("Content-Type: text/plain; charset=UTF-8\n"); - msg.printf("Content-Transfer-Encoding: 8bit\n"); - msg.printf("From: %s\n", conf.mailfrom); - msg.printf("To: %s\n", conf.mailto[num]); -*/ - msg.printf("Subject: Message from weather\n"); - format_str(conf.mailmesg[num], buf, sizeof_1(buf)); - msg.printf("\n%s\n", buf); - + snprintf(buf, sizeof(buf), "From: %s\nTo: %s\nSubject: Message from weather\n\n", conf.mailfrom, conf.mailto); + format_str(conf.mailmesg[num], &buf[strlen(buf)], sizeof_len(buf)); #ifdef DEBUG pc.printf("SMTP %s %s %s\r\n%s\r\n", conf.smtphost, conf.mailfrom, conf.mailto[num], buf); #endif - smtp.setTimeout(NET_TIMEOUT); - ret = smtp.send(&msg); - if (ret != HTTP_OK) { - pc.printf("Mail failure: %d\r\n", ret); - return -1; -#ifdef DEBUG - } else { - pc.printf("Mail success: %d\r\n", ret); - pc.printf("SMTP response %s", smtp.getLastResponse().c_str()); + host.setName(conf.smtphost); + host.setPort(conf.smtpport); + return sendmail(conf.mailfrom, conf.mailto[num], buf, &host, conf.smtpuser, conf.smtppwd); #endif - } -#endif - return 0; } int ntp (char *hostname) {