BLE example with Environmental Sensing service.
Dependencies: BSP_B-L475E-IOT01
EnvironmentalService_v2.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__ 00018 #define __BLE_ENVIRONMENTAL_SERVICE_H__ 00019 00020 #include "ble/BLE.h" 00021 //#include "debug.h" 00022 00023 /** 00024 * @note 00025 * Modified service (refer to EnvironmentalService.h): 00026 * - notifications added 00027 * - conversion to temperature, humidity and pressure integer values done in main.cpp 00028 * - updateTemperature() input argument type changed from float to TemperatureType_t 00029 */ 00030 00031 /** 00032 * @class EnvironmentalService 00033 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement. 00034 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml 00035 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml 00036 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml 00037 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml 00038 */ 00039 class EnvironmentalService { 00040 public: 00041 typedef int16_t TemperatureType_t; 00042 typedef uint16_t HumidityType_t; 00043 typedef uint32_t PressureType_t; 00044 00045 /** 00046 * @brief EnvironmentalService constructor. 00047 * @param ble Reference to BLE device. 00048 * @param temperature_en Enable this characteristic. 00049 * @param humidity_en Enable this characteristic. 00050 * @param pressure_en Enable this characteristic. 00051 */ 00052 EnvironmentalService(BLE& _ble) : 00053 ble(_ble), 00054 temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00055 humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00056 pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) 00057 { 00058 static bool serviceAdded = false; /* We should only ever need to add the information service once. */ 00059 if (serviceAdded) { 00060 return; 00061 } 00062 00063 GattCharacteristic *charTable[] = { &temperatureCharacteristic, 00064 &humidityCharacteristic, 00065 &pressureCharacteristic }; 00066 00067 GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00068 00069 ble.gattServer().addService(environmentalService); 00070 serviceAdded = true; 00071 } 00072 00073 /** 00074 * @brief Update temperature characteristic. 00075 * @param newTemperatureVal New temperature measurement. 00076 */ 00077 void updateTemperature(TemperatureType_t newTemperatureVal) 00078 { 00079 temperature = newTemperatureVal; 00080 #ifdef DEBUG 00081 pc.printf("T_char = %i\r\n", temperature); 00082 #endif 00083 ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t)); 00084 } 00085 00086 /** 00087 * @brief Update humidity characteristic. 00088 * @param newHumidityVal New humidity measurement. 00089 */ 00090 void updateHumidity(HumidityType_t newHumidityVal) 00091 { 00092 humidity = newHumidityVal; 00093 #ifdef DEBUG 00094 pc.printf("H_char = %u\r\n", humidity); 00095 #endif 00096 ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t)); 00097 } 00098 00099 /** 00100 * @brief Update pressure characteristic. 00101 * @param newPressureVal New pressure measurement. 00102 */ 00103 void updatePressure(PressureType_t newPressureVal) 00104 { 00105 pressure = newPressureVal; 00106 #ifdef DEBUG 00107 pc.printf("P_char = %u\r\n", pressure); 00108 #endif 00109 ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t)); 00110 } 00111 00112 private: 00113 BLE& ble; 00114 00115 TemperatureType_t temperature; 00116 HumidityType_t humidity; 00117 PressureType_t pressure; 00118 00119 ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic; 00120 ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic; 00121 ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic; 00122 }; 00123 00124 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/
Generated on Wed Jul 13 2022 01:02:12 by 1.7.2