BLE sending gps data
Dependencies: BLE_API SerialGPS X_NUCLEO_IDB0XA1 mbed
main.cpp@0:39c88011a5a5, 2016-01-13 (annotated)
- Committer:
- VictorMC
- Date:
- Wed Jan 13 15:49:05 2016 +0000
- Revision:
- 0:39c88011a5a5
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
VictorMC | 0:39c88011a5a5 | 1 | #include "mbed.h" |
VictorMC | 0:39c88011a5a5 | 2 | #include "ble/BLE.h" |
VictorMC | 0:39c88011a5a5 | 3 | #include "ble/services/HeartRateService.h" |
VictorMC | 0:39c88011a5a5 | 4 | #include "ble/services/BatteryService.h" |
VictorMC | 0:39c88011a5a5 | 5 | #include "ble/services/DeviceInformationService.h" |
VictorMC | 0:39c88011a5a5 | 6 | #include "SerialGPS.h" |
VictorMC | 0:39c88011a5a5 | 7 | |
VictorMC | 0:39c88011a5a5 | 8 | |
VictorMC | 0:39c88011a5a5 | 9 | |
VictorMC | 0:39c88011a5a5 | 10 | Serial pc(USBTX, USBRX); |
VictorMC | 0:39c88011a5a5 | 11 | |
VictorMC | 0:39c88011a5a5 | 12 | /** |
VictorMC | 0:39c88011a5a5 | 13 | * PC_10 - TX pin (RX on the GPS module side) |
VictorMC | 0:39c88011a5a5 | 14 | * PC_11 - RX pin (TX on the GPS module side) |
VictorMC | 0:39c88011a5a5 | 15 | * 9600 - GPS baud rate |
VictorMC | 0:39c88011a5a5 | 16 | */ |
VictorMC | 0:39c88011a5a5 | 17 | |
VictorMC | 0:39c88011a5a5 | 18 | SerialGPS gps(PC_10, PC_11, 9600); |
VictorMC | 0:39c88011a5a5 | 19 | |
VictorMC | 0:39c88011a5a5 | 20 | DigitalOut led1(LED1); |
VictorMC | 0:39c88011a5a5 | 21 | |
VictorMC | 0:39c88011a5a5 | 22 | BLE ble; |
VictorMC | 0:39c88011a5a5 | 23 | |
VictorMC | 0:39c88011a5a5 | 24 | const static char DEVICE_NAME[] = "NUCLEO_GPS"; |
VictorMC | 0:39c88011a5a5 | 25 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
VictorMC | 0:39c88011a5a5 | 26 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
VictorMC | 0:39c88011a5a5 | 27 | static volatile bool triggerSensorPolling = false; |
VictorMC | 0:39c88011a5a5 | 28 | |
VictorMC | 0:39c88011a5a5 | 29 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
VictorMC | 0:39c88011a5a5 | 30 | { |
VictorMC | 0:39c88011a5a5 | 31 | ble.gap().startAdvertising(); // restart advertising |
VictorMC | 0:39c88011a5a5 | 32 | } |
VictorMC | 0:39c88011a5a5 | 33 | |
VictorMC | 0:39c88011a5a5 | 34 | void periodicCallback(void) |
VictorMC | 0:39c88011a5a5 | 35 | { |
VictorMC | 0:39c88011a5a5 | 36 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
VictorMC | 0:39c88011a5a5 | 37 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
VictorMC | 0:39c88011a5a5 | 38 | * heavy-weight sensor polling from the main thread. */ |
VictorMC | 0:39c88011a5a5 | 39 | triggerSensorPolling = true; |
VictorMC | 0:39c88011a5a5 | 40 | } |
VictorMC | 0:39c88011a5a5 | 41 | |
VictorMC | 0:39c88011a5a5 | 42 | int main(void) |
VictorMC | 0:39c88011a5a5 | 43 | { |
VictorMC | 0:39c88011a5a5 | 44 | int k; |
VictorMC | 0:39c88011a5a5 | 45 | led1 = 1; |
VictorMC | 0:39c88011a5a5 | 46 | Ticker ticker; |
VictorMC | 0:39c88011a5a5 | 47 | ticker.attach(periodicCallback, 1); // blink LED every second |
VictorMC | 0:39c88011a5a5 | 48 | |
VictorMC | 0:39c88011a5a5 | 49 | ble.init(); |
VictorMC | 0:39c88011a5a5 | 50 | ble.gap().onDisconnection(disconnectionCallback); |
VictorMC | 0:39c88011a5a5 | 51 | |
VictorMC | 0:39c88011a5a5 | 52 | /* Setup primary service. */ |
VictorMC | 0:39c88011a5a5 | 53 | uint16_t hrmCounter = 100; |
VictorMC | 0:39c88011a5a5 | 54 | HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
VictorMC | 0:39c88011a5a5 | 55 | |
VictorMC | 0:39c88011a5a5 | 56 | /* Setup auxiliary service. */ |
VictorMC | 0:39c88011a5a5 | 57 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
VictorMC | 0:39c88011a5a5 | 58 | |
VictorMC | 0:39c88011a5a5 | 59 | /* Setup advertising. */ |
VictorMC | 0:39c88011a5a5 | 60 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
VictorMC | 0:39c88011a5a5 | 61 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
VictorMC | 0:39c88011a5a5 | 62 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
VictorMC | 0:39c88011a5a5 | 63 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
VictorMC | 0:39c88011a5a5 | 64 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
VictorMC | 0:39c88011a5a5 | 65 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
VictorMC | 0:39c88011a5a5 | 66 | ble.gap().startAdvertising(); |
VictorMC | 0:39c88011a5a5 | 67 | |
VictorMC | 0:39c88011a5a5 | 68 | pc.printf("Start loop\n\r"); |
VictorMC | 0:39c88011a5a5 | 69 | // infinite loop |
VictorMC | 0:39c88011a5a5 | 70 | while (1) { |
VictorMC | 0:39c88011a5a5 | 71 | // check for trigger from periodicCallback() |
VictorMC | 0:39c88011a5a5 | 72 | if (triggerSensorPolling && ble.getGapState().connected){ |
VictorMC | 0:39c88011a5a5 | 73 | triggerSensorPolling = false; |
VictorMC | 0:39c88011a5a5 | 74 | pc.printf("Waiting for sample..\n\r"); |
VictorMC | 0:39c88011a5a5 | 75 | if (gps.sample()) { |
VictorMC | 0:39c88011a5a5 | 76 | for(k=0;k<=256;k++){ |
VictorMC | 0:39c88011a5a5 | 77 | pc.printf("%c", gps.msg[k]); |
VictorMC | 0:39c88011a5a5 | 78 | } |
VictorMC | 0:39c88011a5a5 | 79 | pc.printf("\n\r"); |
VictorMC | 0:39c88011a5a5 | 80 | pc.printf("sats %d, lat %f, lont %f, alt %f, geoid %f, time %f\n\r", gps.sats, gps.latitude, gps.longitude, gps.alt, gps.geoid, gps.time); |
VictorMC | 0:39c88011a5a5 | 81 | hrService.updateHeartRate((uint16_t)(gps.longitude*1000)); |
VictorMC | 0:39c88011a5a5 | 82 | } |
VictorMC | 0:39c88011a5a5 | 83 | } else { |
VictorMC | 0:39c88011a5a5 | 84 | ble.waitForEvent(); // low power wait for event |
VictorMC | 0:39c88011a5a5 | 85 | } |
VictorMC | 0:39c88011a5a5 | 86 | } |
VictorMC | 0:39c88011a5a5 | 87 | } |