My first ESPhome project. Soil moisture sensor,

Hello everyone,
I would like to introduce my latest project to you. Nothing earth-shattering, but for me the first project I realized with ESPhome.

It is a ground humidity sensor based on a Wemos D1 mini, a small OLED display with SSD1306 chip and of course a capacitive sensor.

The sensor is working so far and could be used in this way, but I noticed a few small things which I would like to discuss with you.

Here is the YAML code:

# ESPhome Soil Moisture Sensor v 0.1
esphome:
  name: soilsensor
  platform: ESP8266
  board: d1_mini

# WIFI Credentials
wifi:
  ssid: "MY WIFI"
  password: "password"

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

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "password"

ota:
  password: "password"

# Deep Sleep Feature. Needs adjustments...
deep_sleep:
  run_duration: 70s
  sleep_duration: 120min
  
i2c:
  sda: D2
  scl: D1
  scan: True
  
sensor:
  platform: adc
  pin: A0
  id: moisture_sensor
  name: "Soil Moisture"
  unit_of_measurement: "%"
  update_interval: 10s
  filters:
# Sensor dry around 0.825 Volt, sensor wet around 0.425 Volt. Volts are were flickering
      - calibrate_linear:
          - 0.825 -> 0.0
          - 0.425 -> 100.0
          
display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    reset_pin: D0
    address: 0x3C
# Display output needs adjustments
    lambda: |-
      it.printf(0, 10, id(my_font), "Feuchtigkeit");
      it.printf(0, 32, id(my_font), "liegt bei:");
      it.printf(0, 54, id(my_font), "%.1f %%", id(moisture_sensor).state);
      
font:
  - file: "arial.ttf"
    id: my_font
    size: 20

I noticed the following:

  1. the values of the sensor are not very stable and fluctuate a bit. Not too bad, but it will get better. Since I want to do the whole thing battery powered anyway and therefore have to use Deep Sleep, I thought of the following. After waking up, the values are determined several times within one minute and then simply calculate an average value for this measurement.
  2. i had already written it, battery operation and deep sleep should also be included. I played a bit with the possibilities and the sensor goes to sleep after 70 seconds, but it doesn’t wake up properly. After the Deep Sleep phase I can see what’s happening by the power consumption. However, the power consumption is always about half as much as before the first deep sleep phase. Wifi can not be connected either.
  3. is there a possibility to display the battery status of the sensor? I would like to realize this with a battery shield for the Wemos D1 mini. The A0 pin of the Wemos is already occupied by the humidity sensor…
  4. in general I would like to interrupt the deep sleep somehow. That would make maintenance a bit easier.
  5. I’m also not quite satisfied with the output on the display. There is certainly room for improvement.

So far my thoughts first. What are your thoughts about this project? What would you change in the code and why?

Maybe you have some ideas how to realize my wishes?

I am happy about your suggestions and comments.

Thanks a lot

JAD

3 Likes

I would move to an esp32 as it has

  1. more adc pins
  2. Better deep sleep options (eg the ability to wake up when a gpio is activated by a pushbutton).

Also

  1. There are a number of filters built into esphome, you can filter the variable input.
  2. The dispay stuff is very subjective. You’ll have to play around with fonts etc.
1 Like

Hi,

thanks for your comment.

To the hardware choice: I chose the Wemos D1 mini (ESP8266) because I still had a few of them here and they were sufficient for what I had in mind.

Regarding your comments on the software:
I already tried a little bit with the filters but I didn’t get any result that would have fit. Hence my question in the forum if anyone had a tip.

Concerning the display I have to agree with you - it’s all quite subjective. But there must be a way to make a line break more elegant, right?

As I said, would be very grateful for helpful suggestions.

Greetings

JAD

Perhaps take a pic and we can see what you mean.

Well you didn’t say that, so you should perhaps tell us what you tried.

If you want two analogue inputs, it is insufficient :slight_smile:

Very basic question: is it possible to connect more than one of those soil moisture sensors to one esp32?

Very basic answer: yes.

1 Like

Thank you very much for sharing your project here!
It was working on my system 1:1. Great job!

Did you in the mean time find a way to fix the deep sleep problem?

Answer to your second point: i had the same issue. i solved it by bridging RST and D0 like on this page.

now the restore from deepsleep is succesfull.

I was told to use mqtt rather than api but when I do so it does not work…

Here is my code:

# ESPhome Soil Moisture Sensor v 0.1
esphome:
  name: garden-moisture-sensor
  platform: ESP8266
  board: d1_mini

# WIFI Credentials
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

# Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Soilsensor Fallback Hotspot"
    password: !secret wifi_password

captive_portal:

# Enable logging
logger:

mqtt:
  broker: 192.168.x.xxx
  port: xxxx
  username: xxxx
  password: xxxx
  birth_message:
  will_message:
  on_message:
    - topic: /bedford/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: bedford/sleep_mode
      payload: 'ON'
      then:
        - deep_sleep.enter: deep_sleep_1

ota:
  password: "xxxxxxx"

# Deep Sleep Feature. Needs adjustments...
deep_sleep:
  id: deep_sleep_1
  run_duration: 10s
  sleep_duration: 60min

sensor:
  - platform: adc
    pin: A0
    name: "Soil Moisture"
    update_interval: 600s
    unit_of_measurement: "%"
    filters:
    - median:
        window_size: 7
        send_every: 4
        send_first_at: 1
    - calibrate_linear:
        - 0.85 -> 0.00
        - 0.44 -> 100.00
    - lambda: |
        if (x < 0) return 0; 
        else if (x > 100) return 100;
        else return (x);

Automation.yaml:

- alias: bedford sleep after mqtt receipt
  trigger:
    - platform: mqtt
      topic: bedford/sensor/bedford_pres/state
  action:
    - data:
        payload: "ON"
        topic: bedford/sleep_mode
      service: mqtt.publish
1 Like

You need to manually create the sensors in home assistant since you are not using the API. You can’t use the api and deep sleep

sensor:
  - platform: mqtt
    name: 'Soil Moisture'
    state_topic: "garden-moisture-sensor/sensor/voltage/soil_moisture"
    unit_of_measurement: '%'
    device_class: 'wet'
    qos: 2
    payload_available: "online"
    payload_not_available: "offline"

No, esphome supports mqtt discovery, so there should be no need to configure anything in hs.

I got it to work, anyone monitoring the battery? I read in order to read the battery i need pin a0 but it’s used for the moisture sensor.

Anyone knows how to get the sleep state from the d1 mini? I can control the sleep/ota via MQTT but how can I get the current state (ON or OFF) of the sleep mode?

Like I want to create a button that shows ON when sleep mode is ON and OFF when it’s OFF.

When the device is asleep you can’t get any info out of it.

But can i get the last state of deep sleep before it goes to sleep and send it to hassio?

By state i mean if it’s enabled or disabled.

So I figured it out finally, got my Lovelace button with everything i wanted…

Hey, I have the same problem at the moment. Could you update your current code and how you integrated it so that it I can see the state for the button.
Did you get the battery monitoring working? Thanks in advance!

Hey JAD…finally reading this post (two years later…haha). I’m putting together a soil moisture sensor using a Capacitive Soil Moisture Sensor v1.2 and an ESP32. I’d also like to be able to monitor the battery. I can get the sensor to deliver raw data to serial monitor, but the actual coding has me stumped. Any recommendations/suggestions? I’ve read your code but I’m not sure what’s needed/not needed with the different hardware.

Thanking you in advance for any help.

s