Introduction: ESP8266 Soil Moisture Sensor to Domoticz
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: "http://arduino.esp8266.com/stable/package_esp8266com_index.json" (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!