Automatic Irrigation System Using Arduino Uno and ESP8266 (ESP-01S) Wi-Fi Module

by ksnlebid in Circuits > Arduino

2680 Views, 1 Favorites, 0 Comments

Automatic Irrigation System Using Arduino Uno and ESP8266 (ESP-01S) Wi-Fi Module

photo_2022-08-20_15-20-57.jpg

Greetings! In this project you'll be able to see how to build an automatic irrigation system and create the Android application (using Java programming language), which allows you to control the system via ESP8266 (ESP-01S) Wi-Fi module and Arduino Uno board.

Supplies

photo_2022-08-20_15-03-40.jpg

Hardware:

  • Arduino Uno Board
  • Breadboard (I've used a breadboard with 830 tie points)
  • 3-6V Mini DC Water Pump
  • ~ 6mm Tubing
  • 4.7k Ohm Resistor
  • YL-69 Soil Moisture Sensor
  • Water Level Sensor (T1592)
  • DS18B20 Temperature Sensor
  • 5V Single-Channel Relay Module (I've had JQC-3FF-S-Z)
  • ESP8266 (ESP-01S) Wi-Fi Module
  • 6V Battery (I've used four 1.5V AA batteries)
  • Female/Female, Male/Male, Female/Male Jumper Wires
  • USB AM-BM Cable

Software:

  • Android Studio
  • Arduino IDE

Project Explanation

Through the created Android application, a user will have the opportunity to view the values ​​of soil moisture and water level sensors presented in percentages, as well as the temperature value obtained using the DS18B20 temperature sensor (by default, in degrees Celsius). In addition, the application allows you to change the behavior of the built system, namely: turn on/off automatic watering, change the intensity of watering (low, medium and high), and also choose the unit of temperature measurement (degrees Celsius or Fahrenheit).

Watering is directly controlled by the Arduino Uno board, which will automatically water the plant only if an automatic mode is selected in the application (which is active by default), there is enough water in the tank, and the soil is not wet enough. If the manual system control mode is selected, watering will be carried out manually by the user by pressing the appropriate button in the Android application.

The ESP8266 (ESP-01S) Wi-Fi module will be used to exchange data between the Arduino Uno board and the Android application.

Android Application

Code for the provided Android application is located on this Github repositorium. It is full of the helping comments, which explain purpose of the classes made as well as functions created. If you are not quite sure about how to launch and run an Android application on your phone, please, check this article. In Step 5 I'll explain how to enable access to your ESP8266 Wi-Fi module and, therefore, to receive the most of this app.

Schematics

Automatic Irrigation System_bb.png
photo_2022-08-20_15-20-57.jpg
Automatic Irrigation System_bb2.png

The way components are wired can be seen on the left image.

Here is my way of implementing this project's connections on the upper-right side image.

There are two differences in the wirings between the Fritzing image on the left and my implementation on the right. First, the relay module we see on the Fritzing schematics is not 5V single-channel JQC-3FF-S-Z relay module. The reason for that is that I haven't found a Fritzing component for the used relay module. Secondly, we can notice that there is a 3-wire DS18B20 temperature sensor on the Fritzing image, whereas I've used a 2-wire sensor in my project. If you use the 3-wire temperature sensor, then you can go on with the schematics of connection shown on the Fritzing image. Otherwise, if your case is the same as mine, then you need to provide power to the same wire that you get data from. Therefore, the Fritzing schematics for this case is provided on the lower-right side image.

Here is a full list of the components connections applied to this irrigation system:

ESP8266 (ESP-01S):

  • ESP RX ---> Arduino 3
  • ESP TX ---> Arduino 2
  • ESP 3V3 --> Arduino 3V3
  • ESP EN --> Arduino 3V3
  • ESP GND --> Arduino GND

5V Relay Module:

  • Relay VCC --> Arduino 5V
  • Relay IN --> Arduino 11
  • Relay GND --> Arduino GND
  • Relay NO --> Pump VCC
  • Relay COM --> Battery VCC

3-6V Mini DC Water Pump:

  • Pump VCC --> Relay NO
  • Pump GND - Arduino GND

6V Battery:

  • Battery VCC --> Relay COM
  • Battery GND --> Arduino GND

Water Level Sensor T1592:

  • WLS + --> Arduino 12
  • WLS - --> Arduino GND
  • WLS S --> Arduino A2

YL-69 Soil Moisture Sensor:

  • SMS A0 --> Arduino A1
  • SMS VCC --> Arduino 13
  • SMS GND --> Arduino GND

DS 18B20 Temperature sensor (for a 2-wire connection):

  • TS VDD --> 4.7k Ohm Resistor --> Arduino 5V
  • TS VDD --> Arduino A0
  • TS GND --> Arduino GND

DS 18B20 Temperature sensor (for a 3-wire connection):

  • TS VDD --> 4.7k Ohm Resistor --> TS DATA
  • TS VDD --> Arduino 5V
  • TS DATA --> Arduino A0
  • TS GND --> Arduino GND

Arduino Code

Arduino IDE.png

There is the .ino file a little bit below, that contains the code for the Arduino board. The program uses such libraries as OneWire (made by Paul Stoffregen), DallasTemperature (made by Miles Burton) and SerialSoftware. SerialSoftware library goes already preinstallled, as you install Arduino IDE, but both OneWire and DallasTemperature libraries are not. If you haven't already downloaded these libraries, you need to open Arduino IDE, then go to Sketch/Include Library/Manage Libraries…, which will open a new window. There you can input these libraries titles and search for them made by the corresponding authors. Here's an example of how can this window look like on the image above. 

The code below is followed by the comments, which explain purpose of the created functions as well as some other vital parts of the program.

Run and Test the App + the System

First page.png
Arduino code.png
WiFi assistant.png
Network details.png
Serial monitor.png
Exchange speed.png

If you run the created Android application, you will see its first page, which tells us that in order to send requests to the ESP8266 Wi-Fi module and thus control the irrigation system, we need to provide the Wi-Fi module's IP address and also used port number.

We've defined a server's port number in the .ino file, which is on the colored-in-yellow-background line in one of the above images, and it is 80.

There are several ways to find the Wi-Fi module's IP address. The easiest one is to go to your smartphone's Wi-Fi settings and click on the ESP8266 Wi-Fi module available network. It will bring you to more detailed information about the network, including the relevant IP address, which I found under the Router option (192.168.4.1 in my case). The second option is to use a serial monitor, which will be available once you upload the code to your Arduino board. To open the serial monitor go to Tools/Serial Monitor. Soon enough you will see a result of the ESP8266 Wi-Fi module configuration, where you can find the IP address on the same line with +CIFSR.

If instead of seeing meaningful characters on the serial monitor, you witness a sequence of some random characters, try to change the speed of data exchange (measured in baud).

Also be aware that you can't send requests from your mobile device to the ESP8266 Wi-Fi module right at the moment you connect the Arduino board to a power source. You need to wait a little bit for the Wi-Fi module to be configured. Once you see the "Server Ready" is printed on the serial monitor, you know, that the ESP8266 has been configured, and now you can send requests to it freely. 

We can watch the way this Android application works on the "Android App" video, which is located below. The way of how the automatic irrigation system works when the automatic mode is enabled can be seen on the "Automatic Watering" video, whereas the "Manual Watering" video shows the work of the automatic irrigation system when the automatic mode is disabled.

Through these videos you can see that the work of the application, as well as the system itself, is far from perfect, which opens the way for further improvements for this project.

The most notable drawback of the completed project is that there is often no response from the Wi-Fi module. I have a few guesses as to why this might be happening. From a software point of view, the problem may be to use the AsyncTask class to send the requests and process the response in the background. This class provides a list of functions that greatly facilitate the implementation of background tasks, but it is far from the best solution because of 3 issues associated with this class: memory leaks, cancellation of background work and computational cost (taken from this post). From a hardware viewpoint, in my opinion, the cause of the delay could be either a long execution of one iteration of the loop() function, or a reboot of the ESP8266 Wi-Fi module (if it occurs) due to potentially insufficient voltage from the Arduino board.

Another bug that could be noticed in the "Manual Watering" was that instead of the expected message with the content "The plant has been successfully watered" we received "The plant hasn't been successfully watered" despite the fact that watering took place. Given that the Arduino Uno board received and processed this request and sent the response back to the device via the Wi-Fi module (this can be seen on the serial monitor), the problem might be not hardware related. This again brings me back to the idea that it is necessary to use another way of implementing the background task without using AsyncTask.

Anyway, that's it for today. If you have any questions, suggestions, wishes or objections, please let me know in the comments and I will try to respond to them as constructively as possible. Thank you for your attention and time spent on familiarizing yourself with this project!

Resources:

  1. The Arduino code (check other projects made by this person, if you haven’t done it yet - they are truly wonderful and extremely helpful): http://allaboutee.com/2015/01/20/esp8266-android-application-for-arduino-pin-control/