Introduction: BLYNK & ESP8266: WATERING PLANTS AUTOMATICALLY USING SOLAR PANEL

About: Hi everyone, I'm Nguyen Peter. My favorite subjects are Physics and Computer science. Although I'm studying to participate in Vietnam Olympiad in Informatics, but I also love Arduino, too. My favorite algorith…

Nowadays, with the extreme development of science and technology, supporting tools and machines are increasingly ingrained in all areas of life: from long-standing traditional industries such as textiles, agriculture, ... to high-tech industries that require extreme precision. In addition, they are also entertainment, communication, ... to help people communicate with each other easily and quickly, especially during the Covid-19 pandemic, when people have to limit direct communication with each other.

Over the past 4 technological eras, people have also paid more and more attention to the issue of automation. With the current 4.0 era, the phrase "automation" is no longer a stranger, it has been ingrained in the subconscious of every family, it is present in every job we do, the tools we use.

Along with the advancement brought by science and technology, we are paying more and more attention to the relationship between energy and the environment. We need to combine the development of technology and environmental protection to call it sustainable development. Since then, clean energy such as wind power, solar power, ... has been invented to reduce the harmful effects of environmental pollution.

And if you want to combine both automation and clean energy to use in your green garden, welcome to this article. Simple and easy to do but extremely useful, especially for those who are always busy with work. But you don’t have much money? Don't worry, the things you need for this project are easy to find and very cheap. The most important thing is whether you have enough patience to finish this project or not. No worries, just 1 day free, you can complete this project already. Fighting!

Before beginning, we need to choose a processor capable of connecting to Wifi and supporting familiar programming tools. And I find that an esp8266 is a great choice.

Interesting things about this processor:

  • Pretty small (just as small as your eraser).
  • Good ability to receive / transmit Wifi.
  • Extremely low power consumption (about 100mA – 400mA in active state and less than 50mA in sleep state).
  • Low cost (only about $2.5 – $6, data in May 2021).

What you gain after this article:

  • Know how to use esp8266.
  • Know how to use the Blynk app.
  • Over-the-air (OTA) update using HTTP.
  • Energy management for the project.

Step 1: Preparation

1. Hardware

Esp8266 NodeMCU x1

Solar panel 6V - 10W x1

Relay 5v x1

TP4056 Lithium charge controller x1

18650 Lithium battery 3.7-4.2v x4

DC Boost Converter T64 x1

Temperature and humidity sensor DHT11 x1

Diode 1N4007 x1

Resistor 1 kΩ x1

Button x1

Water valve/ pump 12v x1

Breadboard x1

Jumpers

2. Software

Arduino software: Download here.

Blynk software: Download on App store or Google play.

Step 2: Install Software

  1. Download and install Arduino IDE: Guide.
  2. Install esp8266 board in IDE: Guide.
  3. Setup Blynk application:

Step 1 (Login): After installing Blynk to your smartphone, you log in Blynk account if you have already or create a new account if this is the first time you have logged in.

Step 2 (QR scan): After logging in, you click on the QR scan icon in the toolbar.

Then, you put the device and scan in the QR code above.

Now, a program created by us is available on your device. If necessary, you can design a new version on your own.

Step 3 (Take Auth token): Click on the screw icon, then click Email all. Auth Token will be sent to your email. This Auth Token is used to add to the code so that the software knows that your esp8266 is used in the correct project.

Step 3: Schematic

TP4056 integrates Lithium battery charger for efficient continuous operation: when it is sunny, it both charges the battery and supplies power to the device; otherwise, it draws power from the battery to supply the device.

Because the output source on TP4056 is 3.7v - 4.2v, we need to add a boost converter to 5v for the device to work stably.

You can replace the 9v battery in the schematic with a 12v source or three 18650 batteries connected in series. So how long will three 18650 batteries connect in series to provide power? Now we have to solve the problem of energy using the calculation formula:

The formula for calculating work: A = UIt (Wh).

The formula for converting between Wh and mAh: Wh = (mAh x V) / 1000.

We consider each 18650 battery operating at a voltage of 3.7v, 1100mAh. The power supply of 3 batteries is:

A1 = 3 x 3.7 x 1100 / 1000 = 12.21 (Wh)

The pump I use operates at a voltage of 12v, 0.6A. The time for each watering is 20 seconds. Watering an average of 5 times per day. The energy consumed by the pump in 1 day is:

A2 = 12 x 0.6 x (20 x 5 / 3600) = 0.2 (Wh)

If put under ideal conditions, the number of days that the batteries supply the pump are: T = A1 / A2 = 61.05 days (great). If we let the waste be 50%, we still have about 1 month without charging.

Principle of operation: We can set a fixed time for esp8266 to water plants. When the preset time is reached, the esp8266 activates the relay to make the pump work. Irrigation time can be changed in the code.

DHT11 sensor measures temperature and humidity and updates on the Blynk app every 5 seconds.

Button used to reset esp8266 in case of need.

When esp8266 is not connected to Wifi, deep sleep mode is activated to save power. In deep sleep mode, esp8266 only consumes about 20µA (information from manufacturer). There are also other sleep modes, you can refer to here.

Step 4: Programming

First of all, you have to download the following libraries:

Download DHT11 library here.

Download Blynk library here.

Download SimpleTimer library here.

Download NTPtimeESP library here.

My source code on Github: https://github.com/djxone123456/Automatic-irrigation-system

Code setup:

#define APSSID "Runandgo"
#define APPSK "djxone123456"

In here, you change SSID and password information to be the same as the wifi router so that esp8266 connects to the network.

Watering setup:

#define startWaterTime 8 //Time water start [integer]
#define endWaterTime 16 //Time water end [integer]
#define timeBTW 2 // After timeBTW hour(s) [integer], water again
#define waterLength 20 //(second) [integer]
#define timeSleep 10 //(second) [integer]

Explain:

StartWaterTime: Time to start watering in the day (hours)

EndWaterTime: Time to finish watering in the day (hours)

TimeBTW: Time interval between 2 waterings (hours)

WaterLength: Length of each watering (seconds)

TimeSleep: Time to deep sleep to save power (seconds). After sleeping, esp8266 resets automatically.

Set up pin connection between esp8266 and DHT11 with Relay:

#define DHTPIN D5
#define Relay D7 // Pump

After sending the Auth Token to your email mentioned in Step 2, the email information will appear as shown in the picture.

At this point, you just need to copy the Auth Token and change the code below:

const char *auth = "bTTEbBILDbMs-Lm_N4TJ5vMtBDnXubBk"; // Paste Blynk token in here

Every 5 seconds, esp8266 gets time on ch.pool.ntp.org using getTimeWatering() function. To make it get the correct time, you need to change the time zone where you live in this code (for example, in Vietnam, the time zone is GMT +7):

dateTime = NTPch.getNTPtime(7.0, 0); // Timezone

Hooray, that's it 😀. Now you just need to upload the code to your little esp8266 to start controlling it.

Note: Step 5 is an optional part and requires a lot of time and effort. If you can easily update the esp8266 directly with your PC, you can go straight to step 6.

Step 5: Over-the-air (OTA) Update Using HTTP

Software updates are an important part of any project, whether it's big or small. But if the number of devices that need to be updated is large or the devices are located in unsuitable conditions to update directly through the computer, then remote updating is an inevitable way to solve this problem. At this point, all you have to do is press a button or type commands from your computer and all devices are updated automatically. So, I will show you how to update remotely via HTTP.

First, you need to create a website. It can be a web server or rented web hosting. For me, taking advantage of free web hosting with 000webhost is an option. Making:

  • Go to 000webhost.
  • Enter the email and password information you want to create and press SIGN UP.
  • After creating the account, check the verification mail sent via email. Then you've got a free hosting service.
  • Next, the screen will appear as shown in Figure 1, click “It’s not my first rodeo, take me to the panel.”
  • Go to the website naming section. You type the project name and domain you want and press SUBMIT (Figure 2).
  • Select Upload your site (Figure 3). At this point, you have created your own free website.
  • Click on the public_html folder (Figure 4). You only use this directory as primary.
  • Create file “firmware_ver” to check the firmware version for esp8266 (Figure 5 + 6). Purpose: If the information on this file is different from the current firmware on the esp8266, it will trigger the OTA process.

Posting updates to the website:

  • Go to Arduino IDE -> Sketch -> Export compiled Binary, then wait for the program to finish compiling.
  • Open the folder containing the project's .ino file. At this point, you will see a .bin file and rename it to firmware.bin
  • Drag and drop the newly created firmware.bin file onto the folder containing the created firmware_ver file.
  • Note that, in the file ota_firmware.h, you must change the existing link to a link to your website.
const char *website = "http://waterautomatic.000webhostapp.com/firmware.bin";
http.begin(client, "http://waterautomatic.000webhostapp.com/firmware_ver"); //HTTP URL for getting firmware version

Step 6: How to Use Automatic Watering Software on Blynk?

To run the app, you need to tap the play ▶️ button at the top right. If now the app appears similar to the image above, congratulations, you have succeeded!

The information is shown on the app:

  • The first row is to measure Temperature and Humidity.
  • The second row is a graph of temperature, humidity and heat index. You can easily assess the temperature evolution taking place in each defined time period.
  • The last row is the command-line console. In Windows it can be called Command Prompt or in Linux is Terminal. You can control it by typing the information you want to control. For example, if you want to water your plants right now, you just need to type “water”. These commands can be viewed on the project's sublibrary Blynkprocess.h.

Built-in command lines:

Help List available commands.

Version Print the firmware version.

Update Software update for esp8266.

Restart Restart esp8266.

Water Water plants.

Clrscr Delete previous command lines.

Temp Temperature and humidity information.

Step 7: Enjoy Your Achievement

Congratulations, you have completed this project. This project was written and tested for almost a month when I was just starting my summer vacation. Up to now, my esp8266 has been working for almost 2 weeks 😀. My family really enjoyed this project, which does not require high costs but is very useful. This project will help us to have colorful orchid pots or green salad plants. From now on, I just need to wait for the day to enjoy my results 😊. I hope you are as successful as I am. See you in future projects. And if you did, don't hesitate to share it with us.

If you want to understand my code, you can read through it. I've already commented on it well. Hope some part of my code will help your own projects 😊. Have a nice day.

Arduino Contest

Participated in the
Arduino Contest