Reading SenseAir LP8 CO2 sensor over bluetooth low energy
Dependencies: BLE_API mbed nRF51822
main.cpp
- Committer:
- jony1401
- Date:
- 2017-08-14
- Revision:
- 2:d02255d8c36f
- Parent:
- 1:b512a405b584
- Child:
- 3:933dd59ad44d
File content as of revision 2:d02255d8c36f:
#include "mbed.h" #include "BLE.h" #include "LP8_Service.h" #include "LP8.h" //Sensor and ble Configuration parameters #define SENSOR_TIMER 20.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 BLE ble; //BLE object // setup Pins DigitalOut Res(P0_0, 1); //reset, pull Low for reset DigitalIn RDY(P0_10); //rdy DigitalOut VBB_EN(P0_5, 0); //set to low at startup Serial Device(P0_9, P0_11); //tx, rx // Timers Timer lp8Wait; //timer for sensor communication Timer bCalibration; //timer for background calibration //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 bool triggerSensor = false; //sensor polling flag bool doCalibration = false; //background calibration flag LP8_Service *lp8ServicePtr; //pointer to lp8 BLE service object uint8_t ccByte = 0x20; //calculation control byte to LP8 //**************************** ble functions ******************************* // on Disconnect, Restart BroadCasting void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { //turn of sensor at ble disconnect VBB_EN.write( 0 ); //restart broadcast ble.gap().startAdvertising(); } //sensor polling interupt void triggerSensorPollingInterupt() { triggerSensor = true; }; //app interupt for calibration... void calibrationCallback(const GattWriteCallbackParams *eventData) { ccByte = eventData-> data[0]; doCalibration = true; } //**************************** main ************************************** int main() { wait(1); Ticker lp8Timer; //timer object for sensor polling interupts //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 //************************ 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 //************************ 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(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; // // setup ble advertising parameters ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); //general bluetooth information(only support for ble ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); //service list 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 // Wait for initialization to complete. while (ble.hasInitialized() == false) { /* spin loop */ } //*************************** 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; } } /* 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 } else { ble.waitForEvent(); } //ble save energy } };