Introduction: ESP32 WiFi SOIL MOISTURE SENSOR
Cheap soil moisture sensors which send an electrical signal through soil to measure the resistance of the soil is all failing. Electrolysis makes this sensors of no practical use. See more about electrolysis here. The sensor used in this project is a capacitive sensor and no conductive metal is in touch with wet soil.
After loading the firmware (Arduino sketch), the sensor will create a web server on itself and log into your WiFi router. You can now access the web side on the ESP32 sensor. No data is send to the cloud.
Some familiarity with Arduino Programming, Soldering skills and HTML etc may assist. If you build this project, please upload photos via the upload link below. Enjoy the hacking.
This sensor can read:
- Soil Moisture (My code will calculate the gravimetric soil water content)
- Air temperature and relative humidity
Supplies
- Wemos® Higrow ESP32 WiFi + bluetooth Battery + DHT11 Soil Temperature And Humidity Sensor Module
- For data logging (optianal)
- a Micro SD Card Module.
- Bread Board and OR
- Soldering Iron, solder paste.
- Six Female to Male jumper cables.
Step 1: INSTALL AND CONFIGURE THE ARDUINO IDE ON YOUR PC/MAC
This setup procedure should work on a Windows PC, MAC and LINUX (x86) system. Unfortunately for Raspberry PI users the LINUX (ARM) Libraries for the ESP32 Board is not available yet (although there was real clever people who compiled them from source) .
- Download and Install the Arduino IDE
- In the Preferences menu, add https://dl.espressif.com/dl/package_esp32_index.json to the Aditional Boards Manager URL.
- Install the ESP32 Libraries from Tools>Board>Boards Manager. Search for esp32 by espressif systems
- Select your Board in the menu: Tools>Board>"WeMos" WiFi & Bluetooth Battery
- Install the required DHT11 Libraries from Tools>Manage Libraries...
- You need to install DHT sensor library by Adafruit Version 1.3.7 (Or later?)
- You need to install Adafruit Unified Sensor by Adafruit Version 1.0.3
Step 2: ATTACH MICRO SD CARD FOR LOGGING (OPTIONAL)
If you need to log your data, attach a Pololu Micro SD Card Module. Other SD cards may have different wiring and code.
Step 3: DOWNLOAD THE ARDUINO SKETCH
- Download the applicable arduino sketch from GitHub and open it.
- Esp32_SoilMoisture_WebServer.ino should be use if no micro sd card module is attached.
- Esp32_SoilMoisture_WebServer_DataLog.ino requires an micro sd card and continues internet access to the NTP server. This option has very accurate time, but use large amounts of current and may deplete the battery.
- Esp32_SoilMoisture_WebServer_DataLog_Int_RTC.ino requires an micro sd card and internet access to the NTP server after reset. It uses the internal RTC of the ESP32 to update the date/time received at reset from the NTP server. This is the most power efficient solution, but the time may not be as accurate.
Step 4: GRAVIMETRIC WATER CONTENT-CALIBRATION
How should I interpret the soil moisture reading from GPIO 32?
One method is to calculate the gravimetric water content of the soil. It is calculated as:
(Mass of Water in Soil Sample)/(Mass of Dry Soil in Sample)
I have collected dry soil out of my garden (It is Limpopo, South Africa, August month and the soil is dry, really dry). You can dry soil in a oven.
- Weight you dry soil
- pour the dry soil in a container, insert the sensor in the soil and take a raw sensor reading (use the web interface). Record the Water Mass (=0 at this stage) and the sensor reading.
- Remove the sensor, add 10 ml (gram) water, mix the soil and water properly and record your Water Mass (=10 at this stage) and the sensor value.
- Go on with this process as far as you like, or until adding water does not influence the sensor reading anymore.
- My results is in the attached excel sheet. GWC is calculated as gwc=exp(-0.0015*SensorValue + 0.7072)
Attachments
Step 5: Future Developments
- Attach a external RTC (Real Time Clock). Currently, NTP (Network Time Protocol) is used to get the time for data logging. This requires WiFi and is current intensive
- Add push buttons to start and stop the WiFi and web server to save battery usage.
- Ad a GPRS module and disable WiFi. This will save power.
20 Comments
Question 1 year ago
Can it work with raspberry pi?
2 years ago
Hey, has anyone came up with a good source of programming for this sensor and to work with a raspberry pi 4? Any help would be massively appreciated
Question 3 years ago on Step 3
Hi,
Thanks for the guide. I can't upload the sketch though, however I try. I keep getting timeout messages, while the module is plugged in and recognized on COM3.
I have tried in ON/OFF state, pressing the boot button as you wrote, but nothing. The blue LED keeps blinking while the switch is ON and a green LED is on all the time except I guess during boot, when there's a red LED. I have the item from Aliexpress (DIY more).
Upload settings:
Wemos(Wifi&Bluetooth battery)
Upload speed 115200
CPU 80MHz
This is what I keep getting.
esptool.py v2.6
Serial port COM3
Connecting....
Traceback (most recent call last):
File "esptool.py", line 2959, in
File "esptool.py", line 2952, in _main
File "esptool.py", line 2653, in main
File "esptool.py", line 460, in connect
File "esptool.py", line 440, in _connect_attempt
File "esptool.py", line 379, in sync
File "esptool.py", line 322, in command
File "esptool.py", line 285, in write
File "site-packages\serial\serialwin32.py", line 323, in write
serial.serialutil.SerialTimeoutException: Write timeout
Failed to execute script esptool
An error occurred while uploading the sketch
Any ideas? Cheers!
SOLUTION:
Check Windows device manager if the CP2104 driver is correctly installed - if not, then just choose update driver and windows will sort it out. Then the COM port becomes available and you can upload as described.
Answer 2 years ago
Hello i have the same device DIY MORE "DM ESP32 DHT11 WIFI Bluetooth Soil Temperature Humidity Sensor18650" (https://www.diymore.cc/products/esp32-wifi-bluetoo...).
I didn't succeed to use the embedded micro usb port.
But, instead, i used classical usb to TTL (e.g.: https://www.amazon.com/HiLetgo-CP2102-Converter-A...) converter.
To be able to upload you have to press "BOOT" and "EN" at the same time then first release "EN" then release "BOOT" and the sketch will upload properly.
I used the sketch from this page and it is working well.
Answer 3 years ago
Is it possible to load any other ESP32 sketch like blink?
3 years ago
What is the problem with this code ? I cannot upload the sensor data to Thingspeak but its working in the Arduino IDE
#include <WiFi.h>
#include <WiFiMulti.h>
String apiKey = "ALDIRBG8K88DMEER"; // Enter your Write API key from ThingSpeak
const char *ssid = "Redmi"; // replace with your wifi ssid and wpa2 key
const char *pass = "1234562850";
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
pinMode(34,INPUT);
}
void loop()
{
int moisture=analogRead(34);
if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr += "&Field1=";
postStr += String(moisture);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Moisture content in soil in Percentage:");
Serial.println((moisture/1024)*100);
}
client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
delay(2000);
}
3 years ago
Hello, How about enabling Deep Sleep to save power and instead of the web server just have MQTT?
3 years ago
good day
I already have the excel data but what data do I take and where do I put them in the Arduino code?
Question 3 years ago on Step 3
Is it a Unix only device?
There is no h files in the zip file Higrow-ESP32-WiFi-Soil-Moisture-Sensor-master.zip
-------------------------
Arduino : 1.8.10 (Windows 10), Carte : "Maple Mini, Original (17k RAM,108k Flash), 72MHz (Normal), Smallest (default)"
Plusieurs bibliothèque trouvées pour "SP
#include "FS.h"
Regards
JPD
Answer 3 years ago
I could upload the sketch from windows 7 and 8.
Reply 3 years ago
Here it is:
ESP-IDF Tools Installerfor Windows
https://dl.espressif.com/dl/esp-idf-tools-setup-2....
Reply 3 years ago
Yes, it would be nice. I went to a site where a group still try to make this work hellplessly.
https://hackaday.io/project/25253-higrow-plants-monitoring-sensor
Reply 3 years ago
What I ment was: “It was posable for me to use the arduino ide in windows 7 and 8 to upload the sketch to the ESP32.
Can you upload the blink app?
Did you add the additional boards manager url (step 1)?
Did you install the esp32 libraries?
Did you select the correct board?
Try to run the example in file>examples>examples for wemos wifi&bluetooth battery>SD(esp32). They use the FS.h library.
FS.h was supposed to be installed with the esp32 libraries.
FS.h should be in C:\Users\*****\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.2\libraries\FS\src on windows 8
You can download it from https://github.com/espressif/arduino-esp32/blob/master/libraries/FS/src/FS.h
Reply 3 years ago
1- Yes and many more.
2- I have UNO, MEGA 2506, DUE, pro mini, they all work
3- I tried but it does not appear in the arduino 15. Upload a zip :"the file does not contain a valid library" I dont know how to do it manually: C:/Users/.....Desktop/arduino-esp32-master/libraries/FS/src/FS.h
4- Yes
5- examples for wemos wifi&bluetooth battery does not exists
Thank you for your attention.
Reply 3 years ago
1) Could you install the blink sketch on the ESP32 higrow soil sensor?
4) Install the ESP32 Libraries from Tools>Board>Boards Manager. Search for esp32 by espressif systems.2) It seems like the your ESP32 libraries did not install. Make sure you go through all the steps.
3) You should have added the following line in the arduino ide in file>preferences:
This resource may help: https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/
Reply 3 years ago
esp32 by espressif systems : It does not appear in the list. Maybe it will appear later....
3 years ago
https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/windows.m
I made some progress. Althoug I could run the WIFI scan sketch. I am not sure I used the right card.
The only card pluged in is the one of highgrow capacitive probe.
After pressing on the first button (near the boost button) of the card The display whent crasy.
{
Light: I%M/⸮`<⸮-q.ii⸮\mt⸮a⸮55⸮[X⸮I2C Soil Moisture Sensor Address: 20
Sensor Firmware version: FF
}
So it is communicating although it is connected to the computer by a usb C cable.
Question 3 years ago on Step 3
Is it a Unix only device?
There is no h files in the zip file Higrow-ESP32-WiFi-Soil-Moisture-Sensor-master.zip
-------------------------
Arduino : 1.8.10 (Windows 10), Carte : "Maple Mini, Original (17k RAM,108k Flash), 72MHz (Normal), Smallest (default)"
Plusieurs bibliothèque trouvées pour "SP
#include "FS.h"
Question 3 years ago on Introduction
Hello, can I ask about modifying this for use on NB-IoT networks?
Answer 3 years ago
You are welcome to do it. I do not have any NB-IoT experience. What I do know is that it should be possible to connect a gprs module to the ESP32 and send data to the internet. I could not do it for know because I do not have a GPRS module and time . This is only my hobby and I do not do contract work. Adding a GPRS module (and disabling WiFi) may solve many of the power consumption problems the device currently have.
If you build this instructable, please upload photos. I will apreciate.