![](/media/cache/profiles/879133cb99667d06679a190317fe9b99.jpg.50x50_q85.jpg)
Locator mobile version
Dependencies: BLE_API mbed nRF51822
Fork of WeatherStation by
Diff: main.cpp
- Revision:
- 4:fddb2d7c7c61
- Parent:
- 3:b6d2c5195055
- Child:
- 5:fe4888cc60cc
--- a/main.cpp Thu Nov 12 22:42:05 2015 +0000 +++ b/main.cpp Sat Nov 14 21:09:57 2015 +0000 @@ -7,14 +7,21 @@ #include "WeatherService.h" #include "BatteryService.h" #include "BMP180.h" +#include "DHT.h" -BLE ble; + +float temperature; +float pressure; +float humidity; + +BLE ble; + DigitalOut okLED(LED1); DigitalOut errLED(LED2); DigitalOut instrumentsPower(p30); BMP180 bmp180; -Serial pc(USBTX, USBRX); +DHT dht(p5,DHT11); const static char DEVICE_NAME[] = "Weather Station"; @@ -25,9 +32,9 @@ static volatile bool triggerSensorPolling = false; -/* Restart Advertising on disconnection*/ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { + /* Restart Advertising on disconnection*/ ble.gap().startAdvertising(); } @@ -37,35 +44,51 @@ triggerSensorPolling = true; } +void updateFromBMP180() { + uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I); + + printf("BMP-180 is 0x%x\n\r", c); + printf("BMP-180 should be 0x55\n"); + + if(c == 0x55) { + printf("BMP-180 online\n"); + + printf("Calibrating BMP-180..."); + bmp180.BMP180Calibration(); + printf("done\n"); + } + else + { + printf("BMP-180 unreachable\n"); + return; + } + + temperature = (float)bmp180.BMP180GetTemperature()/10.0f; + pressure = (float)bmp180.BMP180GetPressure(); +} + +void updateFromDHT() { + wait(3); + int err = dht.readData(); + if (err == 0) { + humidity = dht.ReadHumidity(); + printf("Dew point is %4.2f \n",dht.CalcdewPoint(dht.ReadTemperature(CELCIUS), humidity)); + printf("Dew point (fast) is %4.2f \n",dht.CalcdewPointFast(dht.ReadTemperature(CELCIUS), humidity)); + } + else { + printf("Error reading DHT: %d \n", err); + } +} + int main(void) { - pc.printf("Start\n"); + printf("Start\n"); okLED = 1; errLED = 1; - instrumentsPower = 1; - wait(1); - uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I); - - pc.printf("BMP-180 is 0x%x\n\r", c); - pc.printf("BMP-180 should be 0x55\n"); - - if(c == 0x55) { - okLED = 0; - pc.printf("BMP-180 online...\n"); - - bmp180.BMP180Calibration(); - pc.printf("BMP-180 calibration complete...\n"); - } - else - { - errLED = 0; - pc.printf("BMP-180 unreachable...\n"); - while(1); // idle here forever - } Ticker ticker; - ticker.attach(blink, 2); + ticker.attach(blink, 5); ble.init(); ble.gap().onDisconnection(disconnectionCallback); @@ -89,18 +112,20 @@ okLED = 1; triggerSensorPolling = false; - float temperature = (float)bmp180.BMP180GetTemperature()/10.0f; - float pressure = (float)bmp180.BMP180GetPressure(); - float altitude = 44330.0f*( 1.0f - pow((pressure/101325.0f), (1.0f/5.255f))); - - pc.printf("Temp: %.1f C\n", temperature); - pc.printf("Pressure: %.3f Pa\n", pressure); - pc.printf("Altitude: %.1f m\n", altitude); - pc.printf("---\n"); + instrumentsPower = 1; + wait(1); + updateFromBMP180(); + updateFromDHT(); + instrumentsPower = 0; + + printf("Temp: %.1f C \n", temperature); + printf("Pressure: %.3f Pa \n", pressure); + printf("Humidity: %.1f % \n", humidity); + printf("---\n"); weatherService.updateTemperature(temperature); weatherService.updatePressure(pressure); - weatherService.updateHumidity(0); + weatherService.updateHumidity(humidity); batteryService.updateBatteryLevel(battery.read());