* 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:
Mon Nov 27 14:30:14 2017 +0000
Revision:
5:0082f495e58e
Parent:
4:028d880ac83e
new version

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