Skip to content

Commit

Permalink
Merge pull request #17 from fmatray/master
Browse files Browse the repository at this point in the history
Correct issue #16
  • Loading branch information
avaldebe authored Nov 27, 2020
2 parents 4105980 + 8a81ecb commit ae66d05
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ The available status/error codes and pre-defined error messages are:
For easy of use, the error message are pre-defined with `#define`.
See the [examples](examples/) for error handling implementation.
## Temperature and humidity offset correction (Optional)
Some sensors have a small deviation with temperature and humidity readings.
The compensate is possible with a correction offset defined by set_temp_offset() and set_rhum_offset().
The result will be reading + offset. Default offset values are O.
Example:
```c++
set_temp_offset(-0.6);
set_rhum_offset(2);
```

## Contribute

If you have read this far, this is the library for your project
Expand Down
8 changes: 4 additions & 4 deletions src/PMserial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ void SerialPM::decodeBuffer(bool tsi_mode, bool truncated_num)
hcho = buff2word(28) * 1e-3;
break;
case PMS5003T:
temp = int8_t(n5p0) * 1e-1; // cast to signed integer
rhum = n10p0 * 1e-1;
temp = int16_t(n5p0) * 1e-1 + temp_offset; // cast to signed integer 16bits
rhum = n10p0 * 1e-1 + rhum_offset;
n5p0 = 0;
n10p0 = 0;
break;
case PMS5003ST:
hcho = buff2word(28) * 1e-3;
temp = int8_t(buff2word(30)) * 1e-1; // cast to signed integer
rhum = buff2word(32) * 1e-1;
temp = int16_t(buff2word(30)) * 1e-1 + temp_offset; // cast to signed integer 16bits
rhum = buff2word(32) * 1e-1 + rhum_offset;
break;
default:
break;
Expand Down
11 changes: 11 additions & 0 deletions src/PMserial.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ class SerialPM
inline bool has_number_concentration() { return (status == OK) && (pms != PMS3003); }
inline bool has_temperature_humidity() { return (status == OK) && ((pms == PMS5003T) || (pms == PMS5003ST)); }
inline bool has_formaldehyde() { return (status == OK) && ((pms == PMS5003S) || (pms == PMS5003ST)); }

// adding offsets works well in normal range
// might introduce under- or overflow at the ends of the sensor range
inline void set_rhum_offset(float offset) { rhum_offset = offset; };
inline void set_temp_offset(float offset) { temp_offset = offset; };
inline float get_rhum_offset() { return rhum_offset; };
inline float get_temp_offset() { return temp_offset; };
#ifdef PMS_DEBUG
#ifdef HAS_HW_SERIAL
inline void print_buffer(Stream &term, const char *fmt)
Expand All @@ -154,6 +161,10 @@ class SerialPM
uint8_t rx, tx; // Serial1 pins on ESP32
#endif

// Correct Temperature & Humidity
float temp_offset = 0.0;
float rhum_offset = 0.0;

// utility functions
STATUS trigRead();
bool checkBuffer(size_t bufferLen);
Expand Down

0 comments on commit ae66d05

Please sign in to comment.