Reading SenseAir LP8 CO2 sensor over bluetooth low energy

Dependencies:   BLE_API mbed nRF51822

Committer:
jony1401
Date:
Fri Apr 21 13:26:06 2017 +0000
Revision:
0:ee3787c8e209
Child:
1:b512a405b584
alpha v 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jony1401 0:ee3787c8e209 1 #include "mbed.h"
jony1401 0:ee3787c8e209 2 #include "BLE.h"
jony1401 0:ee3787c8e209 3 #include "LP8_Service.h"
jony1401 0:ee3787c8e209 4 #include "LP8.h"
jony1401 0:ee3787c8e209 5
jony1401 0:ee3787c8e209 6 //setup ble stack
jony1401 0:ee3787c8e209 7 BLE ble; //BLE object
jony1401 0:ee3787c8e209 8
jony1401 0:ee3787c8e209 9 // Pins and timers needed for lp8 communication
jony1401 0:ee3787c8e209 10 DigitalIn RDY(P0_8); //
jony1401 0:ee3787c8e209 11 DigitalOut VBB(P0_5);
jony1401 0:ee3787c8e209 12 DigitalOut VBB_EN(P0_4); //
jony1401 0:ee3787c8e209 13 Serial Device(P0_9, P0_11); //tx, rx
jony1401 0:ee3787c8e209 14 DigitalOut led1(P0_19, 1); //board led
jony1401 0:ee3787c8e209 15 Timer lp8Wait; //timer for sensor communication
jony1401 0:ee3787c8e209 16
jony1401 0:ee3787c8e209 17
jony1401 0:ee3787c8e209 18
jony1401 0:ee3787c8e209 19 //Sensor and ble Configuration parameters
jony1401 0:ee3787c8e209 20 #define SENSOR_TIMER 10.0 //lp8 polling interval (seconds)
jony1401 0:ee3787c8e209 21 #define BLE_ADV_INTERVAL 500 //advertisment interval (milliseconds)
jony1401 0:ee3787c8e209 22
jony1401 0:ee3787c8e209 23
jony1401 0:ee3787c8e209 24
jony1401 0:ee3787c8e209 25 //device name and uuid list setup
jony1401 0:ee3787c8e209 26 const static char DEVICE_NAME[] = "SenseAir LP8";
jony1401 0:ee3787c8e209 27 static const uint16_t uuid16_list[] = {LP8_Service::LP8_SERVICE_UUID};
jony1401 0:ee3787c8e209 28
jony1401 0:ee3787c8e209 29
jony1401 0:ee3787c8e209 30 //check for sensor triggering and uppdating Gatt Server
jony1401 0:ee3787c8e209 31 bool triggerSensor = false; //check for sensor polling
jony1401 0:ee3787c8e209 32 LP8_Service *lp8ServicePtr; //pointer to lp8 service object
jony1401 0:ee3787c8e209 33
jony1401 0:ee3787c8e209 34
jony1401 0:ee3787c8e209 35
jony1401 0:ee3787c8e209 36 //**************************** ble functions *******************************
jony1401 0:ee3787c8e209 37 //**************************** *******************************
jony1401 0:ee3787c8e209 38 // on Disconnect, Restart BroadCasting
jony1401 0:ee3787c8e209 39 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
jony1401 0:ee3787c8e209 40 {
jony1401 0:ee3787c8e209 41 //reset I/O pin, necessary?
jony1401 0:ee3787c8e209 42 // VBB_EN.write ( 1 );
jony1401 0:ee3787c8e209 43
jony1401 0:ee3787c8e209 44 //restart broadcast
jony1401 0:ee3787c8e209 45 ble.gap().startAdvertising();
jony1401 0:ee3787c8e209 46 }
jony1401 0:ee3787c8e209 47 //timer function callback function
jony1401 0:ee3787c8e209 48 void triggerSensorPollingInterupt()
jony1401 0:ee3787c8e209 49 {
jony1401 0:ee3787c8e209 50 triggerSensor = true;
jony1401 0:ee3787c8e209 51 };
jony1401 0:ee3787c8e209 52
jony1401 0:ee3787c8e209 53
jony1401 0:ee3787c8e209 54
jony1401 0:ee3787c8e209 55 //main
jony1401 0:ee3787c8e209 56 int main(void)
jony1401 0:ee3787c8e209 57 {
jony1401 0:ee3787c8e209 58 led1 = 1; //turn off led
jony1401 0:ee3787c8e209 59
jony1401 0:ee3787c8e209 60 Ticker lp8Timer; //timer object for sensor polling interupts
jony1401 0:ee3787c8e209 61 lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER); //trigger sensor reading every X.Y sec
jony1401 0:ee3787c8e209 62
jony1401 0:ee3787c8e209 63 ble.init();
jony1401 0:ee3787c8e209 64 ble.gap().onDisconnection(disconnectionCallback); //do callback if disconnection occurs
jony1401 0:ee3787c8e209 65
jony1401 0:ee3787c8e209 66
jony1401 0:ee3787c8e209 67 int co2Value = 400; //initial CO2 value
jony1401 0:ee3787c8e209 68 bool initCheck = true; //check for init sensor state or lost state
jony1401 0:ee3787c8e209 69 bool successCheck = false;
jony1401 0:ee3787c8e209 70
jony1401 0:ee3787c8e209 71 //setup LP8 peripheral communication
jony1401 0:ee3787c8e209 72 LP8 lp8(Device, VBB, VBB_EN, RDY, lp8Wait);
jony1401 0:ee3787c8e209 73
jony1401 0:ee3787c8e209 74
jony1401 0:ee3787c8e209 75 //setup for GattService
jony1401 0:ee3787c8e209 76 /* Setup Gatt server */
jony1401 0:ee3787c8e209 77 LP8_Service lp8Service(ble, co2Value); //pass in ble object and initial co2 value
jony1401 0:ee3787c8e209 78 lp8ServicePtr = &lp8Service; //set pointer to "real" lp8 service object
jony1401 0:ee3787c8e209 79
jony1401 0:ee3787c8e209 80
jony1401 0:ee3787c8e209 81 /* setup advertising parameters */
jony1401 0:ee3787c8e209 82 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); //general bluetooth information(only support for ble
jony1401 0:ee3787c8e209 83 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); //service list
jony1401 0:ee3787c8e209 84 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
jony1401 0:ee3787c8e209 85 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jony1401 0:ee3787c8e209 86 ble.gap().setAdvertisingInterval(BLE_ADV_INTERVAL); /* advertising interval in ms. */
jony1401 0:ee3787c8e209 87 ble.gap().startAdvertising(); //start broadcast
jony1401 0:ee3787c8e209 88
jony1401 0:ee3787c8e209 89 /* SpinWait for initialization to complete. This is necessary because the
jony1401 0:ee3787c8e209 90 * BLE object is used in the main loop below. */
jony1401 0:ee3787c8e209 91 while (ble.hasInitialized() == false) { /* spin loop */ }
jony1401 0:ee3787c8e209 92
jony1401 0:ee3787c8e209 93
jony1401 0:ee3787c8e209 94
jony1401 0:ee3787c8e209 95 //start the loop
jony1401 0:ee3787c8e209 96 while ( true )
jony1401 0:ee3787c8e209 97 {
jony1401 0:ee3787c8e209 98 if(triggerSensor && ble.gap().getState().connected ) //trigger when timer interupts ticks and there is an established connection
jony1401 0:ee3787c8e209 99 {
jony1401 0:ee3787c8e209 100 if ( initCheck ) {
jony1401 0:ee3787c8e209 101 successCheck = lp8.lp8Init();
jony1401 0:ee3787c8e209 102
jony1401 0:ee3787c8e209 103 // if ( successCheck ) {
jony1401 0:ee3787c8e209 104 initCheck = false;
jony1401 0:ee3787c8e209 105 // led1 = 0; //turn on led
jony1401 0:ee3787c8e209 106 // }
jony1401 0:ee3787c8e209 107 }
jony1401 0:ee3787c8e209 108 else {
jony1401 0:ee3787c8e209 109 lp8.lp8Talk();
jony1401 0:ee3787c8e209 110 led1 = 0; //on when talking
jony1401 0:ee3787c8e209 111 }
jony1401 0:ee3787c8e209 112
jony1401 0:ee3787c8e209 113 //reset sensor status
jony1401 0:ee3787c8e209 114 triggerSensor = false;
jony1401 0:ee3787c8e209 115 //update gattServer with new CO2 value
jony1401 0:ee3787c8e209 116 lp8ServicePtr->updateCo2Value( lp8.getValue() );
jony1401 0:ee3787c8e209 117 }
jony1401 0:ee3787c8e209 118
jony1401 0:ee3787c8e209 119 else { ble.waitForEvent(); } //ble save energy
jony1401 0:ee3787c8e209 120 }
jony1401 0:ee3787c8e209 121
jony1401 0:ee3787c8e209 122 };
jony1401 0:ee3787c8e209 123