Reading SenseAir LP8 CO2 sensor over bluetooth low energy
Dependencies: BLE_API mbed nRF51822
Diff: main.cpp
- Revision:
- 1:b512a405b584
- Parent:
- 0:ee3787c8e209
- Child:
- 2:d02255d8c36f
diff -r ee3787c8e209 -r b512a405b584 main.cpp --- a/main.cpp Fri Apr 21 13:26:06 2017 +0000 +++ b/main.cpp Mon Jun 05 11:10:28 2017 +0000 @@ -7,18 +7,16 @@ BLE ble; //BLE object // Pins and timers needed for lp8 communication -DigitalIn RDY(P0_8); // -DigitalOut VBB(P0_5); -DigitalOut VBB_EN(P0_4); // +DigitalIn RDY(P0_5); // +DigitalOut VBB_EN(P0_29, 0); //set to low at startup Serial Device(P0_9, P0_11); //tx, rx -DigitalOut led1(P0_19, 1); //board led Timer lp8Wait; //timer for sensor communication //Sensor and ble Configuration parameters -#define SENSOR_TIMER 10.0 //lp8 polling interval (seconds) -#define BLE_ADV_INTERVAL 500 //advertisment interval (milliseconds) +#define SENSOR_TIMER 20.0 //lp8 polling interval (seconds) +#define BLE_ADV_INTERVAL 1000 //advertisment interval (milliseconds) @@ -28,23 +26,21 @@ //check for sensor triggering and uppdating Gatt Server -bool triggerSensor = false; //check for sensor polling +bool triggerSensor = false; //check for sensor polling, used with interupt LP8_Service *lp8ServicePtr; //pointer to lp8 service object //**************************** ble functions ******************************* -//**************************** ******************************* // on Disconnect, Restart BroadCasting void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { - //reset I/O pin, necessary? -// VBB_EN.write ( 1 ); - + //turn of sensor at ble disconnect + VBB_EN.write( 0 ); //restart broadcast ble.gap().startAdvertising(); } -//timer function callback function +//timer function callback void triggerSensorPollingInterupt() { triggerSensor = true; @@ -55,7 +51,7 @@ //main int main(void) { - led1 = 1; //turn off led + wait(1); Ticker lp8Timer; //timer object for sensor polling interupts lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER); //trigger sensor reading every X.Y sec @@ -64,12 +60,17 @@ ble.gap().onDisconnection(disconnectionCallback); //do callback if disconnection occurs - int co2Value = 400; //initial CO2 value - bool initCheck = true; //check for init sensor state or lost state - bool successCheck = false; + int co2Value = 400; //initial CO2 value to display + bool initCheck = true; //check for init sensor state (or lost state) + bool successCheck = false; //check for sensor communication + //calibration control bytes for the lp 8 + uint8_t subsequentMeasurement = 0x20; //lp8 calculation control byte for subs. measurements + uint8_t noResetABC = 0x70; //abc calibration byte with no reset on filters + uint8_t resetABC = 0x72; //abc calibration AND resets filters + uint8_t backgroundCalibration = 0x52; //background calibration using unfilterd data + resets filters -//setup LP8 peripheral communication - LP8 lp8(Device, VBB, VBB_EN, RDY, lp8Wait); +//setup LP8 object + LP8 lp8(Device, VBB_EN, RDY, lp8Wait); //constructor needs 4 arguments //setup for GattService @@ -78,45 +79,42 @@ lp8ServicePtr = &lp8Service; //set pointer to "real" lp8 service object -/* setup advertising parameters */ +/* 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 + ble.gap().setAdvertisingInterval(BLE_ADV_INTERVAL); /* advertising interval in ms. */ + ble.gap().startAdvertising(); //start broadcast - /* SpinWait for initialization to complete. This is necessary because the - * BLE object is used in the main loop below. */ + /* SpinWait for initialization to complete. */ while (ble.hasInitialized() == false) { /* spin loop */ } -//start the loop +//start the main loop while ( true ) { - if(triggerSensor && ble.gap().getState().connected ) //trigger when timer interupts ticks and there is an established connection + if(triggerSensor && ble.gap().getState().connected ) //trigger when timer interupts and there is an established ble connection { if ( initCheck ) { - successCheck = lp8.lp8Init(); + successCheck = lp8.lp8Init(); //initialize talking with the lp8 (first call on startup) -// if ( successCheck ) { + if ( successCheck ) { initCheck = false; -// led1 = 0; //turn on led -// } + } } else { - lp8.lp8Talk(); - led1 = 0; //on when talking + lp8.lp8Talk( subsequentMeasurement ); //Communication with the lp8, sends calculation control: 0x20 == subs. talks } + //reset polling check + triggerSensor = false; - //reset sensor status - triggerSensor = false; - //update gattServer with new CO2 value + //update the gattServer with new CO2 value lp8ServicePtr->updateCo2Value( lp8.getValue() ); } - else { ble.waitForEvent(); } //ble save energy + else { ble.waitForEvent(); } //ble save energy } };