Transmit temperature data via BLE

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BLE.h"
00003 #include "BatteryService.h"
00004 #include "DeviceInformationService.h"
00005 #include "HealthThermometerService.h"
00006 #include "hts221.h"
00007 
00008 #define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0
00009 
00010 BLEDevice  ble;
00011 DigitalOut led1(p1);
00012 DigitalOut led2(p2);
00013 
00014 const static char     DEVICE_NAME[]        = "BLEThermo";
00015 static const uint16_t uuid16_list[]        = {GattService::UUID_BATTERY_SERVICE,
00016                                               GattService::UUID_DEVICE_INFORMATION_SERVICE,
00017                                               GattService::UUID_HEALTH_THERMOMETER_SERVICE,};
00018                                               
00019 static volatile bool  triggerSensorPolling = false;
00020 
00021 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00022 {
00023     ble.startAdvertising(); // restart advertising
00024 }
00025 
00026 void onConnectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *params)
00027 {
00028     #if UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL
00029     
00030     #define MIN_CONN_INTERVAL 250  /**< Minimum connection interval (250 ms) */
00031     #define MAX_CONN_INTERVAL 350  /**< Maximum connection interval (350 ms). */
00032     #define CONN_SUP_TIMEOUT  6000 /**< Connection supervisory timeout (6 seconds). */
00033     #define SLAVE_LATENCY     4
00034 
00035         Gap::ConnectionParams_t gap_conn_params;
00036         gap_conn_params.minConnectionInterval        = Gap::MSEC_TO_GAP_DURATION_UNITS(MIN_CONN_INTERVAL);
00037         gap_conn_params.maxConnectionInterval        = Gap::MSEC_TO_GAP_DURATION_UNITS(MAX_CONN_INTERVAL);
00038         gap_conn_params.connectionSupervisionTimeout = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_SUP_TIMEOUT);
00039         gap_conn_params.slaveLatency                 = SLAVE_LATENCY;
00040         ble.updateConnectionParams(handle, &gap_conn_params);
00041     #endif /* #if UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL */
00042 }
00043 
00044 void periodicCallback(void)
00045 {
00046     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00047     triggerSensorPolling = true;
00048 }
00049 
00050 float tempCelsius = 25.50;
00051 float humi = 55;
00052 int humiMax = 100; 
00053 
00054 int main(void)
00055 {     
00056     led2 = 0;
00057     led1 = 0;   
00058     if (hts221_init())
00059     {
00060         HTS221_Calib();
00061     
00062         Ticker ticker;
00063         ticker.attach(periodicCallback, 1);
00064     
00065         ble.init();
00066         ble.onDisconnection(disconnectionCallback);
00067 #if UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL
00068         ble.onConnection(onConnectionCallback);
00069 #endif /* #if UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL */
00070 
00071         
00072         HealthThermometerService    ThermoService(ble, tempCelsius , HealthThermometerService::LOCATION_EAR);
00073         BatteryService              battery(ble);
00074         DeviceInformationService    deviceInfo(ble, "Cyntec", "Combo module", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00075     
00076         /* Setup advertising. */
00077         /* Setting advertising string*/
00078         ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00079         ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00080         ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG);
00081         ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00082         /* Setting advertising parameters*/
00083         ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00084         ble.setAdvertisingInterval(1000); 
00085         ble.setAdvertisingTimeout(0x1e);  //Timeout, stop advertising after 30sec
00086         ble.startAdvertising();
00087 
00088         while (true) {
00089             if (triggerSensorPolling && ble.getGapState().connected) {
00090                 triggerSensorPolling = false;
00091             
00092                 HTS221_ReadTempHumi(&tempCelsius, &humi);
00093                 ThermoService.updateTemperature(tempCelsius);
00094             
00095             } else {
00096                 ble.waitForEvent();
00097             }
00098         }
00099     } else {
00100         while(true)
00101         {
00102             led2 = !led2;
00103             wait(1);
00104         }
00105     } 
00106 }