Moisture sensor: random results

Hello,

I have started recently in getting in the fantastic world of ESPHome devices. My first project is to use a Moisture sensor (more precisely Capacitive Soil Moisture Sensor v1.2) to keep track of when I should water plants.

I started earlier this week (below I’m going to include the source code) and I was getting some consistent results. Then from yesterday evening I started getting quite random results.

Here below the chart of the sensor where is visible when the ESP started returning quite random results (25 Aug evening). From that moment I tried to:

  • Re-upload the sketch
  • Change the sensor (with an equal)

And nothing still getting very weird results. Do you have any idea of why this is happening? The code is the same, no changes were made by my self.

Let me know your thoughts, thanks in advance
Ciao

esphome:
  name: esphome-soil_moisture
  friendly_name: ESPHome Soil Moisture

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_key

ota:
  platform: esphome


wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "esphome-soil_moisture"
    password: !secret ap_password

captive_portal:

#Soil moisture sensor
sensor:
  - platform: adc
    pin: GPIO35
    name: "Soil Moisture"
    id:  soil_moisture
    accuracy_decimals: 1
    update_interval: 5s
    icon: mdi:flower
    unit_of_measurement: '%'
    attenuation: 11db
    filters:
      - lambda: |-
          if (x > 2) { 				// if over 2 volts, we are dry
            return 0;
          } else if (x < 1.0) { 			// if under 1 volts, we are fully saturated
            return 100;
          } else {
            return (2-x) / (2-1.0) * 100.0; 	// use a linear fit for any values in between
          }
      - median:
          window_size: 7
          send_every: 4
          send_first_at: 1

  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 5s
    filters:
      - median:
          window_size: 7
          send_every: 4
          send_first_at: 1

I suspect the logic of your calculation is not working as you would expect, and you are returning the wrong value especially when the reading approaches 1.

Try:

            return (2-x)  * 100.0; 	// use a linear fit for any values in between

Which is essentially as you are trying to do already… So could be wrong - not an expert on C++ order of calculation.

1 Like

Thank you very much for your feedback.

I tried a few things:

  • Your suggestion to change the code
  • I have also change the filter from using the median, to using the moving average.

I’m still under the impression that is not working as intended, but I’ll leave on for a few hours and let see how is going to be the result

sensor:
  - platform: adc
    pin: GPIO35
    name: "Soil Moisture"
    id:  soil_moisture
    accuracy_decimals: 1
    update_interval: 5s
    icon: mdi:flower
    unit_of_measurement: '%'
    attenuation: 11db
    filters:
      - lambda: |-
          if (x > 2.2) { 				// if over 2 volts, we are dry
            return 0;
          } else if (x < 0.8) { 			// if under 1 volts, we are fully saturated
            return 100;
          } else {
            return (2.2-x) * 100.0; 	// use a linear fit for any values in between
          }
      - sliding_window_moving_average:
          window_size: 19
          send_every: 4
          send_first_at: 1

Didn’t really analyze your math to see if that’s the issue, but regardless you can make things a lot easier for yourself and just use the calibrate linear filter:

Thanks for your feedback, I’ll take a look into this also.

Meanwhile after a few hours (without doing anything) the sensor restarted working properly.

Pls see below, during the evening stopped with weird data and started working properly. In the morning I watered the plant and everything restarted working properly.

How is it wired up, on a breadboard maybe? That jumps out to me as a poor connection issue.

1 Like