![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is an example to broadcast measured value of BME280 through BLE GATT service.
please refer detail information at GitHub. https://github.com/soramame21/BLE_Server_BME280
source/main.cpp@3:4b41a1ba65c8, 2017-08-23 (annotated)
- Committer:
- Ren Boting
- Date:
- Wed Aug 23 13:40:01 2017 +0900
- Revision:
- 3:4b41a1ba65c8
- Parent:
- 1:9d1a3835d884
- Child:
- 4:a937975ebe7e
Refined the application.
1. Update version of mbed-os
2. Clean source code and comments in main.cpp
3. Replace thread solution by using EventQueue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edamame22 | 0:f22fa5cc4d56 | 1 | #include "mbed.h" |
edamame22 | 0:f22fa5cc4d56 | 2 | #include "BME280.h" |
edamame22 | 0:f22fa5cc4d56 | 3 | #include "ble/BLE.h" |
Ren Boting | 3:4b41a1ba65c8 | 4 | #include "ble/services/EnvironmentalService.h" |
edamame22 | 0:f22fa5cc4d56 | 5 | |
Ren Boting | 3:4b41a1ba65c8 | 6 | #if defined(TARGET_TY51822R3) |
edamame22 | 0:f22fa5cc4d56 | 7 | BME280 sensor(I2C_SDA0, I2C_SCL0); |
edamame22 | 0:f22fa5cc4d56 | 8 | #else |
edamame22 | 0:f22fa5cc4d56 | 9 | BME280 sensor(I2C_SDA, I2C_SCL); |
edamame22 | 0:f22fa5cc4d56 | 10 | #endif |
edamame22 | 0:f22fa5cc4d56 | 11 | |
edamame22 | 0:f22fa5cc4d56 | 12 | const static char DEVICE_NAME[] = "BME280"; |
edamame22 | 0:f22fa5cc4d56 | 13 | const static uint16_t uuid16_list[] = {GattService::UUID_ENVIRONMENTAL_SERVICE}; |
Ren Boting | 3:4b41a1ba65c8 | 14 | static EnvironmentalService *service = NULL; |
Ren Boting | 3:4b41a1ba65c8 | 15 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
edamame22 | 0:f22fa5cc4d56 | 16 | |
edamame22 | 0:f22fa5cc4d56 | 17 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
edamame22 | 0:f22fa5cc4d56 | 18 | { |
edamame22 | 0:f22fa5cc4d56 | 19 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
edamame22 | 0:f22fa5cc4d56 | 20 | } |
edamame22 | 0:f22fa5cc4d56 | 21 | |
edamame22 | 0:f22fa5cc4d56 | 22 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
edamame22 | 0:f22fa5cc4d56 | 23 | { |
Ren Boting | 3:4b41a1ba65c8 | 24 | BLE &ble = params->ble; |
edamame22 | 0:f22fa5cc4d56 | 25 | ble_error_t error = params->error; |
Ren Boting | 3:4b41a1ba65c8 | 26 | service = new EnvironmentalService(ble); |
edamame22 | 0:f22fa5cc4d56 | 27 | |
edamame22 | 0:f22fa5cc4d56 | 28 | printf("Inside BLE..starting payload creation..\r\n"); |
edamame22 | 0:f22fa5cc4d56 | 29 | ble.gap().onDisconnection(disconnectionCallback); |
edamame22 | 0:f22fa5cc4d56 | 30 | |
edamame22 | 0:f22fa5cc4d56 | 31 | /* Setup advertising. */ |
edamame22 | 0:f22fa5cc4d56 | 32 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
edamame22 | 0:f22fa5cc4d56 | 33 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
edamame22 | 0:f22fa5cc4d56 | 34 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
edamame22 | 0:f22fa5cc4d56 | 35 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Ren Boting | 3:4b41a1ba65c8 | 36 | ble.gap().setAdvertisingInterval(1000); |
edamame22 | 0:f22fa5cc4d56 | 37 | error = ble.gap().startAdvertising(); |
edamame22 | 0:f22fa5cc4d56 | 38 | |
Ren Boting | 3:4b41a1ba65c8 | 39 | printf("ble.gap().startAdvertising() => %u\r\n", error); |
edamame22 | 0:f22fa5cc4d56 | 40 | |
edamame22 | 0:f22fa5cc4d56 | 41 | } |
edamame22 | 0:f22fa5cc4d56 | 42 | |
Ren Boting | 3:4b41a1ba65c8 | 43 | void readSensorCallback(void) |
Ren Boting | 3:4b41a1ba65c8 | 44 | { |
edamame22 | 0:f22fa5cc4d56 | 45 | float tmp_t, tmp_p, tmp_h; |
Ren Boting | 3:4b41a1ba65c8 | 46 | |
Ren Boting | 3:4b41a1ba65c8 | 47 | if(service != NULL) { |
Ren Boting | 3:4b41a1ba65c8 | 48 | tmp_t = sensor.getTemperature(); |
Ren Boting | 3:4b41a1ba65c8 | 49 | tmp_p = sensor.getPressure(); |
Ren Boting | 3:4b41a1ba65c8 | 50 | tmp_h = sensor.getHumidity(); |
Ren Boting | 3:4b41a1ba65c8 | 51 | service->updatePressure(tmp_p); |
Ren Boting | 3:4b41a1ba65c8 | 52 | service->updateTemperature(tmp_t); |
Ren Boting | 3:4b41a1ba65c8 | 53 | service->updateHumidity(tmp_h); |
Ren Boting | 3:4b41a1ba65c8 | 54 | printf("%04.2f hPa, %2.2f degC, %2.2f %%\r\n", tmp_p, tmp_t, tmp_h ); |
edamame22 | 0:f22fa5cc4d56 | 55 | } |
edamame22 | 0:f22fa5cc4d56 | 56 | } |
edamame22 | 0:f22fa5cc4d56 | 57 | |
Ren Boting | 3:4b41a1ba65c8 | 58 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) |
Ren Boting | 3:4b41a1ba65c8 | 59 | { |
Ren Boting | 3:4b41a1ba65c8 | 60 | BLE &ble = BLE::Instance(); |
Ren Boting | 3:4b41a1ba65c8 | 61 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
edamame22 | 0:f22fa5cc4d56 | 62 | } |
edamame22 | 0:f22fa5cc4d56 | 63 | |
Ren Boting | 3:4b41a1ba65c8 | 64 | int main() |
Ren Boting | 3:4b41a1ba65c8 | 65 | { |
Ren Boting | 3:4b41a1ba65c8 | 66 | eventQueue.call_every(1000, readSensorCallback); |
edamame22 | 0:f22fa5cc4d56 | 67 | |
Ren Boting | 3:4b41a1ba65c8 | 68 | BLE &ble = BLE::Instance(); |
Ren Boting | 3:4b41a1ba65c8 | 69 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Ren Boting | 3:4b41a1ba65c8 | 70 | ble.init(bleInitComplete); |
edamame22 | 0:f22fa5cc4d56 | 71 | |
Ren Boting | 3:4b41a1ba65c8 | 72 | eventQueue.dispatch_forever(); |
Ren Boting | 3:4b41a1ba65c8 | 73 | |
Ren Boting | 3:4b41a1ba65c8 | 74 | return 0; |
edamame22 | 0:f22fa5cc4d56 | 75 | } |