George Roussos / BLE_Temperature_Exercise

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 // Add the temperature sensor service and characteristics
00029 // <Write your code here>
00030 
00031 
00032 // Function constructors
00033 uint32_t quick_ieee11073_from_float(float temperature);
00034 void updateTemp(void);
00035 
00036 class GapEventHandler : public GapEvents {
00037     // When a client device connects we need to turn off the advertising LED
00038     virtual void onConnected(void){
00039         adStateLed = 0;
00040     }
00041 
00042     // When a client device disconnects we need to start advertising again
00043     virtual void onDisconnected(void) {
00044         nrf.getGap().startAdvertising(advParams);
00045         adStateLed = 1;
00046     }
00047 };
00048 
00049 
00050 int main(void){
00051     
00052     // Start the one second ticker
00053     oneSec.attach(updateTemp, 1);
00054     
00055     nrf.getGap().setEventHandler(new GapEventHandler());
00056     
00057     // Start the BLE radio
00058     nrf.init();
00059     nrf.reset();    
00060 
00061     // Add GAP data, but don't start advertising
00062     advData.addFlags((GapAdvertisingData::Flags)(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE));
00063     advData.addData(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t*)deviceName, sizeof(deviceName));
00064     advData.addAppearance(GapAdvertisingData::GENERIC_THERMOMETER);
00065     nrf.getGap().setAdvertisingData(advData, scanResponse);
00066 
00067     // Temperature Sensor Service
00068     // Add the characteristic to the tempSensorService and add the service to the GATT server
00069     // <Write your code here>
00070     
00071     // Finally start advertising (can't start advertising before all services are added)
00072     nrf.getGap().startAdvertising(advParams);
00073     adStateLed = 1;
00074 
00075     while(1){
00076     }
00077 }
00078 
00079 // Update temperature
00080 void updateTemp(void){
00081       oneSecLed = !oneSecLed;
00082       
00083       probe.convertTemperature(true, DS1820::all_devices); // initialise DS1820 digital temperature sensor
00084       float temperature = probe.temperature();             // obtain reading 
00085       //float temperature = ((LM35*3.3)-0.600)*100.0;      // for use with analog LM35 sensor from Maplin
00086       uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
00087       memcpy(tempSensorPayload+1, &temp_ieee11073, 4);
00088       nrf.getGattServer().updateValue(tempSensorChar.handle, tempSensorPayload, sizeof(tempSensorPayload));
00089 }
00090 
00091 // Quick conversion to an IEEE11073 format
00092 uint32_t quick_ieee11073_from_float(float temperature){
00093     uint8_t  exponent = 0xFF;
00094     uint32_t mantissa = (uint32_t)(temperature*10);
00095     return ( ((uint32_t)exponent) << 24) | mantissa;
00096 }
00097