Skip to content

Support for setting ENVDATA using temperature in C and humidity as percent #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ application_version KEYWORD2

geterror_id KEYWORD2
set_envdata KEYWORD2
set_envdataDHT KEYWORD2
set_envdata210 KEYWORD2
get_baseline KEYWORD2
set_baseline KEYWORD2
Expand Down
48 changes: 48 additions & 0 deletions src/ccs811.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
ccs811.cpp - Library for the CCS811 digital gas sensor for monitoring indoor air quality from ams.
2019 feb 02 v11 Erwin Ried Added envdata with C and % as parameters
2019 jan 22 v10 Maarten Pennings Added F() on all strings, added get/set_baseline()
2019 jan 15 v9 Maarten Pennings Function set_envdata did not write T; flash now uses PROGMEM array; flash now works without app, better PRINT macros, removed warnings
2018 dec 06 v8 Maarten Pennings Added firmware flash routine
Expand Down Expand Up @@ -320,6 +321,53 @@ bool CCS811::set_envdata(uint16_t t, uint16_t h) {
return ok;
}

// Writes temperature and relativeHumidity (with temperature in C and humidity as percent) to ENV_DATA . Returns false on I2C problems.
bool CCS811::set_envdataDHT(float temperature, float relativeHumidity) {
// Based on: https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library/blob/master/src/SparkFunCCS811.cpp
//Check for invalid temperatures
temperature = max(-25,min(temperature,50));

//Check for invalid humidity
relativeHumidity = max(0,min(relativeHumidity,100));
Comment on lines +327 to +331
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this was not compiling on my side. Maybe I had wired settings in the compiler. But changing that to the following code worked for me:

Suggested change
//Check for invalid temperatures
temperature = max(-25,min(temperature,50));
//Check for invalid humidity
relativeHumidity = max(0,min(relativeHumidity,100));
//Check for invalid temperatures
temperature = max(-25.0f,min(temperature,50.0f));
//Check for invalid humidity
relativeHumidity = max(0.0f,min(relativeHumidity,100.0f));


uint32_t rH = relativeHumidity * 1000; //42.348 becomes 42348
uint32_t temp = temperature * 1000; //23.2 becomes 23200

byte envdata[4];

//Split value into 7-bit integer and 9-bit fractional

//Incorrect way from datasheet.
//envData[0] = ((rH % 1000) / 100) > 7 ? (rH / 1000 + 1) << 1 : (rH / 1000) << 1;
//envData[1] = 0; //CCS811 only supports increments of 0.5 so bits 7-0 will always be zero
//if (((rH % 1000) / 100) > 2 && (((rH % 1000) / 100) < 8))
//{
// envData[0] |= 1; //Set 9th bit of fractional to indicate 0.5%
//}

//Correct rounding. See issue 8: https://github.com/sparkfun/Qwiic_BME280_CCS811_Combo/issues/8
envdata[0] = (rH + 250) / 500;
envdata[1] = 0; //CCS811 only supports increments of 0.5 so bits 7-0 will always be zero

temp += 25000; //Add the 25C offset
//Split value into 7-bit integer and 9-bit fractional
//envData[2] = ((temp % 1000) / 100) > 7 ? (temp / 1000 + 1) << 1 : (temp / 1000) << 1;
//envData[3] = 0;
//if (((temp % 1000) / 100) > 2 && (((temp % 1000) / 100) < 8))
//{
// envData[2] |= 1; //Set 9th bit of fractional to indicate 0.5C
//}

//Correct rounding
envdata[2] = (temp + 250) / 500;
envdata[3] = 0;

wake_up();
// Serial.print(" [T="); Serial.print(t); Serial.print(" H="); Serial.print(h); Serial.println("] ");
bool ok = i2cwrite(CCS811_ENV_DATA,4,envdata);
wake_down();
return ok;
}

// Writes t and h (in ENS210 format) to ENV_DATA. Returns false on I2C problems.
bool CCS811::set_envdata210(uint16_t t, uint16_t h) {
Expand Down
2 changes: 2 additions & 0 deletions src/ccs811.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
ccs811.h - Library for the CCS811 digital gas sensor for monitoring indoor air quality from ams.
2019 feb 02 v11 Erwin Ried Added envdata with C and % as parameters
2019 jan 22 v10 Maarten Pennings Added F() on all strings, added get/set_baseline()
2019 jan 15 v9 Maarten Pennings Flash&i2cwrite now use const array
2018 dec 06 v8 Maarten Pennings Added firmware flash routine
Expand Down Expand Up @@ -75,6 +76,7 @@ class CCS811 {
int application_version(void); // Gets version of the CCS811 application (returns -1 on I2C failure)
int get_errorid(void); // Gets the ERROR_ID [same as 'err' part of 'errstat' in 'read'] (returns -1 on I2C failure)
bool set_envdata(uint16_t t, uint16_t h); // Writes t and h to ENV_DATA (see datasheet for format). Returns false on I2C problems.
bool set_envdataDHT(float temperature, float relativeHumidity); // Writes temperature and relativeHumidity (with temperature in C and humidity as percent) to ENV_DATA . Returns false on I2C problems.
bool set_envdata210(uint16_t t, uint16_t h); // Writes t and h (in ENS210 format) to ENV_DATA. Returns false on I2C problems.
bool get_baseline(uint16_t *baseline); // Reads (encoded) baseline from BASELINE. Returns false on I2C problems. Get it, just before power down (but only when sensor was on at least 20min) - see CCS811_AN000370
bool set_baseline(uint16_t baseline); // Writes (encoded) baseline to BASELINE. Returns false on I2C problems. Set it, after power up (and after 20min)
Expand Down