* 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:
Thu Nov 02 18:38:02 2017 +0000
Revision:
2:65ed7cd0480c
Parent:
1:8db3d642a94f
Child:
3:d6fbd4f3a3d4
Version 0.8 full service

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 0:ee08053aaf57 1 /**
DuyLionTran 0:ee08053aaf57 2 * This is the code for "BLE Device for motorbike". The device is attached on any bike at will.
DuyLionTran 0:ee08053aaf57 3 * User can control 2 switches and these switches can control anything that user wants ie: turn on
DuyLionTran 0:ee08053aaf57 4 * the bike, turn on the alarm system of the bike, turn on the light...
DuyLionTran 0:ee08053aaf57 5 * Temperature sensor is also included in the device. User can view the temperature when he/she gets
DuyLionTran 0:ee08053aaf57 6 * near the bike.
DuyLionTran 0:ee08053aaf57 7
DuyLionTran 0:ee08053aaf57 8 * Revision:
DuyLionTran 0:ee08053aaf57 9 * v0_1: only one switch is used.
DuyLionTran 0:ee08053aaf57 10 * v0_5: another switch is added.
DuyLionTran 0:ee08053aaf57 11 * v0_7: thermometer is added. User can view thermomether data on the phone.
DuyLionTran 2:65ed7cd0480c 12 * v0_8: humidity and air quality are added. User can view measured data on the phone.
DuyLionTran 0:ee08053aaf57 13 */
DuyLionTran 0:ee08053aaf57 14
DuyLionTran 0:ee08053aaf57 15 /* ======================== INCLUDES ========================= */
DuyLionTran 0:ee08053aaf57 16 #include <events/mbed_events.h>
DuyLionTran 0:ee08053aaf57 17 #include <mbed.h>
DuyLionTran 0:ee08053aaf57 18 #include "ble/BLE.h"
DuyLionTran 0:ee08053aaf57 19 #include "bike_service.h"
DuyLionTran 0:ee08053aaf57 20
DuyLionTran 0:ee08053aaf57 21 /* ======================== DEFINES ========================== */
DuyLionTran 0:ee08053aaf57 22
DuyLionTran 0:ee08053aaf57 23 /* ======================= VARIABLES ========================= */
DuyLionTran 0:ee08053aaf57 24 /* GLOBAL VARIABLES */
DuyLionTran 2:65ed7cd0480c 25 int16_t currentTemperature = 19;
DuyLionTran 2:65ed7cd0480c 26 uint8_t currentHumidity = 35;
DuyLionTran 2:65ed7cd0480c 27 uint16_t currentAirQuality = 550;
DuyLionTran 0:ee08053aaf57 28
DuyLionTran 0:ee08053aaf57 29 /* PRIVATE VARIABLES */
DuyLionTran 0:ee08053aaf57 30 const static char DEVICE_NAME[] = "WAVE ALPHA 8214";
DuyLionTran 0:ee08053aaf57 31 static const uint16_t uuid16_list[] = {bikeService::BIKE_SERVICE_UUID};
DuyLionTran 0:ee08053aaf57 32
DuyLionTran 0:ee08053aaf57 33 /* STRUCTS/CLASSESS */
DuyLionTran 0:ee08053aaf57 34 DigitalOut switch1(LED2, 0);
DuyLionTran 0:ee08053aaf57 35 DigitalOut switch2(LED3, 0);
DuyLionTran 0:ee08053aaf57 36 static EventQueue eventQueue(EVENTS_EVENT_SIZE * 20);
DuyLionTran 0:ee08053aaf57 37 bikeService *bikeServicePtr;
DuyLionTran 0:ee08053aaf57 38
DuyLionTran 0:ee08053aaf57 39 /* ================== FUNCTION PROTOTYPES ==================== */
DuyLionTran 0:ee08053aaf57 40
DuyLionTran 0:ee08053aaf57 41 /* ==================== FUNCTION DETAILS ===================== */
DuyLionTran 0:ee08053aaf57 42 /* A connection should be re-enabled after the device and the device
DuyLionTran 0:ee08053aaf57 43 * disconnected
DuyLionTran 0:ee08053aaf57 44 */
DuyLionTran 2:65ed7cd0480c 45 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
DuyLionTran 0:ee08053aaf57 46 (void) params;
DuyLionTran 0:ee08053aaf57 47 BLE::Instance().gap().startAdvertising();
DuyLionTran 0:ee08053aaf57 48 }
DuyLionTran 0:ee08053aaf57 49
DuyLionTran 2:65ed7cd0480c 50 void main_event(void) {
DuyLionTran 0:ee08053aaf57 51 /* Any code can be added here */
DuyLionTran 2:65ed7cd0480c 52 currentTemperature++;
DuyLionTran 2:65ed7cd0480c 53 currentHumidity += 7;
DuyLionTran 2:65ed7cd0480c 54 currentAirQuality += 129;
DuyLionTran 2:65ed7cd0480c 55 bikeServicePtr->updateAllCharacteristics(currentTemperature, currentHumidity, currentAirQuality);
DuyLionTran 0:ee08053aaf57 56 }
DuyLionTran 0:ee08053aaf57 57
DuyLionTran 0:ee08053aaf57 58 /**
DuyLionTran 0:ee08053aaf57 59 * @brief This callback allows the bikeService to receive updates to the switchState Characteristic.
DuyLionTran 0:ee08053aaf57 60 * @param[in] params
DuyLionTran 0:ee08053aaf57 61 */
DuyLionTran 0:ee08053aaf57 62 void receiveDataCallback(const GattWriteCallbackParams *params) {
DuyLionTran 0:ee08053aaf57 63 if ((params->handle == bikeServicePtr->getValueHandle()) && (params->len == 1)) {
DuyLionTran 0:ee08053aaf57 64 uint8_t receiveData = *(params->data);
DuyLionTran 0:ee08053aaf57 65
DuyLionTran 0:ee08053aaf57 66 switch (receiveData) {
DuyLionTran 0:ee08053aaf57 67 case 0x00: switch1 = 0; break;
DuyLionTran 0:ee08053aaf57 68 case 0x01: switch1 = 1; break;
DuyLionTran 0:ee08053aaf57 69 case 0x10: switch2 = 0; break;
DuyLionTran 0:ee08053aaf57 70 case 0x11: switch2 = 1; break;
DuyLionTran 0:ee08053aaf57 71
DuyLionTran 0:ee08053aaf57 72 default: break;
DuyLionTran 0:ee08053aaf57 73 }
DuyLionTran 0:ee08053aaf57 74 }
DuyLionTran 0:ee08053aaf57 75 }
DuyLionTran 0:ee08053aaf57 76
DuyLionTran 0:ee08053aaf57 77 /**
DuyLionTran 0:ee08053aaf57 78 * @brief This function is called when the ble initialization process has failled
DuyLionTran 0:ee08053aaf57 79 */
DuyLionTran 1:8db3d642a94f 80 void onBleInitError(BLE &ble, ble_error_t error)
DuyLionTran 1:8db3d642a94f 81 {
DuyLionTran 0:ee08053aaf57 82 /* Error handle here */
DuyLionTran 0:ee08053aaf57 83 }
DuyLionTran 0:ee08053aaf57 84
DuyLionTran 0:ee08053aaf57 85 /**
DuyLionTran 0:ee08053aaf57 86 * @brief Callback triggered when the ble initialization process has finished
DuyLionTran 0:ee08053aaf57 87 */
DuyLionTran 1:8db3d642a94f 88 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
DuyLionTran 1:8db3d642a94f 89 {
DuyLionTran 1:8db3d642a94f 90 BLE& ble = params->ble;
DuyLionTran 1:8db3d642a94f 91 ble_error_t error = params->error;
DuyLionTran 0:ee08053aaf57 92
DuyLionTran 0:ee08053aaf57 93 if (error != BLE_ERROR_NONE) {
DuyLionTran 0:ee08053aaf57 94 onBleInitError(ble, error);
DuyLionTran 0:ee08053aaf57 95 return;
DuyLionTran 0:ee08053aaf57 96 }
DuyLionTran 0:ee08053aaf57 97
DuyLionTran 0:ee08053aaf57 98 /* Ensure that it is the default instance of BLE */
DuyLionTran 0:ee08053aaf57 99 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
DuyLionTran 0:ee08053aaf57 100 return;
DuyLionTran 0:ee08053aaf57 101 }
DuyLionTran 0:ee08053aaf57 102
DuyLionTran 0:ee08053aaf57 103 ble.gap().onDisconnection(disconnectionCallback);
DuyLionTran 0:ee08053aaf57 104 ble.gattServer().onDataWritten(receiveDataCallback);
DuyLionTran 0:ee08053aaf57 105
DuyLionTran 0:ee08053aaf57 106 bool initialSwitchState = false;
DuyLionTran 2:65ed7cd0480c 107 bikeServicePtr = new bikeService(ble, initialSwitchState, currentTemperature, currentHumidity, currentAirQuality);
DuyLionTran 0:ee08053aaf57 108
DuyLionTran 0:ee08053aaf57 109 /* setup advertising */
DuyLionTran 0:ee08053aaf57 110 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
DuyLionTran 0:ee08053aaf57 111 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
DuyLionTran 0:ee08053aaf57 112 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
DuyLionTran 0:ee08053aaf57 113 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
DuyLionTran 0:ee08053aaf57 114 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
DuyLionTran 0:ee08053aaf57 115 ble.gap().startAdvertising();
DuyLionTran 0:ee08053aaf57 116 }
DuyLionTran 0:ee08053aaf57 117
DuyLionTran 0:ee08053aaf57 118 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
DuyLionTran 0:ee08053aaf57 119 BLE &ble = BLE::Instance();
DuyLionTran 0:ee08053aaf57 120 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
DuyLionTran 0:ee08053aaf57 121 }
DuyLionTran 0:ee08053aaf57 122
DuyLionTran 0:ee08053aaf57 123 /* MAIN FUNCTION */
DuyLionTran 0:ee08053aaf57 124 int main() {
DuyLionTran 0:ee08053aaf57 125 eventQueue.call_every(500, main_event);
DuyLionTran 0:ee08053aaf57 126
DuyLionTran 0:ee08053aaf57 127 BLE &ble = BLE::Instance();
DuyLionTran 0:ee08053aaf57 128 ble.onEventsToProcess(scheduleBleEventsProcessing);
DuyLionTran 0:ee08053aaf57 129 ble.init(bleInitComplete);
DuyLionTran 0:ee08053aaf57 130
DuyLionTran 0:ee08053aaf57 131 eventQueue.dispatch_forever();
DuyLionTran 0:ee08053aaf57 132
DuyLionTran 0:ee08053aaf57 133 return 0;
DuyLionTran 0:ee08053aaf57 134 }