BLE Thermometer example

Dependencies:   BSP_B-L475E-IOT01

This example is a fork of the following mbed-os example:

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-Thermometer/

Please read the documentation in this page.

Committer:
bcostm
Date:
Wed Jul 26 08:10:04 2017 +0000
Revision:
39:ac224f69256b
Parent:
27:0c99c4b33d56
Add reading of board temp sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 2:cc8349878a7d 1 /* mbed Microcontroller Library
mbed_official 2:cc8349878a7d 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 2:cc8349878a7d 3 *
mbed_official 2:cc8349878a7d 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 2:cc8349878a7d 5 * you may not use this file except in compliance with the License.
mbed_official 2:cc8349878a7d 6 * You may obtain a copy of the License at
mbed_official 2:cc8349878a7d 7 *
mbed_official 2:cc8349878a7d 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 2:cc8349878a7d 9 *
mbed_official 2:cc8349878a7d 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 2:cc8349878a7d 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 2:cc8349878a7d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 2:cc8349878a7d 13 * See the License for the specific language governing permissions and
mbed_official 2:cc8349878a7d 14 * limitations under the License.
mbed_official 2:cc8349878a7d 15 */
mbed_official 2:cc8349878a7d 16
mbed_official 11:4e356c89ad66 17 #include <events/mbed_events.h>
mbed_official 2:cc8349878a7d 18 #include "mbed.h"
mbed_official 2:cc8349878a7d 19 #include "ble/BLE.h"
mbed_official 2:cc8349878a7d 20 #include "ble/services/HealthThermometerService.h"
mbed_official 2:cc8349878a7d 21
bcostm 39:ac224f69256b 22 // Uncomment this line if you want to use the board temperature sensor instead of
bcostm 39:ac224f69256b 23 // a simulated one.
bcostm 39:ac224f69256b 24 #define USE_BOARD_TEMP_SENSOR
bcostm 39:ac224f69256b 25
bcostm 39:ac224f69256b 26 #ifdef USE_BOARD_TEMP_SENSOR
bcostm 39:ac224f69256b 27 #include "stm32l475e_iot01_tsensor.h"
bcostm 39:ac224f69256b 28 #endif
bcostm 39:ac224f69256b 29
mbed_official 2:cc8349878a7d 30 DigitalOut led1(LED1, 1);
mbed_official 2:cc8349878a7d 31
mbed_official 2:cc8349878a7d 32 const static char DEVICE_NAME[] = "Therm";
mbed_official 2:cc8349878a7d 33 static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE};
mbed_official 2:cc8349878a7d 34
mbed_official 2:cc8349878a7d 35 static float currentTemperature = 39.6;
mbed_official 2:cc8349878a7d 36 static HealthThermometerService *thermometerServicePtr;
mbed_official 2:cc8349878a7d 37
mbed_official 27:0c99c4b33d56 38 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
mbed_official 2:cc8349878a7d 39
mbed_official 2:cc8349878a7d 40 /* Restart Advertising on disconnection*/
mbed_official 2:cc8349878a7d 41 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbed_official 2:cc8349878a7d 42 {
mbed_official 2:cc8349878a7d 43 BLE::Instance().gap().startAdvertising();
mbed_official 2:cc8349878a7d 44 }
mbed_official 2:cc8349878a7d 45
mbed_official 2:cc8349878a7d 46 void updateSensorValue(void) {
mbed_official 2:cc8349878a7d 47 /* Do blocking calls or whatever is necessary for sensor polling.
mbed_official 2:cc8349878a7d 48 In our case, we simply update the Temperature measurement. */
bcostm 39:ac224f69256b 49 #ifdef USE_BOARD_TEMP_SENSOR
bcostm 39:ac224f69256b 50 currentTemperature = BSP_TSENSOR_ReadTemp();
bcostm 39:ac224f69256b 51 #else
mbed_official 2:cc8349878a7d 52 currentTemperature = (currentTemperature + 0.1 > 43.0) ? 39.6 : currentTemperature + 0.1;
bcostm 39:ac224f69256b 53 #endif
mbed_official 2:cc8349878a7d 54 thermometerServicePtr->updateTemperature(currentTemperature);
mbed_official 2:cc8349878a7d 55 }
mbed_official 2:cc8349878a7d 56
mbed_official 2:cc8349878a7d 57 void periodicCallback(void)
mbed_official 2:cc8349878a7d 58 {
mbed_official 2:cc8349878a7d 59 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
mbed_official 2:cc8349878a7d 60
mbed_official 2:cc8349878a7d 61 if (BLE::Instance().gap().getState().connected) {
mbed_official 11:4e356c89ad66 62 eventQueue.call(updateSensorValue);
mbed_official 2:cc8349878a7d 63 }
mbed_official 2:cc8349878a7d 64 }
mbed_official 2:cc8349878a7d 65
mbed_official 2:cc8349878a7d 66 void onBleInitError(BLE &ble, ble_error_t error)
mbed_official 2:cc8349878a7d 67 {
mbed_official 2:cc8349878a7d 68 /* Initialization error handling should go here */
mbed_official 2:cc8349878a7d 69 }
mbed_official 2:cc8349878a7d 70
mbed_official 2:cc8349878a7d 71 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
mbed_official 2:cc8349878a7d 72 {
mbed_official 2:cc8349878a7d 73 BLE& ble = params->ble;
mbed_official 2:cc8349878a7d 74 ble_error_t error = params->error;
mbed_official 2:cc8349878a7d 75
mbed_official 2:cc8349878a7d 76 if (error != BLE_ERROR_NONE) {
mbed_official 2:cc8349878a7d 77 onBleInitError(ble, error);
mbed_official 2:cc8349878a7d 78 return;
mbed_official 2:cc8349878a7d 79 }
mbed_official 2:cc8349878a7d 80
mbed_official 2:cc8349878a7d 81 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
mbed_official 2:cc8349878a7d 82 return;
mbed_official 2:cc8349878a7d 83 }
mbed_official 2:cc8349878a7d 84
mbed_official 2:cc8349878a7d 85 ble.gap().onDisconnection(disconnectionCallback);
mbed_official 2:cc8349878a7d 86
mbed_official 2:cc8349878a7d 87 /* Setup primary service. */
mbed_official 2:cc8349878a7d 88 thermometerServicePtr = new HealthThermometerService(ble, currentTemperature, HealthThermometerService::LOCATION_EAR);
mbed_official 2:cc8349878a7d 89
mbed_official 2:cc8349878a7d 90 /* setup advertising */
mbed_official 2:cc8349878a7d 91 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
mbed_official 2:cc8349878a7d 92 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
mbed_official 2:cc8349878a7d 93 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR);
mbed_official 2:cc8349878a7d 94 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
mbed_official 2:cc8349878a7d 95 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
mbed_official 2:cc8349878a7d 96 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
mbed_official 2:cc8349878a7d 97 ble.gap().startAdvertising();
mbed_official 2:cc8349878a7d 98 }
mbed_official 2:cc8349878a7d 99
mbed_official 2:cc8349878a7d 100 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
mbed_official 2:cc8349878a7d 101 BLE &ble = BLE::Instance();
mbed_official 11:4e356c89ad66 102 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
mbed_official 2:cc8349878a7d 103 }
mbed_official 2:cc8349878a7d 104
mbed_official 2:cc8349878a7d 105 int main()
mbed_official 2:cc8349878a7d 106 {
bcostm 39:ac224f69256b 107 #ifdef USE_BOARD_TEMP_SENSOR
bcostm 39:ac224f69256b 108 BSP_TSENSOR_Init();
bcostm 39:ac224f69256b 109 #endif
bcostm 39:ac224f69256b 110
mbed_official 11:4e356c89ad66 111 eventQueue.call_every(500, periodicCallback);
mbed_official 2:cc8349878a7d 112
mbed_official 2:cc8349878a7d 113 BLE &ble = BLE::Instance();
mbed_official 2:cc8349878a7d 114 ble.onEventsToProcess(scheduleBleEventsProcessing);
mbed_official 2:cc8349878a7d 115 ble.init(bleInitComplete);
mbed_official 2:cc8349878a7d 116
mbed_official 11:4e356c89ad66 117 eventQueue.dispatch_forever();
mbed_official 2:cc8349878a7d 118
mbed_official 2:cc8349878a7d 119 return 0;
mbed_official 2:cc8349878a7d 120 }