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
Diff: main.cpp
- Revision:
- 0:4265d973a98f
- Child:
- 1:86d4b7431fbe
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Dec 10 17:15:15 2010 +0000 @@ -0,0 +1,299 @@ +/* + * mbed Weather Platform + * Copyright (c) 2010 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ +/** mbed Weather Platform + * @auther Suga koubou Co.,Ltd. + */ +#include "mbed.h" +#include "BMP085.h" +#include "SHT.h" +#include "WeatherMeters.h" +//#include "I2CLCD.h" +#include "ConfigFile.h" +#include "SDFileSystem.h" +#include "EthernetNetIf.h" +#include "NTPClient.h" +#include "HTTPClient.h" + +#define VERSION "mbed Weather Platform 0.1a (C) 2010 Suga koubou Co.,Ltd." +#define TIMEZONE 9 + +//#define NONBLOCKING + +Serial pc(USBTX, USBRX); +int seq = 0; +char filename[20]; +ConfigFile conf; +DigitalOut led1(LED1), led2(LED2), led3(LED3); +SDFileSystem sd(p5, p6, p7, p8, "sd"); + +// Sensors +float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2; +I2C i2c(p9, p10); +BMP085 bmp085(i2c, BMP085_oss4); +//I2CLCD i2clcd(i2c); +SHT sht15(p12, p11, SHT_high); // sclock, data +WeatherMeters wmeters(p21, p15, p22); // anemo, vane, rain +AnalogIn ailight(p16), aimoist(p18), aiuv(p17); + +// Ethernet +EthernetNetIf *eth; +NTPClient *ntp; +HTTPClient *clientP; +DigitalOut led_g(p25), led_y(p26); +DigitalIn eth_link(P1_25), eth_speed(P1_26); + + +float get_photo (AnalogIn &ain) { + float f; + + f = ain * 5.0 / 1000; // A + return f / 0.0000026; // lx +} + +float get_moist (AnalogIn &ain) { + float f; + + f = ain * 5.0; // V + return f / ((3.3 - f) / 10.0); // k ohm +} + +float get_uv (AnalogIn &ain) { + float f; + + f = ain * 5.0 / 100000; // A + return f / 0.000384; // mW/cm2 +} + +void writefile (char *buf) { + FILE *fp; + + led3 = 1; + fp = fopen(filename, "a"); + if (fp) { + fprintf(fp, buf); + fclose(fp); + } else { + led2 = 0; + conf.filetype = 0; + } + led3 = 0; +} + +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()); + } +} + +void pachube (char *buf) { + char uri[100]; + HTTPText csvContent("text/csv"); + + led3 = 1; + 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(); + clientP->post(uri, csvContent, NULL, &cb_clientP); + Net::poll(); +#else + clientP->post(uri, csvContent, NULL); +#endif + led3 = 0; +} + +void cb_settime (NTPResult status) { + if (status == NTP_OK) { + 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(); +} + +void ntpdate () { + ntp = new NTPClient; + Host ntpserver(IpAddr(), 123, conf.ntpserver); + +#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 init () { + FILE *fp; + + conf.load("/sd/weather.cf"); + + if (conf.ipaddr[0]) { + // use ethernet + + eth_link.mode(PullUp); + eth_speed.mode(PullUp); + led_g = eth_link ? 1 : 0; + led_y = eth_speed ? 1 : 0; + if (conf.ipaddr[0] == 255) { + // dhcp + eth = new EthernetNetIf; + } else { + // static + eth = new EthernetNetIf(conf.ipaddr, conf.netmask, conf.gateway, conf.nameserver); + } + + EthernetErr ethErr = eth->setup(); + if(ethErr) { + // error + conf.ipaddr = IpAddr(0, 0, 0, 0); + } else + if (conf.ipaddr[0] == 255) { + // succeed dhcp + conf.ipaddr = eth->getIp(); + } + pc.printf("Ethernet: %d.%d.%d.%d\r\n", eth->getIp()[0], eth->getIp()[1], eth->getIp()[2], eth->getIp()[3]); + + if (conf.ipaddr[0] && conf.ntpserver[0]) { + // ntp + pc.printf("Ntp: %s\r\n", conf.ntpserver); + ntpdate(); + } + + if (conf.ipaddr[0]) { + clientP = new HTTPClient; + } + } + + if (conf.filetype) { + // seq num + fp = fopen("/sd/weather.seq", "r"); + if (fp) { + fscanf(fp, "%d", &seq); + fclose(fp); + } + seq ++; + fp = fopen("/sd/weather.seq", "w"); + if (fp) { + fprintf(fp, "%d", seq); + fclose(fp); + // create csv filename + if (conf.filetype == 1) { + sprintf(filename, "/sd/w%05d.csv", seq); + } else + if (conf.filetype == 2) { + sprintf(filename, "/usb/w%05d.csv", seq); + } + pc.printf("Filename: %s\r\n", filename); + led2 = 1; + } + } +} + +int main () { + int i; + Timer timer; + time_t sec; + char buf[100]; + + led1 = 1; + init(); + pc.printf(VERSION "\r\n\r\n"); + pc.printf("[%s|%s|%s|%s]\r\n", conf.pachube_apikey, conf.pachube_feedid, conf.twitter_user, conf.twitter_pwd); + + if (conf.filetype) { + strcpy(buf, "date,pres(hPa),temp(`C),humi(%%),anemo(m/s),vane(`),rain(mm),light(lx),uv(mW/cm2),moist(kohm),\r\n"); + writefile(buf); + } + + timer.start(); +#ifdef NONBLOCKING + while (timer.read() < 5) { + Net::poll(); + } + timer.reset(); +#endif + + while(1) { + led1 = 0; + + sec = time(NULL); + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&sec)); + + // sensors + bmp085.update(); + pres = bmp085.get_pressure(); + temp2 = bmp085.get_temperature(); + + sht15.update(SHT_high); + temp = sht15.get_temperature(); + humi = sht15.get_humidity(); + + anemo = wmeters.get_windspeed(); + vane = wmeters.get_windvane(); + rain = wmeters.get_raingauge(); + + light = get_photo(ailight); + moist = get_moist(aimoist); + uv = get_uv(aiuv); +/* + i2clcd.locate(0, 0); + i2clcd.printf("%4.1f hPa", p); + i2clcd.locate(0, 1); + i2clcd.printf("%2.1f C / %2.1f %%", t, h); +*/ + + sprintf(&buf[strlen(buf)], ",%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f\r\n", pres, temp, humi, anemo, vane, rain, light, uv, moist, temp2); + pc.printf(buf); + if (conf.filetype) { + // csv + writefile(buf); + } +/* + if (actionscount) { + // pin action + i = check_action(); + if (i == 0) { + outpin = 0; + } else + if (i == 1) { + outpin = 1; + } + } +*/ + if (conf.ipaddr[0]) { + if (conf.pachube_apikey[0] && conf.pachube_feedid[0]) { + // pachube + pachube(&buf[20]); + } + if (conf.twitter_user[0] && conf.twitter_pwd[0]) { + // twitter + } + } + + led1 = 1; + + while (timer.read() < conf.interval) { +// wait(1); +// pc.putc('.'); + led_g = eth_link ? 1 : 0; + led_y = eth_speed ? 1 : 0; +#ifdef NONBLOCKING + Net::poll(); +#endif + } + timer.reset(); + } +}