* This is the code for "BLE Device for motorbike". The device is attached on any bike at will. * User can control 2 switches and these switches can control anything that user wants ie: turn on * the bike, turn on the alarm system of the bike, turn on the light... * Temperature sensor is also included in the device. User can view the temperature when he/she gets * near the bike. * For the next version, humidity and air quality sensor are also added.

Dependencies:   DHT22

Committer:
DuyLionTran
Date:
Fri Nov 03 04:24:42 2017 +0000
Revision:
3:d6fbd4f3a3d4
Parent:
2:65ed7cd0480c
BLE_Bike version 0.8.1 air quality sensor is removed due to lack of meterial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 0:ee08053aaf57 1 #ifndef __BLE_BIKE_SERVICE_H__
DuyLionTran 0:ee08053aaf57 2 #define __BLE_BIKE_SERVICE_H__
DuyLionTran 0:ee08053aaf57 3
DuyLionTran 0:ee08053aaf57 4 #include "ble/BLE.h"
DuyLionTran 0:ee08053aaf57 5
DuyLionTran 0:ee08053aaf57 6 /**
DuyLionTran 0:ee08053aaf57 7 * @class bikeService
DuyLionTran 0:ee08053aaf57 8 * @brief This is a customized service for a device controlling the bike via BLE
DuyLionTran 0:ee08053aaf57 9 */
DuyLionTran 0:ee08053aaf57 10 class bikeService {
DuyLionTran 1:8db3d642a94f 11 public:
DuyLionTran 0:ee08053aaf57 12 const static uint16_t BIKE_SERVICE_UUID = 0xA580;
DuyLionTran 0:ee08053aaf57 13 const static uint16_t SWITCH_CONTROL_UUID = 0xA581;
DuyLionTran 1:8db3d642a94f 14 const static uint16_t TEMPERATURE_MEASURE_UUID = 0xA582;
DuyLionTran 2:65ed7cd0480c 15 const static uint16_t HUMIDITY_MEASURE_UUID = 0xA583;
DuyLionTran 2:65ed7cd0480c 16 const static uint16_t AIR_QUALITY_MEASURE_UUID = 0xA584;
DuyLionTran 1:8db3d642a94f 17
DuyLionTran 0:ee08053aaf57 18
DuyLionTran 0:ee08053aaf57 19 /**
DuyLionTran 0:ee08053aaf57 20 * @param[in] _ble BLE object for the underlying controller.
DuyLionTran 3:d6fbd4f3a3d4 21 * @param[in] currentTemperature The temperature measured from the sensor
DuyLionTran 3:d6fbd4f3a3d4 22 * @param[in] currentHumidity The humidity measured from the sensor
DuyLionTran 0:ee08053aaf57 23 */
DuyLionTran 0:ee08053aaf57 24
DuyLionTran 3:d6fbd4f3a3d4 25 bikeService(BLE &_ble, bool initialSwitchState, int8_t currentTemperature, uint8_t currentHumidity /*, uint16_t airQuality */) :
DuyLionTran 0:ee08053aaf57 26 ble(_ble),
DuyLionTran 2:65ed7cd0480c 27 switchState(SWITCH_CONTROL_UUID, &initialSwitchState),
DuyLionTran 2:65ed7cd0480c 28 temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &currentTemperature, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
DuyLionTran 2:65ed7cd0480c 29 humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &currentHumidity, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
DuyLionTran 2:65ed7cd0480c 30 airQualityCharacteristic(AIR_QUALITY_MEASURE_UUID, &airQuality, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
DuyLionTran 0:ee08053aaf57 31 {
DuyLionTran 0:ee08053aaf57 32 /* Create and add the service */
DuyLionTran 3:d6fbd4f3a3d4 33 GattCharacteristic *charTable[] = {&switchState, &temperatureCharacteristic, &humidityCharacteristic /*, &airQualityCharacteristic */};
DuyLionTran 0:ee08053aaf57 34 GattService bikeService(BIKE_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
DuyLionTran 2:65ed7cd0480c 35 ble.gattServer().addService(bikeService);
DuyLionTran 0:ee08053aaf57 36 }
DuyLionTran 0:ee08053aaf57 37
DuyLionTran 0:ee08053aaf57 38 GattAttribute::Handle_t getValueHandle() const {
DuyLionTran 1:8db3d642a94f 39 return switchState.getValueHandle();
DuyLionTran 0:ee08053aaf57 40 }
DuyLionTran 0:ee08053aaf57 41
DuyLionTran 3:d6fbd4f3a3d4 42 void updateTemperatureCharacteristic(int8_t newTemperature) {
DuyLionTran 2:65ed7cd0480c 43 currentTemperature = newTemperature;
DuyLionTran 3:d6fbd4f3a3d4 44 ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &currentTemperature, sizeof(int8_t));
DuyLionTran 2:65ed7cd0480c 45 }
DuyLionTran 2:65ed7cd0480c 46
DuyLionTran 2:65ed7cd0480c 47 void updateAirQualityCharacteristic(uint8_t newHumidity) {
DuyLionTran 2:65ed7cd0480c 48 currentHumidity = newHumidity;
DuyLionTran 2:65ed7cd0480c 49 ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &currentHumidity, sizeof(uint8_t));
DuyLionTran 2:65ed7cd0480c 50 }
DuyLionTran 2:65ed7cd0480c 51
DuyLionTran 2:65ed7cd0480c 52 void updateTemperatureCharacteristic(uint16_t newAirQuality) {
DuyLionTran 2:65ed7cd0480c 53 airQuality = newAirQuality;
DuyLionTran 2:65ed7cd0480c 54 ble.gattServer().write(airQualityCharacteristic.getValueHandle(), (uint8_t *) &airQuality, sizeof(uint16_t));
DuyLionTran 2:65ed7cd0480c 55 }
DuyLionTran 2:65ed7cd0480c 56
DuyLionTran 3:d6fbd4f3a3d4 57 void updateAllCharacteristics(int8_t newTemperature, uint8_t newHumidity /*, uint16_t newAirQuality */) {
DuyLionTran 1:8db3d642a94f 58 currentTemperature = newTemperature;
DuyLionTran 2:65ed7cd0480c 59 currentHumidity = newHumidity;
DuyLionTran 3:d6fbd4f3a3d4 60 /* airQuality = newAirQuality; */
DuyLionTran 1:8db3d642a94f 61
DuyLionTran 3:d6fbd4f3a3d4 62 ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &currentTemperature, sizeof(int8_t));
DuyLionTran 2:65ed7cd0480c 63 ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &currentHumidity, sizeof(uint8_t));
DuyLionTran 3:d6fbd4f3a3d4 64 /* ble.gattServer().write(airQualityCharacteristic.getValueHandle(), (uint8_t *) &airQuality, sizeof(uint16_t)); */
DuyLionTran 1:8db3d642a94f 65 }
DuyLionTran 0:ee08053aaf57 66 private:
DuyLionTran 3:d6fbd4f3a3d4 67 int8_t currentTemperature;
DuyLionTran 2:65ed7cd0480c 68 uint8_t currentHumidity;
DuyLionTran 2:65ed7cd0480c 69 uint16_t airQuality;
DuyLionTran 0:ee08053aaf57 70
DuyLionTran 2:65ed7cd0480c 71 BLE &ble;
DuyLionTran 1:8db3d642a94f 72 ReadWriteGattCharacteristic<bool> switchState;
DuyLionTran 3:d6fbd4f3a3d4 73 ReadOnlyGattCharacteristic<int8_t> temperatureCharacteristic;
DuyLionTran 2:65ed7cd0480c 74 ReadOnlyGattCharacteristic<uint8_t> humidityCharacteristic;
DuyLionTran 2:65ed7cd0480c 75 ReadOnlyGattCharacteristic<uint16_t> airQualityCharacteristic;
DuyLionTran 0:ee08053aaf57 76 };
DuyLionTran 0:ee08053aaf57 77
DuyLionTran 0:ee08053aaf57 78 #endif /* __BLE_BIKE_SERVICE_H__ */