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
Revision 14:ee6cc1632166, committed 2011-03-29
- Comitter:
- okini3939
- Date:
- Tue Mar 29 18:15:27 2011 +0000
- Parent:
- 13:20c0f845df68
- Child:
- 15:07bfa25ba6ae
- Commit message:
Changed in this revision
--- a/conf.cpp Sat Mar 19 21:24:01 2011 +0000 +++ b/conf.cpp Tue Mar 29 18:15:27 2011 +0000 @@ -9,7 +9,7 @@ extern Serial xbee; extern Config conf; -extern float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2; +extern Sensor sensor, offset, sensor_old; extern I2C i2c; extern I2CLCD *lcd; extern I2CLEDDisp *leddisp; @@ -33,7 +33,7 @@ int check_action (char type) { int i, j, count; - float value; + float value, vold; time_t sec = time(NULL) + (60 * 60 * 9); struct tm *tim = localtime(&sec); @@ -44,22 +44,40 @@ for (j = 0; j < conf.actions[i].count; j ++) { switch (conf.actions[i].exps[j].key) { case 'P': - value = pres; + value = sensor.pres; + vold = sensor_old.pres; break; case 'T': - value = temp; + value = sensor.temp; + vold = sensor_old.temp; break; case 'H': - value = humi; + value = sensor.humi; + vold = sensor_old.humi; break; case 'A': - value = anemo; + value = sensor.anemo; + vold = sensor_old.anemo; break; case 'V': - value = vane; + value = sensor.vane; + vold = sensor_old.vane; break; case 'R': - value = rain; + value = sensor.rain; + vold = sensor_old.rain; + break; + case 'L': + value = sensor.light; + vold = sensor_old.light; + break; + case 'U': + value = sensor.uv; + vold = sensor_old.uv; + break; + case 'M': + value = sensor.moist; + vold = sensor_old.moist; break; case 'y': @@ -127,6 +145,16 @@ count ++; } break; + case EXP_FALL: + if (value < conf.actions[i].exps[j].value && vold >= conf.actions[i].exps[j].value) { + count ++; + } + break; + case EXP_RISE: + if (value > conf.actions[i].exps[j].value && vold <= conf.actions[i].exps[j].value) { + count ++; + } + break; default: count ++; @@ -187,7 +215,16 @@ exp->expression = EXP_MOD; exp->value = atof(&buf[2]); break; - + + case '_': + exp->expression = EXP_FALL; + exp->value = atof(&buf[2]); + break; + case '^': + exp->expression = EXP_RISE; + exp->value = atof(&buf[2]); + break; + default: exp->expression = EXP_NULL; break; @@ -250,6 +287,8 @@ conf.snmp_commname[0] = 0; conf.leddisp_mesg[0] = 0; conf.lcd_mesg[0] = 0; + memset(&offset, 0, sizeof(offset)); + memset(&sensor_old, 0, sizeof(sensor_old)); if (! cfg.read(file)) { return -1; @@ -352,5 +391,15 @@ } } + if (cfg.getValue("OFFSET[P]", buf, sizeof(buf))) { + offset.pres = atof(chop(buf)); + } + if (cfg.getValue("OFFSET[T]", buf, sizeof(buf))) { + offset.temp = atof(chop(buf)); + } + if (cfg.getValue("OFFSET[H]", buf, sizeof(buf))) { + offset.humi = atof(chop(buf)); + } + return 0; }
--- a/conf.h Sat Mar 19 21:24:01 2011 +0000 +++ b/conf.h Tue Mar 29 18:15:27 2011 +0000 @@ -4,7 +4,7 @@ #include "mbed.h" #include "EthernetNetIf.h" -#define CONFIG_FILE "/sd/weather.cfg" +#define CONFIG_FILE "weather.cfg" #define CF_ACTION_EXPS 10 @@ -20,6 +20,8 @@ EXP_GT, EXP_MOD, EXP_NMOD, + EXP_FALL, + EXP_RISE, }; enum eINPUTTYPE { @@ -40,6 +42,10 @@ struct tExpression exps[CF_ACTION_EXPS]; }; +struct Sensor { + float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2; +}; + struct Config { int interval; IpAddr ipaddr, netmask, gateway, nameserver;
--- a/func.cpp Sat Mar 19 21:24:01 2011 +0000 +++ b/func.cpp Tue Mar 29 18:15:27 2011 +0000 @@ -24,7 +24,7 @@ extern EthernetNetIf *eth; extern NTPClient *ntp; extern HTTPClient *clientP, *clientT; -extern float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2; +extern Sensor sensor; void writefile (char *buf) { FILE *fp; @@ -49,7 +49,8 @@ strcat(post_data, conf.stations_id); strcat(post_data, "&pin="); strcat(post_data, conf.stations_pin); - sprintf(&post_data[strlen(post_data)], "&d0=%.2f&d1=%.2f&d2=%.2f&d3=%.2f&d4=%.2f&d5=%.2f&d6=%.2f&d7=%.2f&d8=%.2f&d9=%.2f", pres, temp, humi, anemo, vane, rain, light, uv, moist, temp2); + sprintf(&post_data[strlen(post_data)], "&d0=%.2f&d1=%.2f&d2=%.2f&d3=%.2f&d4=%.2f&d5=%.2f&d6=%.2f&d7=%.2f&d8=%.2f&d9=%.2f", + sensor.pres, sensor.temp, sensor.humi, sensor.anemo, sensor.vane, sensor.rain, sensor.light, sensor.uv, sensor.moist, sensor.temp2); postContent.puts(post_data); #ifdef NONBLOCKING @@ -103,37 +104,52 @@ time_t sec = time(NULL); struct tm *tim = localtime(&sec); - flg = 0; j = 0; for (i = 0; i < strlen(fmt) && j < len; i ++) { c = fmt[i]; if (c == '%') { + flg = 0; i ++; c = fmt[i]; + + if (c == '.') { + // float + if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { + flg = fmt[i + 1] - '0'; + i ++; + + c = fmt[i + 1]; + i ++; + } + } + switch (c) { case 'P': - value = pres; + value = sensor.pres; break; case 'T': - value = temp; + value = sensor.temp; break; case 'H': - value = humi; + value = sensor.humi; break; case 'A': - value = anemo; + value = sensor.anemo; break; case 'V': - value = vane; + value = sensor.vane; break; case 'R': - value = rain; + value = sensor.rain; break; case 'L': - value = light; + value = sensor.light; break; case 'U': - value = uv; + value = sensor.uv; + break; + case 'M': + value = sensor.moist; break; case 'y': @@ -155,10 +171,6 @@ value = tim->tm_sec; break; - case '%': - buf[j] = c; - j ++; - continue; case '0': case '1': case '2': @@ -169,6 +181,7 @@ case '7': case '8': case '9': + // Ascii c = c - '0'; if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { c = (c << 3) | (fmt[i + 1] - '0'); @@ -181,23 +194,23 @@ buf[j] = c; j ++; continue; - case '.': - if (fmt[i + 1] >= '0' && fmt[i + 1] <= '9') { - flg = fmt[i + 1] - '0'; - i ++; - } - continue; + default: + buf[j] = c; + j ++; continue; } - if (c < 'a' && flg == 1) { + switch (flg) { + case 1: sprintf(&buf[j], "%.1f", value); - } else - if (c < 'a' && flg == 2) { + break; + case 2: sprintf(&buf[j], "%.2f", value); - } else { + break; + default: sprintf(&buf[j], "%02d", (int)value); + break; } j = strlen(buf); } else {
--- a/main.cpp Sat Mar 19 21:24:01 2011 +0000 +++ b/main.cpp Tue Mar 29 18:15:27 2011 +0000 @@ -8,7 +8,7 @@ * @brief mbed Weather Platform */ -#define VERSION "mbed Weather Platform 0.2.0 (C) 2011 Suga koubou Co.,Ltd." +#define VERSION "mbed Weather Platform 0.2.1 (C) 2011 Suga koubou Co.,Ltd." //#define NONBLOCKING // ethernet function non-bloking @@ -29,13 +29,14 @@ Serial pc(USBTX, USBRX), xbee(p13, p14); int seq = 0; char filename[20]; -Config conf; +struct Config conf; DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); +LocalFileSystem local("local"); SDFileSystem sd(p5, p6, p7, p8, "sd"); MSCFileSystem *usb; // Sensors -float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2; +struct Sensor sensor, offset, sensor_old; I2C i2c(p9, p10); BMP085 bmp085(i2c, BMP085_oss4); SHT sht15(p12, p11, SHT_high); // sclock, data @@ -180,14 +181,18 @@ if (check_action('X')) { xbee.printf(buf); } + sensor_old = sensor; } void init () { FILE *fp; - config(CONFIG_FILE); + if (config("/sd/" CONFIG_FILE) == -1) { + pc.printf("local strage\r\n", conf.interval); + config("/local/" CONFIG_FILE); + } - pc.printf("Interval: %d sec\r\n", conf.interval); + pc.printf("\r\nInterval: %d sec\r\n", conf.interval); if (conf.ipaddr[0]) { // use ethernet @@ -336,26 +341,27 @@ // sensors __disable_irq(); bmp085.update(); - pres = bmp085.get_pressure(); - temp2 = bmp085.get_temperature(); + sensor.pres = bmp085.get_pressure() + offset.pres; + sensor.temp2 = bmp085.get_temperature(); sht15.update(SHT_high); - temp = sht15.get_temperature(); - humi = sht15.get_humidity(); + sensor.temp = sht15.get_temperature() + offset.temp; + sensor.humi = sht15.get_humidity() + offset.humi; - anemo = wmeters.get_windspeed(); - vane = wmeters.get_windvane(); - rain = wmeters.get_raingauge(); + sensor.anemo = wmeters.get_windspeed(); + sensor.vane = wmeters.get_windvane(); + sensor.rain = wmeters.get_raingauge(); - light = get_photo(ailight); + sensor.light = get_photo(ailight); if (conf.inputtype == INPUT_MOIST) { - moist = get_moist(*aimoist); + sensor.moist = get_moist(*aimoist); } else { - moist = get_counter(0); + sensor.moist = get_counter(0); } - uv = get_uv(aiuv); + sensor.uv = get_uv(aiuv); - 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); + sprintf(&buf[strlen(buf)], ",%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f\r\n", + sensor.pres, sensor.temp, sensor.humi, sensor.anemo, sensor.vane, sensor.rain, sensor.light, sensor.uv, sensor.moist, sensor.temp2); if (conf.filetype) { // csv writefile(buf);
--- a/snmp.cpp Sat Mar 19 21:24:01 2011 +0000 +++ b/snmp.cpp Tue Mar 29 18:15:27 2011 +0000 @@ -9,6 +9,7 @@ #include "Agentbed.h" #include "EthernetNetIf.h" #include "ConfigFile.h" +#include "conf.h" AgentbedClass Agentbed; @@ -57,7 +58,7 @@ SNMP_ERR_CODES status; extern DigitalOut led_y; -extern float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2; +extern Sensor sensor; void pduReceived() { @@ -128,34 +129,34 @@ // response packet from get-request - enterprises switch (oid[strlen(enterprises)]) { case '0': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, pres); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.pres); break; case '1': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, temp); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.temp); break; case '2': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, humi); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.humi); break; case '3': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, anemo); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.anemo); break; case '4': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, vane); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.vane); break; case '5': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, rain); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.rain); break; case '6': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, light); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.light); break; case '7': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, uv); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.uv); break; case '8': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, moist); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.moist); break; case '9': - status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, temp2); + status = pdu.VALUE.encode(SNMP_SYNTAX_OPAQUE_FLOAT, sensor.temp2); break; } pdu.error = status;