BMP180 and BLE

Dependencies:   BLE_API mbed nRF51822

Fork of WeatherStation by Weather man

Committer:
phivari
Date:
Wed Jan 10 16:42:44 2018 +0000
Revision:
6:25ed8e2df02e
Parent:
5:fe4888cc60cc
WeatherStation_BLE_BMP180

Who changed what in which revision?

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