The library returns a float. Over the weekend I hacked together a firmware to round it to 0.01% RH (matching the spec in the datasheet) and the results look great to me:
Compare to the image in my original post. Note the noise is very small, well under 1.8%.
Patch, for reference.
I am releasing this under CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
diff --git a/examples/BASIC/BASIC.ino b/examples/BASIC/BASIC.ino
index 4099b71..1c93f77 100644
--- a/examples/BASIC/BASIC.ino
+++ b/examples/BASIC/BASIC.ino
@@ -554,10 +554,10 @@ static void tempHumUpdate(void) {
measurements.Humidity = ag.sht.getRelativeHumidity();
Serial.printf("Temperature in C: %0.2f\r\n", measurements.Temperature);
- Serial.printf("Relative Humidity: %d\r\n", measurements.Humidity);
+ Serial.printf("Relative Humidity: %0.2f\r\n", measurements.Humidity);
Serial.printf("Temperature compensated in C: %0.2f\r\n",
measurements.Temperature);
- Serial.printf("Relative Humidity compensated: %d\r\n",
+ Serial.printf("Relative Humidity compensated: %0.2f\r\n",
measurements.Humidity);
// Update compensation temperature and humidity for SGP41
diff --git a/examples/DiyProIndoorV3_3/DiyProIndoorV3_3.ino b/examples/DiyProIndoorV3_3/DiyProIndoorV3_3.ino
index df059b7..f516d61 100644
--- a/examples/DiyProIndoorV3_3/DiyProIndoorV3_3.ino
+++ b/examples/DiyProIndoorV3_3/DiyProIndoorV3_3.ino
@@ -606,10 +606,10 @@ static void tempHumUpdate(void) {
measurements.Humidity = ag.sht.getRelativeHumidity();
Serial.printf("Temperature in C: %0.2f\r\n", measurements.Temperature);
- Serial.printf("Relative Humidity: %d\r\n", measurements.Humidity);
+ Serial.printf("Relative Humidity: %0.2f\r\n", measurements.Humidity);
Serial.printf("Temperature compensated in C: %0.2f\r\n",
measurements.Temperature);
- Serial.printf("Relative Humidity compensated: %d\r\n",
+ Serial.printf("Relative Humidity compensated: %0.2f\r\n",
measurements.Humidity);
// Update compensation temperature and humidity for SGP41
diff --git a/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino b/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino
index 20be418..cd4ee35 100644
--- a/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino
+++ b/examples/DiyProIndoorV4_2/DiyProIndoorV4_2.ino
@@ -649,10 +649,10 @@ static void tempHumUpdate(void) {
measurements.Humidity = ag.sht.getRelativeHumidity();
Serial.printf("Temperature in C: %0.2f\r\n", measurements.Temperature);
- Serial.printf("Relative Humidity: %d\r\n", measurements.Humidity);
+ Serial.printf("Relative Humidity: %0.2f\r\n", measurements.Humidity);
Serial.printf("Temperature compensated in C: %0.2f\r\n",
measurements.Temperature);
- Serial.printf("Relative Humidity compensated: %d\r\n",
+ Serial.printf("Relative Humidity compensated: %0.2f\r\n",
measurements.Humidity);
// Update compensation temperature and humidity for SGP41
diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino
index 978c345..2ebac97 100644
--- a/examples/OneOpenAir/OneOpenAir.ino
+++ b/examples/OneOpenAir/OneOpenAir.ino
@@ -1048,7 +1048,7 @@ static void updatePm(void) {
Serial.printf("[1] PM10 ug/m3: %d\r\n", measurements.pm10_1);
Serial.printf("[1] PM3.0 Count: %d\r\n", measurements.pm03PCount_1);
Serial.printf("[1] Temperature in C: %0.2f\r\n", measurements.temp_1);
- Serial.printf("[1] Relative Humidity: %d\r\n", measurements.hum_1);
+ Serial.printf("[1] Relative Humidity: %0.2f\r\n", measurements.hum_1);
Serial.printf("[1] Temperature compensated in C: %0.2f\r\n",
ag->pms5003t_1.temperatureCompensated(measurements.temp_1));
Serial.printf("[1] Relative Humidity compensated: %f\r\n",
@@ -1078,7 +1078,7 @@ static void updatePm(void) {
Serial.printf("[2] PM10 ug/m3: %d\r\n", measurements.pm10_2);
Serial.printf("[2] PM3.0 Count: %d\r\n", measurements.pm03PCount_2);
Serial.printf("[2] Temperature in C: %0.2f\r\n", measurements.temp_2);
- Serial.printf("[2] Relative Humidity: %d\r\n", measurements.hum_2);
+ Serial.printf("[2] Relative Humidity: %0.2f\r\n", measurements.hum_2);
Serial.printf("[2] Temperature compensated in C: %0.2f\r\n",
ag->pms5003t_1.temperatureCompensated(measurements.temp_2));
Serial.printf("[2] Relative Humidity compensated: %d\r\n",
@@ -1217,10 +1217,10 @@ static void tempHumUpdate(void) {
measurements.Humidity = ag->sht.getRelativeHumidity();
Serial.printf("Temperature in C: %0.2f\r\n", measurements.Temperature);
- Serial.printf("Relative Humidity: %d\r\n", measurements.Humidity);
+ Serial.printf("Relative Humidity: %0.2f\r\n", measurements.Humidity);
Serial.printf("Temperature compensated in C: %0.2f\r\n",
measurements.Temperature);
- Serial.printf("Relative Humidity compensated: %d\r\n",
+ Serial.printf("Relative Humidity compensated: %0.2f\r\n",
measurements.Humidity);
// Update compensation temperature and humidity for SGP41
diff --git a/src/AgOledDisplay.cpp b/src/AgOledDisplay.cpp
index 8b07ada..94fe843 100644
--- a/src/AgOledDisplay.cpp
+++ b/src/AgOledDisplay.cpp
@@ -38,12 +38,12 @@ void OledDisplay::showTempHum(bool hasStatus) {
/** Show humidty */
if (utils::isValidHumidity(value.Humidity)) {
- snprintf(buf, sizeof(buf), "%d%%", value.Humidity);
+ snprintf(buf, sizeof(buf), "%0.0f%%", value.Humidity);
} else {
snprintf(buf, sizeof(buf), "-%%");
}
- if (value.Humidity > 99) {
+ if (strnlen(buf, sizeof(buf)) > 3) {
DISP()->drawStr(97, 10, buf);
} else {
DISP()->drawStr(105, 10, buf);
diff --git a/src/AgValue.cpp b/src/AgValue.cpp
index 015a788..d7252d9 100644
--- a/src/AgValue.cpp
+++ b/src/AgValue.cpp
@@ -43,9 +43,9 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
}
}
if (utils::isValidHumidity(this->Humidity)) {
- root["rhum"] = this->Humidity;
+ root["rhum"] = ag->round2(this->Humidity);
if (localServer) {
- root["rhumCompensated"] = this->Humidity;
+ root["rhumCompensated"] = ag->round2(this->Humidity);
}
}
}
@@ -87,7 +87,7 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
if (localServer) {
val = ag->pms5003t_2.humidityCompensated((this->hum_1 + this->hum_2) / 2.0f);
if (utils::isValidHumidity(val)) {
- root["rhumCompensated"] = (int)val;
+ root["rhumCompensated"] = ag->round2(val);
}
}
}
@@ -124,12 +124,12 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
}
}
if (utils::isValidHumidity(this->hum_1)) {
- root["rhum"] = this->hum_1;
+ root["rhum"] = ag->round2(this->hum_1);
if (localServer) {
val = ag->pms5003t_1.humidityCompensated(this->hum_1);
if (utils::isValidHumidity(val)) {
- root["rhumCompensated"] = (int)val;
+ root["rhumCompensated"] = ag->round2(val);
}
}
}
@@ -161,12 +161,12 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
}
}
if(utils::isValidHumidity(this->hum_2)) {
- root["rhum"] = this->hum_2;
+ root["rhum"] = ag->round2(this->hum_2);
if (localServer) {
val = ag->pms5003t_2.humidityCompensated(this->hum_2);
if (utils::isValidHumidity(val)) {
- root["rhumCompensated"] = (int)val;
+ root["rhumCompensated"] = ag->round2(val);
}
}
}
@@ -199,11 +199,11 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
}
}
if (utils::isValidHumidity(this->hum_1)) {
- root["rhum"] = this->hum_1;
+ root["rhum"] = ag->round2(this->hum_1);
if(localServer) {
val = ag->pms5003t_1.humidityCompensated(this->hum_1);
if(utils::isValidHumidity(val)) {
- root["rhumCompensated"] = (int)val;
+ root["rhumCompensated"] = ag->round2(val);
}
}
}
@@ -232,12 +232,12 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
}
}
if (utils::isValidHumidity(this->hum_2)) {
- root["rhum"] = this->hum_2;
+ root["rhum"] = ag->round2(this->hum_2);
if(localServer) {
val = ag->pms5003t_1.humidityCompensated(this->hum_2);
if(utils::isValidHumidity(val)) {
- root["rhumCompensated"] = (int)val;
+ root["rhumCompensated"] = ag->round2(val);
}
}
}
@@ -269,12 +269,12 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
}
}
if (utils::isValidHumidity(this->hum_1)) {
- root["channels"]["1"]["rhum"] = this->hum_1;
+ root["channels"]["1"]["rhum"] = ag->round2(this->hum_1);
if (localServer) {
val = ag->pms5003t_1.humidityCompensated(this->hum_1);
if (utils::isValidHumidity(val)) {
- root["channels"]["1"]["rhumCompensated"] = (int)val;
+ root["channels"]["1"]["rhumCompensated"] = ag->round2(val);
}
}
}
@@ -305,12 +305,12 @@ String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
}
}
if (utils::isValidHumidity(this->hum_2)) {
- root["channels"]["2"]["rhum"] = this->hum_2;
+ root["channels"]["2"]["rhum"] = ag->round2(this->hum_2);
if (localServer) {
val = ag->pms5003t_1.humidityCompensated(this->hum_2);
if (utils::isValidHumidity(val)) {
- root["channels"]["2"]["rhumCompensated"] = (int)val;
+ root["channels"]["2"]["rhumCompensated"] = ag->round2(val);
}
}
}
diff --git a/src/AgValue.h b/src/AgValue.h
index 3176482..9dcc8ab 100644
--- a/src/AgValue.h
+++ b/src/AgValue.h
@@ -33,7 +33,7 @@ public:
~Measurements() {}
float Temperature;
- int Humidity;
+ float Humidity;
int CO2;
int TVOC;
int TVOCRaw;
@@ -45,14 +45,14 @@ public:
int pm10_1;
int pm03PCount_1;
float temp_1;
- int hum_1;
+ float hum_1;
int pm25_2;
int pm01_2;
int pm10_2;
int pm03PCount_2;
float temp_2;
- int hum_2;
+ float hum_2;
int pm1Value01;
int pm1Value25;