This is mandatory
Dependencies: BLE_API mbed nRF51822 SHT21_ncleee
main.cpp@4:fddb2d7c7c61, 2015-11-14 (annotated)
- Committer:
- PostaL
- Date:
- Sat Nov 14 21:09:57 2015 +0000
- Revision:
- 4:fddb2d7c7c61
- Parent:
- 3:b6d2c5195055
- Child:
- 5:fe4888cc60cc
Added reading of humidity from DHT11 sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PostaL | 3:b6d2c5195055 | 1 | #define I2C_SDA p3 |
PostaL | 3:b6d2c5195055 | 2 | #define I2C_SCL p4 |
PostaL | 0:7f951f57dbd2 | 3 | |
PostaL | 0:7f951f57dbd2 | 4 | #include "mbed.h" |
PostaL | 0:7f951f57dbd2 | 5 | #include "BLE.h" |
PostaL | 2:654ee4b3950f | 6 | #include "battery.h" |
PostaL | 2:654ee4b3950f | 7 | #include "WeatherService.h" |
PostaL | 2:654ee4b3950f | 8 | #include "BatteryService.h" |
PostaL | 3:b6d2c5195055 | 9 | #include "BMP180.h" |
PostaL | 4:fddb2d7c7c61 | 10 | #include "DHT.h" |
PostaL | 0:7f951f57dbd2 | 11 | |
PostaL | 4:fddb2d7c7c61 | 12 | |
PostaL | 4:fddb2d7c7c61 | 13 | float temperature; |
PostaL | 4:fddb2d7c7c61 | 14 | float pressure; |
PostaL | 4:fddb2d7c7c61 | 15 | float humidity; |
PostaL | 4:fddb2d7c7c61 | 16 | |
PostaL | 4:fddb2d7c7c61 | 17 | BLE ble; |
PostaL | 4:fddb2d7c7c61 | 18 | |
PostaL | 3:b6d2c5195055 | 19 | DigitalOut okLED(LED1); |
PostaL | 3:b6d2c5195055 | 20 | DigitalOut errLED(LED2); |
PostaL | 3:b6d2c5195055 | 21 | DigitalOut instrumentsPower(p30); |
PostaL | 0:7f951f57dbd2 | 22 | |
PostaL | 3:b6d2c5195055 | 23 | BMP180 bmp180; |
PostaL | 4:fddb2d7c7c61 | 24 | DHT dht(p5,DHT11); |
PostaL | 3:b6d2c5195055 | 25 | |
PostaL | 3:b6d2c5195055 | 26 | const static char DEVICE_NAME[] = "Weather Station"; |
PostaL | 3:b6d2c5195055 | 27 | |
PostaL | 3:b6d2c5195055 | 28 | static const uint16_t serviceList[] = { |
PostaL | 2:654ee4b3950f | 29 | GattService::UUID_ENVIRONMENTAL_SERVICE, |
PostaL | 2:654ee4b3950f | 30 | GattService::UUID_BATTERY_SERVICE |
PostaL | 2:654ee4b3950f | 31 | }; |
PostaL | 3:b6d2c5195055 | 32 | |
PostaL | 0:7f951f57dbd2 | 33 | static volatile bool triggerSensorPolling = false; |
PostaL | 0:7f951f57dbd2 | 34 | |
PostaL | 0:7f951f57dbd2 | 35 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
PostaL | 0:7f951f57dbd2 | 36 | { |
PostaL | 4:fddb2d7c7c61 | 37 | /* Restart Advertising on disconnection*/ |
PostaL | 0:7f951f57dbd2 | 38 | ble.gap().startAdvertising(); |
PostaL | 0:7f951f57dbd2 | 39 | } |
PostaL | 0:7f951f57dbd2 | 40 | |
PostaL | 1:f7fe82a00d4e | 41 | void blink(void) |
PostaL | 0:7f951f57dbd2 | 42 | { |
PostaL | 3:b6d2c5195055 | 43 | // statusLED = !statusLED; |
PostaL | 0:7f951f57dbd2 | 44 | triggerSensorPolling = true; |
PostaL | 0:7f951f57dbd2 | 45 | } |
PostaL | 0:7f951f57dbd2 | 46 | |
PostaL | 4:fddb2d7c7c61 | 47 | void updateFromBMP180() { |
PostaL | 4:fddb2d7c7c61 | 48 | uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I); |
PostaL | 4:fddb2d7c7c61 | 49 | |
PostaL | 4:fddb2d7c7c61 | 50 | printf("BMP-180 is 0x%x\n\r", c); |
PostaL | 4:fddb2d7c7c61 | 51 | printf("BMP-180 should be 0x55\n"); |
PostaL | 4:fddb2d7c7c61 | 52 | |
PostaL | 4:fddb2d7c7c61 | 53 | if(c == 0x55) { |
PostaL | 4:fddb2d7c7c61 | 54 | printf("BMP-180 online\n"); |
PostaL | 4:fddb2d7c7c61 | 55 | |
PostaL | 4:fddb2d7c7c61 | 56 | printf("Calibrating BMP-180..."); |
PostaL | 4:fddb2d7c7c61 | 57 | bmp180.BMP180Calibration(); |
PostaL | 4:fddb2d7c7c61 | 58 | printf("done\n"); |
PostaL | 4:fddb2d7c7c61 | 59 | } |
PostaL | 4:fddb2d7c7c61 | 60 | else |
PostaL | 4:fddb2d7c7c61 | 61 | { |
PostaL | 4:fddb2d7c7c61 | 62 | printf("BMP-180 unreachable\n"); |
PostaL | 4:fddb2d7c7c61 | 63 | return; |
PostaL | 4:fddb2d7c7c61 | 64 | } |
PostaL | 4:fddb2d7c7c61 | 65 | |
PostaL | 4:fddb2d7c7c61 | 66 | temperature = (float)bmp180.BMP180GetTemperature()/10.0f; |
PostaL | 4:fddb2d7c7c61 | 67 | pressure = (float)bmp180.BMP180GetPressure(); |
PostaL | 4:fddb2d7c7c61 | 68 | } |
PostaL | 4:fddb2d7c7c61 | 69 | |
PostaL | 4:fddb2d7c7c61 | 70 | void updateFromDHT() { |
PostaL | 4:fddb2d7c7c61 | 71 | wait(3); |
PostaL | 4:fddb2d7c7c61 | 72 | int err = dht.readData(); |
PostaL | 4:fddb2d7c7c61 | 73 | if (err == 0) { |
PostaL | 4:fddb2d7c7c61 | 74 | humidity = dht.ReadHumidity(); |
PostaL | 4:fddb2d7c7c61 | 75 | printf("Dew point is %4.2f \n",dht.CalcdewPoint(dht.ReadTemperature(CELCIUS), humidity)); |
PostaL | 4:fddb2d7c7c61 | 76 | printf("Dew point (fast) is %4.2f \n",dht.CalcdewPointFast(dht.ReadTemperature(CELCIUS), humidity)); |
PostaL | 4:fddb2d7c7c61 | 77 | } |
PostaL | 4:fddb2d7c7c61 | 78 | else { |
PostaL | 4:fddb2d7c7c61 | 79 | printf("Error reading DHT: %d \n", err); |
PostaL | 4:fddb2d7c7c61 | 80 | } |
PostaL | 4:fddb2d7c7c61 | 81 | } |
PostaL | 4:fddb2d7c7c61 | 82 | |
PostaL | 0:7f951f57dbd2 | 83 | int main(void) |
PostaL | 0:7f951f57dbd2 | 84 | { |
PostaL | 4:fddb2d7c7c61 | 85 | printf("Start\n"); |
PostaL | 3:b6d2c5195055 | 86 | |
PostaL | 3:b6d2c5195055 | 87 | okLED = 1; |
PostaL | 3:b6d2c5195055 | 88 | errLED = 1; |
PostaL | 3:b6d2c5195055 | 89 | |
PostaL | 0:7f951f57dbd2 | 90 | Ticker ticker; |
PostaL | 4:fddb2d7c7c61 | 91 | ticker.attach(blink, 5); |
PostaL | 0:7f951f57dbd2 | 92 | |
PostaL | 0:7f951f57dbd2 | 93 | ble.init(); |
PostaL | 0:7f951f57dbd2 | 94 | ble.gap().onDisconnection(disconnectionCallback); |
PostaL | 0:7f951f57dbd2 | 95 | |
PostaL | 2:654ee4b3950f | 96 | /* Setup weather service. */ |
PostaL | 2:654ee4b3950f | 97 | WeatherService weatherService(ble); |
PostaL | 2:654ee4b3950f | 98 | BatteryService batteryService(ble, 0); |
PostaL | 2:654ee4b3950f | 99 | Battery battery(BATTERY_PIN); |
PostaL | 0:7f951f57dbd2 | 100 | |
PostaL | 0:7f951f57dbd2 | 101 | /* setup advertising */ |
PostaL | 0:7f951f57dbd2 | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
PostaL | 3:b6d2c5195055 | 103 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)serviceList, sizeof(serviceList)); |
PostaL | 2:654ee4b3950f | 104 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN); |
PostaL | 0:7f951f57dbd2 | 105 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
PostaL | 0:7f951f57dbd2 | 106 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
PostaL | 0:7f951f57dbd2 | 107 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
PostaL | 0:7f951f57dbd2 | 108 | ble.gap().startAdvertising(); |
PostaL | 0:7f951f57dbd2 | 109 | |
PostaL | 0:7f951f57dbd2 | 110 | while (true) { |
PostaL | 0:7f951f57dbd2 | 111 | if (triggerSensorPolling && ble.getGapState().connected) { |
PostaL | 3:b6d2c5195055 | 112 | okLED = 1; |
PostaL | 0:7f951f57dbd2 | 113 | triggerSensorPolling = false; |
PostaL | 0:7f951f57dbd2 | 114 | |
PostaL | 4:fddb2d7c7c61 | 115 | instrumentsPower = 1; |
PostaL | 4:fddb2d7c7c61 | 116 | wait(1); |
PostaL | 4:fddb2d7c7c61 | 117 | updateFromBMP180(); |
PostaL | 4:fddb2d7c7c61 | 118 | updateFromDHT(); |
PostaL | 4:fddb2d7c7c61 | 119 | instrumentsPower = 0; |
PostaL | 4:fddb2d7c7c61 | 120 | |
PostaL | 4:fddb2d7c7c61 | 121 | printf("Temp: %.1f C \n", temperature); |
PostaL | 4:fddb2d7c7c61 | 122 | printf("Pressure: %.3f Pa \n", pressure); |
PostaL | 4:fddb2d7c7c61 | 123 | printf("Humidity: %.1f % \n", humidity); |
PostaL | 4:fddb2d7c7c61 | 124 | printf("---\n"); |
PostaL | 3:b6d2c5195055 | 125 | |
PostaL | 3:b6d2c5195055 | 126 | weatherService.updateTemperature(temperature); |
PostaL | 3:b6d2c5195055 | 127 | weatherService.updatePressure(pressure); |
PostaL | 4:fddb2d7c7c61 | 128 | weatherService.updateHumidity(humidity); |
PostaL | 3:b6d2c5195055 | 129 | |
PostaL | 3:b6d2c5195055 | 130 | |
PostaL | 2:654ee4b3950f | 131 | batteryService.updateBatteryLevel(battery.read()); |
PostaL | 0:7f951f57dbd2 | 132 | } else { |
PostaL | 0:7f951f57dbd2 | 133 | ble.waitForEvent(); |
PostaL | 0:7f951f57dbd2 | 134 | } |
PostaL | 0:7f951f57dbd2 | 135 | } |
PostaL | 0:7f951f57dbd2 | 136 | } |