Reading SenseAir LP8 CO2 sensor over bluetooth low energy
Dependencies: BLE_API mbed nRF51822
Diff: main.cpp
- Revision:
- 3:933dd59ad44d
- Parent:
- 2:d02255d8c36f
diff -r d02255d8c36f -r 933dd59ad44d main.cpp --- a/main.cpp Mon Aug 14 20:52:48 2017 +0000 +++ b/main.cpp Fri Aug 18 18:20:41 2017 +0000 @@ -4,10 +4,8 @@ #include "LP8.h" //Sensor and ble Configuration parameters -#define SENSOR_TIMER 20.0 //lp8 polling interval (seconds) +#define SENSOR_TIMER 16.0 //lp8 polling interval (seconds) #define BLE_ADV_INTERVAL 500 //advertisment interval (milliseconds) -#define BACKGROUND_CALIBRATION_TIME 180 //timer to do background calibration (seconds) - //setup ble stack @@ -21,42 +19,58 @@ // Timers Timer lp8Wait; //timer for sensor communication -Timer bCalibration; //timer for background calibration - +Ticker lp8Timer; //timer object for sensor polling //BLE device name and uuid list setup const static char DEVICE_NAME[] = "SenseAir LP8"; static const uint16_t uuid16_list[] = {LP8_Service::LP8_SERVICE_UUID}; - -//check for sensor triggering and uppdating the GattServer +//check for sensor triggering bool triggerSensor = false; //sensor polling flag bool doCalibration = false; //background calibration flag +bool initCheck = true; //check for init sensor state -LP8_Service *lp8ServicePtr; //pointer to lp8 BLE service object +LP8_Service *lp8ServicePtr; // + uint8_t ccByte = 0x20; //calculation control byte to LP8 -//**************************** ble functions ******************************* -// on Disconnect, Restart BroadCasting + +//**************************** ble callback functions *************************** +// on Disconnect interupt void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { + initCheck = true; //turn of sensor at ble disconnect VBB_EN.write( 0 ); + //cancel sensor timer + lp8Timer.detach(); + //restart broadcast ble.gap().startAdvertising(); } -//sensor polling interupt +//sensor polling void triggerSensorPollingInterupt() { triggerSensor = true; + + //reset callback timer + lp8Timer.detach(); + lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER); + }; -//app interupt for calibration... + +void connectionCallback(const Gap::ConnectionCallbackParams_t *params) +{ + lp8Timer.attach( triggerSensorPollingInterupt, 4 ); +} + +//on BLE data written void calibrationCallback(const GattWriteCallbackParams *eventData) { + doCalibration = true; ccByte = eventData-> data[0]; - doCalibration = true; } @@ -66,41 +80,30 @@ { wait(1); - - Ticker lp8Timer; //timer object for sensor polling interupts + ble.init(); - //timer callback - lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER); //trigger sensor reading every X sec - - ble.init(); - - //disconnect callback - ble.gap().onDisconnection( disconnectionCallback ); //do callback if ble disconnect - //on data recevied callback - ble.onDataWritten( calibrationCallback ); //trigger for calibration control + //callbacks + ble.gap().onConnection( connectionCallback ); + ble.gap().onDisconnection( disconnectionCallback ); // + ble.gattServer().onDataWritten( calibrationCallback ); // //************************ LP8 variables ********************************* - int co2Value = 400; //initial CO2 value - double tempValue = 20.0; //init temp value - int Vcap = 3000; //mV - uint32_t errorFlag = 0; //error bits from lp8 + int co2Value = 99; //initial CO2 value + float tempValue = 99; //init temp value + int Vcap = 99; //mV + uint32_t errorFlag = 0; //error bits from lp8 + uint8_t sentCCbyte = 0x99; -//************************ Checks ********************************* - bool initCheck = true; //check for init sensor state - bool successCheck = false; //check for sensor communication -//************************ Calculation control bytes ******************** -// uint8_t sM = 0x20; //lp8 calculation control byte using Subesquent Measurments mode -// uint8_t bC = 0x52; //Background Calibration using unfilterd data + reset filters +//setup LP8 object +LP8 *lp8 = new LP8(Device, VBB_EN, RDY, Res, lp8Wait); - -//setup LP8 object - LP8 lp8(Device, VBB_EN, RDY, Res, lp8Wait); //pass pins: serial, vbb, rdy, res, timer //Setup GattService - LP8_Service lp8Service(ble, co2Value, tempValue, - Vcap, errorFlag, ccByte); // - lp8ServicePtr = &lp8Service; // +// LP8_Service lp8Service(ble, co2Value, tempValue, + // Vcap, errorFlag, ccByte, sentCCbyte); + lp8ServicePtr = new LP8_Service(ble, co2Value, tempValue, + Vcap, errorFlag, ccByte, sentCCbyte); // lp8Service; // setup ble advertising parameters @@ -109,7 +112,7 @@ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(BLE_ADV_INTERVAL); /* advertising interval in ms. */ - ble.gap().startAdvertising(); //start broadcast + ble.gap().startAdvertising(); // Wait for initialization to complete. while (ble.hasInitialized() == false) { @@ -121,41 +124,40 @@ //*************************** start the main loop *********************************** while ( true ) { + if(triggerSensor && ble.gap().getState().connected ) //trigger when timer interupts and there is an established ble connection { if ( initCheck ) { - - successCheck = lp8.lp8Init(); //initialize talking with the lp8 (first call on startup) - - if ( successCheck ) { - initCheck = false; - } + lp8->responsePurge(50); //purge buffer + if ( lp8->lp8Init() != true ) { + //initCheck = true; + } + else { + initCheck = false; + + } + } + + else if ( lp8->lp8Talk( ccByte ) != true ) { + //restart with init sequence if sensor state is lost or communication fails + initCheck = true; + ccByte = 0xff; //app error flag } -/* - else if ( doCalibration && ccByte == bC ){ - lp8.lp8Talk( bC ); //do background calibration - bCalibration.start(); - if( bCalibration.read() >= BACKGROUND_CALIBRATION_TIME ){ - bCalibration.stop(); - bCalibration.reset(); - doCalibration = false; - ccByte = 0xff; - } - } -*/ - else { - lp8.lp8Talk( ccByte ); //Communication with lp8, pass Calculation control byte - } - + //reset polling check triggerSensor = false; - //update the gattServer - lp8ServicePtr->updateCo2Value( lp8.getValue() ); //get CO2 value - lp8ServicePtr->updateTempValue( lp8.getTempValue() ); // - lp8ServicePtr->updateVcapValue( lp8.getVcapValue() ); // - lp8ServicePtr->updateError( lp8.getErrorStatus() ); // - lp8ServicePtr->updateDataWritten( lp8.getCCbyte() ); //send back the calculation control byte that was used in LP8 measurement + //update the gattServer characteristic values + lp8ServicePtr->updateCo2Value( lp8->getValue() ); //get CO2 value + lp8ServicePtr->updateTempValue( lp8->getTempValue() ); // + lp8ServicePtr->updateVcapValue( lp8->getVcapValue() ); // + lp8ServicePtr->updateError( lp8->getErrorStatus() ); // + lp8ServicePtr->updateDataWritten( lp8->getCCbyte() ); //send back the calculation control byte that was used in LP8 measurement + lp8ServicePtr->updateReWrite( ccByte ); + + //reset calibration control + ccByte = 0x20; /* resets to 0x20 (subs. talk) after one sample. + Check lp8Talk() function. */ } else { ble.waitForEvent(); } //ble save energy