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!