Skip to content

Commit

Permalink
capsules: chirp_i2c_moisture: Improve reliability and accuracy
Browse files Browse the repository at this point in the history
To ensure that we always get the capacitance we issue the
GET_CAPACITANCE command twice. The first to start a fresh
request and the second to get the latest value.

Previously we missed the second request as it was just a read operation
instead of a write/read with the second GET_CAPACITANCE request. This
returned out of date data. Fix this by issueing two GET_CAPACITANCE
requests.

While here let's convert the data processing to return a percentage
of moisture (from air to full water) instead of returning the dry basis
moisture content.

Signed-off-by: Alistair Francis <[email protected]>
  • Loading branch information
alistair23 committed Nov 19, 2024
1 parent a1966b8 commit a371fb9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions capsules/extra/src/chirp_i2c_moisture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ impl<'a, I: I2CDevice> I2CClient for ChirpI2cMoisture<'a, I> {
Operation::None => (),
Operation::Moisture => {
self.state.set(DeviceState::FinalMoisture);
if let Err((e, buf)) = self.i2c.read(buffer, 2) {
buffer[0] = GET_CAPACITANCE;
if let Err((e, buf)) = self.i2c.write_read(buffer, 1, 2) {
self.buffer.replace(buf);
self.op.set(Operation::None);

Expand All @@ -163,11 +164,12 @@ impl<'a, I: I2CDevice> I2CClient for ChirpI2cMoisture<'a, I> {
Operation::Moisture => {
let capacitance = (((buffer[0] as u32) << 8) | (buffer[1] as u32)) as f32;

// Based on results from
// https://github.com/Miceuz/i2c-moisture-sensor/raw/master/Soil%20Moisture%20Sensor%20Calibration.pdf
let moisture_content = (0.01007 * capacitance * capacitance)
+ (0.24885 * capacitance)
- 625.0872;
// 247 is the capacitance in air
// 510 is the capacitance in water
// Use those to calculate the moisture percentage, which is rougly linear
// https://github.com/Miceuz/i2c-moisture-sensor/blob/master/README.md#how-to-interpret-the-readings
// Note that this gives moisture in hundredths of a percent
let moisture_content = ((capacitance - 247.0) / (510.0 - 247.0)) * 10000.0;

self.state.set(DeviceState::Normal);
self.buffer.replace(buffer);
Expand Down

0 comments on commit a371fb9

Please sign in to comment.