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-07-04
- Revision:
- 0:bdb53686c194
- Child:
- 1:6c7141895545
File content as of revision 0:bdb53686c194:
#include "mbed.h" #include "weather.h" #include "EthernetNetIf.h" #ifdef USE_NTP #include "NTPClient.h" #endif #include "HTTPClient.h" #ifdef USE_EMAIL #include "SMTPClient.h" #endif #define STATION_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_g2(p24),led_g(p25), led_y(p26); static DigitalIn eth_link(P1_25), eth_speed(P1_26); static volatile int ethernet_flg = 0; static HTTPClient *http; int weatherstations () { char post_data[200]; char id[10], pin[40]; // 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))) { return -1; } #ifdef DEBUG pc.printf("S: %s %s\r\n", id, pin); #endif LED_NET_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)); postContent.puts(post_data); http->resetRequestHeaders(); http->basicAuth(NULL, NULL); http->setTimeout(NET_TIMEOUT); ret = http->post(STATION_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 } return 0; } int pachube (char *csv) { char uri[100]; char apikey[70], feedid[10]; // 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))) { return -1; } #ifdef DEBUG pc.printf("P: %s %s\r\n", apikey, feedid); #endif LED_NET_ON; csvContent.set(csv); strcpy(uri, PACHUBE_URL); strcat(uri, chop(feedid)); strcat(uri, ".csv?_method=put"); http->resetRequestHeaders(); http->basicAuth(NULL, NULL); http->setTimeout(NET_TIMEOUT); http->setRequestHeader("X-PachubeApiKey", chop(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 } return 0; } int twitter (int num) { char buf[170], tmp[128]; char user[20], pwd[20], key[20]; // 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))) { return -1; } #ifdef DEBUG pc.printf("T: %s %s %s\r\n", user, pwd, tmp); #endif LED_NET_ON; format_str(tmp, buf, sizeof(buf)); msg["status"] = buf; http->resetRequestHeaders(); http->setTimeout(NET_TIMEOUT); http->basicAuth(chop(user), chop(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 } 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]; 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))) { return -1; } LED_NET_ON; if (cfg.getValue("SMTPPORT", buf, sizeof(buf))) { port = atoi(buf); } smtpserver.setName(chop(smtphost)); smtpserver.setPort(port); smtp.setServer(smtpserver); smtp.setHeloDomain("mbed"); msg.setFrom(chop(from)); msg.addTo(chop(to)); #ifdef DEBUG pc.printf("SMTP %s %s %s\r\n", smtphost, from, to); #endif /* 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("Subject: Message from weather\n"); format_str(tmp, buf, sizeof(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)); } 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 ntpdate (char *hostname) { #ifdef USE_NTP static NTPClient ntp; Host ntpserver; NTPResult ret; time_t sec; if (! ethernet_flg) return 0; LED_NET_ON; ntpserver.setName(hostname); ntpserver.setPort(123); 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); return -1; #ifdef DEBUG } else { 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); led_g = eth_link ? 1 : 0; led_g2 = 1; LED_NET_ON; if (cfg.getValue("IPADDRESS", buf, sizeof(buf))) { if (strncmp(buf, "DHCP", 4) == 0) { // 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); } if (eth == NULL) return -1; ethErr = eth->setup(); if (ethErr) { // error 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(); http = new HTTPClient; if (http == NULL) return -1; } if (cfg.getValue("NTPSERVER", buf, sizeof(buf))) { pc.printf("NTP: %s\r\n", buf); ntpdate(chop(buf)); } if (cfg.getValue("SNMP_COMMNAME", buf, sizeof(buf))) { snmp_init(chop(buf)); pc.printf("SNMP community name: %s\r\n", buf); } return 0; } void pool_net () { if (ethernet_flg) { Net::poll(); // ethernet LED green led_g = eth_link ? 1 : 0; led_g2 = 1; } else { // ethernet LED orange led_g = eth_link ? 0 : 1; led_g2 = 0; } }