BMP180 and BLE

Dependencies:   BLE_API mbed nRF51822

Fork of WeatherStation by Weather man

Committer:
PostaL
Date:
Thu Nov 12 22:42:05 2015 +0000
Revision:
3:b6d2c5195055
Parent:
2:654ee4b3950f
Child:
4:fddb2d7c7c61
Added BMP180 measurements

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 0:7f951f57dbd2 10
PostaL 0:7f951f57dbd2 11 BLE ble;
PostaL 3:b6d2c5195055 12 DigitalOut okLED(LED1);
PostaL 3:b6d2c5195055 13 DigitalOut errLED(LED2);
PostaL 3:b6d2c5195055 14 DigitalOut instrumentsPower(p30);
PostaL 0:7f951f57dbd2 15
PostaL 3:b6d2c5195055 16 BMP180 bmp180;
PostaL 3:b6d2c5195055 17 Serial pc(USBTX, USBRX);
PostaL 3:b6d2c5195055 18
PostaL 3:b6d2c5195055 19 const static char DEVICE_NAME[] = "Weather Station";
PostaL 3:b6d2c5195055 20
PostaL 3:b6d2c5195055 21 static const uint16_t serviceList[] = {
PostaL 2:654ee4b3950f 22 GattService::UUID_ENVIRONMENTAL_SERVICE,
PostaL 2:654ee4b3950f 23 GattService::UUID_BATTERY_SERVICE
PostaL 2:654ee4b3950f 24 };
PostaL 3:b6d2c5195055 25
PostaL 0:7f951f57dbd2 26 static volatile bool triggerSensorPolling = false;
PostaL 0:7f951f57dbd2 27
PostaL 0:7f951f57dbd2 28 /* Restart Advertising on disconnection*/
PostaL 0:7f951f57dbd2 29 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
PostaL 0:7f951f57dbd2 30 {
PostaL 0:7f951f57dbd2 31 ble.gap().startAdvertising();
PostaL 0:7f951f57dbd2 32 }
PostaL 0:7f951f57dbd2 33
PostaL 1:f7fe82a00d4e 34 void blink(void)
PostaL 0:7f951f57dbd2 35 {
PostaL 3:b6d2c5195055 36 // statusLED = !statusLED;
PostaL 0:7f951f57dbd2 37 triggerSensorPolling = true;
PostaL 0:7f951f57dbd2 38 }
PostaL 0:7f951f57dbd2 39
PostaL 0:7f951f57dbd2 40 int main(void)
PostaL 0:7f951f57dbd2 41 {
PostaL 3:b6d2c5195055 42 pc.printf("Start\n");
PostaL 3:b6d2c5195055 43
PostaL 3:b6d2c5195055 44 okLED = 1;
PostaL 3:b6d2c5195055 45 errLED = 1;
PostaL 3:b6d2c5195055 46 instrumentsPower = 1;
PostaL 3:b6d2c5195055 47 wait(1);
PostaL 3:b6d2c5195055 48 uint8_t c = bmp180.readByte(BMP180_ADDRESS, BMP180_WHO_AM_I);
PostaL 3:b6d2c5195055 49
PostaL 3:b6d2c5195055 50 pc.printf("BMP-180 is 0x%x\n\r", c);
PostaL 3:b6d2c5195055 51 pc.printf("BMP-180 should be 0x55\n");
PostaL 3:b6d2c5195055 52
PostaL 3:b6d2c5195055 53 if(c == 0x55) {
PostaL 3:b6d2c5195055 54 okLED = 0;
PostaL 3:b6d2c5195055 55 pc.printf("BMP-180 online...\n");
PostaL 3:b6d2c5195055 56
PostaL 3:b6d2c5195055 57 bmp180.BMP180Calibration();
PostaL 3:b6d2c5195055 58 pc.printf("BMP-180 calibration complete...\n");
PostaL 3:b6d2c5195055 59 }
PostaL 3:b6d2c5195055 60 else
PostaL 3:b6d2c5195055 61 {
PostaL 3:b6d2c5195055 62 errLED = 0;
PostaL 3:b6d2c5195055 63 pc.printf("BMP-180 unreachable...\n");
PostaL 3:b6d2c5195055 64 while(1); // idle here forever
PostaL 3:b6d2c5195055 65 }
PostaL 3:b6d2c5195055 66
PostaL 0:7f951f57dbd2 67 Ticker ticker;
PostaL 3:b6d2c5195055 68 ticker.attach(blink, 2);
PostaL 0:7f951f57dbd2 69
PostaL 0:7f951f57dbd2 70 ble.init();
PostaL 0:7f951f57dbd2 71 ble.gap().onDisconnection(disconnectionCallback);
PostaL 0:7f951f57dbd2 72
PostaL 2:654ee4b3950f 73 /* Setup weather service. */
PostaL 2:654ee4b3950f 74 WeatherService weatherService(ble);
PostaL 2:654ee4b3950f 75 BatteryService batteryService(ble, 0);
PostaL 2:654ee4b3950f 76 Battery battery(BATTERY_PIN);
PostaL 0:7f951f57dbd2 77
PostaL 0:7f951f57dbd2 78 /* setup advertising */
PostaL 0:7f951f57dbd2 79 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
PostaL 3:b6d2c5195055 80 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)serviceList, sizeof(serviceList));
PostaL 2:654ee4b3950f 81 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN);
PostaL 0:7f951f57dbd2 82 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
PostaL 0:7f951f57dbd2 83 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
PostaL 0:7f951f57dbd2 84 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
PostaL 0:7f951f57dbd2 85 ble.gap().startAdvertising();
PostaL 0:7f951f57dbd2 86
PostaL 0:7f951f57dbd2 87 while (true) {
PostaL 0:7f951f57dbd2 88 if (triggerSensorPolling && ble.getGapState().connected) {
PostaL 3:b6d2c5195055 89 okLED = 1;
PostaL 0:7f951f57dbd2 90 triggerSensorPolling = false;
PostaL 0:7f951f57dbd2 91
PostaL 3:b6d2c5195055 92 float temperature = (float)bmp180.BMP180GetTemperature()/10.0f;
PostaL 3:b6d2c5195055 93 float pressure = (float)bmp180.BMP180GetPressure();
PostaL 3:b6d2c5195055 94 float altitude = 44330.0f*( 1.0f - pow((pressure/101325.0f), (1.0f/5.255f)));
PostaL 0:7f951f57dbd2 95
PostaL 3:b6d2c5195055 96 pc.printf("Temp: %.1f C\n", temperature);
PostaL 3:b6d2c5195055 97 pc.printf("Pressure: %.3f Pa\n", pressure);
PostaL 3:b6d2c5195055 98 pc.printf("Altitude: %.1f m\n", altitude);
PostaL 3:b6d2c5195055 99 pc.printf("---\n");
PostaL 3:b6d2c5195055 100
PostaL 3:b6d2c5195055 101 weatherService.updateTemperature(temperature);
PostaL 3:b6d2c5195055 102 weatherService.updatePressure(pressure);
PostaL 2:654ee4b3950f 103 weatherService.updateHumidity(0);
PostaL 3:b6d2c5195055 104
PostaL 3:b6d2c5195055 105
PostaL 2:654ee4b3950f 106 batteryService.updateBatteryLevel(battery.read());
PostaL 0:7f951f57dbd2 107 } else {
PostaL 0:7f951f57dbd2 108 ble.waitForEvent();
PostaL 0:7f951f57dbd2 109 }
PostaL 0:7f951f57dbd2 110 }
PostaL 0:7f951f57dbd2 111 }