* 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 16:13:17 2017 +0000
Revision:
0:ee08053aaf57
Child:
1:8db3d642a94f
Version 0.7.1 Temperature added

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 * For the next version, humidity and air quality sensor are also added.
DuyLionTran 0:ee08053aaf57 8
DuyLionTran 0:ee08053aaf57 9 * Revision:
DuyLionTran 0:ee08053aaf57 10 * v0_1: only one switch is used.
DuyLionTran 0:ee08053aaf57 11 * v0_5: another switch is added.
DuyLionTran 0:ee08053aaf57 12 * v0_7: thermometer is added. User can view thermomether 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 0:ee08053aaf57 25 uint16_t currentTemperature = 18;
DuyLionTran 0:ee08053aaf57 26
DuyLionTran 0:ee08053aaf57 27 /* PRIVATE VARIABLES */
DuyLionTran 0:ee08053aaf57 28 const static char DEVICE_NAME[] = "WAVE ALPHA 8214";
DuyLionTran 0:ee08053aaf57 29 static const uint16_t uuid16_list[] = {bikeService::BIKE_SERVICE_UUID};
DuyLionTran 0:ee08053aaf57 30
DuyLionTran 0:ee08053aaf57 31 /* STRUCTS/CLASSESS */
DuyLionTran 0:ee08053aaf57 32 DigitalOut switch1(LED2, 0);
DuyLionTran 0:ee08053aaf57 33 DigitalOut switch2(LED3, 0);
DuyLionTran 0:ee08053aaf57 34 static EventQueue eventQueue(EVENTS_EVENT_SIZE * 20);
DuyLionTran 0:ee08053aaf57 35 bikeService *bikeServicePtr;
DuyLionTran 0:ee08053aaf57 36
DuyLionTran 0:ee08053aaf57 37 /* ================== FUNCTION PROTOTYPES ==================== */
DuyLionTran 0:ee08053aaf57 38
DuyLionTran 0:ee08053aaf57 39 /* ==================== FUNCTION DETAILS ===================== */
DuyLionTran 0:ee08053aaf57 40 /* A connection should be re-enabled after the device and the device
DuyLionTran 0:ee08053aaf57 41 * disconnected
DuyLionTran 0:ee08053aaf57 42 */
DuyLionTran 0:ee08053aaf57 43 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
DuyLionTran 0:ee08053aaf57 44 (void) params;
DuyLionTran 0:ee08053aaf57 45 BLE::Instance().gap().startAdvertising();
DuyLionTran 0:ee08053aaf57 46 }
DuyLionTran 0:ee08053aaf57 47
DuyLionTran 0:ee08053aaf57 48 void main_event(void)
DuyLionTran 0:ee08053aaf57 49 {
DuyLionTran 0:ee08053aaf57 50 /* Any code can be added here */
DuyLionTran 0:ee08053aaf57 51 currentTemperature++;
DuyLionTran 0:ee08053aaf57 52 bikeServicePtr->updateTemperatureCharacteristic(currentTemperature);
DuyLionTran 0:ee08053aaf57 53 }
DuyLionTran 0:ee08053aaf57 54
DuyLionTran 0:ee08053aaf57 55 /**
DuyLionTran 0:ee08053aaf57 56 * @brief This callback allows the bikeService to receive updates to the switchState Characteristic.
DuyLionTran 0:ee08053aaf57 57 * @param[in] params
DuyLionTran 0:ee08053aaf57 58 */
DuyLionTran 0:ee08053aaf57 59 void receiveDataCallback(const GattWriteCallbackParams *params) {
DuyLionTran 0:ee08053aaf57 60 if ((params->handle == bikeServicePtr->getValueHandle()) && (params->len == 1)) {
DuyLionTran 0:ee08053aaf57 61 uint8_t receiveData = *(params->data);
DuyLionTran 0:ee08053aaf57 62
DuyLionTran 0:ee08053aaf57 63 switch (receiveData) {
DuyLionTran 0:ee08053aaf57 64 case 0x00: switch1 = 0; break;
DuyLionTran 0:ee08053aaf57 65 case 0x01: switch1 = 1; break;
DuyLionTran 0:ee08053aaf57 66 case 0x10: switch2 = 0; break;
DuyLionTran 0:ee08053aaf57 67 case 0x11: switch2 = 1; break;
DuyLionTran 0:ee08053aaf57 68
DuyLionTran 0:ee08053aaf57 69 default: break;
DuyLionTran 0:ee08053aaf57 70 }
DuyLionTran 0:ee08053aaf57 71 }
DuyLionTran 0:ee08053aaf57 72 }
DuyLionTran 0:ee08053aaf57 73
DuyLionTran 0:ee08053aaf57 74 /**
DuyLionTran 0:ee08053aaf57 75 * @brief This function is called when the ble initialization process has failled
DuyLionTran 0:ee08053aaf57 76 */
DuyLionTran 0:ee08053aaf57 77 void onBleInitError(BLE &ble, ble_error_t error) {
DuyLionTran 0:ee08053aaf57 78 /* Error handle here */
DuyLionTran 0:ee08053aaf57 79 }
DuyLionTran 0:ee08053aaf57 80
DuyLionTran 0:ee08053aaf57 81 /**
DuyLionTran 0:ee08053aaf57 82 * @brief Callback triggered when the ble initialization process has finished
DuyLionTran 0:ee08053aaf57 83 */
DuyLionTran 0:ee08053aaf57 84 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) {
DuyLionTran 0:ee08053aaf57 85 BLE &ble = params->ble;
DuyLionTran 0:ee08053aaf57 86 ble_error_t error = params->error;
DuyLionTran 0:ee08053aaf57 87
DuyLionTran 0:ee08053aaf57 88 if (error != BLE_ERROR_NONE) {
DuyLionTran 0:ee08053aaf57 89 onBleInitError(ble, error);
DuyLionTran 0:ee08053aaf57 90 return;
DuyLionTran 0:ee08053aaf57 91 }
DuyLionTran 0:ee08053aaf57 92
DuyLionTran 0:ee08053aaf57 93 /* Ensure that it is the default instance of BLE */
DuyLionTran 0:ee08053aaf57 94 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
DuyLionTran 0:ee08053aaf57 95 return;
DuyLionTran 0:ee08053aaf57 96 }
DuyLionTran 0:ee08053aaf57 97
DuyLionTran 0:ee08053aaf57 98 ble.gap().onDisconnection(disconnectionCallback);
DuyLionTran 0:ee08053aaf57 99 ble.gattServer().onDataWritten(receiveDataCallback);
DuyLionTran 0:ee08053aaf57 100
DuyLionTran 0:ee08053aaf57 101 bool initialSwitchState = false;
DuyLionTran 0:ee08053aaf57 102 bikeServicePtr = new bikeService(ble, initialSwitchState, currentTemperature);
DuyLionTran 0:ee08053aaf57 103
DuyLionTran 0:ee08053aaf57 104 /* setup advertising */
DuyLionTran 0:ee08053aaf57 105 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
DuyLionTran 0:ee08053aaf57 106 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
DuyLionTran 0:ee08053aaf57 107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
DuyLionTran 0:ee08053aaf57 108 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
DuyLionTran 0:ee08053aaf57 109 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
DuyLionTran 0:ee08053aaf57 110 ble.gap().startAdvertising();
DuyLionTran 0:ee08053aaf57 111
DuyLionTran 0:ee08053aaf57 112 }
DuyLionTran 0:ee08053aaf57 113
DuyLionTran 0:ee08053aaf57 114 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
DuyLionTran 0:ee08053aaf57 115 BLE &ble = BLE::Instance();
DuyLionTran 0:ee08053aaf57 116 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
DuyLionTran 0:ee08053aaf57 117 }
DuyLionTran 0:ee08053aaf57 118
DuyLionTran 0:ee08053aaf57 119 /* MAIN FUNCTION */
DuyLionTran 0:ee08053aaf57 120 int main() {
DuyLionTran 0:ee08053aaf57 121 eventQueue.call_every(500, main_event);
DuyLionTran 0:ee08053aaf57 122
DuyLionTran 0:ee08053aaf57 123 BLE &ble = BLE::Instance();
DuyLionTran 0:ee08053aaf57 124 ble.onEventsToProcess(scheduleBleEventsProcessing);
DuyLionTran 0:ee08053aaf57 125 ble.init(bleInitComplete);
DuyLionTran 0:ee08053aaf57 126
DuyLionTran 0:ee08053aaf57 127 eventQueue.dispatch_forever();
DuyLionTran 0:ee08053aaf57 128
DuyLionTran 0:ee08053aaf57 129 return 0;
DuyLionTran 0:ee08053aaf57 130 }