George Roussos / BLE_Temperature

Dependencies:   DS1820

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*----------------------------------------------------------------------------
00002 LAB EXERCISE - BLE Thermometer
00003  ----------------------------------------
00004     In this exercise you will write a program which will turn your board into
00005     a BLE thermometer. You will be able to check the temperature with any device
00006     that can read BLE temperature readings.
00007     
00008     GOOD LUCK!
00009  *----------------------------------------------------------------------------*/
00010 
00011 #include "mbed.h"
00012 #include "DS1820.h"
00013 #include "nRF51822n.h"
00014 
00015 nRF51822n   nrf;
00016 DS1820 probe(P0_20);
00017 //AnalogIn LM35(P0_2); // Uncomment for analog LM35 sensor from Maplin
00018 Ticker      oneSec;
00019 
00020 // LEDs for indication
00021 DigitalOut  oneSecLed(LED1);
00022 DigitalOut  adStateLed(LED2);
00023 
00024 const static char   deviceName[] = "** Temperature Sensor **";
00025 
00026 // Health Thermometer Service
00027 uint8_t             tempSensorPayload[5] = { 0, 0, 0, 0, 0 };
00028 GattService         tempSensorService (GattService::UUID_HEALTH_THERMOMETER_SERVICE);
00029 GattCharacteristic  tempSensorChar (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, 5, 5, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
00030 
00031 // Advertising data and parameters
00032 GapAdvertisingData   advData;
00033 GapAdvertisingData   scanResponse;
00034 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
00035 
00036 // Function constructors
00037 uint32_t quick_ieee11073_from_float(float temperature);
00038 void updateTemp(void);
00039 
00040 class GapEventHandler : public GapEvents {
00041     // When a client device connects we need to turn off the advertising LED
00042     virtual void onConnected(void){
00043         adStateLed = 0;
00044     }
00045 
00046     // When a client device disconnects we need to start advertising again
00047     virtual void onDisconnected(void) {
00048         nrf.getGap().startAdvertising(advParams);
00049         adStateLed = 1;
00050     }
00051 };
00052 
00053 
00054 int main(void){
00055     
00056     // Start the one second ticker
00057     oneSec.attach(updateTemp, 1);
00058     
00059     nrf.getGap().setEventHandler(new GapEventHandler());
00060     
00061     // Start the BLE radio
00062     nrf.init();
00063     nrf.reset();    
00064 
00065     // Add GAP data, but don't start advertising
00066     advData.addFlags((GapAdvertisingData::Flags)(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE));
00067     advData.addData(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t*)deviceName, sizeof(deviceName));
00068     advData.addAppearance(GapAdvertisingData::GENERIC_THERMOMETER);
00069     nrf.getGap().setAdvertisingData(advData, scanResponse);
00070 
00071     // Temperature Sensor Service
00072     tempSensorService.addCharacteristic(tempSensorChar);
00073     nrf.getGattServer().addService(tempSensorService);
00074     
00075     // Finally start advertising (can't start advertising before all services are added)
00076     nrf.getGap().startAdvertising(advParams);
00077     adStateLed = 1;
00078 
00079     while(1){
00080     }
00081 }
00082 
00083 // Update temperature
00084 void updateTemp(void){
00085       oneSecLed = !oneSecLed;
00086       
00087       probe.convertTemperature(true, DS1820::all_devices); // initialise DS1820 digital temperature sensor
00088       float temperature = probe.temperature();             // obtain reading 
00089       //float temperature = ((LM35*3.3)-0.600)*100.0;      // for use with analog LM35 sensor from Maplin
00090       uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
00091       memcpy(tempSensorPayload+1, &temp_ieee11073, 4);
00092       nrf.getGattServer().updateValue(tempSensorChar.handle, tempSensorPayload, sizeof(tempSensorPayload));
00093 }
00094 
00095 // Quick conversion to an IEEE11073 format
00096 uint32_t quick_ieee11073_from_float(float temperature){
00097     uint8_t  exponent = 0xFF;
00098     uint32_t mantissa = (uint32_t)(temperature*10);
00099     return ( ((uint32_t)exponent) << 24) | mantissa;
00100 }
00101