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:
- 2:a3e5edf84f74
- Parent:
- 1:6c7141895545
- Child:
- 3:058292da2cee
--- a/net.cpp Sun Jul 10 15:36:46 2011 +0000 +++ b/net.cpp Wed Aug 24 13:22:32 2011 +0000 @@ -12,14 +12,17 @@ #include "weather.h" #include "EthernetNetIf.h" #ifdef USE_NTP -#include "NTPClient.h" +//#include "NTPClient.h" +#include "TinySNTP.h" #endif +#ifdef USE_HTTP #include "HTTPClient.h" +#endif #ifdef USE_EMAIL #include "SMTPClient.h" #endif -#define STATION_URL "http://weather.sugakoubou.com/p" +#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/" @@ -27,42 +30,38 @@ 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; -static HTTPClient *http; -#ifdef USE_NTP -static NTPClient ntp; +#ifdef USE_HTTP +//static HTTPClient *http; #endif -#ifdef USE_EMAIL -static SMTPClient smtp; -#endif +//static NTPClient *ntp; int weatherstations () { - char post_data[200]; - char id[10], pin[40]; -// HTTPClient http; +#ifdef USE_HTTP + char post_data[150]; + HTTPClient http; HTTPResult ret; HTTPText postContent("application/x-www-form-urlencoded"); if (! ethernet_flg) return 0; - if (! cfg.getValue("STATIONS_ID", id, sizeof(id)) || - ! cfg.getValue("STATIONS_PIN", pin, sizeof(pin))) { + if (! conf.stations_id[0] || ! conf.stations_pin[0]) { return -1; } -#ifdef DEBUG - pc.printf("S: %s %s\r\n", id, pin); -#endif 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(post_data)); - strcat(post_data, "&fcd="); - strcat(post_data, chop(id)); - strcat(post_data, "&pin="); - strcat(post_data, chop(pin)); + 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); - http->resetRequestHeaders(); - http->basicAuth(NULL, NULL); - http->setTimeout(NET_TIMEOUT); - ret = http->post(STATION_URL, postContent, NULL); +#ifdef DEBUG + pc.printf("S: %s %s\r\n%s\r\n", conf.stations_id, conf.stations_pin, post_data); +#endif +// http->resetRequestHeaders(); +// http->basicAuth(NULL, NULL); + http.setTimeout(NET_TIMEOUT); + ret = http.post(STATIONS_URL, postContent, NULL); if (ret != HTTP_OK && ret != HTTP_PROCESSING) { pc.printf("Weather Statuons failure: %d\r\n", ret); @@ -72,36 +71,36 @@ pc.printf("Weather Stations success: %d\r\n", ret); #endif } +#endif return 0; } -int pachube (char *csv) { - char uri[100]; - char apikey[70], feedid[10]; -// HTTPClient http; +int pachube () { +#ifdef USE_HTTP + char buf[100], uri[100]; + HTTPClient http; HTTPResult ret; HTTPText csvContent("text/csv"); if (! ethernet_flg) return 0; - if (! cfg.getValue("PACHUBE_APIKEY", apikey, sizeof(apikey)) || - ! cfg.getValue("PACHUBE_FEEDID", feedid, sizeof(feedid))) { + if (! conf.pachube_apikey[0] || ! conf.pachube_feedid[0]) { return -1; } -#ifdef DEBUG - pc.printf("P: %s %s\r\n", apikey, feedid); -#endif LED_NET_ACT_ON; - csvContent.set(csv); - strcpy(uri, PACHUBE_URL); - strcat(uri, chop(feedid)); - strcat(uri, ".csv?_method=put"); + 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); - http->resetRequestHeaders(); - http->basicAuth(NULL, NULL); - http->setTimeout(NET_TIMEOUT); - http->setRequestHeader("X-PachubeApiKey", chop(apikey)); - ret = http->post(uri, csvContent, NULL); +#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); @@ -111,35 +110,33 @@ pc.printf("Pachube success: %d\r\n", ret); #endif } +#endif return 0; } int twitter (int num) { - char buf[170], tmp[128]; - char user[20], pwd[20], key[20]; -// HTTPClient http; +#ifdef USE_HTTP + char buf[FORMAT_STR_SIZE]; + HTTPClient http; HTTPMap msg; HTTPResult ret; if (! ethernet_flg || num >= CF_TWITTER_NUM) return 0; - sprintf(key, "TWITTER_MESG[%d]", num); - if (! cfg.getValue("TWITTER_USER", user, sizeof(user)) || - ! cfg.getValue("TWITTER_PWD", pwd, sizeof(pwd)) || - ! cfg.getValue(key, tmp, sizeof(tmp))) { + if (! conf.twitter_user[0] || ! conf.twitter_pwd[0] || ! conf.twitter_mesg[num][0]) { return -1; } -#ifdef DEBUG - pc.printf("T: %s %s %s\r\n", user, pwd, tmp); -#endif LED_NET_ACT_ON; - format_str(tmp, buf, sizeof(buf)); + format_str(conf.twitter_mesg[num], buf, sizeof_1(buf)); msg["status"] = buf; - http->resetRequestHeaders(); - http->setTimeout(NET_TIMEOUT); - http->basicAuth(chop(user), chop(pwd)); - ret = http->post(TWITTER_URL, msg, NULL); +#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); @@ -149,57 +146,49 @@ pc.printf("Twitter success: %d\r\n", ret); #endif } +#endif return 0; } int email (int num) { #ifdef USE_EMAIL - int port = 25; - char buf[170], smtphost[40], from[40], to[40], tmp[128]; - char key[40], key2[40]; + char buf[FORMAT_STR_SIZE]; + static SMTPClient smtp; Host smtpserver; EmailMessage msg; SMTPResult ret; if (! ethernet_flg || num >= CF_MAIL_NUM) return 0; - sprintf(key, "MAIL_MESG[%d]", num); - sprintf(key2, "MAIL_TO[%d]", num); - if (! cfg.getValue("SMTPSERVER", smtphost, sizeof(smtphost)) || - ! cfg.getValue("MAIL_FROM", from, sizeof(from)) || - ! cfg.getValue(key2, to, sizeof(to)) || - ! cfg.getValue(key, tmp, sizeof(tmp))) { + if (! conf.smtphost[0] || ! conf.mailfrom[0] || + ! conf.mailto[num][0] || ! conf.mailmesg[num][0]) { return -1; } LED_NET_ACT_ON; - if (cfg.getValue("SMTPPORT", buf, sizeof(buf))) { - port = atoi(buf); - } - smtpserver.setName(chop(smtphost)); - smtpserver.setPort(port); + 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(chop(from)); - msg.addTo(chop(to)); -#ifdef DEBUG - pc.printf("SMTP %s %s %s\r\n", smtphost, from, to); -#endif + 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", from); - msg.printf("To: %s\n", to); + msg.printf("From: %s\n", conf.mailfrom); + msg.printf("To: %s\n", conf.mailto[num]); */ msg.printf("Subject: Message from weather\n"); - format_str(tmp, buf, sizeof(buf)); + format_str(conf.mailmesg[num], buf, sizeof_1(buf)); msg.printf("\n%s\n", buf); - if (cfg.getValue("SMTPUSER", key, sizeof(key)) && - cfg.getValue("SMTPPWD", key2, sizeof(key2))) { - // SMTP auth - smtp.setAuth(chop(key), chop(key2)); - } +#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); @@ -216,24 +205,42 @@ return 0; } -int ntpdate (char *hostname) { +int ntp (char *hostname) { #ifdef USE_NTP + time_t sec; + + if (! ethernet_flg) return 0; + pc.printf("NTP: %s\r\n", hostname); + + if (! ntpdate(hostname, &sec)) { + sec = sec + (60 * 60 * TIMEZONE); + set_time(sec); +#ifdef DEBUG + pc.printf("NTP success %s\r\n", ctime(&sec)); +#endif + } else { + pc.printf("NTP failure\r\n"); + return -1; + } + +/* +// NTPClient ntp; Host ntpserver; NTPResult ret; time_t sec; if (! ethernet_flg) return 0; + pc.printf("NTP: %s\r\n", hostname); LED_NET_ACT_ON; ntpserver.setName(hostname); ntpserver.setPort(123); - ret = ntp.setTime(ntpserver); + ret = ntp->setTime(ntpserver); if (ret == NTP_OK) { sec = time(NULL) + (60 * 60 * TIMEZONE); set_time(sec); } -// ntp.close(); if (ret != NTP_OK) { pc.printf("NTP failure: %d\r\n", ret); @@ -243,56 +250,31 @@ pc.printf("NTP success %s\r\n", ctime(&sec)); #endif } +*/ #endif return 0; } int init_net () { - char buf[128]; - int ip0, ip1, ip2, ip3; - IpAddr ipaddr, netmask, gateway, nameserver; EthernetErr ethErr; eth_link.mode(PullUp); eth_speed.mode(PullUp); - if (cfg.getValue("IPADDRESS", buf, sizeof(buf))) { + if (conf.ipaddr[0]) { if (! eth_link) { LED_NET_G_ON; } LED_NET_ACT_ON; - if (strncmp(buf, "DHCP", 4) == 0) { + if (conf.ipaddr[0] == 255) { // dhcp ip address - eth = new EthernetNetIf; - } else { // static ip address - - sscanf(chop(buf), "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); - ipaddr = IpAddr(ip0, ip1, ip2, ip3); - - if (cfg.getValue("NETMASK", buf, sizeof(buf))) { - sscanf(chop(buf), "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); - netmask = IpAddr(ip0, ip1, ip2, ip3); - } - - if (cfg.getValue("GATEWAY", buf, sizeof(buf))) { - sscanf(chop(buf), "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); - gateway = IpAddr(ip0, ip1, ip2, ip3); - } - - if (cfg.getValue("NAMESERVER", buf, sizeof(buf))) { - sscanf(chop(buf), "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3); - nameserver = IpAddr(ip0, ip1, ip2, ip3); - } - - eth = new EthernetNetIf(ipaddr, netmask, gateway, nameserver); - + eth = new EthernetNetIf(conf.ipaddr, conf.netmask, conf.gateway, conf.nameserver); } - if (eth == NULL) return -1; ethErr = eth->setup(); if (ethErr) { @@ -305,18 +287,18 @@ ethernet_flg = 1; pool_net(); - http = new HTTPClient; - if (http == NULL) return -1; +#ifdef USE_HTTP +// http = new HTTPClient; +#endif +// ntp = new NTPClient; } - if (cfg.getValue("NTPSERVER", buf, sizeof(buf))) { - pc.printf("NTP: %s\r\n", buf); - ntpdate(chop(buf)); + if (conf.ntpserver[0]) { + ntp(conf.ntpserver); } - if (cfg.getValue("SNMP_COMMNAME", buf, sizeof(buf))) { - snmp_init(chop(buf)); - pc.printf("SNMP community name: %s\r\n", buf); + if (conf.snmp_commname[0]) { + snmp_init(conf.snmp_commname); } return 0;