Reading SenseAir LP8 CO2 sensor over bluetooth low energy

Dependencies:   BLE_API mbed nRF51822

Revision:
2:d02255d8c36f
Parent:
1:b512a405b584
Child:
3:933dd59ad44d
diff -r b512a405b584 -r d02255d8c36f LP8.h
--- a/LP8.h	Mon Jun 05 11:10:28 2017 +0000
+++ b/LP8.h	Mon Aug 14 20:52:48 2017 +0000
@@ -1,17 +1,18 @@
 #ifndef LP8_H
 #define LP8_H
 
-/* To initialize the lp8 object, you need to pass a serial (tx, rx), an DigitalOut signal for the en_vbb,
-an DigitalIn for the ready signal and a timer object. */
+/* To initialize the lp8 object, you need to pass a serial (tx, rx), a DigitalOut signal for the en_vbb,
+an DigitalIn for the ready signal, DigitalOut for Reset and a timer object. */
 
 class LP8 
 {
 public:
 //constructor
-    LP8(Serial &device, DigitalOut &vbb_en, DigitalIn &rdy, Timer &_timer):  
+    LP8(Serial &device, DigitalOut &vbb_en, DigitalIn &rdy, DigitalOut &res ,Timer &_timer):  
      Device(device),
      VBB_EN(vbb_en),
      RDY(rdy),
+     RES(res),
      lp8Wait(_timer)
     {
         Device.baud(9600);                                                              //set baud rate to 9600 
@@ -52,15 +53,22 @@
         co2     = 400;                                                                  //co2 value
         counter = 0;                                                                    //
         CRC     = 0x0000;                                                               //crc value
-        _timeMe = 0.350;                                                                //wait timer(ms)
+
     };
 
     
-//LP8 Initialization                  .
+//LP8 Initialization and first message                  
     bool lp8Init(){
+
+//            //Reset LP8
+//            RES.write( 0 );                                                                 //reset
+//            timeIt( 1.2 );
+//            RES.write( 1 );                                                                 //enable
+            
+            //Enable Sensor      
             VBB_EN.write( 1 );                                                              //power on
             //wait for rdy signal
-            timeIt( _timeMe );                                                              //wait for lp8 rdy signal
+            timeIt( 0.35 );                                                                 //wait for lp8 rdy signal
             transmitPacket(firstWrite, 8);                                                  //Send out msg (and nr of bytes) over serial line
             Response( 4 );                                                                  //read 4 bytes response     
             //compute crc
@@ -69,10 +77,11 @@
             stateRead[5] = (uint8_t)CRC;                                                    //crc_l
             stateRead[6] = (uint8_t)(CRC >> 8);                                             //crc_h
             //wait for rdy
-            timeIt( _timeMe );                                                              //
+            timeIt( 0.35 );                                                                 //
             transmitPacket(stateRead, 7);                                                   //transmit packet  
             Response( 49 );                                                                 //read sensor state and co2 value(s)
             VBB_EN.write( 0 );                                                              //power off lp8                 
+
             //was the talk a success? (simple check...)
             if ( getValue() < 1 ) {
                 return 1; }
@@ -82,6 +91,7 @@
     
 //send subsequent messages to the lp8
     void lp8Talk(uint8_t ccByte){
+
             //transfer previous sensor state to the new msg out
             for (int u = 4; u < 23+4; u++) {
                 stateWrite[u+2] = response[u];
@@ -93,9 +103,10 @@
             //add new crc value to send list
             stateWrite[29] = (uint8_t)CRC;   
             stateWrite[30] = (uint8_t)(CRC >> 8);
+
             //initialize new transfer sequence    
             VBB_EN.write( 1 );                                                              //power on sensor
-            timeIt( _timeMe );
+            timeIt( 0.35 );
             transmitPacket(stateWrite, 31);                                                 //Send out msg with previous state (and nr of elements) over serial line
             Response( 4 );                                                                  //read 4 bytes response     
             //compute crc
@@ -103,7 +114,7 @@
             //add crc value to the read request transmit package
             stateRead[5] = (uint8_t)CRC;                                                    //crc_l
             stateRead[6] = (uint8_t)(CRC >> 8);                                             //crc_h
-            timeIt( _timeMe );
+            timeIt( 0.35 );
             //send read request
             transmitPacket(stateRead, 7);                                                   //transmit packet  
             //read sensor response
@@ -111,8 +122,8 @@
             VBB_EN.write( 0 );                                                              //power off        
        };
         
-//get co2 value from lp8 response    
-    unsigned long getValue()
+//get value from lp8 response    
+    unsigned long getValue()    /* CO2 Value */
     {
         int high    = response[29];
         int low     = response[30]; 
@@ -120,10 +131,47 @@
     
         return val;
     }
-
+    
+    double getTempValue()
+    {
+        int h    = response[33];
+        int l    = response[34]; 
+        unsigned long _temp = h * 256 + l; 
+        
+        double _tempVal = 0.01 * _temp;
+          
+        return _tempVal;
+    }
+    
+    int getVcapValue(){
+        
+        int hB    = response[35];
+        int lB    = response[36]; 
+        unsigned long temp = hB * 256 + lB; 
+        
+        return temp;
+    }
+    
+    uint32_t getErrorStatus(){
+        
+        uint32_t tmp  = 0;  
+        tmp += response[39] << (32-8);
+        tmp += response[40] << (32-16);
+        tmp += response[41] << (32-24);
+        tmp += response[42];
+        
+        return tmp;
+    }   
+    
+    //get calculation Control byte from lp8 response
+    uint8_t getCCbyte(){
+        uint8_t responseCCbyte = response[0];
+        return responseCCbyte;    
+    }
+    
 
 /************************************************* Helper Functions ********************************************/
-/*************************************************                  ********************************************/
+
 //purge response buffer
     void responsePurge(int bytesToPurge){
         for (int j = 0; j < bytesToPurge; j++) {
@@ -131,7 +179,7 @@
             }
         };
             
-//read from the lp8    
+//read response from lp8 (not energy efficient...)   
     void Response(int bytesToRead ){
         lp8Wait.start();                    //poll rx line for 0.5 seconds
         do {
@@ -140,7 +188,7 @@
                 counter++;
                 }
             }
-        while( lp8Wait.read() < 0.5 );
+        while( lp8Wait.read() < 0.2 );
         counter = 0;
         lp8Wait.stop();
         lp8Wait.reset();                                                        
@@ -157,7 +205,7 @@
         };
     
 //timer
-    void timeIt(float &timeMe){
+    void timeIt(float timeMe){
             //start amd stop timer...
             lp8Wait.start();
             while (lp8Wait.read() < timeMe ) { /* W A I T I N G */ }
@@ -187,10 +235,12 @@
     
 //variables and buffers
 private:
-
+    //pins
     Serial      &Device;
     DigitalOut  &VBB_EN;    
     DigitalIn   &RDY;
+    DigitalOut  &RES;
+
     Timer       &lp8Wait;
     
     //msg containers 
@@ -198,11 +248,12 @@
     uint8_t  stateWrite[31]; 
     uint8_t  stateRead[7];
     uint8_t  response[60];
-    
-    int         co2;                                                                       //CO2 initial value   
+
+    //    
+    int         co2;                                                                       //CO2 initial value 
+    int         tempValue;  
     int         counter;                                                                   //simple counter 
     uint16_t    CRC;                                                                       //modbus crc value
-    float       _timeMe;                                                                   //timer value
 
 
 };