uses BBC micro:bit to measure and display indoor air quality using Bosch BME680 and/or Sensirion SGP30

Dependencies:   microbit

uses Bosch BME680 and/or Sensirion SGP30 sensors to measure indor air quality

sensors should be connected to BBC micro:bit using i2c

commands are received and data is being sent using uBit / nordic radio protocol

display ---

last line always indicates: - first dot: bme680 detected - second dot: sgp30 detected - third dot: sgp 30 setting humidity/temperature - fourth dor: sgp30 measuring - fith dot: bme680 measuring

the detect dots should be in a stable state (not blinking) the measuring dots should be blinking (constant light means: measurement failed)

if only one bme680 is present: - first 3 lines indicate gas resistence (air quality / more dots == worse quality) - fourth line indicates humidity level

if only sgp30 is present: - first two lines indicate SGP30 VOC level - third and fourth line indicate sgp30 CO2 level

if both sensors are present: - first line indicates SGP30 VOC level - second line line indicates sgp30 CO2 level - third line indicates bme680 gas resistence (air quality) - fourth line indicates bme 680 humidity level

buttons - B display state, switches betweeen - full bright - low light - display off

AB reset sgp30 baseline in non volatile storage

data logging -- during measurements the minimum and mximum values for each measured value (temperature, air pressure, humidity,gas resistance, VOC, CO2) are being stored in non volatile storage those (and the last measurement results) are being shown when btn A has been pressed

SGP30/sgp30.cpp

Committer:
jsa1969
Date:
2018-12-04
Revision:
5:9a04d6d0c2ff
Parent:
2:544117df8c65
Child:
6:fc6f2f08724b

File content as of revision 5:9a04d6d0c2ff:

#include "sgp30.h"
#include "physics.h"

// the i2c address
#define SGP30_I2CADDR_DEFAULT 0x58     ///< SGP30 has only one I2C address

// commands and constants
#define SGP30_FEATURESET       0x0020  ///< The required set for this library
#define SGP30_CRC8_POLYNOMIAL  0x31    ///< Seed for SGP30's CRC polynomial
#define SGP30_CRC8_INIT        0xFF    ///< Init value for CRC
#define SGP30_WORD_LEN         2       ///< 2 bytes per word

Sgp30::Sgp30(I2cCallbacks *callbacks) {
    _callbacks = callbacks;
    _i2caddr = SGP30_I2CADDR_DEFAULT <<1;
}

bool Sgp30::begin() {
  uint8_t command[2];
  command[0] = 0x36;
  command[1] = 0x82;
  if (!readWordFromCommand(command, 2, 10, serialnumber, 3)) {
    return false;
  }
  
  uint16_t featureset;
  command[0] = 0x20;
  command[1] = 0x2F;
  if (! readWordFromCommand(command, 2, 10, &featureset, 1)) {
    return false;
  }
  
  //Serial.print("Featureset 0x"); Serial.println(featureset, HEX);
  if (featureset != SGP30_FEATURESET)  {
    return false;
  }
  
  if (! IAQinit()) { 
    return false;
  }
  
  return true;
}

bool Sgp30::IAQmeasure(void) {
    uint8_t command[2];
    command[0] = 0x20;
    command[1] = 0x08;
    uint16_t reply[2];
    if (! readWordFromCommand(command, 2, 12, reply, 2)) {
        return false;
    }
    TVOC = reply[1];
    eCO2 = reply[0];
    return true;
}

bool Sgp30::test(void) {
    uint8_t command[2];
    command[0] = 0x20;
    command[1] = 0x32;
    uint16_t reply[2];
    if (! readWordFromCommand(command, 2, 220, reply, 2)) {
        return false;
    }
    
    return reply[0]==0xD4 && reply[1]==0x00;
}
bool Sgp30::setHumidity(uint32_t rel_humidity, uint32_t temperature) {
    return setHumidity(Physics::absHumidity(rel_humidity, temperature));
}

bool Sgp30::IAQinit(void) {
  uint8_t command[2];
  command[0] = 0x20;
  command[1] = 0x03;
  return readWordFromCommand(command, 2, 10);
}

bool Sgp30::setHumidity(uint32_t absolute_humidity) {
  if (absolute_humidity > 256000) {
    return false;
  }
  
  if (absolute_humidity == _absolute_humidity) {
      return true;
  }

  uint16_t ah_scaled = (uint16_t)(((uint64_t)absolute_humidity * 256 * 16777) >> 24);
  uint8_t command[5];
  command[0] = 0x20;
  command[1] = 0x61;
  command[2] = ah_scaled >> 8;
  command[3] = ah_scaled & 0xFF;
  command[4] = generateCRC(command+2, 2);

  if (readWordFromCommand(command, 5, 10)) {
      _absolute_humidity = absolute_humidity;
      return true;
  } else {
    return false;
  }
}

bool Sgp30::readWordFromCommand(uint8_t command[], uint8_t commandLength, uint16_t delayms, uint16_t *readdata, uint8_t readlen)
{
    if (_callbacks->write(_i2caddr, command, commandLength) != MICROBIT_OK) {
        return false;
    }

    _callbacks->delay_ms(delayms);

    if (readlen == 0) 
        return true;

    uint8_t replylen = readlen * (SGP30_WORD_LEN +1);
    uint8_t replybuffer[replylen];
    if (_callbacks->read(_i2caddr,replybuffer,replylen) != MICROBIT_OK) {
        return false;
    }

    for (uint8_t i=0; i<readlen; i++) {
        uint8_t crc = generateCRC(replybuffer+i*3, 2);
        if (crc != replybuffer[i * 3 + 2])
        return false;
        // success! store it
        readdata[i] = replybuffer[i*3];
        readdata[i] <<= 8;
        readdata[i] |= replybuffer[i*3 + 1];
    }
    
    return true;
}

uint8_t Sgp30::generateCRC(uint8_t *data, uint8_t datalen) {
  // calculates 8-Bit checksum with given polynomial
  uint8_t crc = SGP30_CRC8_INIT;

  for (uint8_t i=0; i<datalen; i++) {
    crc ^= data[i];
    for (uint8_t b=0; b<8; b++) {
      if (crc & 0x80)
    crc = (crc << 1) ^ SGP30_CRC8_POLYNOMIAL;
      else
    crc <<= 1;
    }
  }
  return crc;
}