ESP rain detector

Thx for the tip. Which part of the code do you suggest?

Its easy to overthink or overcomplicate things. If you take a step back and not think of things as if this sensor is a rain detector or that sensor is a motion sensor or whatever. If you approach it from a more basic level and see them as either, they are a sensor and what do they output to the esp. Its either a digital logic signal(High/Low) or its an analog(variable voltage level) then its much easier. If it has an AO pin, its analog and a DO is digital or it can have both and its up to you which you use.

All sensors that use a DO or AO work the same way. Youre either watching for a logic level change from High to Low or vice-versa and you use a binary_sensor or with AO, whatever your sensing, rain, light, pressure etc. Your watching for voltage levels to increase or decrease to determine how much or little the sensor is sensng.

Now its easy to do the code part because you have 2 options(for the most part)
Analog.

Or.

Digital.

Bad idea and a waste (fast wear) of hardware in this case. :put_litter_in_its_place:

…if you want the anode to oxidize within few (or one :point_down:) months that’s very correct :joy:

You just don’t want to have sensor like this permanent connected to DC voltage because it degrades faster that everyone would want. So don’t underthink @Fallingaway24 :wink:

You can take it all and adapt it for your needs. Just check the extensive readme as it contains everything one might want to now :bulb:

There’s two ways to do things like this. Your way, where you give them every single thing for them to copy and a wiring diagram. It works but, what do they learn? NOTHING. The other way is to push them to actually read the material so they do learn and dont have to rely on everyone else.

Now, let’s not pretend that you’ve solved the problem with these sensors degrading by adding a switch. The copper strips will seperate if theyre sitting out in the sun for to long as well. These sensors are junk, they’re meant for learning and not a long term solution. Anyone who uses these will be replacing them either very frequently or just frequently.

Hi, can anyone explain how the switch GPIO19 work inside the example from hugokernel ? for my understanding is necessary during measurement switch on GPIO19 measure a value and then switch off. I can not unterstand how this work ?

Actual i only read the ADC Value , but i think this enough to compare inside HA.

Becauss constant current causes those cheap sensors to corrode and fail. You toggle the switch to take a measurement instead of taking measurements 24/7 continuously.

These cheao sensors are for educational purpose. They’re not meant for long term use. Buy yourself a real one instead.

Take 2 minutes to read about the electrochemical reaction that happens (already linked in a previous post) - priceless! :joy:

image

That’s exactly the way to do it - but don’t tell anybody :shushing_face:

We recommend not powering the sensor constantly to prevent corrosion of the probes

Soil Moisture Sensor Hookup Guide - SparkFun Learn :brain: :warning:

I wonder I wonder… this SparkFun $6.50 moisture sensors are easily five times more expensive than a cheap capacitive moisture sensor from aliexpress - so they are good and other then the cheap capacitive ones don’t corrode - right Justin? :thinking:

i’m doing it and trying to learn by example. nice job.

The fact that you want to pick a fight over the time these will corrode and fail but, you completely disregard the obvious problem with these being exposed to water or the outside elements. It makes it really difficult not to laugh at you. You obviously do more talking than actual doing or else you would know.

as i understood, feeding power only when reading will avoid the corrosion, or at least make it last much longer than having it powered all the time right? like the with the cheap rain sensor?

1 Like

it’s a 10K resistor and you will get it almost anywhere (ebay, aliexpress, electronics supply shop on the corner… ).

it turns GPIO19 high to provide power (3V3) to the pin, so resistance can be read by GPIO33 (ADC). it turn it on and off, so the rain sensor does not get corroded due electrochemical reaction.

Right @orange-assistant ? If not, please correct me.

1 Like

Correct, you can expect 1, maximum 2 months lifetime of the sensor when powered permanently. On the other hand the github from hugo kernel shows this nice photo after 6 month usage with his solution (only apply power to the sensor for taking measurements): :point_down:

As you see even the (non inox) screws suffered more over this time :pinched_fingers:

Very much correct, super simple and avoids monthly dead sensor that would need to be replaced if one would follow @Fallingaway24 wrong advice given in this thread :wink:

By the looks you are the only person that tries to pick a fight here… :person_facepalming:

I also very much find your presentation as a teacher quite amusing :point_down:

On the other hand basic chemistry skills are obviously not present. And even though a link to wikipedia - to read up the missing knowledge - is present you ignore it completely. :person_shrugging:

So you are somewhat the evidence that the thing you wanna achieve (teach others how to do it by present links to the knowledge) doesn’t work :rofl:

sorry , my question not was why. I want know how this function works inside the program.

GPIO19 is defined as a switch in Line 399 in the raindetector.yaml :point_down:

A script in Line 80 toggles that switch for measurement :point_down:

That’s like the magic behind that :raised_hands:

1 Like

Ah , ok thank you , the full code give me some clarity

Could someone please tell me what I am doing wrong?

I made this sensor…

And copied the code from here: CODE

Why do I get this error?

INFO ESPHome 2024.3.2
INFO Reading configuration /config/esphome/rain-sensor.yaml...
Failed config

sensor.resistance: [source /config/esphome/rain-sensor.yaml:52]
  platform: resistance
  sensor: source_sensor
  id: real_resistance_sensor
  name: rain resistance
  configuration: DOWNSTREAM
  resistor: 1000
  reference_voltage: 3.3V
  internal: True
  icon: mdi:omega
  filters: 
    - lambda: return max((float)$min_resistance, x);
    - lambda: return min((float)$max_resistance, x);
  on_value: 
    then: 
      - if: 
          condition: 
            lambda: |-
              return (
                  id(real_resistance_sensor).state > $min_resistance
                  &&
                  id(real_resistance_sensor).state <= $max_resistance
              );
          then: 
            - 
              Unable to find action with the name 'sensor.template.publish'.
              sensor.template.publish: 
                id: resistance_sensor
                state: !lambda |-
                  return id(real_resistance_sensor).state;

on this Esphome code?

esphome:
  name: rain-sensor
  friendly_name: rain-sensor

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "liC61y1Npw1PK/RBfiG1vdaS0n0SKLzsgCSCSrlskcU="

ota:
  password: "16eea3eb58797448c81e1be86799b374"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Rain-Sensor Fallback Hotspot"
    password: "rPc7XbsKnszN"

captive_portal:

sensor:
  - platform: adc
    id: source_sensor
    pin: GPIO33
    name: ADC
    attenuation: 11db
    internal: true

    # It is important to have a low update interval so that
    # the measurement has time to be done correctly during
    # the activation of the voltage AND taking into account the median filter
    update_interval: 250ms

    filters:
      - multiply: 0.846153 # 3.9 (11db attenuation full-scale voltage) -> 3.3V
      - median:
          window_size: 7
          send_every: 4
          send_first_at: 3

  - platform: resistance
    sensor: source_sensor
    id: real_resistance_sensor
    name: "rain resistance"
    configuration: DOWNSTREAM
    resistor: 1000
    reference_voltage: 3.3V
    internal: true
    icon: "mdi:omega"
    filters:
      - lambda: 'return max((float)$min_resistance, x);'
      - lambda: 'return min((float)$max_resistance, x);'
    on_value:
      then:
        - if:
            condition:
              lambda: |-
                  return (
                      id(real_resistance_sensor).state > $min_resistance
                      &&
                      id(real_resistance_sensor).state <= $max_resistance
                  );
            then:
              - sensor.template.publish:
                  id: resistance_sensor
                  state: !lambda 'return id(real_resistance_sensor).state;'

switch:
  - platform: gpio
    id: resistance_bias
    name: "rain resistance bias"
    icon: "mdi:power"
    pin:
      number: GPIO19
      mode: OUTPUT

Thank you!

I have ESP32-VROOM-32D and It says it already raining with a dry sensor. Is the problem the wrong ESP32? Also making the sensor wet nothing changes. I wired it like in this post. The layout differs from the one in the wiring pic in this post.

This thread is super useful. Thought I would accumulate a final working version for the original sensor post

substitutions:
  name: esph-29697f-ows-1
  friendly_name: Outdoor Weather Station (ESPH-29697f)
  board: nodemcuv2
  pin_power: D7
  pin_rs_analog: A0
  pin_rs_digital: D2
  sensor_update_interval: 5s
  sensor_warmup: 10ms
  sensor_cooldown: 10ms

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: '1.0'

esp8266:
  board: ${board}

logger: # Enable logging and other configuration
api: # Enable Home Assistant API
ota: # Allow Over-The-Air updates
improv_serial: # Allow provisioning Wi-Fi via serial
web_server: # To have a "next url" for improv serial

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

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp8266.yaml@main
  import_full_config: true


# --------------------------------------------------------------------------------------------

sensor:
  - platform: adc
    pin: ${pin_rs_analog}
    id: water_resistance
    internal: true
    filters:
      - calibrate_linear: # trim erroneous 5% edges
          - 0.05 -> 0.0
          - 0.95 -> 100.0
  
  - platform: template
    name: "Raindrop Coverage Sensor"
    id: raindrop_coverage_sensor
    accuracy_decimals: 1
    unit_of_measurement: "%"
    icon: "mdi:water-percent"

binary_sensor:
  - platform: template
    name: "Rain Sensor"
    id: rain_sensor
    device_class: moisture
    icon: "mdi:umbrella"

# we read data every 5 seconds and update the sensors
interval:
  - interval: ${sensor_update_interval}
    then:
      - output.turn_on: sensor_power
      - delay: ${sensor_warmup}
      - component.update: water_resistance
      - sensor.template.publish:
          id: raindrop_coverage_sensor
          state: !lambda |-
            return std::max(0.0, std::min(100.0, 100.0 - id(water_resistance).state));
      - lambda: |-
          bool rain_state = !digitalRead(${pin_rs_digital});
          id(rain_sensor).publish_state(rain_state);
      - delay: ${sensor_cooldown}
      - output.turn_off: sensor_power
      

# This Pin provides power to the sensor. We use one of the pins so that
# we can power on the sensor right before reading data and power back off.
# This reduces power consumption, but more importantly, being exposed to
# water, it reduces the side effects of sensor's physical corrosion.
output:
  - platform: gpio
    pin: ${pin_power}
    id: sensor_power

1 Like

Hi,

Thanks for sharing this. Can you share the wiring diagram of this sensor with esp board? Its really a good idea to use a GPIO switch to turn on the sensor and turning off after collecting the data, it is also good for the sensor. However how do you initiate the sensor power on? Are you doing this every few minutes or using any weather based integration and turn it only on if the status cloudy? Can you please share any additional logic that you use (if any)?

1 Like