embernet project fire detection system sensor test

Dependencies:   mbed CCS811 BME280

Committer:
cege1808
Date:
Fri Mar 08 22:55:26 2019 +0000
Revision:
1:91de8a17fbea
Parent:
0:24692a601d5e
update mbed and sgp sensor;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cege1808 0:24692a601d5e 1 /*!
cege1808 0:24692a601d5e 2 * @file Adafruit_SGP30.cpp
cege1808 0:24692a601d5e 3 *
cege1808 0:24692a601d5e 4 * @mainpage Adafruit SGP30 gas sensor driver
cege1808 0:24692a601d5e 5 *
cege1808 0:24692a601d5e 6 * @section intro_sec Introduction
cege1808 0:24692a601d5e 7 *
cege1808 0:24692a601d5e 8 * This is the documentation for Adafruit's SGP30 driver for the
cege1808 0:24692a601d5e 9 * Arduino platform. It is designed specifically to work with the
cege1808 0:24692a601d5e 10 * Adafruit SGP30 breakout: http://www.adafruit.com/products/3709
cege1808 0:24692a601d5e 11 *
cege1808 0:24692a601d5e 12 * These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
cege1808 0:24692a601d5e 13 * to interface with the breakout.
cege1808 0:24692a601d5e 14 *
cege1808 0:24692a601d5e 15 * Adafruit invests time and resources providing this open source code,
cege1808 0:24692a601d5e 16 * please support Adafruit and open-source hardware by purchasing
cege1808 0:24692a601d5e 17 * products from Adafruit!
cege1808 0:24692a601d5e 18 *
cege1808 0:24692a601d5e 19 *
cege1808 0:24692a601d5e 20 * @section author Author
cege1808 0:24692a601d5e 21 * Written by Ladyada for Adafruit Industries.
cege1808 0:24692a601d5e 22 *
cege1808 0:24692a601d5e 23 * @section license License
cege1808 0:24692a601d5e 24 * BSD license, all text here must be included in any redistribution.
cege1808 0:24692a601d5e 25 *
cege1808 0:24692a601d5e 26 */
cege1808 0:24692a601d5e 27
cege1808 0:24692a601d5e 28 #include "mbed.h"
cege1808 0:24692a601d5e 29 #include "Adafruit_SGP30.h"
cege1808 0:24692a601d5e 30
cege1808 0:24692a601d5e 31 //#define I2C_DEBUG 1
cege1808 0:24692a601d5e 32
cege1808 0:24692a601d5e 33 /**************************************************************************/
cege1808 0:24692a601d5e 34 /*!
cege1808 0:24692a601d5e 35 @brief Instantiates a new SGP30 class
cege1808 0:24692a601d5e 36 */
cege1808 0:24692a601d5e 37 /**************************************************************************/
cege1808 0:24692a601d5e 38 Adafruit_SGP30::Adafruit_SGP30(PinName sda, PinName scl) : _i2c(sda, scl) {
cege1808 0:24692a601d5e 39 }
cege1808 0:24692a601d5e 40
cege1808 0:24692a601d5e 41 /**************************************************************************/
cege1808 0:24692a601d5e 42 /*!
cege1808 0:24692a601d5e 43 @brief Setups the hardware and detects a valid SGP30. Initializes I2C
cege1808 0:24692a601d5e 44 then reads the serialnumber and checks that we are talking to an SGP30
cege1808 0:24692a601d5e 45 @param theWire Optional pointer to I2C interface, otherwise use Wire
cege1808 0:24692a601d5e 46 @returns True if SGP30 found on I2C, False if something went wrong!
cege1808 0:24692a601d5e 47 */
cege1808 0:24692a601d5e 48 /**************************************************************************/
cege1808 0:24692a601d5e 49 bool Adafruit_SGP30::begin() {
cege1808 0:24692a601d5e 50 //Mbed use 8bit addr
cege1808 0:24692a601d5e 51 _i2caddr = SGP30_I2CADDR_DEFAULT << 1;
cege1808 0:24692a601d5e 52
cege1808 0:24692a601d5e 53 uint8_t command[3];
cege1808 0:24692a601d5e 54 command[0] = 0x36;
cege1808 0:24692a601d5e 55 command[1] = 0x82;
cege1808 0:24692a601d5e 56 command[2] = 0x0;
cege1808 0:24692a601d5e 57 if (! readWordFromCommand(command, 2, 5, serialnumber, 3))
cege1808 0:24692a601d5e 58 return false;
cege1808 0:24692a601d5e 59
cege1808 0:24692a601d5e 60 uint16_t featureset;
cege1808 0:24692a601d5e 61 command[0] = 0x20;
cege1808 0:24692a601d5e 62 command[1] = 0x2F;
cege1808 0:24692a601d5e 63 if (! readWordFromCommand(command, 2, 10, &featureset, 1))
cege1808 0:24692a601d5e 64 return false;
cege1808 0:24692a601d5e 65 //Serial.print("Featureset 0x"); Serial.println(featureset, HEX);
cege1808 0:24692a601d5e 66 if (featureset != SGP30_FEATURESET)
cege1808 0:24692a601d5e 67 return false;
cege1808 0:24692a601d5e 68 if (! IAQinit())
cege1808 0:24692a601d5e 69 return false;
cege1808 0:24692a601d5e 70
cege1808 0:24692a601d5e 71 return true;
cege1808 0:24692a601d5e 72 }
cege1808 0:24692a601d5e 73
cege1808 0:24692a601d5e 74 /**************************************************************************/
cege1808 0:24692a601d5e 75 /*!
cege1808 0:24692a601d5e 76 @brief Commands the sensor to begin the IAQ algorithm. Must be called after startup.
cege1808 0:24692a601d5e 77 @returns True if command completed successfully, false if something went wrong!
cege1808 0:24692a601d5e 78 */
cege1808 0:24692a601d5e 79 /**************************************************************************/
cege1808 0:24692a601d5e 80 bool Adafruit_SGP30::IAQinit(void) {
cege1808 0:24692a601d5e 81 uint8_t command[2];
cege1808 0:24692a601d5e 82 command[0] = 0x20;
cege1808 0:24692a601d5e 83 command[1] = 0x03;
cege1808 0:24692a601d5e 84 return readWordFromCommand(command, 2, 10);
cege1808 0:24692a601d5e 85 }
cege1808 0:24692a601d5e 86
cege1808 0:24692a601d5e 87 /**************************************************************************/
cege1808 0:24692a601d5e 88 /*!
cege1808 0:24692a601d5e 89 @brief Commands the sensor to take a single eCO2/VOC measurement. Places results in {@link TVOC} and {@link eCO2}
cege1808 0:24692a601d5e 90 @returns True if command completed successfully, false if something went wrong!
cege1808 0:24692a601d5e 91 */
cege1808 0:24692a601d5e 92 /**************************************************************************/
cege1808 0:24692a601d5e 93 bool Adafruit_SGP30::IAQmeasure(void) {
cege1808 0:24692a601d5e 94 uint8_t command[2];
cege1808 0:24692a601d5e 95 command[0] = 0x20;
cege1808 0:24692a601d5e 96 command[1] = 0x08;
cege1808 0:24692a601d5e 97 uint16_t reply[2];
cege1808 0:24692a601d5e 98 if (! readWordFromCommand(command, 2, 12, reply, 2))
cege1808 0:24692a601d5e 99 return false;
cege1808 0:24692a601d5e 100 TVOC = reply[1];
cege1808 0:24692a601d5e 101 eCO2 = reply[0];
cege1808 0:24692a601d5e 102 return true;
cege1808 0:24692a601d5e 103 }
cege1808 0:24692a601d5e 104
cege1808 0:24692a601d5e 105 /**************************************************************************/
cege1808 0:24692a601d5e 106 /*!
cege1808 0:24692a601d5e 107 @brief Request baseline calibration values for both CO2 and TVOC IAQ calculations. Places results in parameter memory locaitons.
cege1808 0:24692a601d5e 108 @param eco2_base A pointer to a uint16_t which we will save the calibration value to
cege1808 0:24692a601d5e 109 @param tvoc_base A pointer to a uint16_t which we will save the calibration value to
cege1808 0:24692a601d5e 110 @returns True if command completed successfully, false if something went wrong!
cege1808 0:24692a601d5e 111 */
cege1808 0:24692a601d5e 112 /**************************************************************************/
cege1808 0:24692a601d5e 113 bool Adafruit_SGP30::getIAQBaseline(uint16_t *eco2_base, uint16_t *tvoc_base) {
cege1808 0:24692a601d5e 114 uint8_t command[2];
cege1808 0:24692a601d5e 115 command[0] = 0x20;
cege1808 0:24692a601d5e 116 command[1] = 0x15;
cege1808 0:24692a601d5e 117 uint16_t reply[2];
cege1808 0:24692a601d5e 118 if (! readWordFromCommand(command, 2, 10, reply, 2))
cege1808 0:24692a601d5e 119 return false;
cege1808 0:24692a601d5e 120 *eco2_base = reply[0];
cege1808 0:24692a601d5e 121 *tvoc_base = reply[1];
cege1808 0:24692a601d5e 122 return true;
cege1808 0:24692a601d5e 123 }
cege1808 0:24692a601d5e 124
cege1808 0:24692a601d5e 125 bool Adafruit_SGP30::getIAQRaw(uint16_t *H2_raw, uint16_t *Eth_raw) {
cege1808 0:24692a601d5e 126 uint8_t command[2];
cege1808 0:24692a601d5e 127 command[0] = 0x20;
cege1808 0:24692a601d5e 128 command[1] = 0x50;
cege1808 0:24692a601d5e 129 uint16_t reply[2];
cege1808 0:24692a601d5e 130 if (! readWordFromCommand(command, 2, 40, reply, 2))
cege1808 0:24692a601d5e 131 return false;
cege1808 0:24692a601d5e 132 *H2_raw = reply[0];
cege1808 0:24692a601d5e 133 *Eth_raw = reply[1];
cege1808 0:24692a601d5e 134 return true;
cege1808 0:24692a601d5e 135 }
cege1808 0:24692a601d5e 136
cege1808 0:24692a601d5e 137 /**************************************************************************/
cege1808 0:24692a601d5e 138 /*!
cege1808 0:24692a601d5e 139 @brief Assign baseline calibration values for both CO2 and TVOC IAQ calculations.
cege1808 0:24692a601d5e 140 @param eco2_base A uint16_t which we will save the calibration value from
cege1808 0:24692a601d5e 141 @param tvoc_base A uint16_t which we will save the calibration value from
cege1808 0:24692a601d5e 142 @returns True if command completed successfully, false if something went wrong!
cege1808 0:24692a601d5e 143 */
cege1808 0:24692a601d5e 144 /**************************************************************************/
cege1808 0:24692a601d5e 145 bool Adafruit_SGP30::setIAQBaseline(uint16_t eco2_base, uint16_t tvoc_base) {
cege1808 0:24692a601d5e 146 uint8_t command[8];
cege1808 0:24692a601d5e 147 command[0] = 0x20;
cege1808 0:24692a601d5e 148 command[1] = 0x1e;
cege1808 0:24692a601d5e 149 command[2] = tvoc_base >> 8;
cege1808 0:24692a601d5e 150 command[3] = tvoc_base & 0xFF;
cege1808 0:24692a601d5e 151 command[4] = generateCRC(command+2, 2);
cege1808 0:24692a601d5e 152 command[5] = eco2_base >> 8;
cege1808 0:24692a601d5e 153 command[6] = eco2_base & 0xFF;
cege1808 0:24692a601d5e 154 command[7] = generateCRC(command+5, 2);
cege1808 0:24692a601d5e 155
cege1808 0:24692a601d5e 156 return readWordFromCommand(command, 8, 10);
cege1808 0:24692a601d5e 157 }
cege1808 0:24692a601d5e 158
cege1808 0:24692a601d5e 159 /**************************************************************************/
cege1808 0:24692a601d5e 160 /*!
cege1808 0:24692a601d5e 161 @brief I2C low level interfacing
cege1808 0:24692a601d5e 162 */
cege1808 0:24692a601d5e 163 /**************************************************************************/
cege1808 0:24692a601d5e 164
cege1808 0:24692a601d5e 165
cege1808 0:24692a601d5e 166 bool Adafruit_SGP30::readWordFromCommand(uint8_t command[], uint8_t commandLength, uint16_t delayms, uint16_t *readdata, uint8_t readlen)
cege1808 0:24692a601d5e 167 {
cege1808 0:24692a601d5e 168 uint8_t retval;
cege1808 0:24692a601d5e 169
cege1808 0:24692a601d5e 170
cege1808 0:24692a601d5e 171 #ifdef I2C_DEBUG
cege1808 0:24692a601d5e 172 printf("\t\t-> ");
cege1808 0:24692a601d5e 173 #endif
cege1808 0:24692a601d5e 174
cege1808 0:24692a601d5e 175 retval = _i2c.write((int)_i2caddr, (const char*)command, (int)commandLength);
cege1808 0:24692a601d5e 176 //0=OK, 1=Fail https://os.mbed.com/docs/v5.9/reference/i2c.html
cege1808 0:24692a601d5e 177 if ( retval != 0)
cege1808 0:24692a601d5e 178 return false;
cege1808 0:24692a601d5e 179
cege1808 0:24692a601d5e 180 #ifdef I2C_DEBUG
cege1808 0:24692a601d5e 181 for (uint8_t i=0; i<commandLength; i++) {
cege1808 0:24692a601d5e 182 printf("0x");
cege1808 0:24692a601d5e 183 printf("%x", command[i]);
cege1808 0:24692a601d5e 184 printf(", ");
cege1808 0:24692a601d5e 185 }
cege1808 0:24692a601d5e 186 #endif
cege1808 0:24692a601d5e 187
cege1808 0:24692a601d5e 188 #ifdef I2C_DEBUG
cege1808 0:24692a601d5e 189 printf("\n");
cege1808 0:24692a601d5e 190 #endif
cege1808 0:24692a601d5e 191
cege1808 0:24692a601d5e 192 wait_ms(delayms);
cege1808 0:24692a601d5e 193
cege1808 0:24692a601d5e 194
cege1808 0:24692a601d5e 195
cege1808 0:24692a601d5e 196 if (readlen == 0)
cege1808 0:24692a601d5e 197 return true;
cege1808 0:24692a601d5e 198
cege1808 0:24692a601d5e 199 uint8_t replylen = readlen * (SGP30_WORD_LEN +1);
cege1808 0:24692a601d5e 200 uint8_t replybuffer[replylen];
cege1808 0:24692a601d5e 201
cege1808 0:24692a601d5e 202 retval = _i2c.read((int)_i2caddr, (char*)replybuffer, (int)replylen);
cege1808 0:24692a601d5e 203
cege1808 0:24692a601d5e 204 //0=OK, 1=Fail https://os.mbed.com/docs/v5.9/reference/i2c.html
cege1808 0:24692a601d5e 205 if ( retval != 0)
cege1808 0:24692a601d5e 206 return false;
cege1808 0:24692a601d5e 207
cege1808 0:24692a601d5e 208 #ifdef I2C_DEBUG
cege1808 0:24692a601d5e 209 printf("\t\t<- ");
cege1808 0:24692a601d5e 210 for (uint8_t i=0; i<replylen; i++) {
cege1808 0:24692a601d5e 211 printf("0x");
cege1808 0:24692a601d5e 212 printf("%x", replybuffer[i]);
cege1808 0:24692a601d5e 213 printf(", ");
cege1808 0:24692a601d5e 214 }
cege1808 0:24692a601d5e 215 #endif
cege1808 0:24692a601d5e 216
cege1808 0:24692a601d5e 217
cege1808 0:24692a601d5e 218 #ifdef I2C_DEBUG
cege1808 0:24692a601d5e 219 printf("\n");
cege1808 0:24692a601d5e 220 #endif
cege1808 0:24692a601d5e 221
cege1808 0:24692a601d5e 222 for (uint8_t i=0; i<readlen; i++) {
cege1808 0:24692a601d5e 223 uint8_t crc = generateCRC(replybuffer+i*3, 2);
cege1808 0:24692a601d5e 224 #ifdef I2C_DEBUG
cege1808 0:24692a601d5e 225 printf("\t\tCRC calced: 0x");
cege1808 0:24692a601d5e 226 printf("%x", crc);
cege1808 0:24692a601d5e 227 printf(" vs. 0x");
cege1808 0:24692a601d5e 228 printf("%x\n" ,replybuffer[i * 3 + 2];
cege1808 0:24692a601d5e 229 #endif
cege1808 0:24692a601d5e 230 if (crc != replybuffer[i * 3 + 2])
cege1808 0:24692a601d5e 231 return false;
cege1808 0:24692a601d5e 232 // success! store it
cege1808 0:24692a601d5e 233 readdata[i] = replybuffer[i*3];
cege1808 0:24692a601d5e 234 readdata[i] <<= 8;
cege1808 0:24692a601d5e 235 readdata[i] |= replybuffer[i*3 + 1];
cege1808 0:24692a601d5e 236 #ifdef I2C_DEBUG
cege1808 0:24692a601d5e 237 printf("\t\tRead: 0x");
cege1808 0:24692a601d5e 238 printf("%x\n", readdata[i]);
cege1808 0:24692a601d5e 239 #endif
cege1808 0:24692a601d5e 240 }
cege1808 0:24692a601d5e 241 return true;
cege1808 0:24692a601d5e 242 }
cege1808 0:24692a601d5e 243
cege1808 0:24692a601d5e 244 uint8_t Adafruit_SGP30::generateCRC(uint8_t *data, uint8_t datalen) {
cege1808 0:24692a601d5e 245 // calculates 8-Bit checksum with given polynomial
cege1808 0:24692a601d5e 246 uint8_t crc = SGP30_CRC8_INIT;
cege1808 0:24692a601d5e 247
cege1808 0:24692a601d5e 248 for (uint8_t i=0; i<datalen; i++) {
cege1808 0:24692a601d5e 249 crc ^= data[i];
cege1808 0:24692a601d5e 250 for (uint8_t b=0; b<8; b++) {
cege1808 0:24692a601d5e 251 if (crc & 0x80)
cege1808 0:24692a601d5e 252 crc = (crc << 1) ^ SGP30_CRC8_POLYNOMIAL;
cege1808 0:24692a601d5e 253 else
cege1808 0:24692a601d5e 254 crc <<= 1;
cege1808 0:24692a601d5e 255 }
cege1808 0:24692a601d5e 256 }
cege1808 0:24692a601d5e 257 return crc;
cege1808 0:24692a601d5e 258 }