ble- temperature

/* mbed Microcontroller Library

  • Copyright (c) 2006-2015 ARM Limited
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  • http://www.apache.org/licenses/LICENSE-2.0
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
  • /
  1. include "mbed.h"
  2. include "ble/BLE.h"
  3. include "ble/services/HeartRateService.h"
  4. include "x_nucleo_iks01a1.h"

/* Instantiate the expansion board */ static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);

/* Retrieve the composing elements of the expansion board */ static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;

/* Helper function for printing floats & doubles */ static char *printDouble(char* str, double v, int decimalDigits=2) { int i = 1; int intPart, fractPart; int len; char *ptr;

/* prepare decimal digits multiplicator */ for (;decimalDigits!=0; i*=10, decimalDigits);

/* calculate integer & fractinal parts */ intPart = (int)v; fractPart = (int)((v-(double)(int)v)*i);

/* fill in integer part */ sprintf(str, "%i.", intPart);

/* prepare fill in of fractional part */ len = strlen(str); ptr = &str[len];

/* fill in leading fractional zeros */ for (i/=10;i>1; i/=10, ptr++) { if(fractPart >= i) break;

  • ptr = '0'; }

/* fill in (rest of) fractional part */ sprintf(ptr, "%i", fractPart);

return str; }

DigitalOut led1(LED1, 1);

const static char DEVICE_NAME[] = "JARDINOTIQUE"; static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};

static volatile bool triggerSensorPolling = false;

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { (void)params; BLE::Instance().gap().startAdvertising(); restart advertising }

void periodicCallback(void) { led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ /* Note that the periodicCallback() executes in interrupt context, so it is safer to do

  • heavy-weight sensor polling from the main thread. */ triggerSensorPolling = true; }

void onBleInitError(BLE &ble, ble_error_t error) { (void)ble; (void)error; /* Initialization error handling should go here */ }

void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; ble_error_t error = params->error; uint8_t id; float value1; if (error != BLE_ERROR_NONE) { onBleInitError(ble, error); return; }

if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; }

ble.gap().onDisconnection(disconnectionCallback);

/* Setup primary service. */ uint8_t hrmCounter = temp_sensor1->GetTemperature(&value1); init HRM to 60bps HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);

/* Setup advertising. */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(1000); /* 1000ms */ ble.gap().startAdvertising();

infinite loop while (true) { check for trigger from periodicCallback() if (triggerSensorPolling && ble.getGapState().connected) { triggerSensorPolling = false;

Do blocking calls or whatever is necessary for sensor polling. In our case, we simply update the HRM measurement. hrmCounter++;

60 <= HRM bps <= 100

update bps hrService.updateHeartRate(hrmCounter); } else { ble.waitForEvent(); low power wait for event } } }

int main(void) { uint8_t id; float value1, value2; char buffer1[32], buffer2[32]; int32_t axes[3]; Ticker ticker; ticker.attach(periodicCallback, 1); blink LED every second printDouble(buffer1, value1);

BLE::Instance().init(bleInitComplete); }

Download repository: zip gz

Files at revision -1:000000000000

Name Size Actions
[up]