/* mbed Weather Plathome + XBee 2011/03 : Ver.0.1 2011/04 : Ver.0.2 get_WindDirection(float value); 2011/04 : Ver.0.3 mbed_reset(); Copyleft 2011 Moo Soft(Akiyosi Yokoyama) http://moosoft.jp mbed Weather Platform http://mbed.org/users/okini3939/notebook/weather-platform/ I2C LCD http://mbed.org/users/okini3939/notebook/i2c-lcd-library/ LCD : SC1602BS*B , SD1602HULB-XA ... Etc. // ******************************* // h.suga http://mbed.org/users/okini3939/ http://www.sugakoubou.com galileo7 http://www.galileo-7.com // ******************************* // */ #include using std::string; #include "mbed.h" #include "BMP085.h" #include "SHT.h" #include "WeatherMeters.h" #include "I2CLCD.h" //#include "I2CLEDDisp.h" extern "C" { void mbed_reset(); // mbed Reset } #define W_NO "Moo_00001" #define USE_WIND_DIRECTION_STR //#undef USE_WIND_DIRECTION_STR // Timer Interrupt mbed Reset Ticker mbed_reboot; // Serial Port // Serial pc(USBTX, USBRX); Serial xbee(p13, p14); // LED // DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4); BusOut leds(LED1, LED2, LED3, LED4); // Sensor float pub_kiatsu, pub_temp_1, pub_sitsudo, pub_syoudo, pub_uv, pub_fuusoku, pub_kazamuki, pub_uryou, pub_analog_r, pub_temp_2; I2C i2c(p9, p10); // I2C BMP085 bmp085(i2c, BMP085_oss4); // Barometric pressure & Temperature high resolution (4 times) SHT sht15(p12, p11, SHT_high); // Humidity & Temperature sclock, data WeatherMeters wmeters(p21, p15, p22); // Wind speed, Wind Direction, Rainfall // AnalogIn AnalogIn ailight(p16); // Illumination AnalogIn aiuv(p17); // UV(Ultra Violet) AnalogIn aimoist(p18); // Analog R // I2C LCD I2CLCD i2clcd(i2c); // LCD Show Wait Time const float show_wait = 5.0; // Timer Interrupt mbed Reset Time const float rebootWait = 86400; // 24h // var char pub_buf[100]; int pub_leds = 0xF; // function void interrupt_mbed_reset(); // mbed Reset void lcd_disp(); // LCD void get_bmp085(); // Barometric pressure & Temperature void get_sht15(); // Humidity & Temperature void get_weathermeter(); // WeatherMeters Wind speed, Wind direction, Rainfall void get_photo(AnalogIn &ain); // Illumination void get_uv(AnalogIn &ain); // UV(Ultra Violet) void get_moist(AnalogIn &ain); // Analog R void init_xbee(); // Xbee Serial Port void xbee_txData(); // XBee TX Data void measure_txData(); // Timer Interrupt Measure & XBee Tx Data void shift_leds(); // LED string Get_WindDirection(float value); // Wind Direction // Main Loop int main() { init_xbee(); // Xbee Serial Port Initialize // Timer Interrupt mber Reset mbed_reboot.attach(&interrupt_mbed_reset, rebootWait); while (true) { leds = pub_leds; // LED shift_leds(); // LED measure_txData(); // Measure & XBee Tx Data lcd_disp(); // LCD } } // mbed Reset void interrupt_mbed_reset() { mbed_reset(); } // LED void shift_leds() { pub_leds = pub_leds % 0x10; pub_leds--; if (pub_leds < 0x0) { pub_leds = 0xF; } } // Measure & XBee Tx Data void measure_txData() { // ** Sensors Start ** // get_bmp085(); // Barometric pressure & Temperature get_sht15(); // Humidity & Temperature get_photo(ailight); // Illumination get_uv(aiuv); // UV get_moist(aimoist); // Analog R get_weathermeter(); // Wind speed, Wind Direction, Rainfall // *** Sensors End *** // xbee_txData(); // XBee TX Data } // Window Direction string Get_WindDirection(float value) { /* Direction Resistance (Degrees) (Ohms) 0 N 33k 22.5 NNE 6.57k 45 NE 8.2k 67.5 ENE 891 90 E 1k 112.5 ESE 688 135 SE 2.2k 157.5 SSE 1.41k 180 S 3.9k 202.5 SSW 3.14k 225 SW 16k 247.5 WSW 14.12k 270 W 120k 292.5 WNW 42.12k 315 NW 64.9k 337.5 NNW 21.88k */ string retVal = ""; if ((value >= 0.00) & (value <= 11.25)) { retVal = "N"; } else if ((value > 11.25) & (value <= 33.75)) { retVal = "NNE"; } else if ((value > 33.75) & (value <= 56.25)) { retVal = "NE"; } else if ((value > 56.25) & (value <= 78.75)) { retVal = "ENE"; } else if ((value > 78.75) & (value <= 101.25)) { retVal = "E"; } else if ((value > 101.25) & (value <= 123.75)) { retVal = "ESE"; } else if ((value > 123.75) & (value <= 146.25)) { retVal = "SE"; } else if ((value > 146.25) & (value <= 168.75)) { retVal = "SSE"; } else if ((value > 168.75) & (value <= 191.25)) { retVal = "S"; } else if ((value > 191.25) & (value <= 213.75)) { retVal = "SSW"; } else if ((value > 213.75) & (value <= 236.25)) { retVal = "SW"; } else if ((value > 236.25) & (value <= 258.75)) { retVal = "WSW"; } else if ((value > 258.75) & (value <= 281.25)) { retVal = "W"; } else if ((value > 281.25) & (value <= 303.75)) { retVal = "WNW"; } else if ((value > 303.75) & (value <= 326.25)) { retVal = "NW"; } else if ((value > 326.25) & (value <= 348.75)) { retVal = "NNW"; } else if ((value > 348.75) & (value <= 360.00)) { retVal = "N"; } return retVal; } // LCD void lcd_disp() { i2clcd.cls(); // Pressure sprintf(pub_buf, "%-.0f hPa ", pub_kiatsu); i2clcd.printf(pub_buf); // Temperature sprintf(pub_buf, "%-.1f", pub_temp_1); i2clcd.printf(pub_buf); i2clcd.putc(0xDF); i2clcd.printf("C\n"); // Humidity sprintf(pub_buf, "%-.0f %s", pub_sitsudo, "%%"); i2clcd.printf(pub_buf); // Illumination sprintf(pub_buf, " %-.0f lx", pub_syoudo); i2clcd.printf(pub_buf); wait(show_wait); // wait i2clcd.cls(); // Wind Direction #ifdef USE_WIND_DIRECTION_STR i2clcd.printf(Get_WindDirection(pub_kazamuki).c_str()); #else sprintf(pub_buf, "%-.0f", pub_kazamuki); i2clcd.printf(pub_buf); i2clcd.putc(0xDF); // do #endif // Wind speed sprintf(pub_buf, " %-.0f m/s\n", pub_fuusoku); i2clcd.printf(pub_buf); // Rainfall sprintf(pub_buf, "%-.0f mm", pub_uryou); i2clcd.printf(pub_buf); wait(show_wait); // wait i2clcd.cls(); // Analog R sprintf(pub_buf, "%-.0f k", pub_analog_r); i2clcd.printf(pub_buf); i2clcd.putc(0xF4); // ohm i2clcd.printf("\n"); // UV(Ultra Violet) sprintf(pub_buf, "%-.0f mW/cm2", pub_uv); i2clcd.printf(pub_buf); wait(show_wait); // wait } // WeatherMeters pub_fuusoku, pub_kazamuki, pub_uryou void get_weathermeter() { pub_fuusoku = wmeters.get_windspeed(); // Wind speed pub_kazamuki = wmeters.get_windvane(); // Wind direction pub_uryou = wmeters.get_raingauge(); // Rainfall } // Illumination void get_photo (AnalogIn &ain) { float f; f = ain * 5.0 / 1000; // A pub_syoudo = f / 0.0000026; // lx } // Analog R void get_moist (AnalogIn &ain) { float f; f = ain * 5.0; // V f = f / ((3.3 - f) / 10.0); // k ohm if (f < 0) f = 0; pub_analog_r = f; } // UV(Ultra Violet) void get_uv (AnalogIn &ain) { float f; f = ain * 5.0 / 100000; // A f = f / 0.000384; // mW/cm2 if (f < 0) f = 0; pub_uv = f; } // Barometric pressure & Temperature void get_bmp085() { bmp085.update(); pub_kiatsu = bmp085.get_pressure(); pub_temp_2 = bmp085.get_temperature(); } // Humidity & Temperature void get_sht15() { sht15.update(SHT_high); pub_temp_1 = sht15.get_temperature(); pub_sitsudo = sht15.get_humidity(); } // XBee Serial Port void init_xbee() { xbee.format(8, Serial::None, 1); // Default xbee.baud(9600); } // XBee TX Data void xbee_txData() { int ii = 0; while (!xbee.writeable()) { wait(1.0); ii++; if (ii > 10) { interrupt_mbed_reset(); // mbed Reset } } sprintf(pub_buf, "%s,%.0f,%.1f,%.1f,%.0f,%.0f,%.0f,%.0f,%.1f,%.1f,%.1f", W_NO, pub_kiatsu, pub_temp_1, pub_sitsudo, pub_fuusoku, pub_kazamuki, pub_uryou, pub_syoudo, pub_uv, pub_analog_r, pub_temp_2); xbee.printf("%s\r", pub_buf); }