This is mandatory

Dependencies:   BLE_API mbed nRF51822 SHT21_ncleee

Committer:
PostaL
Date:
Mon Dec 21 00:25:08 2015 +0000
Revision:
5:fe4888cc60cc
Parent:
4:fddb2d7c7c61
Added humidity reading + temp. avg. via SHT21

Who changed what in which revision?

UserRevisionLine numberNew 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 5:fe4888cc60cc 10 #include "SHT21_ncleee.h"
PostaL 0:7f951f57dbd2 11
PostaL 4:fddb2d7c7c61 12
PostaL 5:fe4888cc60cc 13 float temperature1;
PostaL 5:fe4888cc60cc 14 float temperature2;
PostaL 4:fddb2d7c7c61 15 float pressure;
PostaL 4:fddb2d7c7c61 16 float humidity;
PostaL 4:fddb2d7c7c61 17
PostaL 4:fddb2d7c7c61 18 BLE ble;
PostaL 4:fddb2d7c7c61 19
PostaL 3:b6d2c5195055 20 DigitalOut okLED(LED1);
PostaL 3:b6d2c5195055 21 DigitalOut errLED(LED2);
PostaL 3:b6d2c5195055 22 DigitalOut instrumentsPower(p30);
PostaL 0:7f951f57dbd2 23
PostaL 3:b6d2c5195055 24 BMP180 bmp180;
PostaL 5:fe4888cc60cc 25 SHT21 sht21(&i2c);
PostaL 5:fe4888cc60cc 26
PostaL 3:b6d2c5195055 27
PostaL 3:b6d2c5195055 28 const static char DEVICE_NAME[] = "Weather Station";
PostaL 3:b6d2c5195055 29
PostaL 3:b6d2c5195055 30 static const uint16_t serviceList[] = {
PostaL 2:654ee4b3950f 31 GattService::UUID_ENVIRONMENTAL_SERVICE,
PostaL 2:654ee4b3950f 32 GattService::UUID_BATTERY_SERVICE
PostaL 2:654ee4b3950f 33 };
PostaL 3:b6d2c5195055 34
PostaL 5:fe4888cc60cc 35 static volatile bool triggerSensorPolling = false;
PostaL 0:7f951f57dbd2 36
PostaL 0:7f951f57dbd2 37 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
PostaL 0:7f951f57dbd2 38 {
PostaL 4:fddb2d7c7c61 39 /* Restart Advertising on disconnection*/
PostaL 0:7f951f57dbd2 40 ble.gap().startAdvertising();
PostaL 0:7f951f57dbd2 41 }
PostaL 0:7f951f57dbd2 42
PostaL 1:f7fe82a00d4e 43 void blink(void)
PostaL 0:7f951f57dbd2 44 {
PostaL 3:b6d2c5195055 45 // statusLED = !statusLED;
PostaL 0:7f951f57dbd2 46 triggerSensorPolling = true;
PostaL 0:7f951f57dbd2 47 }
PostaL 0:7f951f57dbd2 48
PostaL 4:fddb2d7c7c61 49 void updateFromBMP180() {
PostaL 4:fddb2d7c7c61 50 uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I);
PostaL 4:fddb2d7c7c61 51
PostaL 4:fddb2d7c7c61 52 printf("BMP-180 is 0x%x\n\r", c);
PostaL 4:fddb2d7c7c61 53 printf("BMP-180 should be 0x55\n");
PostaL 4:fddb2d7c7c61 54
PostaL 4:fddb2d7c7c61 55 if(c == 0x55) {
PostaL 4:fddb2d7c7c61 56 printf("BMP-180 online\n");
PostaL 4:fddb2d7c7c61 57
PostaL 4:fddb2d7c7c61 58 printf("Calibrating BMP-180...");
PostaL 4:fddb2d7c7c61 59 bmp180.BMP180Calibration();
PostaL 4:fddb2d7c7c61 60 printf("done\n");
PostaL 4:fddb2d7c7c61 61 }
PostaL 4:fddb2d7c7c61 62 else
PostaL 4:fddb2d7c7c61 63 {
PostaL 4:fddb2d7c7c61 64 printf("BMP-180 unreachable\n");
PostaL 4:fddb2d7c7c61 65 return;
PostaL 4:fddb2d7c7c61 66 }
PostaL 4:fddb2d7c7c61 67
PostaL 5:fe4888cc60cc 68 temperature1 = (float)bmp180.BMP180GetTemperature()/10.0f;
PostaL 4:fddb2d7c7c61 69 pressure = (float)bmp180.BMP180GetPressure();
PostaL 4:fddb2d7c7c61 70 }
PostaL 4:fddb2d7c7c61 71
PostaL 5:fe4888cc60cc 72 void updateFromSHT21() {
PostaL 5:fe4888cc60cc 73 temperature2 = sht21.readTemp();
PostaL 5:fe4888cc60cc 74 humidity = sht21.readHumidity();
PostaL 4:fddb2d7c7c61 75 }
PostaL 4:fddb2d7c7c61 76
PostaL 0:7f951f57dbd2 77 int main(void)
PostaL 0:7f951f57dbd2 78 {
PostaL 4:fddb2d7c7c61 79 printf("Start\n");
PostaL 3:b6d2c5195055 80
PostaL 3:b6d2c5195055 81 okLED = 1;
PostaL 3:b6d2c5195055 82 errLED = 1;
PostaL 3:b6d2c5195055 83
PostaL 0:7f951f57dbd2 84 Ticker ticker;
PostaL 4:fddb2d7c7c61 85 ticker.attach(blink, 5);
PostaL 0:7f951f57dbd2 86
PostaL 0:7f951f57dbd2 87 ble.init();
PostaL 0:7f951f57dbd2 88 ble.gap().onDisconnection(disconnectionCallback);
PostaL 0:7f951f57dbd2 89
PostaL 2:654ee4b3950f 90 /* Setup weather service. */
PostaL 2:654ee4b3950f 91 WeatherService weatherService(ble);
PostaL 2:654ee4b3950f 92 BatteryService batteryService(ble, 0);
PostaL 2:654ee4b3950f 93 Battery battery(BATTERY_PIN);
PostaL 0:7f951f57dbd2 94
PostaL 0:7f951f57dbd2 95 /* setup advertising */
PostaL 0:7f951f57dbd2 96 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
PostaL 3:b6d2c5195055 97 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)serviceList, sizeof(serviceList));
PostaL 2:654ee4b3950f 98 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN);
PostaL 0:7f951f57dbd2 99 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
PostaL 0:7f951f57dbd2 100 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
PostaL 0:7f951f57dbd2 101 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
PostaL 0:7f951f57dbd2 102 ble.gap().startAdvertising();
PostaL 0:7f951f57dbd2 103
PostaL 0:7f951f57dbd2 104 while (true) {
PostaL 0:7f951f57dbd2 105 if (triggerSensorPolling && ble.getGapState().connected) {
PostaL 3:b6d2c5195055 106 okLED = 1;
PostaL 0:7f951f57dbd2 107 triggerSensorPolling = false;
PostaL 0:7f951f57dbd2 108
PostaL 4:fddb2d7c7c61 109 instrumentsPower = 1;
PostaL 4:fddb2d7c7c61 110 wait(1);
PostaL 4:fddb2d7c7c61 111 updateFromBMP180();
PostaL 5:fe4888cc60cc 112 updateFromSHT21();
PostaL 4:fddb2d7c7c61 113 instrumentsPower = 0;
PostaL 4:fddb2d7c7c61 114
PostaL 5:fe4888cc60cc 115 float temperature = (temperature1 + temperature2) / 2;
PostaL 5:fe4888cc60cc 116
PostaL 5:fe4888cc60cc 117 printf("Temp1: %.1f ºC \n", temperature1);
PostaL 5:fe4888cc60cc 118 printf("Temp2: %.1f ºC \n", temperature2);
PostaL 5:fe4888cc60cc 119 printf("Temp Avg.: %.1f ºC \n", temperature);
PostaL 4:fddb2d7c7c61 120 printf("Pressure: %.3f Pa \n", pressure);
PostaL 5:fe4888cc60cc 121 printf("Humidity: %.1f%% \n", humidity);
PostaL 4:fddb2d7c7c61 122 printf("---\n");
PostaL 3:b6d2c5195055 123
PostaL 5:fe4888cc60cc 124
PostaL 3:b6d2c5195055 125 weatherService.updateTemperature(temperature);
PostaL 3:b6d2c5195055 126 weatherService.updatePressure(pressure);
PostaL 4:fddb2d7c7c61 127 weatherService.updateHumidity(humidity);
PostaL 3:b6d2c5195055 128
PostaL 3:b6d2c5195055 129
PostaL 2:654ee4b3950f 130 batteryService.updateBatteryLevel(battery.read());
PostaL 0:7f951f57dbd2 131 } else {
PostaL 0:7f951f57dbd2 132 ble.waitForEvent();
PostaL 0:7f951f57dbd2 133 }
PostaL 0:7f951f57dbd2 134 }
PostaL 0:7f951f57dbd2 135 }