BLE example with Environmental Sensing service.

Dependencies:   BSP_B-L475E-IOT01

Hello IoT BLE

This project uses Environmental Sensing Service and its characteristics:

Characteristic values can be read or they might be observed with notifications enabled.

Please note that this example uses slightly modified Environmental service.

Also there have been some opened issues regarding value types Issue 1 and Issue 2.

Note that this application was developed as a part of a larger project Hello IoT, but can be used as a standalone.

(Source files can be found at the end of this page.)

Running the application

The sample application can be seen on any BLE scanner on a smartphone. If you don't have a scanner on your phone, please install:

Hardware requirements are in the main readme.

Building instructions

Building with mbed CLI

If you'd like to use mbed CLI to build this, then you should refer to the main readme. The instructions here relate to using the Mbed Online Compiler.

Building with Mbed Online Compiler

In order to build this example in the Mbed Online Compiler, first import the example using the Import button on the right hand side.

Next, select a platform to build for. This must either be a platform that supports BLE, for example the NRF51-DK, or one of the following:

List of platforms supporting Bluetooth Low Energy.

Or you must also add a piece of hardware and the supporting library that includes a Bluetooth Low Energy driver for that hardware, for example the K64F or NUCLEO_F401RE with the X-NUCLEO-IDB05A1

List of components supporting Bluetooth Low Energy.

Once you have selected your platform, compile the example and drag and drop the resulting binary onto your board.

Note: This example was tested with DISCO_L475VG_IOT01A (ref B-L475E-IOT01A), so when using onboard sensors instead of simulated ones, make sure you select this board.

For general instructions on using the Mbed Online Compiler, please see the mbed Handbook.

Checking for success

Note: Screen captures depicted below show what is expected from this example if the scanner used is nRF Connect. If you encounter any difficulties consider trying another scanner or another version of nRF Connect. Alternative scanners may require reference to their manuals.

  • Build the application and install it on your board as explained in the building instructions.
  • Open the BLE scanner on your phone.
  • Start a scan and connect to a device named IoT_SENSOR.

nRF Scan

  • Discover the services and the characteristics on the device. The Environmental Sensing service has the UUID 0x181A with characteristics Temperature 0x2A6E, Humidity 0x2A6F and Pressure 0x2A6D.

nRF Service

  • At this point you can read or register for notifications. In figure below, all characteristics have been registered for notification and their values should change every second.

nRF Notif

Committer:
jernej_vrscaj
Date:
Sat Dec 29 13:33:02 2018 +0000
Revision:
0:0681ebb27b3c
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jernej_vrscaj 0:0681ebb27b3c 1 /* mbed Microcontroller Library
jernej_vrscaj 0:0681ebb27b3c 2 * Copyright (c) 2006-2013 ARM Limited
jernej_vrscaj 0:0681ebb27b3c 3 *
jernej_vrscaj 0:0681ebb27b3c 4 * Licensed under the Apache License, Version 2.0 (the "License");
jernej_vrscaj 0:0681ebb27b3c 5 * you may not use this file except in compliance with the License.
jernej_vrscaj 0:0681ebb27b3c 6 * You may obtain a copy of the License at
jernej_vrscaj 0:0681ebb27b3c 7 *
jernej_vrscaj 0:0681ebb27b3c 8 * http://www.apache.org/licenses/LICENSE-2.0
jernej_vrscaj 0:0681ebb27b3c 9 *
jernej_vrscaj 0:0681ebb27b3c 10 * Unless required by applicable law or agreed to in writing, software
jernej_vrscaj 0:0681ebb27b3c 11 * distributed under the License is distributed on an "AS IS" BASIS,
jernej_vrscaj 0:0681ebb27b3c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jernej_vrscaj 0:0681ebb27b3c 13 * See the License for the specific language governing permissions and
jernej_vrscaj 0:0681ebb27b3c 14 * limitations under the License.
jernej_vrscaj 0:0681ebb27b3c 15 */
jernej_vrscaj 0:0681ebb27b3c 16
jernej_vrscaj 0:0681ebb27b3c 17 #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
jernej_vrscaj 0:0681ebb27b3c 18 #define __BLE_ENVIRONMENTAL_SERVICE_H__
jernej_vrscaj 0:0681ebb27b3c 19
jernej_vrscaj 0:0681ebb27b3c 20 #include "ble/BLE.h"
jernej_vrscaj 0:0681ebb27b3c 21 //#include "debug.h"
jernej_vrscaj 0:0681ebb27b3c 22
jernej_vrscaj 0:0681ebb27b3c 23 /**
jernej_vrscaj 0:0681ebb27b3c 24 * @note
jernej_vrscaj 0:0681ebb27b3c 25 * Modified service (refer to EnvironmentalService.h):
jernej_vrscaj 0:0681ebb27b3c 26 * - notifications added
jernej_vrscaj 0:0681ebb27b3c 27 * - conversion to temperature, humidity and pressure integer values done in main.cpp
jernej_vrscaj 0:0681ebb27b3c 28 * - updateTemperature() input argument type changed from float to TemperatureType_t
jernej_vrscaj 0:0681ebb27b3c 29 */
jernej_vrscaj 0:0681ebb27b3c 30
jernej_vrscaj 0:0681ebb27b3c 31 /**
jernej_vrscaj 0:0681ebb27b3c 32 * @class EnvironmentalService
jernej_vrscaj 0:0681ebb27b3c 33 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
jernej_vrscaj 0:0681ebb27b3c 34 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
jernej_vrscaj 0:0681ebb27b3c 35 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
jernej_vrscaj 0:0681ebb27b3c 36 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
jernej_vrscaj 0:0681ebb27b3c 37 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
jernej_vrscaj 0:0681ebb27b3c 38 */
jernej_vrscaj 0:0681ebb27b3c 39 class EnvironmentalService {
jernej_vrscaj 0:0681ebb27b3c 40 public:
jernej_vrscaj 0:0681ebb27b3c 41 typedef int16_t TemperatureType_t;
jernej_vrscaj 0:0681ebb27b3c 42 typedef uint16_t HumidityType_t;
jernej_vrscaj 0:0681ebb27b3c 43 typedef uint32_t PressureType_t;
jernej_vrscaj 0:0681ebb27b3c 44
jernej_vrscaj 0:0681ebb27b3c 45 /**
jernej_vrscaj 0:0681ebb27b3c 46 * @brief EnvironmentalService constructor.
jernej_vrscaj 0:0681ebb27b3c 47 * @param ble Reference to BLE device.
jernej_vrscaj 0:0681ebb27b3c 48 * @param temperature_en Enable this characteristic.
jernej_vrscaj 0:0681ebb27b3c 49 * @param humidity_en Enable this characteristic.
jernej_vrscaj 0:0681ebb27b3c 50 * @param pressure_en Enable this characteristic.
jernej_vrscaj 0:0681ebb27b3c 51 */
jernej_vrscaj 0:0681ebb27b3c 52 EnvironmentalService(BLE& _ble) :
jernej_vrscaj 0:0681ebb27b3c 53 ble(_ble),
jernej_vrscaj 0:0681ebb27b3c 54 temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
jernej_vrscaj 0:0681ebb27b3c 55 humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
jernej_vrscaj 0:0681ebb27b3c 56 pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
jernej_vrscaj 0:0681ebb27b3c 57 {
jernej_vrscaj 0:0681ebb27b3c 58 static bool serviceAdded = false; /* We should only ever need to add the information service once. */
jernej_vrscaj 0:0681ebb27b3c 59 if (serviceAdded) {
jernej_vrscaj 0:0681ebb27b3c 60 return;
jernej_vrscaj 0:0681ebb27b3c 61 }
jernej_vrscaj 0:0681ebb27b3c 62
jernej_vrscaj 0:0681ebb27b3c 63 GattCharacteristic *charTable[] = { &temperatureCharacteristic,
jernej_vrscaj 0:0681ebb27b3c 64 &humidityCharacteristic,
jernej_vrscaj 0:0681ebb27b3c 65 &pressureCharacteristic };
jernej_vrscaj 0:0681ebb27b3c 66
jernej_vrscaj 0:0681ebb27b3c 67 GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
jernej_vrscaj 0:0681ebb27b3c 68
jernej_vrscaj 0:0681ebb27b3c 69 ble.gattServer().addService(environmentalService);
jernej_vrscaj 0:0681ebb27b3c 70 serviceAdded = true;
jernej_vrscaj 0:0681ebb27b3c 71 }
jernej_vrscaj 0:0681ebb27b3c 72
jernej_vrscaj 0:0681ebb27b3c 73 /**
jernej_vrscaj 0:0681ebb27b3c 74 * @brief Update temperature characteristic.
jernej_vrscaj 0:0681ebb27b3c 75 * @param newTemperatureVal New temperature measurement.
jernej_vrscaj 0:0681ebb27b3c 76 */
jernej_vrscaj 0:0681ebb27b3c 77 void updateTemperature(TemperatureType_t newTemperatureVal)
jernej_vrscaj 0:0681ebb27b3c 78 {
jernej_vrscaj 0:0681ebb27b3c 79 temperature = newTemperatureVal;
jernej_vrscaj 0:0681ebb27b3c 80 #ifdef DEBUG
jernej_vrscaj 0:0681ebb27b3c 81 pc.printf("T_char = %i\r\n", temperature);
jernej_vrscaj 0:0681ebb27b3c 82 #endif
jernej_vrscaj 0:0681ebb27b3c 83 ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
jernej_vrscaj 0:0681ebb27b3c 84 }
jernej_vrscaj 0:0681ebb27b3c 85
jernej_vrscaj 0:0681ebb27b3c 86 /**
jernej_vrscaj 0:0681ebb27b3c 87 * @brief Update humidity characteristic.
jernej_vrscaj 0:0681ebb27b3c 88 * @param newHumidityVal New humidity measurement.
jernej_vrscaj 0:0681ebb27b3c 89 */
jernej_vrscaj 0:0681ebb27b3c 90 void updateHumidity(HumidityType_t newHumidityVal)
jernej_vrscaj 0:0681ebb27b3c 91 {
jernej_vrscaj 0:0681ebb27b3c 92 humidity = newHumidityVal;
jernej_vrscaj 0:0681ebb27b3c 93 #ifdef DEBUG
jernej_vrscaj 0:0681ebb27b3c 94 pc.printf("H_char = %u\r\n", humidity);
jernej_vrscaj 0:0681ebb27b3c 95 #endif
jernej_vrscaj 0:0681ebb27b3c 96 ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
jernej_vrscaj 0:0681ebb27b3c 97 }
jernej_vrscaj 0:0681ebb27b3c 98
jernej_vrscaj 0:0681ebb27b3c 99 /**
jernej_vrscaj 0:0681ebb27b3c 100 * @brief Update pressure characteristic.
jernej_vrscaj 0:0681ebb27b3c 101 * @param newPressureVal New pressure measurement.
jernej_vrscaj 0:0681ebb27b3c 102 */
jernej_vrscaj 0:0681ebb27b3c 103 void updatePressure(PressureType_t newPressureVal)
jernej_vrscaj 0:0681ebb27b3c 104 {
jernej_vrscaj 0:0681ebb27b3c 105 pressure = newPressureVal;
jernej_vrscaj 0:0681ebb27b3c 106 #ifdef DEBUG
jernej_vrscaj 0:0681ebb27b3c 107 pc.printf("P_char = %u\r\n", pressure);
jernej_vrscaj 0:0681ebb27b3c 108 #endif
jernej_vrscaj 0:0681ebb27b3c 109 ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
jernej_vrscaj 0:0681ebb27b3c 110 }
jernej_vrscaj 0:0681ebb27b3c 111
jernej_vrscaj 0:0681ebb27b3c 112 private:
jernej_vrscaj 0:0681ebb27b3c 113 BLE& ble;
jernej_vrscaj 0:0681ebb27b3c 114
jernej_vrscaj 0:0681ebb27b3c 115 TemperatureType_t temperature;
jernej_vrscaj 0:0681ebb27b3c 116 HumidityType_t humidity;
jernej_vrscaj 0:0681ebb27b3c 117 PressureType_t pressure;
jernej_vrscaj 0:0681ebb27b3c 118
jernej_vrscaj 0:0681ebb27b3c 119 ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
jernej_vrscaj 0:0681ebb27b3c 120 ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
jernej_vrscaj 0:0681ebb27b3c 121 ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
jernej_vrscaj 0:0681ebb27b3c 122 };
jernej_vrscaj 0:0681ebb27b3c 123
jernej_vrscaj 0:0681ebb27b3c 124 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/