Reading and sending sensor data over BLE

Dependencies:   BSP_B-L475E-IOT01

Committer:
samilive2011
Date:
Wed Jul 10 20:09:49 2019 +0000
Revision:
39:ddabcba6eff1
Sending data over BLE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samilive2011 39:ddabcba6eff1 1 /* mbed Microcontroller Library
samilive2011 39:ddabcba6eff1 2 * Copyright (c) 2006-2013 ARM Limited
samilive2011 39:ddabcba6eff1 3 *
samilive2011 39:ddabcba6eff1 4 * Licensed under the Apache License, Version 2.0 (the "License");
samilive2011 39:ddabcba6eff1 5 * you may not use this file except in compliance with the License.
samilive2011 39:ddabcba6eff1 6 * You may obtain a copy of the License at
samilive2011 39:ddabcba6eff1 7 *
samilive2011 39:ddabcba6eff1 8 * http://www.apache.org/licenses/LICENSE-2.0
samilive2011 39:ddabcba6eff1 9 *
samilive2011 39:ddabcba6eff1 10 * Unless required by applicable law or agreed to in writing, software
samilive2011 39:ddabcba6eff1 11 * distributed under the License is distributed on an "AS IS" BASIS,
samilive2011 39:ddabcba6eff1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
samilive2011 39:ddabcba6eff1 13 * See the License for the specific language governing permissions and
samilive2011 39:ddabcba6eff1 14 * limitations under the License.
samilive2011 39:ddabcba6eff1 15 */
samilive2011 39:ddabcba6eff1 16
samilive2011 39:ddabcba6eff1 17 #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__
samilive2011 39:ddabcba6eff1 18 #define __BLE_ENVIRONMENTAL_SERVICE_H__
samilive2011 39:ddabcba6eff1 19
samilive2011 39:ddabcba6eff1 20 #include "ble/BLE.h"
samilive2011 39:ddabcba6eff1 21
samilive2011 39:ddabcba6eff1 22 /**
samilive2011 39:ddabcba6eff1 23 * @class EnvironmentalService
samilive2011 39:ddabcba6eff1 24 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
samilive2011 39:ddabcba6eff1 25 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
samilive2011 39:ddabcba6eff1 26 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
samilive2011 39:ddabcba6eff1 27 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
samilive2011 39:ddabcba6eff1 28 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
samilive2011 39:ddabcba6eff1 29 */
samilive2011 39:ddabcba6eff1 30 class EnvironmentalService {
samilive2011 39:ddabcba6eff1 31 public:
samilive2011 39:ddabcba6eff1 32 typedef int16_t TemperatureType_t;
samilive2011 39:ddabcba6eff1 33 typedef uint16_t HumidityType_t;
samilive2011 39:ddabcba6eff1 34 typedef uint32_t PressureType_t;
samilive2011 39:ddabcba6eff1 35 typedef uint8_t TimeType_t;
samilive2011 39:ddabcba6eff1 36 typedef uint8_t writeValue_t[10];
samilive2011 39:ddabcba6eff1 37 typedef uint8_t readValue_t[10];
samilive2011 39:ddabcba6eff1 38 typedef int16_t Magneto_t[3];
samilive2011 39:ddabcba6eff1 39 typedef int16_t ACCELERO_t[3];
samilive2011 39:ddabcba6eff1 40 typedef float Gyro_t[3];
samilive2011 39:ddabcba6eff1 41
samilive2011 39:ddabcba6eff1 42 const static uint16_t READ_SERVICE_CHARACTERISTIC_UUID = 0xA001;
samilive2011 39:ddabcba6eff1 43 const static uint16_t WRITE_SERVICE_CHARACTERISTIC_UUID = 0xA002;
samilive2011 39:ddabcba6eff1 44 const static uint16_t MAGNETO_SERVICE_CHARACTERISTIC_UUID = 0xA003;
samilive2011 39:ddabcba6eff1 45 const static uint16_t GYRO_SERVICE_CHARACTERISTIC_UUID = 0xA004;
samilive2011 39:ddabcba6eff1 46 const static uint16_t ACCELERO_SERVICE_CHARACTERISTIC_UUID = 0xA005;
samilive2011 39:ddabcba6eff1 47
samilive2011 39:ddabcba6eff1 48 /**
samilive2011 39:ddabcba6eff1 49 * @brief EnvironmentalService constructor.
samilive2011 39:ddabcba6eff1 50 * @param ble Reference to BLE device.
samilive2011 39:ddabcba6eff1 51 * @param temperature_en Enable this characteristic.
samilive2011 39:ddabcba6eff1 52 * @param humidity_en Enable this characteristic.
samilive2011 39:ddabcba6eff1 53 * @param pressure_en Enable this characteristic.
samilive2011 39:ddabcba6eff1 54 */
samilive2011 39:ddabcba6eff1 55 EnvironmentalService(BLE& _ble) :
samilive2011 39:ddabcba6eff1 56 ble(_ble),
samilive2011 39:ddabcba6eff1 57 temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
samilive2011 39:ddabcba6eff1 58 humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
samilive2011 39:ddabcba6eff1 59 pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure),
samilive2011 39:ddabcba6eff1 60 timeCharacteristic(GattCharacteristic::UUID_CURRENT_TIME_CHAR , &timeNow),
samilive2011 39:ddabcba6eff1 61 magnetoCharacteristic(MAGNETO_SERVICE_CHARACTERISTIC_UUID, &magneto),
samilive2011 39:ddabcba6eff1 62 acceleroCharacteristic(ACCELERO_SERVICE_CHARACTERISTIC_UUID, &accelero),
samilive2011 39:ddabcba6eff1 63 gyroCharacteristic(GYRO_SERVICE_CHARACTERISTIC_UUID, &gyro),
samilive2011 39:ddabcba6eff1 64 readState(READ_SERVICE_CHARACTERISTIC_UUID, &readValue)
samilive2011 39:ddabcba6eff1 65
samilive2011 39:ddabcba6eff1 66 {
samilive2011 39:ddabcba6eff1 67 static bool serviceAdded = false; /* We should only ever need to add the information service once. */
samilive2011 39:ddabcba6eff1 68 if (serviceAdded) {
samilive2011 39:ddabcba6eff1 69 return;
samilive2011 39:ddabcba6eff1 70 }
samilive2011 39:ddabcba6eff1 71
samilive2011 39:ddabcba6eff1 72 GattCharacteristic *charTable[] = { &humidityCharacteristic,
samilive2011 39:ddabcba6eff1 73 &pressureCharacteristic,
samilive2011 39:ddabcba6eff1 74 &temperatureCharacteristic,
samilive2011 39:ddabcba6eff1 75 &timeCharacteristic,
samilive2011 39:ddabcba6eff1 76 &readState,
samilive2011 39:ddabcba6eff1 77 &magnetoCharacteristic,
samilive2011 39:ddabcba6eff1 78 &acceleroCharacteristic,
samilive2011 39:ddabcba6eff1 79 &gyroCharacteristic };
samilive2011 39:ddabcba6eff1 80 GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
samilive2011 39:ddabcba6eff1 81
samilive2011 39:ddabcba6eff1 82 ble.gattServer().addService(environmentalService);
samilive2011 39:ddabcba6eff1 83 serviceAdded = true;
samilive2011 39:ddabcba6eff1 84 }
samilive2011 39:ddabcba6eff1 85
samilive2011 39:ddabcba6eff1 86 /**
samilive2011 39:ddabcba6eff1 87 * @brief Update humidity characteristic.
samilive2011 39:ddabcba6eff1 88 * @param newHumidityVal New humidity measurement.
samilive2011 39:ddabcba6eff1 89 */
samilive2011 39:ddabcba6eff1 90 void updateHumidity(HumidityType_t newHumidityVal)
samilive2011 39:ddabcba6eff1 91 {
samilive2011 39:ddabcba6eff1 92 humidity = (HumidityType_t) (newHumidityVal*100);
samilive2011 39:ddabcba6eff1 93 ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
samilive2011 39:ddabcba6eff1 94 }
samilive2011 39:ddabcba6eff1 95
samilive2011 39:ddabcba6eff1 96 void updateTime(TimeType_t newTimeVal)
samilive2011 39:ddabcba6eff1 97 {
samilive2011 39:ddabcba6eff1 98 timeNow = (TimeType_t) newTimeVal;
samilive2011 39:ddabcba6eff1 99 ble.gattServer().write(timeCharacteristic.getValueHandle(), (uint8_t *) &timeNow, sizeof(newTimeVal));
samilive2011 39:ddabcba6eff1 100 }
samilive2011 39:ddabcba6eff1 101
samilive2011 39:ddabcba6eff1 102 /**
samilive2011 39:ddabcba6eff1 103 * @brief Update pressure characteristic.
samilive2011 39:ddabcba6eff1 104 * @param newPressureVal New pressure measurement.
samilive2011 39:ddabcba6eff1 105 */
samilive2011 39:ddabcba6eff1 106 void updatePressure(PressureType_t newPressureVal)
samilive2011 39:ddabcba6eff1 107 {
samilive2011 39:ddabcba6eff1 108 pressure = (PressureType_t) (newPressureVal * 10);
samilive2011 39:ddabcba6eff1 109 ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
samilive2011 39:ddabcba6eff1 110 }
samilive2011 39:ddabcba6eff1 111
samilive2011 39:ddabcba6eff1 112 /**
samilive2011 39:ddabcba6eff1 113 * @brief Update temperature characteristic.
samilive2011 39:ddabcba6eff1 114 * @param newTemperatureVal New temperature measurement.
samilive2011 39:ddabcba6eff1 115 */
samilive2011 39:ddabcba6eff1 116 void updateTemperature(float newTemperatureVal)
samilive2011 39:ddabcba6eff1 117 {
samilive2011 39:ddabcba6eff1 118 temperature = (TemperatureType_t) (newTemperatureVal * 100);
samilive2011 39:ddabcba6eff1 119 ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
samilive2011 39:ddabcba6eff1 120 }
samilive2011 39:ddabcba6eff1 121
samilive2011 39:ddabcba6eff1 122 void updateMagneto(Magneto_t newMagnetoVal)
samilive2011 39:ddabcba6eff1 123 {
samilive2011 39:ddabcba6eff1 124 ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[0], sizeof(int16_t));
samilive2011 39:ddabcba6eff1 125 ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[1], sizeof(int16_t));
samilive2011 39:ddabcba6eff1 126 ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[2], sizeof(int16_t));
samilive2011 39:ddabcba6eff1 127 }
samilive2011 39:ddabcba6eff1 128
samilive2011 39:ddabcba6eff1 129 void updateAccelero(ACCELERO_t newAcceleroVal)
samilive2011 39:ddabcba6eff1 130 {
samilive2011 39:ddabcba6eff1 131 ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[0], sizeof(int16_t));
samilive2011 39:ddabcba6eff1 132 ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[1], sizeof(int16_t));
samilive2011 39:ddabcba6eff1 133 ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[2], sizeof(int16_t));
samilive2011 39:ddabcba6eff1 134 }
samilive2011 39:ddabcba6eff1 135
samilive2011 39:ddabcba6eff1 136 void updateGyro(Gyro_t newGyroVal)
samilive2011 39:ddabcba6eff1 137 {
samilive2011 39:ddabcba6eff1 138 ble.gattServer().write(gyroCharacteristic.getValueHandle(), (uint8_t *) &gyro[0], sizeof(float));
samilive2011 39:ddabcba6eff1 139 ble.gattServer().write(gyroCharacteristic.getValueHandle(), (uint8_t *) &gyro[1], sizeof(float));
samilive2011 39:ddabcba6eff1 140 ble.gattServer().write(gyroCharacteristic.getValueHandle(), (uint8_t *) &gyro[2], sizeof(float));
samilive2011 39:ddabcba6eff1 141 }
samilive2011 39:ddabcba6eff1 142
samilive2011 39:ddabcba6eff1 143 GattAttribute::Handle_t getValueHandle() const
samilive2011 39:ddabcba6eff1 144 {
samilive2011 39:ddabcba6eff1 145 return readState.getValueHandle();
samilive2011 39:ddabcba6eff1 146 }
samilive2011 39:ddabcba6eff1 147
samilive2011 39:ddabcba6eff1 148
samilive2011 39:ddabcba6eff1 149
samilive2011 39:ddabcba6eff1 150
samilive2011 39:ddabcba6eff1 151 private:
samilive2011 39:ddabcba6eff1 152 BLE& ble;
samilive2011 39:ddabcba6eff1 153
samilive2011 39:ddabcba6eff1 154 TemperatureType_t temperature;
samilive2011 39:ddabcba6eff1 155 HumidityType_t humidity;
samilive2011 39:ddabcba6eff1 156 PressureType_t pressure;
samilive2011 39:ddabcba6eff1 157 TimeType_t timeNow;
samilive2011 39:ddabcba6eff1 158 writeValue_t writeValue;
samilive2011 39:ddabcba6eff1 159 readValue_t readValue;
samilive2011 39:ddabcba6eff1 160 Magneto_t magneto;
samilive2011 39:ddabcba6eff1 161 ACCELERO_t accelero;
samilive2011 39:ddabcba6eff1 162 Gyro_t gyro;
samilive2011 39:ddabcba6eff1 163
samilive2011 39:ddabcba6eff1 164 ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
samilive2011 39:ddabcba6eff1 165 ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic;
samilive2011 39:ddabcba6eff1 166 ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic;
samilive2011 39:ddabcba6eff1 167 ReadOnlyGattCharacteristic<TimeType_t> timeCharacteristic;
samilive2011 39:ddabcba6eff1 168 ReadOnlyGattCharacteristic<readValue_t> readState;
samilive2011 39:ddabcba6eff1 169 ReadOnlyGattCharacteristic<Magneto_t> magnetoCharacteristic;
samilive2011 39:ddabcba6eff1 170 ReadOnlyGattCharacteristic<ACCELERO_t> acceleroCharacteristic;
samilive2011 39:ddabcba6eff1 171 ReadOnlyGattCharacteristic<Gyro_t> gyroCharacteristic;
samilive2011 39:ddabcba6eff1 172 };
samilive2011 39:ddabcba6eff1 173
samilive2011 39:ddabcba6eff1 174 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/