ESP32 WiFi SOIL MOISTURE SENSOR

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:

  1. Soil Moisture (My code will calculate the gravimetric soil water content)
  2. Air temperature and relative humidity

Supplies:

  1. Wemos® Higrow ESP32 WiFi + bluetooth Battery + DHT11 Soil Temperature And Humidity Sensor Module
  2. For data logging (optianal)

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) .

  1. Download and Install the Arduino IDE
  2. In the Preferences menu, add https://dl.espressif.com/dl/package_esp32_index.json to the Aditional Boards Manager URL.
  3. Install the ESP32 Libraries from Tools>Board>Boards Manager. Search for esp32 by espressif systems
  4. Select your Board in the menu: Tools>Board>"WeMos" WiFi & Bluetooth Battery
  5. Install the required DHT11 Libraries from Tools>Manage Libraries...
    1. You need to install DHT sensor library by Adafruit Version 1.3.7 (Or later?)
    2. 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

  1. Download the applicable arduino sketch from GitHub and open it.
    1. Esp32_SoilMoisture_WebServer.ino should be use if no micro sd card module is attached.
    2. 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.
    3. 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.
  2. Edit your Router SSID and Password in the sketch.
  3. Compile the sketch while pressing the boot button.
  4. If compiling is successful, press the EN button and immediately start the Arduino Serial Monitor (115200 Baud Rate)
  5. Wait for the Blue LED to go on and OFF
  6. Get the IP address printed in the serial monitor, enter it in your browser. You will now see the sensor data web page.
  7. If you added the micro sd card reader, and you compiled one of the applicable arduino sketches, you can access your data in /datalog.txt of your micro sd card.

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.

  1. Weight you dry soil
  2. 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.
  3. 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.
  4. Go on with this process as far as you like, or until adding water does not influence the sensor reading anymore.
  5. My results is in the attached excel sheet. GWC is calculated as gwc=exp(-0.0015*SensorValue + 0.7072)

Step 5: Future Developments

  1. 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
  2. Add push buttons to start and stop the WiFi and web server to save battery usage.
  3. Ad a GPRS module and disable WiFi. This will save power.

1 Person Made This Project!

Recommendations

  • Battery Powered Contest

    Battery Powered Contest
  • Plywood Challenge

    Plywood Challenge
  • Plastic Contest

    Plastic Contest

17 Discussions

0
saleh.sudan1991
saleh.sudan1991

7 months 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);
}

0
Iansz
Iansz

7 months ago

Hello, How about enabling Deep Sleep to save power and instead of the web server just have MQTT?

0
JesusF46
JesusF46

10 months 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?

gravimetric.jpg
0
J4e8a16n
J4e8a16n

Question 1 year 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

0
JJ Slabbert
JJ Slabbert

Answer 1 year ago

I could upload the sketch from windows 7 and 8.

0
JJ Slabbert
JJ Slabbert

Reply 1 year 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

0
J4e8a16n
J4e8a16n

Reply 1 year 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.


0
JJ Slabbert
JJ Slabbert

Reply 1 year ago

1) Could you install the blink sketch on the ESP32 higrow soil sensor?
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:

https://dl.espressif.com/dl/package_esp32_index.json
4) Install the ESP32 Libraries from Tools>Board>Boards Manager. Search for esp32 by espressif systems.
This resource may help: https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/

0
J4e8a16n
J4e8a16n

Reply 1 year ago

esp32 by espressif systems : It does not appear in the list. Maybe it will appear later....

0
J4e8a16n
J4e8a16n

1 year 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.

git.png
0
J4e8a16n
J4e8a16n

Question 1 year 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"

0
ViktorJ
ViktorJ

Question 1 year 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.

0
JJ Slabbert
JJ Slabbert

Answer 1 year ago

Is it possible to load any other ESP32 sketch like blink?

0
Diver19
Diver19

Question 1 year ago on Introduction

Hello, can I ask about modifying this for use on NB-IoT networks?

0
JJ Slabbert
JJ Slabbert

Answer 1 year 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.