Reading and sending sensor data over BLE

Dependencies:   BSP_B-L475E-IOT01

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sensorservice.h Source File

Sensorservice.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 
00022 /**
00023 * @class EnvironmentalService
00024 * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement.
00025 * Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml
00026 * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml
00027 * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml
00028 * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml
00029 */
00030 class EnvironmentalService {
00031 public:
00032     typedef int16_t  TemperatureType_t;
00033     typedef uint16_t HumidityType_t;
00034     typedef uint32_t PressureType_t;
00035     typedef uint8_t  TimeType_t;
00036     typedef uint8_t writeValue_t[10];
00037     typedef uint8_t readValue_t[10];
00038     typedef int16_t Magneto_t[3];
00039     typedef int16_t ACCELERO_t[3];
00040     typedef float   Gyro_t[3];
00041     
00042     const static uint16_t READ_SERVICE_CHARACTERISTIC_UUID = 0xA001;
00043     const static uint16_t WRITE_SERVICE_CHARACTERISTIC_UUID = 0xA002;
00044     const static uint16_t MAGNETO_SERVICE_CHARACTERISTIC_UUID = 0xA003;
00045     const static uint16_t GYRO_SERVICE_CHARACTERISTIC_UUID = 0xA004;
00046     const static uint16_t ACCELERO_SERVICE_CHARACTERISTIC_UUID = 0xA005;
00047 
00048     /**
00049      * @brief   EnvironmentalService constructor.
00050      * @param   ble Reference to BLE device.
00051      * @param   temperature_en Enable this characteristic.
00052      * @param   humidity_en Enable this characteristic.
00053      * @param   pressure_en Enable this characteristic.
00054      */
00055     EnvironmentalService(BLE& _ble) :
00056         ble(_ble),
00057         temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature),
00058         humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity),
00059         pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure),
00060         timeCharacteristic(GattCharacteristic::UUID_CURRENT_TIME_CHAR , &timeNow),
00061         magnetoCharacteristic(MAGNETO_SERVICE_CHARACTERISTIC_UUID, &magneto),
00062         acceleroCharacteristic(ACCELERO_SERVICE_CHARACTERISTIC_UUID, &accelero),
00063         gyroCharacteristic(GYRO_SERVICE_CHARACTERISTIC_UUID, &gyro),
00064         readState(READ_SERVICE_CHARACTERISTIC_UUID, &readValue)
00065         
00066     {
00067         static bool serviceAdded = false; /* We should only ever need to add the information service once. */
00068         if (serviceAdded) {
00069             return;
00070         }
00071 
00072         GattCharacteristic *charTable[] = { &humidityCharacteristic,
00073                                             &pressureCharacteristic,
00074                                             &temperatureCharacteristic,
00075                                             &timeCharacteristic,
00076                                             &readState,
00077                                             &magnetoCharacteristic,
00078                                             &acceleroCharacteristic,
00079                                             &gyroCharacteristic };
00080         GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00081 
00082         ble.gattServer().addService(environmentalService);
00083         serviceAdded = true;
00084     }
00085 
00086     /**
00087      * @brief   Update humidity characteristic.
00088      * @param   newHumidityVal New humidity measurement.
00089      */
00090     void updateHumidity(HumidityType_t newHumidityVal)
00091     {
00092         humidity = (HumidityType_t) (newHumidityVal*100);
00093         ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t));
00094     }
00095     
00096     void updateTime(TimeType_t newTimeVal)
00097     {
00098         timeNow = (TimeType_t) newTimeVal;
00099         ble.gattServer().write(timeCharacteristic.getValueHandle(), (uint8_t *) &timeNow, sizeof(newTimeVal));
00100     }
00101 
00102     /**
00103      * @brief   Update pressure characteristic.
00104      * @param   newPressureVal New pressure measurement.
00105      */
00106     void updatePressure(PressureType_t newPressureVal)
00107     {
00108         pressure = (PressureType_t) (newPressureVal * 10);
00109         ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t));
00110     }
00111 
00112     /**
00113      * @brief   Update temperature characteristic.
00114      * @param   newTemperatureVal New temperature measurement.
00115      */
00116     void updateTemperature(float newTemperatureVal)
00117     {
00118         temperature = (TemperatureType_t) (newTemperatureVal * 100);
00119         ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t));
00120     }
00121     
00122     void updateMagneto(Magneto_t newMagnetoVal)
00123     {
00124         ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[0], sizeof(int16_t));
00125         ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[1], sizeof(int16_t));
00126         ble.gattServer().write(magnetoCharacteristic.getValueHandle(), (uint8_t *) &magneto[2], sizeof(int16_t));
00127     }
00128     
00129     void updateAccelero(ACCELERO_t newAcceleroVal)
00130     {
00131         ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[0], sizeof(int16_t));
00132         ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[1], sizeof(int16_t));
00133         ble.gattServer().write(acceleroCharacteristic.getValueHandle(), (uint8_t *) &accelero[2], sizeof(int16_t));
00134     }
00135     
00136     void updateGyro(Gyro_t newGyroVal)
00137     {
00138         ble.gattServer().write(gyroCharacteristic.getValueHandle(), (uint8_t *) &gyro[0], sizeof(float));
00139         ble.gattServer().write(gyroCharacteristic.getValueHandle(), (uint8_t *) &gyro[1], sizeof(float));
00140         ble.gattServer().write(gyroCharacteristic.getValueHandle(),  (uint8_t *)  &gyro[2],  sizeof(float));
00141     }
00142     
00143     GattAttribute::Handle_t getValueHandle() const
00144     {
00145         return readState.getValueHandle();
00146     }
00147     
00148     
00149     
00150     
00151 private:
00152     BLE& ble;
00153 
00154     TemperatureType_t temperature;
00155     HumidityType_t    humidity;
00156     PressureType_t    pressure;
00157     TimeType_t        timeNow;
00158     writeValue_t writeValue;
00159     readValue_t readValue;
00160     Magneto_t   magneto;
00161     ACCELERO_t   accelero;
00162     Gyro_t      gyro;
00163 
00164     ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic;
00165     ReadOnlyGattCharacteristic<HumidityType_t>    humidityCharacteristic;
00166     ReadOnlyGattCharacteristic<PressureType_t>    pressureCharacteristic;
00167     ReadOnlyGattCharacteristic<TimeType_t>        timeCharacteristic;
00168     ReadOnlyGattCharacteristic<readValue_t> readState;
00169     ReadOnlyGattCharacteristic<Magneto_t> magnetoCharacteristic;
00170     ReadOnlyGattCharacteristic<ACCELERO_t> acceleroCharacteristic;
00171     ReadOnlyGattCharacteristic<Gyro_t> gyroCharacteristic;
00172 };
00173 
00174 #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/