Reading and sending sensor data over BLE
Dependencies: BSP_B-L475E-IOT01
source/main.cpp@39:ddabcba6eff1, 2019-07-10 (annotated)
- Committer:
- samilive2011
- Date:
- Wed Jul 10 20:09:49 2019 +0000
- Revision:
- 39:ddabcba6eff1
- Parent:
- 27:ff1fb7b5f9ee
Sending data over BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 11:7404978b24e7 | 1 | #include <events/mbed_events.h> |
mbed_official | 2:864ddfb70a9c | 2 | #include <mbed.h> |
mbed_official | 2:864ddfb70a9c | 3 | #include "ble/BLE.h" |
samilive2011 | 39:ddabcba6eff1 | 4 | #include "Sensorservice.h" |
samilive2011 | 39:ddabcba6eff1 | 5 | #include "stm32l475e_iot01_tsensor.h" |
samilive2011 | 39:ddabcba6eff1 | 6 | #include "stm32l475e_iot01_hsensor.h" |
samilive2011 | 39:ddabcba6eff1 | 7 | #include "stm32l475e_iot01_magneto.h" |
samilive2011 | 39:ddabcba6eff1 | 8 | #include "stm32l475e_iot01_gyro.h" |
samilive2011 | 39:ddabcba6eff1 | 9 | #include "stm32l475e_iot01_accelero.h" |
samilive2011 | 39:ddabcba6eff1 | 10 | #include "stm32l475e_iot01_psensor.h" |
samilive2011 | 39:ddabcba6eff1 | 11 | |
samilive2011 | 39:ddabcba6eff1 | 12 | |
samilive2011 | 39:ddabcba6eff1 | 13 | EnvironmentalService *SensorPtr; |
samilive2011 | 39:ddabcba6eff1 | 14 | static float currentTemperature = 0.0; |
samilive2011 | 39:ddabcba6eff1 | 15 | static float currentHumidity = 0.0; |
samilive2011 | 39:ddabcba6eff1 | 16 | static float currentPressure = 0.0; |
samilive2011 | 39:ddabcba6eff1 | 17 | static int16_t MagnetoXYZ[3] ={0}; |
samilive2011 | 39:ddabcba6eff1 | 18 | static int16_t AcceleroXYZ[3] ={0}; |
samilive2011 | 39:ddabcba6eff1 | 19 | static float GyroXYZ[3] ={0}; |
samilive2011 | 39:ddabcba6eff1 | 20 | |
samilive2011 | 39:ddabcba6eff1 | 21 | Serial pc(USBTX,USBRX); |
mbed_official | 2:864ddfb70a9c | 22 | |
mbed_official | 2:864ddfb70a9c | 23 | DigitalOut alivenessLED(LED1, 0); |
mbed_official | 2:864ddfb70a9c | 24 | DigitalOut actuatedLED(LED2, 0); |
samilive2011 | 39:ddabcba6eff1 | 25 | DigitalOut led(LED1, 1); |
samilive2011 | 39:ddabcba6eff1 | 26 | uint16_t customServiceUUID = 0xA000; /*Service*/ |
samilive2011 | 39:ddabcba6eff1 | 27 | uint16_t readCharUUID = 0xA001; /*Read*/ |
samilive2011 | 39:ddabcba6eff1 | 28 | uint16_t writeCharUUID = 0xA002; /*Write*/ |
mbed_official | 2:864ddfb70a9c | 29 | |
samilive2011 | 39:ddabcba6eff1 | 30 | const static char DEVICE_NAME[] = "Projet"; |
samilive2011 | 39:ddabcba6eff1 | 31 | static const uint16_t uuid16_list[] = {GattService::UUID_ENVIRONMENTAL_SERVICE}; |
samilive2011 | 39:ddabcba6eff1 | 32 | |
mbed_official | 2:864ddfb70a9c | 33 | |
samilive2011 | 39:ddabcba6eff1 | 34 | static uint8_t writeValue[10] = {0}; |
samilive2011 | 39:ddabcba6eff1 | 35 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
samilive2011 | 39:ddabcba6eff1 | 36 | |
mbed_official | 2:864ddfb70a9c | 37 | |
samilive2011 | 39:ddabcba6eff1 | 38 | GattCharacteristic *characteristics[] = {&writeChar}; |
samilive2011 | 39:ddabcba6eff1 | 39 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
samilive2011 | 39:ddabcba6eff1 | 40 | |
samilive2011 | 39:ddabcba6eff1 | 41 | static EventQueue eventQueue(10 * EVENTS_EVENT_SIZE); |
samilive2011 | 39:ddabcba6eff1 | 42 | |
mbed_official | 2:864ddfb70a9c | 43 | |
mbed_official | 2:864ddfb70a9c | 44 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
mbed_official | 2:864ddfb70a9c | 45 | { |
mbed_official | 2:864ddfb70a9c | 46 | (void) params; |
mbed_official | 2:864ddfb70a9c | 47 | BLE::Instance().gap().startAdvertising(); |
mbed_official | 2:864ddfb70a9c | 48 | } |
mbed_official | 2:864ddfb70a9c | 49 | |
samilive2011 | 39:ddabcba6eff1 | 50 | |
samilive2011 | 39:ddabcba6eff1 | 51 | void updateTSensorValue(void) { |
samilive2011 | 39:ddabcba6eff1 | 52 | currentTemperature = BSP_TSENSOR_ReadTemp(); |
samilive2011 | 39:ddabcba6eff1 | 53 | currentHumidity = BSP_HSENSOR_ReadHumidity(); |
samilive2011 | 39:ddabcba6eff1 | 54 | currentPressure = BSP_PSENSOR_ReadPressure(); |
samilive2011 | 39:ddabcba6eff1 | 55 | BSP_MAGNETO_GetXYZ(MagnetoXYZ); |
samilive2011 | 39:ddabcba6eff1 | 56 | BSP_GYRO_GetXYZ(GyroXYZ); |
samilive2011 | 39:ddabcba6eff1 | 57 | BSP_ACCELERO_AccGetXYZ(AcceleroXYZ); |
samilive2011 | 39:ddabcba6eff1 | 58 | SensorPtr->updateTemperature(currentTemperature); |
samilive2011 | 39:ddabcba6eff1 | 59 | SensorPtr->updateHumidity(currentHumidity); |
samilive2011 | 39:ddabcba6eff1 | 60 | SensorPtr->updatePressure(currentPressure); |
samilive2011 | 39:ddabcba6eff1 | 61 | SensorPtr->updateAccelero(AcceleroXYZ); |
samilive2011 | 39:ddabcba6eff1 | 62 | SensorPtr->updateMagneto(MagnetoXYZ); |
samilive2011 | 39:ddabcba6eff1 | 63 | SensorPtr->updateGyro(GyroXYZ); |
mbed_official | 2:864ddfb70a9c | 64 | } |
mbed_official | 2:864ddfb70a9c | 65 | |
samilive2011 | 39:ddabcba6eff1 | 66 | |
samilive2011 | 39:ddabcba6eff1 | 67 | void blinkCallback(void) |
samilive2011 | 39:ddabcba6eff1 | 68 | { |
samilive2011 | 39:ddabcba6eff1 | 69 | alivenessLED = !alivenessLED; |
samilive2011 | 39:ddabcba6eff1 | 70 | if (BLE::Instance().gap().getState().connected) { |
samilive2011 | 39:ddabcba6eff1 | 71 | eventQueue.call(updateTSensorValue); |
mbed_official | 2:864ddfb70a9c | 72 | } |
mbed_official | 2:864ddfb70a9c | 73 | } |
mbed_official | 2:864ddfb70a9c | 74 | |
samilive2011 | 39:ddabcba6eff1 | 75 | /********Lecture des donées et traitement selon leurs taille*******/ |
samilive2011 | 39:ddabcba6eff1 | 76 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
samilive2011 | 39:ddabcba6eff1 | 77 | if ((params->handle == writeChar.getValueHandle())) { |
samilive2011 | 39:ddabcba6eff1 | 78 | if(params->len == 1) { |
samilive2011 | 39:ddabcba6eff1 | 79 | actuatedLED = *(params->data); |
samilive2011 | 39:ddabcba6eff1 | 80 | pc.printf("Data received: length = %d\n\r",params->len); |
samilive2011 | 39:ddabcba6eff1 | 81 | if(actuatedLED == 1){ |
samilive2011 | 39:ddabcba6eff1 | 82 | pc.printf("LED is on\n\r"); |
samilive2011 | 39:ddabcba6eff1 | 83 | } |
samilive2011 | 39:ddabcba6eff1 | 84 | else{ |
samilive2011 | 39:ddabcba6eff1 | 85 | pc.printf("LED is off\n\r"); |
samilive2011 | 39:ddabcba6eff1 | 86 | } |
samilive2011 | 39:ddabcba6eff1 | 87 | for(int x=0; x < params->len; x++) { |
samilive2011 | 39:ddabcba6eff1 | 88 | pc.printf("%x", params->data[x]); |
samilive2011 | 39:ddabcba6eff1 | 89 | pc.printf("\n\r"); |
samilive2011 | 39:ddabcba6eff1 | 90 | } |
samilive2011 | 39:ddabcba6eff1 | 91 | } |
samilive2011 | 39:ddabcba6eff1 | 92 | else { |
samilive2011 | 39:ddabcba6eff1 | 93 | pc.printf("Data received: length = %d, data = 0x",params->len); |
samilive2011 | 39:ddabcba6eff1 | 94 | for(int x=0; x < params->len; x++) { |
samilive2011 | 39:ddabcba6eff1 | 95 | pc.printf("%x", params->data[x]); |
samilive2011 | 39:ddabcba6eff1 | 96 | } |
samilive2011 | 39:ddabcba6eff1 | 97 | pc.printf("\n\r"); |
samilive2011 | 39:ddabcba6eff1 | 98 | } |
samilive2011 | 39:ddabcba6eff1 | 99 | |
samilive2011 | 39:ddabcba6eff1 | 100 | } |
samilive2011 | 39:ddabcba6eff1 | 101 | } |
samilive2011 | 39:ddabcba6eff1 | 102 | |
samilive2011 | 39:ddabcba6eff1 | 103 | |
mbed_official | 2:864ddfb70a9c | 104 | void onBleInitError(BLE &ble, ble_error_t error) |
mbed_official | 2:864ddfb70a9c | 105 | { |
samilive2011 | 39:ddabcba6eff1 | 106 | pc.printf("Erreur\n\r"); |
mbed_official | 2:864ddfb70a9c | 107 | } |
mbed_official | 2:864ddfb70a9c | 108 | |
samilive2011 | 39:ddabcba6eff1 | 109 | |
mbed_official | 2:864ddfb70a9c | 110 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
mbed_official | 2:864ddfb70a9c | 111 | { |
mbed_official | 2:864ddfb70a9c | 112 | BLE& ble = params->ble; |
mbed_official | 2:864ddfb70a9c | 113 | ble_error_t error = params->error; |
mbed_official | 2:864ddfb70a9c | 114 | |
mbed_official | 2:864ddfb70a9c | 115 | if (error != BLE_ERROR_NONE) { |
mbed_official | 2:864ddfb70a9c | 116 | onBleInitError(ble, error); |
mbed_official | 2:864ddfb70a9c | 117 | return; |
mbed_official | 2:864ddfb70a9c | 118 | } |
mbed_official | 2:864ddfb70a9c | 119 | |
mbed_official | 2:864ddfb70a9c | 120 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
mbed_official | 2:864ddfb70a9c | 121 | return; |
mbed_official | 2:864ddfb70a9c | 122 | } |
mbed_official | 2:864ddfb70a9c | 123 | |
mbed_official | 2:864ddfb70a9c | 124 | ble.gap().onDisconnection(disconnectionCallback); |
samilive2011 | 39:ddabcba6eff1 | 125 | SensorPtr = new EnvironmentalService(ble); |
mbed_official | 2:864ddfb70a9c | 126 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
mbed_official | 2:864ddfb70a9c | 127 | |
mbed_official | 2:864ddfb70a9c | 128 | |
samilive2011 | 39:ddabcba6eff1 | 129 | /************************** advertising***********************/ |
mbed_official | 2:864ddfb70a9c | 130 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
mbed_official | 2:864ddfb70a9c | 131 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
mbed_official | 2:864ddfb70a9c | 132 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
mbed_official | 2:864ddfb70a9c | 133 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
mbed_official | 2:864ddfb70a9c | 134 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
samilive2011 | 39:ddabcba6eff1 | 135 | ble.addService(customService); |
mbed_official | 2:864ddfb70a9c | 136 | ble.gap().startAdvertising(); |
mbed_official | 2:864ddfb70a9c | 137 | } |
mbed_official | 2:864ddfb70a9c | 138 | |
mbed_official | 2:864ddfb70a9c | 139 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
mbed_official | 2:864ddfb70a9c | 140 | BLE &ble = BLE::Instance(); |
mbed_official | 11:7404978b24e7 | 141 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
mbed_official | 2:864ddfb70a9c | 142 | } |
mbed_official | 2:864ddfb70a9c | 143 | |
samilive2011 | 39:ddabcba6eff1 | 144 | /******************************MAIN*******************************/ |
samilive2011 | 39:ddabcba6eff1 | 145 | |
mbed_official | 2:864ddfb70a9c | 146 | int main() |
mbed_official | 2:864ddfb70a9c | 147 | { |
samilive2011 | 39:ddabcba6eff1 | 148 | BSP_TSENSOR_Init(); |
samilive2011 | 39:ddabcba6eff1 | 149 | BSP_HSENSOR_Init(); |
samilive2011 | 39:ddabcba6eff1 | 150 | BSP_PSENSOR_Init(); |
samilive2011 | 39:ddabcba6eff1 | 151 | BSP_MAGNETO_Init(); |
samilive2011 | 39:ddabcba6eff1 | 152 | BSP_GYRO_Init(); |
samilive2011 | 39:ddabcba6eff1 | 153 | BSP_ACCELERO_Init(); |
samilive2011 | 39:ddabcba6eff1 | 154 | pc.printf("BLE is Working\n\r"); |
mbed_official | 11:7404978b24e7 | 155 | eventQueue.call_every(500, blinkCallback); |
samilive2011 | 39:ddabcba6eff1 | 156 | |
mbed_official | 2:864ddfb70a9c | 157 | |
mbed_official | 2:864ddfb70a9c | 158 | BLE &ble = BLE::Instance(); |
mbed_official | 2:864ddfb70a9c | 159 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
mbed_official | 2:864ddfb70a9c | 160 | ble.init(bleInitComplete); |
mbed_official | 2:864ddfb70a9c | 161 | |
mbed_official | 11:7404978b24e7 | 162 | eventQueue.dispatch_forever(); |
mbed_official | 2:864ddfb70a9c | 163 | |
mbed_official | 2:864ddfb70a9c | 164 | return 0; |
mbed_official | 2:864ddfb70a9c | 165 | } |