|
| 1 | + /****************************** |
| 2 | + * Copyright (C) 2017 by Tobias Wartzek |
| 3 | + * @file bme680_main.c |
| 4 | + * @date 07.10.2017 |
| 5 | + * @version 1.0 |
| 6 | + * @brief Interface to BME680 from Raspberry Pi |
| 7 | + * |
| 8 | + * |
| 9 | + * Read out temperature, pressure, humidity and gas sensor ohmic values |
| 10 | + * via I²C and Raspberry Pi. |
| 11 | + * |
| 12 | + * History |
| 13 | + * Version Date Detail |
| 14 | + * 1.0 07.10.2017 Initial creation |
| 15 | + * |
| 16 | + ******************************/ |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <time.h> |
| 23 | +#include <linux/i2c-dev.h> |
| 24 | +#include <fcntl.h> |
| 25 | +#include <string.h> |
| 26 | +#include <sys/ioctl.h> |
| 27 | +#include <sys/types.h> |
| 28 | +#include <sys/stat.h> |
| 29 | +#include <unistd.h> |
| 30 | +#include "bme680.h" |
| 31 | + |
| 32 | +#define DESTZONE "TZ=Europe/Stockholm" // Our destination time zone |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +// I2C Linux device handle |
| 37 | +int g_i2cFid; |
| 38 | + |
| 39 | +// open the Linux device |
| 40 | +void i2cOpen() |
| 41 | +{ |
| 42 | + g_i2cFid = open("/dev/i2c-1", O_RDWR); |
| 43 | + if (g_i2cFid < 0) { |
| 44 | + perror("i2cOpen"); |
| 45 | + exit(1); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// close the Linux device |
| 50 | +void i2cClose() |
| 51 | +{ |
| 52 | + close(g_i2cFid); |
| 53 | +} |
| 54 | + |
| 55 | +// set the I2C slave address for all subsequent I2C device transfers |
| 56 | +void i2cSetAddress(int address) |
| 57 | +{ |
| 58 | + if (ioctl(g_i2cFid, I2C_SLAVE, address) < 0) { |
| 59 | + perror("i2cSetAddress"); |
| 60 | + exit(1); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +void user_delay_ms(uint32_t period) |
| 67 | +{ |
| 68 | + |
| 69 | + sleep(period/1000); |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) |
| 76 | +{ |
| 77 | + int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ |
| 78 | + |
| 79 | + uint8_t reg[1]; |
| 80 | + reg[0]=reg_addr; |
| 81 | + |
| 82 | + if (write(g_i2cFid, reg, 1) != 1) { |
| 83 | + perror("user_i2c_read_reg"); |
| 84 | + rslt = 1; |
| 85 | + } |
| 86 | + if (read(g_i2cFid, reg_data, len) != len) { |
| 87 | + perror("user_i2c_read_data"); |
| 88 | + rslt = 1; |
| 89 | + } |
| 90 | + |
| 91 | + return rslt; |
| 92 | +} |
| 93 | + |
| 94 | +int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) |
| 95 | +{ |
| 96 | + int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ |
| 97 | + |
| 98 | + |
| 99 | + uint8_t reg[16]; |
| 100 | + reg[0]=reg_addr; |
| 101 | + |
| 102 | + for (int i=1; i<len+1; i++) |
| 103 | + reg[i] = reg_data[i-1]; |
| 104 | + |
| 105 | + if (write(g_i2cFid, reg, len+1) != len+1) { |
| 106 | + perror("user_i2c_write"); |
| 107 | + rslt = 1; |
| 108 | + exit(1); |
| 109 | + } |
| 110 | + |
| 111 | + return rslt; |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +void write2file(char *outputFile, struct tm tm, struct bme680_field_data data) |
| 116 | +{ |
| 117 | + // Write measurement to output file if specified. |
| 118 | + if(outputFile != NULL) |
| 119 | + { |
| 120 | + FILE *f = fopen(outputFile, "a"); |
| 121 | + if (f == NULL) |
| 122 | + { |
| 123 | + printf("Error opening file!\n"); |
| 124 | + //exit(1); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + fprintf(f,"%d-%02d-%02d %02d:%02d:%02d ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 129 | + fprintf(f,"T: %.2f degC, P: %.2f hPa, H: %.2f %%rH", data.temperature / 100.0f, |
| 130 | + data.pressure / 100.0f, data.humidity / 1000.0f ); |
| 131 | + fprintf(f,", G: %d Ohms", data.gas_resistance); |
| 132 | + fprintf(f,"\r\n"); |
| 133 | + fclose(f); |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +int main(int argc, char *argv[] ) |
| 140 | +{ |
| 141 | + // create lock file first |
| 142 | + FILE *f = fopen("~bme680i2c.lock", "w"); |
| 143 | + if (f == NULL) |
| 144 | + { |
| 145 | + printf("Error opening file!\n"); |
| 146 | + exit(1); |
| 147 | + } |
| 148 | + fprintf(f,"I2C locked by BME680 readout. \r\n"); |
| 149 | + fclose(f); |
| 150 | + |
| 151 | + |
| 152 | + int delay = 3; |
| 153 | + int nMeas = 3; |
| 154 | + char *outputFile = NULL; |
| 155 | + |
| 156 | + // Input argument parser |
| 157 | + if( argc == 2 ) { |
| 158 | + delay = strtol(argv[1], NULL, 10); |
| 159 | + } |
| 160 | + else if( argc == 3 ) { |
| 161 | + delay = strtol(argv[1], NULL, 10); |
| 162 | + nMeas = strtol(argv[2], NULL, 10); |
| 163 | + } |
| 164 | + else if( argc == 4 ) { |
| 165 | + delay = strtol(argv[1], NULL, 10); |
| 166 | + nMeas = strtol(argv[2], NULL, 10); |
| 167 | + outputFile = argv[3]; |
| 168 | + } |
| 169 | + else { |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + printf("**** BME680 start measurements ****\n"); |
| 175 | + |
| 176 | + time_t t = time(NULL); |
| 177 | + putenv(DESTZONE); // Switch to destination time zone |
| 178 | + |
| 179 | + |
| 180 | + // open Linux I2C device |
| 181 | + i2cOpen(); |
| 182 | + |
| 183 | + // set address of the BME680 |
| 184 | + i2cSetAddress(BME680_I2C_ADDR_SECONDARY); |
| 185 | + |
| 186 | + // init device |
| 187 | + struct bme680_dev gas_sensor; |
| 188 | + |
| 189 | + gas_sensor.dev_id = BME680_I2C_ADDR_SECONDARY; |
| 190 | + gas_sensor.intf = BME680_I2C_INTF; |
| 191 | + gas_sensor.read = user_i2c_read; |
| 192 | + gas_sensor.write = user_i2c_write; |
| 193 | + gas_sensor.delay_ms = user_delay_ms; |
| 194 | + |
| 195 | + int8_t rslt = BME680_OK; |
| 196 | + rslt = bme680_init(&gas_sensor); |
| 197 | + |
| 198 | + uint8_t set_required_settings; |
| 199 | + |
| 200 | + /* Set the temperature, pressure and humidity settings */ |
| 201 | + gas_sensor.tph_sett.os_hum = BME680_OS_2X; |
| 202 | + gas_sensor.tph_sett.os_pres = BME680_OS_4X; |
| 203 | + gas_sensor.tph_sett.os_temp = BME680_OS_8X; |
| 204 | + gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3; |
| 205 | + |
| 206 | + /* Set the remaining gas sensor settings and link the heating profile */ |
| 207 | + gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; |
| 208 | + /* Create a ramp heat waveform in 3 steps */ |
| 209 | + gas_sensor.gas_sett.heatr_temp = 320; /* degree Celsius */ |
| 210 | + gas_sensor.gas_sett.heatr_dur = 150; /* milliseconds */ |
| 211 | + |
| 212 | + /* Select the power mode */ |
| 213 | + /* Must be set before writing the sensor configuration */ |
| 214 | + gas_sensor.power_mode = BME680_FORCED_MODE; |
| 215 | + |
| 216 | + /* Set the required sensor settings needed */ |
| 217 | + set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL |
| 218 | + | BME680_GAS_SENSOR_SEL; |
| 219 | + |
| 220 | + /* Set the desired sensor configuration */ |
| 221 | + rslt = bme680_set_sensor_settings(set_required_settings,&gas_sensor); |
| 222 | + |
| 223 | + /* Set the power mode */ |
| 224 | + rslt = bme680_set_sensor_mode(&gas_sensor); |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | + /* Get the total measurement duration so as to sleep or wait till the |
| 229 | + * measurement is complete */ |
| 230 | + uint16_t meas_period; |
| 231 | + bme680_get_profile_dur(&meas_period, &gas_sensor); |
| 232 | + user_delay_ms(meas_period + delay*1000); /* Delay till the measurement is ready */ |
| 233 | + |
| 234 | + |
| 235 | + struct bme680_field_data data; |
| 236 | + |
| 237 | + struct tm tm = *localtime(&t); |
| 238 | + |
| 239 | + int i=0; |
| 240 | + int backupCounter = 0; |
| 241 | + |
| 242 | + while(i<nMeas && backupCounter < nMeas+5) { |
| 243 | + |
| 244 | + // Get sensor data |
| 245 | + rslt = bme680_get_sensor_data(&data, &gas_sensor); |
| 246 | + |
| 247 | + // Avoid using measurements from an unstable heating setup |
| 248 | + if(data.status & BME680_HEAT_STAB_MSK) |
| 249 | + { |
| 250 | + t = time(NULL); |
| 251 | + tm = *localtime(&t); |
| 252 | + printf("%d-%02d-%02d %02d:%02d:%02d ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 253 | + printf("T: %.2f degC, P: %.2f hPa, H: %.2f %%rH", data.temperature / 100.0f, |
| 254 | + data.pressure / 100.0f, data.humidity / 1000.0f ); |
| 255 | + printf(", G: %d Ohms", data.gas_resistance); |
| 256 | + printf("\r\n"); |
| 257 | + write2file(outputFile, tm, data); |
| 258 | + i++; |
| 259 | + } |
| 260 | + |
| 261 | + // Trigger a meausurement |
| 262 | + rslt = bme680_set_sensor_mode(&gas_sensor); /* Trigger a measurement */ |
| 263 | + |
| 264 | + // Wait for a measurement to complete |
| 265 | + user_delay_ms(meas_period + delay*1000); /* Wait for the measurement to complete */ |
| 266 | + |
| 267 | + backupCounter++; |
| 268 | + } |
| 269 | + |
| 270 | + |
| 271 | + printf("**** Measurement finished ****\n"); |
| 272 | + |
| 273 | + |
| 274 | + |
| 275 | + // close Linux I2C device |
| 276 | + i2cClose(); |
| 277 | + |
| 278 | + // delete lock file |
| 279 | + remove("~bme680i2c.lock"); |
| 280 | + |
| 281 | + return 0; |
| 282 | +} |
| 283 | + |
0 commit comments