Reading SenseAir LP8 CO2 sensor over bluetooth low energy

Dependencies:   BLE_API mbed nRF51822

main.cpp

Committer:
jony1401
Date:
2017-06-05
Revision:
1:b512a405b584
Parent:
0:ee3787c8e209
Child:
2:d02255d8c36f

File content as of revision 1:b512a405b584:

#include "mbed.h"
#include "BLE.h"
#include "LP8_Service.h"
#include "LP8.h"

//setup ble stack
BLE             ble;                                                                    //BLE object

// Pins and timers needed for lp8 communication
DigitalIn       RDY(P0_5);                                                              //
DigitalOut      VBB_EN(P0_29, 0);                                                       //set to low at startup
Serial          Device(P0_9, P0_11);                                                    //tx, rx
Timer           lp8Wait;                                                                //timer for sensor communication



//Sensor and ble Configuration parameters
#define     SENSOR_TIMER                        20.0                                    //lp8 polling interval (seconds)
#define     BLE_ADV_INTERVAL                    1000                                    //advertisment interval (milliseconds)



//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 Gatt Server
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)
{
    //turn of sensor at ble disconnect
    VBB_EN.write( 0 );
    //restart broadcast
    ble.gap().startAdvertising();
}
//timer function callback
void triggerSensorPollingInterupt()
{
    triggerSensor = true;
};



//main 
int main(void)
{
    wait(1); 
    
    Ticker lp8Timer;                                                                    //timer object for sensor polling interupts
    lp8Timer.attach(&triggerSensorPollingInterupt, SENSOR_TIMER);                       //trigger sensor reading every X.Y sec
    
    ble.init();
    ble.gap().onDisconnection(disconnectionCallback);                                   //do callback if disconnection occurs


    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 object 
    LP8               lp8(Device, VBB_EN, RDY, lp8Wait);                                //constructor needs 4 arguments
     
     
//setup for GattService
/* Setup Gatt server */
    LP8_Service      lp8Service(ble, co2Value);                                         //pass in ble object and initial co2 value
    lp8ServicePtr     = &lp8Service;                                                    //set pointer to "real" lp8 service object


/* 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
    
        /* SpinWait 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 {
                lp8.lp8Talk( subsequentMeasurement );                                   //Communication with the lp8, sends calculation control: 0x20 == subs. talks
            }
            //reset polling check
            triggerSensor = false;
            
            //update the gattServer with new CO2 value
            lp8ServicePtr->updateCo2Value( lp8.getValue() );
        }
        
        else { ble.waitForEvent(); }                                                    //ble save energy
    }
    
};