This is mandatory
Dependencies: BLE_API mbed nRF51822 SHT21_ncleee
main.cpp@2:654ee4b3950f, 2015-11-09 (annotated)
- Committer:
- PostaL
- Date:
- Mon Nov 09 23:09:16 2015 +0000
- Revision:
- 2:654ee4b3950f
- Parent:
- 1:f7fe82a00d4e
- Child:
- 3:b6d2c5195055
Added battery service and reading for tiny ble
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PostaL | 0:7f951f57dbd2 | 1 | /* mbed Microcontroller Library |
PostaL | 0:7f951f57dbd2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
PostaL | 0:7f951f57dbd2 | 3 | * |
PostaL | 0:7f951f57dbd2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
PostaL | 0:7f951f57dbd2 | 5 | * you may not use this file except in compliance with the License. |
PostaL | 0:7f951f57dbd2 | 6 | * You may obtain a copy of the License at |
PostaL | 0:7f951f57dbd2 | 7 | * |
PostaL | 0:7f951f57dbd2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
PostaL | 0:7f951f57dbd2 | 9 | * |
PostaL | 0:7f951f57dbd2 | 10 | * Unless required by applicable law or agreed to in writing, software |
PostaL | 0:7f951f57dbd2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
PostaL | 0:7f951f57dbd2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
PostaL | 0:7f951f57dbd2 | 13 | * See the License for the specific language governing permissions and |
PostaL | 0:7f951f57dbd2 | 14 | * limitations under the License. |
PostaL | 0:7f951f57dbd2 | 15 | */ |
PostaL | 0:7f951f57dbd2 | 16 | |
PostaL | 0:7f951f57dbd2 | 17 | #include "mbed.h" |
PostaL | 0:7f951f57dbd2 | 18 | #include "BLE.h" |
PostaL | 2:654ee4b3950f | 19 | #include "battery.h" |
PostaL | 2:654ee4b3950f | 20 | #include "WeatherService.h" |
PostaL | 2:654ee4b3950f | 21 | #include "BatteryService.h" |
PostaL | 0:7f951f57dbd2 | 22 | |
PostaL | 0:7f951f57dbd2 | 23 | BLE ble; |
PostaL | 0:7f951f57dbd2 | 24 | DigitalOut led1(LED1); |
PostaL | 2:654ee4b3950f | 25 | AnalogIn batteryPin(p1); |
PostaL | 0:7f951f57dbd2 | 26 | |
PostaL | 0:7f951f57dbd2 | 27 | const static char DEVICE_NAME[] = "Weather Station"; |
PostaL | 2:654ee4b3950f | 28 | static const uint16_t uuid16_list[] = { |
PostaL | 2:654ee4b3950f | 29 | GattService::UUID_ENVIRONMENTAL_SERVICE, |
PostaL | 2:654ee4b3950f | 30 | GattService::UUID_BATTERY_SERVICE |
PostaL | 2:654ee4b3950f | 31 | }; |
PostaL | 0:7f951f57dbd2 | 32 | static volatile bool triggerSensorPolling = false; |
PostaL | 0:7f951f57dbd2 | 33 | |
PostaL | 0:7f951f57dbd2 | 34 | /* Restart Advertising on disconnection*/ |
PostaL | 0:7f951f57dbd2 | 35 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
PostaL | 0:7f951f57dbd2 | 36 | { |
PostaL | 0:7f951f57dbd2 | 37 | ble.gap().startAdvertising(); |
PostaL | 0:7f951f57dbd2 | 38 | } |
PostaL | 0:7f951f57dbd2 | 39 | |
PostaL | 1:f7fe82a00d4e | 40 | void blink(void) |
PostaL | 0:7f951f57dbd2 | 41 | { |
PostaL | 2:654ee4b3950f | 42 | // led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
PostaL | 0:7f951f57dbd2 | 43 | triggerSensorPolling = true; |
PostaL | 0:7f951f57dbd2 | 44 | } |
PostaL | 0:7f951f57dbd2 | 45 | |
PostaL | 0:7f951f57dbd2 | 46 | int main(void) |
PostaL | 0:7f951f57dbd2 | 47 | { |
PostaL | 0:7f951f57dbd2 | 48 | led1 = 1; |
PostaL | 0:7f951f57dbd2 | 49 | Ticker ticker; |
PostaL | 1:f7fe82a00d4e | 50 | ticker.attach(blink, 1); |
PostaL | 0:7f951f57dbd2 | 51 | |
PostaL | 0:7f951f57dbd2 | 52 | ble.init(); |
PostaL | 0:7f951f57dbd2 | 53 | ble.gap().onDisconnection(disconnectionCallback); |
PostaL | 0:7f951f57dbd2 | 54 | |
PostaL | 2:654ee4b3950f | 55 | /* Setup weather service. */ |
PostaL | 1:f7fe82a00d4e | 56 | float currentTemperature = 42.22; |
PostaL | 2:654ee4b3950f | 57 | WeatherService weatherService(ble); |
PostaL | 2:654ee4b3950f | 58 | BatteryService batteryService(ble, 0); |
PostaL | 2:654ee4b3950f | 59 | Battery battery(BATTERY_PIN); |
PostaL | 0:7f951f57dbd2 | 60 | |
PostaL | 0:7f951f57dbd2 | 61 | /* setup advertising */ |
PostaL | 0:7f951f57dbd2 | 62 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
PostaL | 0:7f951f57dbd2 | 63 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
PostaL | 2:654ee4b3950f | 64 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN); |
PostaL | 0:7f951f57dbd2 | 65 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
PostaL | 0:7f951f57dbd2 | 66 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
PostaL | 0:7f951f57dbd2 | 67 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
PostaL | 0:7f951f57dbd2 | 68 | ble.gap().startAdvertising(); |
PostaL | 0:7f951f57dbd2 | 69 | |
PostaL | 0:7f951f57dbd2 | 70 | while (true) { |
PostaL | 0:7f951f57dbd2 | 71 | if (triggerSensorPolling && ble.getGapState().connected) { |
PostaL | 0:7f951f57dbd2 | 72 | triggerSensorPolling = false; |
PostaL | 0:7f951f57dbd2 | 73 | |
PostaL | 0:7f951f57dbd2 | 74 | /* Do blocking calls or whatever is necessary for sensor polling. */ |
PostaL | 0:7f951f57dbd2 | 75 | // error = sensor.readData(); |
PostaL | 0:7f951f57dbd2 | 76 | // if (!error) { |
PostaL | 0:7f951f57dbd2 | 77 | // thermometerService.updateTemperature(c); |
PostaL | 0:7f951f57dbd2 | 78 | // } |
PostaL | 0:7f951f57dbd2 | 79 | |
PostaL | 0:7f951f57dbd2 | 80 | /* In our case, we simply update the dummy temperature measurement. */ |
PostaL | 2:654ee4b3950f | 81 | currentTemperature += 0.1; |
PostaL | 2:654ee4b3950f | 82 | weatherService.updateTemperature(currentTemperature); |
PostaL | 2:654ee4b3950f | 83 | weatherService.updatePressure(0); |
PostaL | 2:654ee4b3950f | 84 | weatherService.updateHumidity(0); |
PostaL | 2:654ee4b3950f | 85 | batteryService.updateBatteryLevel(battery.read()); |
PostaL | 0:7f951f57dbd2 | 86 | } else { |
PostaL | 0:7f951f57dbd2 | 87 | ble.waitForEvent(); |
PostaL | 0:7f951f57dbd2 | 88 | } |
PostaL | 0:7f951f57dbd2 | 89 | } |
PostaL | 0:7f951f57dbd2 | 90 | } |