Wi-fi Soil Moisture Sensor with ESPHome

  Переглядів 12,167

Tech Dregs

Tech Dregs

День тому

A quick and easy solution to monitoring plant soil moisture. Integrates into Home Assistant with ESPHome.
Board: Seeed Xiao ESP32-C3, I use a capacitive moisture sensor and 1200mAh battery available on Amazon.
YAML (pay attention to the greater than/less than notes below):
esphome:
name: wifi-moisture-sensor
friendly_name: WiFi Moisture Sensor
on_boot:
then:
- switch.turn_on: sensor_power
on_shutdown:
then:
- switch.turn_off: sensor_power
esp32:
board: esp32-c3-devkitm-1
framework:
type: arduino
Enable logging
logger:
Enable Home Assistant API
api:
encryption:
key: "your_key"
ota:
password: "your_pwd"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: on
power_save_mode: LIGHT
manual_ip:
static_ip: your.ip
gateway: your.gateway
subnet: your.subnet
dns1: your.dns
Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "your_fallback"
password: "fallback_pwd"
captive_portal:
sensor:
- platform: adc
pin: 3
name: "Soil Moisture"
unit_of_measurement: "%"
device_class: MOISTURE
update_interval: 2s
attenuation: 11db #required
filters:
- calibrate_linear: #set your own values here
- 1.45 -&gt 100.00 #replace "&gt" with greater than symbol (YT doesn't allow angled brackets)
- 2.821 -&gt 0.00
- lambda: |
if (x &lt 0) return 0; #replace "&lt" with less than symbol (YT doesn't allow angled brackets)
else if (x &gt 100) return 100;
else return (x);
accuracy_decimals: 0
- platform: adc
pin: 4
allow_other_uses: True
name: "Battery Life"
unit_of_measurement: "%"
device_class: BATTERY
update_interval: 2s
attenuation: 11db #required
filters:
- multiply: 2.0
- calibrate_polynomial: #set your own values here
degree: 4
datapoints:
Map 0.0 (from sensor) to 0.0 (true value)
- 3.4 -&gt 0.0
- 3.6 -&gt 8.43
- 3.81 -&gt 50.06
- 3.86 -&gt 57.63
- 3.92 -&gt 72.56
- 3.96 -&gt 90.96
- 4.05 -&gt 100
- lambda: |
if (x &lt 0) return 0;
else if (x &gt 100) return 100;
else return (x);
- platform: adc
pin: 4
allow_other_uses: True
name: "Battery Voltage"
update_interval: 2s
attenuation: 11db #required
filters:
- multiply: 2.0
- platform: wifi_signal
name: "WiFi Signal Sensor"
update_interval: 2s
switch:
- platform: gpio
pin: GPIO10
id: sensor_power
internal: true
deep_sleep:
run_duration: 10s
sleep_duration: '5:59:50'
=== Timestamps ===
0:00 Introduction
0:44 Commercial Products... Fail
1:10 DIY Solution
3:54 YAML Configuration Overview
7:25 Conclusion

КОМЕНТАРІ: 66
@MattOmondPrintbotOMO
@MattOmondPrintbotOMO 3 місяці тому
Thank you so much for explaining that. I had everything working except that. Great project. Looking forward to making a few.
@UltraEd14
@UltraEd14 10 місяців тому
Amazing work! I'm making my own and this saved me a lot of hassle
@TechDregs
@TechDregs 10 місяців тому
Glad it helped.
@havemannolaf
@havemannolaf 5 місяців тому
Thank you. i wil do it to. 😀
@LorenzKort
@LorenzKort 8 місяців тому
Can you share the 3d printed design? I'd like to make my own :)
@l0gic23
@l0gic23 10 місяців тому
Pretty cool. +1 sub
@tbandras
@tbandras 4 дні тому
Thanks a lot for sharing your project! A quick question: the Xiao C3 is rated up until 3.6V whilst the battery is 3.7V (and I assume the voltage goes even higher, maybe lower 4.x when fully charged). Have you experienced any issues with it, should I be considered if I were to copy your setup with a 3.7V LiPo battery?
@TechDregs
@TechDregs 4 дні тому
Not sure where you're getting the 3.6V from, so IDK what to reference there. The Xiao is made for 3.7V LiPo batteries. The higher charged state voltages (~4.2V) are no problem; that's just how LiPo batteries are.
@IF-VR123
@IF-VR123 10 місяців тому
Thanks, this is great and exactly what I want to create. Would you be willing to share the design for the case and a link to the battery you used (for sizing)?
@TechDregs
@TechDregs 10 місяців тому
I generally no longer share STLs for designs, but one thing that's helpful is that you can usually get STEP files for the SEEED Xiaos either directly from them or from grabcad. I wouldn't get this battery again, so I won't link it. I wasn't paying attention and ordered a 3-wire battery, and the third wire isn't useful (I think it's a thermistor in the battery for temp monitoring).
@lflondonol
@lflondonol 3 місяці тому
This is my very first ESP32 project and I am trying to understand how to connect a 18650 battery to the board. How did you make the connection? Are you connecting it to the 5v pin?
@TechDregs
@TechDregs 3 місяці тому
If you're using a Xiao, the battery connections are on the underside of the board. There are two pads there than you solder the battery connections to.
@Marco1010101
@Marco1010101 2 місяці тому
Ok, my sensor is working, it reads 30 second and after 3h in deep sleep. But I have 2 questions. 1. It seems that the Voltage reading is not very accurate. I have calibrate the value Many times but after some reboot it reads 0.2 0.3 V more or less than the real battery value. I have used two resistor 220k in parallel. 2. How can I do to allowr home assistant to show in the dashboard the last value read when the esp32 is in deep sleep. Thanks in advance for any help.
@TechDregs
@TechDregs 2 місяці тому
For #2, that should just show up automatically over time. For #1, battery voltage in my experience can fluctuate a bit, especially when it's above 4.0V. It seems to settle down and get smoother below that.
@SuperMrZerox
@SuperMrZerox 9 місяців тому
can you send the link of soil sensor and where did you get it connected. with my sensor only stupid values
@TechDregs
@TechDregs 9 місяців тому
Did you calibrate your sensor values? You need to test what voltage it sends in open air and fully submerged in water. I used these: www.amazon.com/Capacitive-Moisture-Corrosion-Resistant-Detection/dp/B07SYBSHGX
@MattOmondPrintbotOMO
@MattOmondPrintbotOMO 4 місяці тому
Can you charge the battery via the Xiao or do you need to de solder and charge via a charger. Thanks/
@TechDregs
@TechDregs 4 місяці тому
If you plug the Xiao into any USB port/charger, it will charge the battery. No need to desolder.
@SuperMrZerox
@SuperMrZerox 9 місяців тому
Can Share all ? the project is amazing. I've been looking for something like this for a long time and now I'm grateful to you for showing it. can you show the wiring properly? Thx
@TechDregs
@TechDregs 9 місяців тому
There's actually very little wiring. The battery is connected to the battery pads on the bottom of the board. I've got a voltage divider on the positive lead, with one side to ground and the other to an analog pin. The sensor board only has three wires: power in, ground, and sensor out. The sensor out just needs to go to an analog pin. The YAML is in the description; so you can see which pins I used there for the sensor and battery voltage readings. One trick that I have done since this video is powering the sensor from a digital GPIO pin, which I enable when the device wakes up and disable when it goes to sleep. That has a huge impact on battery life, as this sensor draws about 5mA when it's on.
@enriqueazvz
@enriqueazvz 5 місяців тому
@@TechDregs great stuff. Would you have the YAML that includes the GPIO pin as the 'smart' power source for the sensor?
@TechDregs
@TechDregs 5 місяців тому
I updated the details with the code. Basically, you just add onboot and onshutdown commands, and add the pin below as a switch. Make sure to change the GPIO to the pin you're using for power to the sensor. I also added a wifi signal sensor, since that can be useful to see.
@enriqueazvz
@enriqueazvz 3 місяці тому
thanks@@TechDregs , I was implementing it on my side and apparently HA after 2023 requires the use of 'allow_other_users' if the same pin is used for several uses (battery life and battery voltage) I replaced pin: 4 for 'pin: number: 4 allow_other_uses: True'
@enriqueazvz
@enriqueazvz 3 місяці тому
What code did you use to calibrate the moisture sensor? (so I can get my min and max values)
@jebradleymd
@jebradleymd Місяць тому
I pasted your yaml code into the code for an ESP-D1 on Home Assistant and did a search and replace of the &gt and &lt markers as directed. When I run the "validate" on the code, I get the following error message: INFO ESPHome 2024.3.1 INFO Reading configuration /config/esphome/d1-1.yaml... ERROR Error while reading config: Invalid YAML syntax: while scanning a simple key in "/config/esphome/d1-1.yaml", line 61, column 9 could not find expected ':' in "/config/esphome/d1-1.yaml", line 62, column 9 I do not see anything where I would think a ":" would be needed.
@TechDregs
@TechDregs Місяць тому
I'm not sure. It could be a spacing issue in your YAML or it could be a change to ESPHome that has occurred since I did this project. Lines 61/62 are in the WIFI setup for me, and I don't see any issues there, so I'm guessing it's in the lines above that somewhere. Just check all the indentation. I haven't updated mine in a while, but next time I do I'll update the code in the details.
@timderks5960
@timderks5960 День тому
Is there any update on this project? How is the battery life? How accurate are your readings? I'd be very interested in a follow up
@TechDregs
@TechDregs День тому
I can't speak to accuracy, as I have no other soil moisture measurement devices that use a different type of sensor. But since I reconfigured the sensor to draw power via GPIO that turns it off when in deep sleep, battery life is over four months on a charge. I did something like a followup in these videos: ukposts.info/have/v-deo/bHWLjY-HpIefq40.html ukposts.info/have/v-deo/kaSllaudcGpykWg.html
@timderks5960
@timderks5960 День тому
@@TechDregs Interesting videos, thanks!
@kherhitesh
@kherhitesh 4 місяці тому
nice video sir, i do as well as your code , but after update ESPHome platform - adc duplicate error show , so what to do any other way?
@TechDregs
@TechDregs 4 місяці тому
Looks like mine died this week (I think water got into it), so I can't test this, but try replacing: pin: 4 with: pin: number: 4 inverted: false allow_other_uses: true In both places that it appears. See if that works. They implemented a change recently to break using a pin multiple times unless you specifically allow it.
@kherhitesh
@kherhitesh 2 місяці тому
@@TechDregs Thank you , New ESPhome update as well as working fine pin multiple
@DivinaFarms_James
@DivinaFarms_James 2 місяці тому
How well did the capacitive sensor hold up?
@TechDregs
@TechDregs 2 місяці тому
It's still going. I haven't looked at it closely in a while... it's just out there doing its job.
@Marco1010101
@Marco1010101 2 місяці тому
can you show me what kind of resin did you have use (black/gray)?
@TechDregs
@TechDregs 2 місяці тому
It's just cheap basic epoxy from a local hardware store.
@Marco1010101
@Marco1010101 3 місяці тому
Hi, great job, what is the battery duration with that configuration?
@TechDregs
@TechDregs 3 місяці тому
With the GPIO power setup, at least 4 months on a charge.
@Marco1010101
@Marco1010101 2 місяці тому
Hello, I'm trying to make my yaml configuration, but I got this error: Pin 0 is used in multiple places. Could you help me? thanks in advance Failed config sensor.adc: [source :55] Pin 0 is used in multiple places. platform: adc pin: number: 0 mode: input: True output: False open_drain: False pullup: False pulldown: False inverted: False ignore_strapping_warning: False drive_strength: 20.0 name: Battery Life unit_of_measurement: % device_class: battery update_interval: 2s attenuation: 11db filters: - multiply: 2.0 - calibrate_polynomial: degree: 4 datapoints: - from: 3.4 to: 0.0 - from: 3.6 to: 8.43 - from: 3.81 to: 50.06 - from: 3.86 to: 57.63 - from: 3.92 to: 72.56 - from: 3.96 to: 90.96 - from: 4.05 to: 100.0 - lambda: !lambda |- if (x > 0) return 0; else if (x > 100) return 100; else return (x); disabled_by_default: False force_update: False accuracy_decimals: 2 state_class: measurement raw: False sensor.adc: [source :80] Pin 0 is used in multiple places. platform: adc pin: number: 0 mode: input: True output: False open_drain: False pullup: False pulldown: False inverted: False ignore_strapping_warning: False drive_strength: 20.0 name: Battery Voltage update_interval: 2s attenuation: 11db filters: - multiply: 2.0 disabled_by_default: False force_update: False unit_of_measurement: V accuracy_decimals: 2 device_class: voltage state_class: measurement raw: False
@TechDregs
@TechDregs 2 місяці тому
You need to allow other uses. It's an update ESPHome has since I made this. Use it every time you setup a pin which is used more than once. pin: number: 0 allow_other_uses: True
@Marco1010101
@Marco1010101 2 місяці тому
@@TechDregs grazie
@dr.m.sanaullahsahar9859
@dr.m.sanaullahsahar9859 4 місяці тому
I can't see a link to your 3D printed parts. May i ask?
@TechDregs
@TechDregs 4 місяці тому
Sorry, I generally no longer upload 3D prints. Too many of my past designs ended up being sold on ebay and etsy.
@MattOmondPrintbotOMO
@MattOmondPrintbotOMO 3 місяці тому
Would you be able to explain the switch code here. switch: - platform: gpio pin: GPIO10 id: sensor_power internal: true Sorry a noob. Have you got a switch wired into pin 10 thanks for a great project
@TechDregs
@TechDregs 3 місяці тому
I'm using pin 10 to provide the power to the soil moisture sensor rather than wiring it directly to the output voltage pins on the ESP32 board. That allows me to turn power to the sensor on and off. This is the routine that sets up pin 10 as a power outlet that can be turned on and off (which ESPHome calls a switch). So, switch: - platform: GPIO tells ESPHome that this will be a binary on/off switch using a GPIO pin. ID: sensor_power is the internal name of that switch so I can refer to it elsewhere (like the boot and shutdown sequences). Internal: true means that it isn't exposed externally, so the switch won't show up in Home Assistant.
@MattOmondPrintbotOMO
@MattOmondPrintbotOMO 3 місяці тому
Sorry just reading this again and doing a little Google I was un aware you could supply 3.5 volts from a gpio pin. Other than vin etc. am I misunderstanding. Probably 😢
@TechDregs
@TechDregs 3 місяці тому
Depends on the pin. Some pins are input only. But GPIO pins can output about 40mA of current at 3.3v. There is also a total power budget that you have to stay under, but if you're only doing one or two pins of output, that's not a concern.
@MattOmondPrintbotOMO
@MattOmondPrintbotOMO 3 місяці тому
Awesome thank you. Love to learn.
@MattOmondPrintbotOMO
@MattOmondPrintbotOMO 3 місяці тому
@@TechDregs Pleas ignore this if you wish. I know you are not a help board. But I am curious. I have it all working. But when it goes into deep sleep it becomes unavailable on the dashboard. I have read on a few forums that it is best to change over to mqtt and then you can use birth and will_message. But I did a quick try and havn't been able to do the switch to mqtt. I am wondering if you have the same issue.
@pemlody
@pemlody 6 місяців тому
How long it runs at battery?
@TechDregs
@TechDregs 6 місяців тому
Currently, it's been almost 4 months since I last charged it. It might last another month or two, looking at the voltage plots.
@lflondonol
@lflondonol 4 місяці тому
How did you get the voltages for the battery?
@TechDregs
@TechDregs 4 місяці тому
See ~2m in the video. Just taking a reading via a voltage divider to an analog pin on the Xiao.
@roldgold5972
@roldgold5972 5 днів тому
How do you think a solar charger would do?
@TechDregs
@TechDregs 4 дні тому
Already done. :) ukposts.info/have/v-deo/kaSllaudcGpykWg.html
@kherhitesh
@kherhitesh 2 місяці тому
how much battery life as well as your deep sleep time?
@TechDregs
@TechDregs 2 місяці тому
I wake mine up for 10s every 6hours (so, 4x a day). The battery I used lasts ~3.5months on a charge.
@kherhitesh
@kherhitesh 2 місяці тому
​@@TechDregs good battery backup 👍
@njain2686
@njain2686 10 місяців тому
How will you charge it?
@TechDregs
@TechDregs 10 місяців тому
I'll just pull it and charge it inside on a cell phone charger.
Піхотинці - про потребу у людях
00:57
Суспільне Новини
Переглядів 1 млн
😱СНЯЛ СУПЕР КОТА НА КАМЕРУ⁉
00:37
OMG DEN
Переглядів 1,8 млн
Nemo - The Code (LIVE) | Switzerland🇨🇭| Grand Final | Eurovision 2024
03:28
Eurovision Song Contest
Переглядів 14 млн
Preventing Plant Death With Technology
14:00
Emilostuff
Переглядів 122 тис.
Making an E-paper Dashboard with ESPHome
22:59
Tech Dregs
Переглядів 129 тис.
MEASUREMENT OF SOIL MOISTURE | iNELS Smart Agriculture
2:19
ELKO EP s.r.o.
Переглядів 19 тис.
Easy Solar Outdoor Weather Sensor with ESPHome and Home Assistant
15:53
How to calibrate METER soil moisture sensors
7:43
METER Group
Переглядів 7 тис.
I Made A More Affordable Smart Home Presence Sensor
11:42
Everything Smart Home
Переглядів 89 тис.
463 Why most Arduino Soil Moisture Sensors suck (incl. solution)
10:59
Andreas Spiess
Переглядів 60 тис.
Automatic Garden Watering System
15:28
Nikodem Bartnik
Переглядів 193 тис.
Water your Garden with Home Assistant 💦
10:32
HandyDadTV
Переглядів 15 тис.
APPLE УБИЛА ЕГО - iMac 27 5K
19:34
ЗЕ МАККЕРС
Переглядів 82 тис.
M4 iPad Pro Impressions: Well This is Awkward
12:51
Marques Brownlee
Переглядів 5 млн
📱 SAMSUNG, ЧТО С ЛИЦОМ? 🤡
0:46
Яблочный Маньяк
Переглядів 615 тис.