ESP8266 Soil Moisture Sensor to Domoticz




Introduction: ESP8266 Soil Moisture Sensor to Domoticz

About: Just a simple dude that likes IOT and related stuff

The ESP8266 is a great little device. Nowadays we can see it implemented on all sorts of projects, creating great gadgets and doing unbelievable things. I wanted to start developing some projects using this great device.

I'm currently working on area of systems integration with closed sourced solutions, but this gave me motivation to start developing some solutions that in the future maybe i can integrate in my daily basis work. But for now i'm going to start creating a network of sensors at my house. I like to see the flow of data and how i can relate several informations from different sensors.

So, for this project I'm going to implement the ESP8266 with a Soil Moisture sensor that is collecting data from my garden and send it to a Domoticz Server.

This Sensor will give me the information whether the plant needs watering, or if everything is alright.

Step 1: Step 1: List of Materials

For this project this is the materials i have used:

  • ESP8266 board with analog input - I like a lot the Wemos D1 mini, you can get it from Aliexpress for 3.61€, from the official store;
  • Soil Moisture Sensor - I have used a Funduino, that is similar to this. You can get it for 0.60€;
  • 1 Electrolytic Capacitor, 470uF.
  • Small / Medium Size Breadboard - Around 1€ here, but you can find it even cheaper;
  • Some Jumper Cables.
  • Computer with arduino IDE.
  • A server or a Raspberry PI Running Domoticz

For This project you will need:
Basic knowledge of electronics, know how to work with digital signals, analog signals;
How to make breadboard connections;

Basic programming skills.

The Wemos D1 mini is a great little board based on the ESP8266 - 12EX, that has a lot of GPIO pins. It provides as well a reset button, and the CH340 USB to Serial Chip.
With this we only need to connect the microUSB cable beetween the Wemos and the computer and you are good to go!

The Soil Moisture Sensor is a simple gadget that works on reading the voltage in the probes. The higher the humidity of the soil, the less resistance to eletrical flow it offers , generating a bigger Voltage signal.Here is a simple explanation from Sparkfun, let's thank that team as well:

"(...)Soil Moisture Sensor is a simple breakout for measuring the moisture in soil and similar materials. The soil moisture sensor is pretty straight forward to use. The two large exposed pads function as probes for the sensor, together acting as a variable resistor. The more water that is in the soil means the better the conductivity between the pads will be and will result in a lower resistance, and a higher SIG out." From Sparkfun.

Step 2: Step 2: Let's Start Programming!

After we get those materials we can start programming, but first things first.

So lets get started!
In your Domoticz server, access Hardware in the Setup menu. Now we see a list of current created devices.
Our new sensor will not be directly connected to Domoticz, it will be a remote sensor sending data to a Virtual device on the server for that purpose.

On this page name a new Hardware. I have named mine "Outside Sensors" being a dummy device type.

After this was added, the new hardware will show on the list and appear the "Create virtual sensors" button.

Let's click this and select the type of device we want. For this project i have used a humidity sensor type and named it "Garden Sensor".
After clicking OK, go to Setup -> Devices. The new sensor should appear on that list just like mine did.

Notice the IDX number. This the device number we want to send data from the ESP8266 to the Domoticz Server.

Now that you have just Domoticz configured and got the board, don't be over-excited. First on the
Arduino IDE we must install the libraries and the drivers to work with the ESP8266 based Models and Wemos D1.

So after starting the arduino IDE we must install these board libraries. To do so, do the following:

Go to File -> Preferences;

On the Additional Boards Manager URLs field enter: "" (without commas)

Press OK and restart the Arduino IDE.

Now if we start a new project, go to tools, we can select the ESP8266 Boards!

Next, copy and paste the code from the Arduino file, or open it using the Arduino IDE.

After that, you must match you SSID, password and Domoticz Server. Notice there is as well a IDX number, which is the virtual dummy sensor ID created in Domoticz.You must correspond this to your Sensor IDX from Domoticz.

Step 3: Step 3: Exploring the Program

Let's explore the code!

Most of the code is self explanatory, but here is some information i think it is useful.

For this project i wanted the device to act accordingly to some specifications. First, i want it to be powered by batteries, so as a non interruptible power supply, it is necessary for the device to be in its lowest consumption. This will allow my device to be placed anywhere in my garden, as long as it gets WiFi signal, and to be powered for some weeks.

In order to achieve this requirement, the ESP has a great little function called deep sleep.

Deep Sleep allows the ESP to enter a ultra low power mode where almost all of its internal circuits are shut down. Only an internal timer and a watchdog are fed, that will make the device reboot when the counter ends.

This counter is the time we want the device to "sleep" for. In this project, i wanted my device to sleep for 15 minutes. After that, it restarts, takes out some readings, sends the data to the server and sleeps again.

I have chosen 15 minutes, because i don't feel like humidity is a variable that changes in such a short time frame. Probably i could configure it for 30 minutes and still achieve very good results.

Another function i wanted to implement is to power the Soil Moisture sensor only when the ESP8266 is on. Altough it's a low power device, i wanted to save energy for now whenever i can.

To accomplish this, the VCC of the Soil moisture sensor is connected to the Digital Pin 8 of the Wemos D1 Mini. Since this device can be fed with 5v or 3.3v it gets enough power for the digital pins of the board. I guess it requires a few milliAmps as well to run.

With this we can accomplish some sort of "Powergating" which is a technique used to power on/off certain parts of digital electronics circuits.

Step 4: Step 4: Let's Make the Connections and Power It On!

Now that we understand how the code works and how it sends data to Domoticz let's make the connections in order to finish this project.

As we can see by the image, the schematic is quite simple. only three components necessary..

Before powering up the Wemos D1 Mini (ESP8266 Board) start by connecting the Soil Moisture pins to the Wemos D1 Mini.

Here is the list of connections:

Soil Moisture Sensor -----> Wemos D1 Mini

Sig Pin ------> Analog 0 (A0)

VCC ------> Digital Pin 8(D8)

GND ------> GND of the Circuit (Wemos D1 Mini or Power source)

Now We can connect the Power to the Wemos D1.

In this Project i was feeding the circuit using 4x1.2v NIMH batteries size AA. Since it has an onboard regulator that makes the conversion from 5v to 3.3v to feed the ESP8266 chip, we're fine.

So, connect 5v to (+) of the battery and GND to (-).

As the ESP8266 can be a power hungry device, specially when it is booting and registering in the WIFI, it is necessary to use a capacitor, 470uF next to the power source. This will allow for that extra juice when the ESP is rebooting. The Capacitor will store energy and allow a faster discharge rate than the batteries. If you skip this step, when your batteries voltage drops a bit, you can experience consecutive reboots in a loop state.

Now that everything is connected, the onboard led should blink a few times, letting us know that it is connecting to the network.

Finally, put the sensor in the vase or garden you want to monitor the humidity and it will start populating the Domoticz Sensor Database with some values. Go to your Domoticz server webpage, enter the Temperature menu and see the new sensor acquiring new data.

We're done for now! I hope you enjoyed this as much as i had doing this great little experiment!


Step 5: Step 5: Future Implementations

A bit more just to get excited about...

At the moment with Deep Sleep and some sort of Power Gating, the power consumption is quite low...but it still can be better.

The Wemos D1 Mini board has integrated some circuits like the CH340 USB to Serial Chip and the Voltage regulator onboard. When the ESP is on deep sleep, this components are still active. A future implementation i want to do in this project is use a bare ESP-12 or a ESP07 from the ESP8266 family. This way i can cut the power a bit more.

Other implementations are the usage of a single Li-ion 3.7V, or 3.2V LifePo4 battery that is tolerable by the ESP. This kind of batteries can maintain its voltage for a longer time and provide enough current for a good time to the board.

A further project would be making this solar powered and recharge its battery whenever it is necessary. But all in its time.

Thanks for reading!

First Time Authors Contest 2016

Participated in the
First Time Authors Contest 2016

Be the First to Share


    • Electronics Contest

      Electronics Contest
    • Science Fair Challenge

      Science Fair Challenge
    • Stick It Challenge

      Stick It Challenge



    1 day ago on Step 2

    Wondering how long does this last until you need to recharge the batteries?


    5 months ago

    Hello David,

    For you all credits. Nice project.
    I must say after some day,s looking for a working example espxxx to domoticz yours found working.
    The most of the posts are stopped after proving that it works with a test.
    But yours is working with real sensor and real connect to Domoticz.
    This is very helpfull for me ( not a programmer skilled kind of guy) so I can puzzle with this working script to have my sensors on Domoticz.
    As I use a esp8266 F DEVKIT V4 ( it works) but waking-up op from deep sleep it crashes. The solution of placing a jumper between rst and D0 didn,t work for me.
    So I commented // out the deep sleep routine.
    Now it remains running.
    Again good work it is of much help for me.
    Kind Regards,


    1 year ago

    I have the following problem.
    I use a D1 mini, with the source code attached here. A soil humidity sensor is connected to it. I followed the tutorial here. The problem is that domoticz does not receive the information. Do you have any idea where the problem is?

    the only change to the code is that I don't use the sleep function.



    Reply 1 year ago

    Hi florin.ssk, Can you send your code so i can take a look?
    Best Regards


    Question 1 year ago

    It works, but i had some trouble with the chipset for which the right driver needed to be download. After the cycle it stays in sleep mode. Remember to indeed connect D0 and the RST pin (after you uploaded the code).

    One question remains, because i don't see anything back in Domoticz... Any idea what i did wrong?


    2 years ago

    Nice work, but it needs a jumper from rst to D0 (gpio16) to wake up from sleep mode.


    3 years ago on Step 5

    Works fine but please remove the Capacitor, it will empty your battery unnecessarily


    4 years ago

    Great Instructable :)

    I got it all working fine, however I have one small problem and I think it probably down to the sensor I am using, when the sensor is completely dry (i.e. in air) I get a reading "Wet 238%) when the sensor is fully submerged in a glass of water I get a reading of "Wet 91%) - Any ideas where I have gone wrong? Thanks.


    Reply 4 years ago

    Hi ed.darby,
    Check the following code line:
    humidity = map(values_avg,0,400,0,100);
    what sensor are you using? as a microcontroller you are using a wemos d1 mini?
    you can try to comment that line and do:
    humidity = values_avg;
    This will show you the adc value in raw - from 0 to 1024, so you can adjust the values on the map function.
    Let me know how it behaves.

    Best Regards.


    5 years ago

    with regard to your future plans, I have a bare esp8266-12 on a BOB that I feed from a 3.7 lipocell. Charge that with a solar panel. First had it on an 8266-01 worked well. does well on the 8266-12 as well.

    using a cheap 6V panel and a TP4056 board. all peanuts at aliexpress

    first version I did with an AP2112 stabilizer second one with RT9013


    5 years ago

    Switching the power to the sensor off, doesn't only save batteries but also saves your sensor from electrolytic degeneration. Wise decision

    DIY Hacks and How Tos

    This looks like a good idea. I could really use one of these to help take care of my indoor plants.