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@4:a937975ebe7e, 2017-08-25 (annotated)
- Committer:
- Ren Boting
- Date:
- Fri Aug 25 15:47:39 2017 +0900
- Revision:
- 4:a937975ebe7e
- Parent:
- 3:4b41a1ba65c8
Move print lines into a Debug option.
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 | 4:a937975ebe7e | 6 | #if defined(_DEBUG) |
Ren Boting | 4:a937975ebe7e | 7 | Serial pc(USBTX, USBRX); |
Ren Boting | 4:a937975ebe7e | 8 | #endif |
Ren Boting | 4:a937975ebe7e | 9 | |
Ren Boting | 3:4b41a1ba65c8 | 10 | #if defined(TARGET_TY51822R3) |
edamame22 | 0:f22fa5cc4d56 | 11 | BME280 sensor(I2C_SDA0, I2C_SCL0); |
edamame22 | 0:f22fa5cc4d56 | 12 | #else |
edamame22 | 0:f22fa5cc4d56 | 13 | BME280 sensor(I2C_SDA, I2C_SCL); |
edamame22 | 0:f22fa5cc4d56 | 14 | #endif |
edamame22 | 0:f22fa5cc4d56 | 15 | |
edamame22 | 0:f22fa5cc4d56 | 16 | const static char DEVICE_NAME[] = "BME280"; |
edamame22 | 0:f22fa5cc4d56 | 17 | const static uint16_t uuid16_list[] = {GattService::UUID_ENVIRONMENTAL_SERVICE}; |
Ren Boting | 3:4b41a1ba65c8 | 18 | static EnvironmentalService *service = NULL; |
Ren Boting | 3:4b41a1ba65c8 | 19 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
edamame22 | 0:f22fa5cc4d56 | 20 | |
edamame22 | 0:f22fa5cc4d56 | 21 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
edamame22 | 0:f22fa5cc4d56 | 22 | { |
edamame22 | 0:f22fa5cc4d56 | 23 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
edamame22 | 0:f22fa5cc4d56 | 24 | } |
edamame22 | 0:f22fa5cc4d56 | 25 | |
edamame22 | 0:f22fa5cc4d56 | 26 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
edamame22 | 0:f22fa5cc4d56 | 27 | { |
Ren Boting | 3:4b41a1ba65c8 | 28 | BLE &ble = params->ble; |
edamame22 | 0:f22fa5cc4d56 | 29 | ble_error_t error = params->error; |
Ren Boting | 3:4b41a1ba65c8 | 30 | service = new EnvironmentalService(ble); |
Ren Boting | 4:a937975ebe7e | 31 | #if defined(_DEBUG) |
Ren Boting | 4:a937975ebe7e | 32 | pc.printf("Inside BLE..starting payload creation..\r\n"); |
Ren Boting | 4:a937975ebe7e | 33 | #endif |
edamame22 | 0:f22fa5cc4d56 | 34 | ble.gap().onDisconnection(disconnectionCallback); |
edamame22 | 0:f22fa5cc4d56 | 35 | |
edamame22 | 0:f22fa5cc4d56 | 36 | /* Setup advertising. */ |
edamame22 | 0:f22fa5cc4d56 | 37 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
edamame22 | 0:f22fa5cc4d56 | 38 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
edamame22 | 0:f22fa5cc4d56 | 39 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
edamame22 | 0:f22fa5cc4d56 | 40 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Ren Boting | 3:4b41a1ba65c8 | 41 | ble.gap().setAdvertisingInterval(1000); |
edamame22 | 0:f22fa5cc4d56 | 42 | error = ble.gap().startAdvertising(); |
Ren Boting | 4:a937975ebe7e | 43 | #if defined(_DEBUG) |
Ren Boting | 4:a937975ebe7e | 44 | pc.printf("ble.gap().startAdvertising() => %u\r\n", error); |
Ren Boting | 4:a937975ebe7e | 45 | #endif |
edamame22 | 0:f22fa5cc4d56 | 46 | } |
edamame22 | 0:f22fa5cc4d56 | 47 | |
Ren Boting | 3:4b41a1ba65c8 | 48 | void readSensorCallback(void) |
Ren Boting | 3:4b41a1ba65c8 | 49 | { |
edamame22 | 0:f22fa5cc4d56 | 50 | float tmp_t, tmp_p, tmp_h; |
Ren Boting | 3:4b41a1ba65c8 | 51 | |
Ren Boting | 3:4b41a1ba65c8 | 52 | if(service != NULL) { |
Ren Boting | 3:4b41a1ba65c8 | 53 | tmp_t = sensor.getTemperature(); |
Ren Boting | 3:4b41a1ba65c8 | 54 | tmp_p = sensor.getPressure(); |
Ren Boting | 3:4b41a1ba65c8 | 55 | tmp_h = sensor.getHumidity(); |
Ren Boting | 3:4b41a1ba65c8 | 56 | service->updatePressure(tmp_p); |
Ren Boting | 3:4b41a1ba65c8 | 57 | service->updateTemperature(tmp_t); |
Ren Boting | 3:4b41a1ba65c8 | 58 | service->updateHumidity(tmp_h); |
Ren Boting | 4:a937975ebe7e | 59 | #if defined(_DEBUG) |
Ren Boting | 4:a937975ebe7e | 60 | pc.printf("%04.2f hPa, %2.2f degC, %2.2f %%\r\n", tmp_p, tmp_t, tmp_h ); |
Ren Boting | 4:a937975ebe7e | 61 | #endif |
edamame22 | 0:f22fa5cc4d56 | 62 | } |
edamame22 | 0:f22fa5cc4d56 | 63 | } |
edamame22 | 0:f22fa5cc4d56 | 64 | |
Ren Boting | 3:4b41a1ba65c8 | 65 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) |
Ren Boting | 3:4b41a1ba65c8 | 66 | { |
Ren Boting | 3:4b41a1ba65c8 | 67 | BLE &ble = BLE::Instance(); |
Ren Boting | 3:4b41a1ba65c8 | 68 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
edamame22 | 0:f22fa5cc4d56 | 69 | } |
edamame22 | 0:f22fa5cc4d56 | 70 | |
Ren Boting | 3:4b41a1ba65c8 | 71 | int main() |
Ren Boting | 3:4b41a1ba65c8 | 72 | { |
Ren Boting | 3:4b41a1ba65c8 | 73 | eventQueue.call_every(1000, readSensorCallback); |
edamame22 | 0:f22fa5cc4d56 | 74 | |
Ren Boting | 3:4b41a1ba65c8 | 75 | BLE &ble = BLE::Instance(); |
Ren Boting | 3:4b41a1ba65c8 | 76 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Ren Boting | 3:4b41a1ba65c8 | 77 | ble.init(bleInitComplete); |
edamame22 | 0:f22fa5cc4d56 | 78 | |
Ren Boting | 3:4b41a1ba65c8 | 79 | eventQueue.dispatch_forever(); |
Ren Boting | 3:4b41a1ba65c8 | 80 | |
Ren Boting | 3:4b41a1ba65c8 | 81 | return 0; |
edamame22 | 0:f22fa5cc4d56 | 82 | } |