etienne maillet / Mbed 2 deprecated MERGE

Dependencies:   IESE5_merge mbed CCS811 BME680

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BME680.h"
00003 #include "CCS811.h"
00004 #include "Adafruit_SGP30.h"
00005 
00006 BME680 myBME680     (A4, A5, 400000 );
00007 Adafruit_SGP30 SGP30(D14, D15);
00008 CCS811 ccs811(D14, D15);
00009 
00010 uint16_t eco2, tvoc;
00011 
00012 Serial pc           ( USBTX, USBRX );
00013 
00014 DigitalOut  myled       ( LED1 );
00015 Ticker      newReading;
00016 
00017 uint32_t    myState = 0;
00018 
00019 
00020 //@brief   FUNCTION PROTOTYPES
00021 void    changeDATA     ( void );
00022 void    user_delay_ms  ( uint32_t  period );
00023 int8_t  user_i2c_read  ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len );
00024 int8_t  user_i2c_write ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len );
00025 
00026 
00027 //@brief FUNCTION FOR APPLICATION MAIN ENTRY.
00028 int main()
00029 {
00030     pc.baud ( 9600 );
00031 
00032     myled   =   1;
00033     wait(3);
00034     myled   =   0;
00035     ccs811.init();
00036     SGP30.begin();
00037     SGP30.IAQinit();
00038 
00039 
00040     struct bme680_dev gas_sensor;
00041 
00042     gas_sensor.dev_id   =   BME680_I2C_ADDR_PRIMARY;
00043     gas_sensor.intf     =   BME680_I2C_INTF;
00044     gas_sensor.read     =   user_i2c_read;
00045     gas_sensor.write    =   user_i2c_write;
00046     gas_sensor.delay_ms =   user_delay_ms;
00047     // amb_temp can be set to 25 prior to configuring the gas sensor
00048     // or by performing a few temperature readings without operating the gas sensor.
00049     gas_sensor.amb_temp =   25;
00050 
00051 
00052     int8_t rslt = BME680_OK;
00053     rslt = myBME680.bme680_init ( &gas_sensor );
00054 
00055 
00056     uint8_t set_required_settings;
00057 
00058     // Set the temperature, pressure and humidity settings
00059     gas_sensor.tph_sett.os_hum  = BME680_OS_2X;
00060     gas_sensor.tph_sett.os_pres = BME680_OS_4X;
00061     gas_sensor.tph_sett.os_temp = BME680_OS_8X;
00062     gas_sensor.tph_sett.filter  = BME680_FILTER_SIZE_3;
00063 
00064     // Set the remaining gas sensor settings and link the heating profile
00065     gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
00066     // Create a ramp heat waveform in 3 steps
00067     gas_sensor.gas_sett.heatr_temp  = 320; // degree Celsius
00068     gas_sensor.gas_sett.heatr_dur   = 150; // milliseconds
00069 
00070     // Select the power mode
00071     // Must be set before writing the sensor configuration
00072     gas_sensor.power_mode = BME680_FORCED_MODE;
00073 
00074     // Set the required sensor settings needed
00075     set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL | BME680_GAS_SENSOR_SEL;
00076 
00077     // Set the desired sensor configuration
00078     rslt = myBME680.bme680_set_sensor_settings ( set_required_settings, &gas_sensor );
00079 
00080     // Set the power mode
00081     rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor );
00082 
00083 
00084     // Get the total measurement duration so as to sleep or wait till the measurement is complete
00085     uint16_t meas_period;
00086     myBME680.bme680_get_profile_dur ( &meas_period, &gas_sensor );
00087 
00088     struct bme680_field_data data;
00089 
00090 
00091     newReading.attach( &changeDATA, 1 );                                        // the address of the function to be attached ( changeDATA ) and the interval ( 1s )
00092 
00093     // Let the callbacks take care of everything
00094     while(1) {
00095         sleep();
00096 
00097         myled = 1;
00098 
00099         if ( myState == 1 ) {
00100             // Delay till the measurement is ready
00101             user_delay_ms ( meas_period );
00102             SGP30.IAQmeasure();
00103             //printf("SGP30 %d,%d\r\n",SGP30.eCO2,SGP30.TVOC);
00104             ccs811.readData(&eco2, &tvoc);
00105             //printf("eCO2 reading :%dppm, TVOC reading :%dppb\r\n", eco2, tvoc);
00106             rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor );
00107 
00108             // Prepare the data to be sent through the UART. NOTE: sprintf does NOT allow float numbers, that is why we round the number and plot them as integer
00109             // Avoid using measurements from an unstable heating setup
00110             if ( data.status & BME680_GASM_VALID_MSK ) {
00111                 //pc.printf( "T: %.2f degC, P: %.2f hPa, H %.2f %%rH, G: %d ohms\r\n", ( data.temperature/100.0f ), ( data.pressure / 100.0f ), ( data.humidity / 1000.0f ), data.gas_resistance );
00112                 //BME680 : Degres, Pression, Humidité, Voc ; SGO30 : eCO2, TVOC ; CCS811 : eco2, tvoc
00113                 pc.printf( "%.2f;%.2f;%.2f;%d;%d;%d;%d;%d\r\n", ( data.temperature/100.0f ), ( data.pressure / 100.0f ), ( data.humidity / 1000.0f ), data.gas_resistance,SGP30.eCO2,SGP30.TVOC, eco2, tvoc);
00114                 
00115             } else {
00116                 pc.printf( "T: %.2f degC, P: %.2f hPa, H %.2f %%rH\r\n", ( data.temperature/100.0f ), ( data.pressure / 100.0f ), ( data.humidity / 1000.0f ) );
00117             }
00118 
00119 
00120             // Trigger the next measurement if you would like to read data out continuously
00121             if ( gas_sensor.power_mode == BME680_FORCED_MODE ) {
00122                 rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor );
00123             }
00124 
00125             myState  =   0;                                                             // Reset the variable
00126         }
00127 
00128         myled = 0;
00129     }
00130 }
00131 
00132 
00133 
00134 
00135  // @brief       changeDATA ( void  )
00136  //
00137  // @details     It changes myState variable
00138  //
00139  // @param[in]    N/A
00140  //
00141  // @param[out]   N/A.
00142  //
00143  //
00144  // @return       N/A..
00145  //
00146  //
00147  // @author      Manuel Caballero
00148  // @date        21/July/2018
00149  // @version     21/July/2018   The ORIGIN
00150  // @pre         N/A
00151  // @warning     N/A.
00152 void changeDATA ( void )
00153 {
00154     myState = 1;
00155 }
00156 
00157 
00158 
00159  // @brief       user_delay_ms ( uint32_t  )
00160  //
00161  // @details     Return control or wait, for a period amount of milliseconds
00162  //
00163  // @param[in]    period:       Delay in milliseconds.
00164  //
00165  // @param[out]   N/A.
00166  //
00167  //
00168  // @return       N/A..
00169  //
00170  //
00171  // @author      Manuel Caballero
00172  // @date        21/July/2018
00173  // @version     21/July/2018   The ORIGIN
00174  // @pre         This is a Bosh pointer function adapted to our system.
00175  // @warning     N/A.
00176 void user_delay_ms ( uint32_t period )
00177 {
00178      // Return control or wait,
00179      // for a period amount of milliseconds
00180 
00181     wait_ms ( period );
00182 }
00183 
00184 
00185 
00186  // @brief       user_i2c_read ( uint8_t , uint8_t reg_addr, uint8_t *reg_data, uint16_t len )
00187  //
00188  // @details     It adapts I2C reading functionality.
00189  //
00190  // @param[in]    dev_id:    I2C address.
00191  // @param[in]    reg_addr:  Register to be read.
00192  // @param[in]    len:       How many bytes to read.
00193  //
00194  // @param[out]   reg_data:  Result.
00195  //
00196  //
00197  // @return       Status of user_i2c_read.
00198  //
00199  //
00200  // @author      Manuel Caballero
00201  // @date        21/July/2018
00202  // @version     21/July/2018   The ORIGIN
00203  // @pre         This is a Bosh pointer function adapted to our system.
00204  // @warning     N/A.
00205 int8_t user_i2c_read ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len )
00206 {
00207     int8_t rslt = 0; // Return 0 for Success, non-zero for failure
00208 
00209     // The parameter dev_id can be used as a variable to store the I2C address of the device
00210 
00211 
00212      // Data on the bus should be like
00213      // |------------+---------------------|
00214      // | I2C action | Data                |
00215      // |------------+---------------------|
00216      // | Start      | -                   |
00217      // | Write      | (reg_addr)          |
00218      // | Stop       | -                   |
00219      // | Start      | -                   |
00220      // | Read       | (reg_data[0])       |
00221      // | Read       | (....)              |
00222      // | Read       | (reg_data[len - 1]) |
00223      // | Stop       | -                   |
00224      // |------------+---------------------|
00225 
00226     // Read data
00227     uint32_t aux     =   0;
00228     aux      =   myBME680._i2c.write ( dev_id, (char*)&reg_addr, 1, true );
00229     aux      =   myBME680._i2c.read  ( dev_id, (char*)&reg_data[0], len );
00230 
00231 
00232 
00233     if ( aux == 0 ) {
00234         rslt     =   0;
00235     } else {
00236         rslt     =   0xFF;
00237     }
00238 
00239 
00240     return rslt;
00241 }
00242 
00243 
00244 
00245 
00246  // @brief       user_i2c_write ( uint8_t , uint8_t reg_addr, uint8_t *reg_data, uint16_t len )
00247  //
00248  // @details     It adapts I2C writing functionality.
00249  //
00250  // @param[in]    dev_id:    I2C address.
00251  // @param[in]    reg_addr:  Register to be read.
00252  // @param[out]   reg_data:  Data to be written.
00253  // @param[in]    len:       How many bytes to read.
00254  //
00255  // @param[out]   N/A.
00256  //
00257  //
00258  // @return       Status of user_i2c_write.
00259  //
00260  //
00261  // @author      Manuel Caballero
00262  // @date        21/July/2018
00263  // @version     21/July/2018   The ORIGIN
00264  // @pre         This is a Bosh pointer function adapted to our system.
00265  // @warning     N/A.
00266 int8_t user_i2c_write ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len )
00267 {
00268     int8_t rslt = 0; // Return 0 for Success, non-zero for failure
00269 
00270     // The parameter dev_id can be used as a variable to store the I2C address of the device
00271 
00272 
00273     // Data on the bus should be like
00274     // |------------+---------------------|
00275     // | I2C action | Data                |
00276     // |------------+---------------------|
00277     // | Start      | -                   |
00278     // | Write      | (reg_addr)          |
00279     // | Write      | (reg_data[0])       |
00280     // | Write      | (....)              |
00281     // | Write      | (reg_data[len - 1]) |
00282     // | Stop       | -                   |
00283     // |------------+---------------------|
00284 
00285     uint32_t     aux     =   0;
00286     char         cmd[16] =  { 0 };
00287     uint32_t     i       =   0;
00288 
00289     // Prepare the data to be sent
00290     cmd[0]   =   reg_addr;
00291     for ( i = 1; i <= len; i++ ) {
00292         cmd[i]   =   reg_data[i - 1];
00293     }
00294 
00295     // Write data
00296     aux      =   myBME680._i2c.write ( dev_id, &cmd[0], len + 1, false );
00297 
00298 
00299 
00300     if ( aux == 0 ) {
00301         rslt     =   0;
00302     } else {
00303         rslt     =   0xFF;
00304     }
00305 
00306 
00307     return rslt;
00308 }