In this project, we’ll design a mobile app-controlled watering system that drip irrigates a home garden or small farm. This system is developed on an ESP8266 microcontroller, which controls the water pump through a 3V relay or an electronic valve.
The user can switch the water’s motor/electronic valve ON or OFF and even set a watering schedule via the app. This means users can set a time for watering or switch the water on or off wherever they are, so long as they have the app installed.
Components required
1. ESP8266 x1
2. 3V relay x1
3. Electrical wires for connecting the motor, relay, and main supply
4. Connecting/DuPont wires for connecting the ESP8266 to the relay
Circuit connections
The device built in this project controls the operation of a water pump or an electronic valve through a 3V relay. The water pump or valve is attached to a relay, which is connected to the ESP8266 microcontroller.
To connect the relay with the microcontroller:
- Connect the relay’s input terminal with ESP8266’s GPIO 16
- Connect the relay’s VCC and GND terminals with ESP8266’s 3V3 and ground pins
To connect the relay with the water pump or electronic valve:
- Connect two wires to a two-pin top
- Connect one of the top’s terminals with the relay terminal that’s normally open
- Connect one of the pump/valve’s terminals with the relay’s common contact.
- Connect the other pump/valve’s terminal with the other top’s terminal
The complete circuit is shown in the below diagram.
Building the app
The mobile app is the heart of this project and is built in the MIT App Inventor. It lets users switch ON/OFF the motor/valve or schedule its operation.
The app has a single screen with buttons to turn the motor/valve’s water ON/OFF and set its watering schedule. There are two options for picking the time: one to set the start and another to set the end time of the water motor/valve’s operation. The time “pickers” and their labels are organized in horizontal arrangements.
A client UDP extension is also part of the screen, which can be downloaded from the this link.
After downloading the extension file, import it into the MIT App Inventor by navigating to Extension-> Import extension.
The main (and only) screen of the app has the below design.
‘Screen1’ has the following properties.
‘Button1’ is used to switch ON/OFF the water pump/valve. It has the following properties.
‘Label1’ for the ‘TimePicker1’ has the following properties.
The ‘TimePicker1’ sets the start time of the motor/valve. It has the following properties.
‘Label2’ for the ‘TimePicker2’ has the following properties.
The ‘TimePicker2’ sets the stop time of the motor/valve. It has the following properties.
‘Button2’ schedules the operation of the motor/valve. It has the following properties.
The app has the following logic designed into the block editor.
Arduino sketch
After making the circuit connections, upload the following sketch to ESP8266. Remember to replace the SSID, Wi-Fi, password, and Weather API key in the app with your own.
How it works
Users can instantly turn the water pump/valve ON/OFF or schedule its operation directly from their mobile app. A single button on the app’s screen turns the motor/valve ON/OFF. When a user taps that button to turn the motor/valve ON, the message “T1” is transmitted to ESP8266 from the app via the Internet over a UDP protocol. ESP8266 receives this message through the Wi-Fi network on the same protocol.
When a user taps the button to turn the motor/valve OFF, the app transmits the message “T0” to ESP8266 via the Internet over a UDP protocol.
A user can also set a schedule for the motor/valve to operate by setting its start and stop time. Simply click on the app’s ‘TimePickers’ and then tap on the ‘Schedule’ button. When the schedule button is selected, a message in the format H<starttime_hours>M<starttime_minutes>X<stoptime_hours>Y<stoptime_minutes> is transmitted to ESP8266 over the UDP protocol. ESP8266 receives this message through the Wi-Fi network.
The UDP packet received from the mobile app is analyzed on ESP8266. If the message received is “T1,” the motor/valve is turned ON by activating the relay. The relay is activated by sending a LOW signal to its input terminal. If the message received is “T0,” the motor/valve is turned OFF by deactivating the relay.
If ESP8266 receives a message to schedule the motor/valve’s operation, the start and stop times are set based on the current local time — retrieved from the Weather API. ESP8266 also operates according to the last message received via the UDP protocol.
The code
The sketch begins by importing the ESP8266WiFi.h, ESP8266HTTPClient.h, and WiFiClient.h libraries, which are required to handle ESP8266’s Wi-Fi connection.
WiFiUdp.h is imported to handle the UDP protocol over Wi-Fi on ESP8266. An object of the WiFiUDP class is instantiated, and the variables are declared for the UDP port and packet. The variables are declared to store the Wi-Fi SSID, Wi-Fi password, and Weather API key. Variables are also declared for the pin connection with the relay and to store the current local time, the start and stop times of the motor/valve, and the start and stop time’s hour and minute values, which are determined by the UDP packet.
- The user-defined connectWiFi() function connects ESP8266 with the Wi-Fi network.
- The convertToMinutes() function converts the local time received from the Weather API into minutes passed (since 12 AM).
- The stringToCharArray() function is defined to convert the string received from the Weather API into a character array.
- The getCurrentTime() function retrieves the current local time from the Weather API.
- The processMsg() function deconstructs the motor/vale’s start and stop times from the UDP message (received from the mobile app).
In the setup() function, the baud rate for the serial port is set to 115200 for debug messages. The pin connecting to the relay is set as the digital output. ESP8266 is connected to the Wi-Fi network by calling the connectWiFi() function. The UDP port is initialized on ESP8266 and configured to “listen’ for messages received through the Internet.
The current time is retrieved from the Weather API using the loop() function and stored in a global variable. The UDP packet is detected, and the first character of the packet is checked to verify if the message is to turn ON/OFF the motor/valve or schedule its operation.
If the message is to turn the motor/valve ON/OFF, the second character of the message is decoded to determine the switching operation. If the message is received to schedule the motor/valve’s operation, the start and stop times are validated and deconstructed from the message, and continuously compared with the current local time.
The results
You may also like:
Filed Under: Electronic Projects, ESP8266, Video
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.