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
net.cpp
- Committer:
- okini3939
- Date:
- 2011-08-24
- Revision:
- 2:a3e5edf84f74
- Parent:
- 1:6c7141895545
- Child:
- 3:058292da2cee
File content as of revision 2:a3e5edf84f74:
/* * Weather Station - mbed Weather Platform * Copyright (c) 2011 Hiroshi Suga * Released under the MIT License: http://mbed.org/license/mit */ /** @file * @brief Weather Station */ #include "mbed.h" #include "weather.h" #include "EthernetNetIf.h" #ifdef USE_NTP //#include "NTPClient.h" #include "TinySNTP.h" #endif #ifdef USE_HTTP #include "HTTPClient.h" #endif #ifdef USE_EMAIL #include "SMTPClient.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/" 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"); if (! ethernet_flg) return 0; if (! conf.stations_id[0] || ! conf.stations_pin[0]) { return -1; } 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); #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); return -1; #ifdef DEBUG } else { pc.printf("Weather Stations success: %d\r\n", ret); #endif } #endif return 0; } int pachube () { #ifdef USE_HTTP char buf[100], uri[100]; HTTPClient http; HTTPResult ret; HTTPText csvContent("text/csv"); if (! ethernet_flg) return 0; if (! conf.pachube_apikey[0] || ! conf.pachube_feedid[0]) { return -1; } LED_NET_ACT_ON; 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); #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); #endif } #endif return 0; } int twitter (int num) { #ifdef USE_HTTP char buf[FORMAT_STR_SIZE]; HTTPClient http; HTTPMap msg; HTTPResult ret; if (! ethernet_flg || num >= CF_TWITTER_NUM) return 0; if (! conf.twitter_user[0] || ! conf.twitter_pwd[0] || ! conf.twitter_mesg[num][0]) { return -1; } LED_NET_ACT_ON; format_str(conf.twitter_mesg[num], buf, sizeof_1(buf)); msg["status"] = 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); #endif } #endif return 0; } int email (int num) { #ifdef USE_EMAIL 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] || ! conf.mailto[num][0] || ! conf.mailmesg[num][0]) { return -1; } 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); #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()); #endif } #endif return 0; } 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); if (ret == NTP_OK) { sec = time(NULL) + (60 * 60 * TIMEZONE); set_time(sec); } if (ret != NTP_OK) { pc.printf("NTP failure: %d\r\n", ret); return -1; #ifdef DEBUG } else { pc.printf("NTP success %s\r\n", ctime(&sec)); #endif } */ #endif return 0; } int init_net () { EthernetErr ethErr; eth_link.mode(PullUp); eth_speed.mode(PullUp); if (conf.ipaddr[0]) { if (! eth_link) { LED_NET_G_ON; } LED_NET_ACT_ON; if (conf.ipaddr[0] == 255) { // dhcp ip address eth = new EthernetNetIf; } else { // static ip address eth = new EthernetNetIf(conf.ipaddr, conf.netmask, conf.gateway, conf.nameserver); } ethErr = eth->setup(); if (ethErr) { // error LED_NET_Y_ON; return -1; } pc.printf("\r\nEthernet: %d.%d.%d.%d\r\n", eth->getIp()[0], eth->getIp()[1], eth->getIp()[2], eth->getIp()[3]); ethernet_flg = 1; pool_net(); #ifdef USE_HTTP // http = new HTTPClient; #endif // ntp = new NTPClient; } if (conf.ntpserver[0]) { ntp(conf.ntpserver); } if (conf.snmp_commname[0]) { snmp_init(conf.snmp_commname); } return 0; } void pool_net () { if (ethernet_flg) { Net::poll(); if (! eth_link) { LED_NET_G_ON; } else { LED_NET_GY_OFF; } } }