Planting4U - Automated Irrigation System With a Website Interface

by ptietz17 in Circuits > Raspberry Pi

1791 Views, 26 Favorites, 0 Comments

Planting4U - Automated Irrigation System With a Website Interface

IMG_20210618_003513.jpg

Have you ever been on holiday and worried about your plants at home? Yes, me neither! However, the people that consider themselves green-fingered or plant-loving might have just stumbled upon a system that finally allows them to enjoy their holiday and forget about their oxygen-producing friends at home.

The Planting4U irrigation system is a newly developed platform that controls the watering procedure while providing essential information to the user via its own website. This allows for complete control and overview from anywhere in the world. This product is easily expandable and can be further developed to suit all your needs.

The following instructable will show you all the steps and methods required for reproducing the automated irrigation system. This DIY project can also be used as a guideline if you feel adventurous and want to create your own smart irrigation system using different components and/or supplies.

The CAD designs and software components for the irrigation system are all available in the following GITHUB repository: https://github.com/Patrick2428/P4U

Supplies

Raspberry Pi Zero - https://www.kiwi-electronics.nl/raspberry-pi-zero-...

2 Channel Relay Module - https://www.reichelt.nl/nl/en/developer-boards-rel...

RPI Analog to Digital Converter - https://www.reichelt.nl/nl/en/raspberry-pi-analog-...

Moisture Sensor - https://www.reichelt.nl/nl/en/arduino-moisture-sen...

Temperature and Humidity Sensor - https://www.reichelt.nl/nl/nl/ontwikkelaarsraden-t...

Mini displacement pump - https://opencircuit.nl/Product/Mini-pomp-2.5V-6V

PVC Hose (7-9mm) - https://opencircuit.nl/Product/PVC-Slang-7-9mm-tra...

Perfboard ( 70 x 100 mm) - https://www.hobbyelectronica.nl/product/prototypin...

Additional:

Electrical wiring; soldering equipment; 3D printer

Skrews: 4x 30mm M3; 8x 10mm M3; 2x 12mm M3

Nuts: 14 x M3

Electrical Hardware Setup

P4U-electrical-circuit.png

The electrical circuit required for the system is shown in the figure above. The Raspberry Pi Zero (RPI) serves as the processing unit for the irrigation system. It instructs the displacement pump to disperse water to the plants whenever the moisture level, detected by the moisture sensor, is too low. Additionally, the relative humidity and the temperature of the environment is obtained using the temperature and humidity sensor.

Since the user might be interested to know the conditions of their plants and the water cycle all important data is saved to a local database, of which the most relevant is uploaded to a locally run website.

A glance at the different electrical connections and data paths:

I2C Interface

The I2C interface on the RPI (SDA and SCL) allows for multiple master and slave connections. This standard allows you to connect multiple devices such as temperature sensors, moisture sensors, and ADCs. This gives you the possibility to connect a large number of sensing units depending on how many plants you have and how much you would like to expand the system. It is important to know that each sensor requires a separate address for the I2C interface to work. This, unfortunately, limits the usage of similar components as they mostly come with 2-6 different address configurations.

In our case, we can connect,

2 x SHT-31D temp and hum sensor with available addresses 0x44 and 0x45.

4 x ADS1115 ADC with available addresses 0x48, 0x49, 0x4A and 0x4B

ADC Connection

The moisture sensor has an analog signal output, which the RPI cannot process as it has no onboard analog to digital converters. For this purpose, an additional ADS1115 ADC is required which can connect up to 4 analog devices. The ADC sends the converted signal to the RPI via I2C. As we established previously up to 4 ADS1115 ADCs can be connected, which allows for a total of 16 moisture sensors to be connected to the system.

Relay Connection

The 2 module relay is used as a switch, to turn the displacement pump on or off. When the relay receives a HIGH signal from the RPI it turns the switch on, allowing for current to flow to the motors of the pump. When the signal is LOW the switch stays off. With this mechanism, the RPI is free to turn the pump on or off depending on the moisture level it receives from the moisture sensors. A relay was used as in the future you might want to replace the pump with a more powerful one which will require a voltage input of 230V/AC.

Hardware Enclosures

System Enclosure 2.PNG
Sensor Enclosure 2.PNG

Since the system involves irrigation with the chance of water splashing around it is advised to seal all the electrical components within an enclosure. The figures shown above depict the CAD designs of both the system and moisture sensor enclosures. Both the CAD and STL files for these housing units are available in the Github repository which can be used for modifying or 3D printing the enclosure.

You will find an additional sprinkler design that can be attached at the end of the hose for better water distribution over your plants.

The system enclosure contains all the main electrical components such as the RPI, the relay modules and the temperature and humidity sensors. The housing is divided into three parts, the top cover, the midsection and the lower cover. All three sections are combined using 4x 30mm M3 screws. The RPI and the relay are held in place in the lower cover using 8x 10mm M3 screws. The Midsection holds the (70x100mm) perfboard onto which the ADC and the temperature and humidity sensor are attached.

The moisture sensor enclosure consists out of two parts, the top and lower cover. This housing unit is responsible for preventing any water damage to the moisture sensor which is placed right next to the plants. It is advised to cover the electronics on the sensor itself with a rubber shrink sleeve to make it 100% watertight. The unit is held together using 2x 12mm M3 screws.

Hardware Assembly

IMG_20210415_223618.jpg
IMG_20210415_224424.jpg

The figures above show the final results of the hardware assembly after soldering and placing the electrical components in the 3D printed enclosures. The cables and connectors can be chosen freely. Additional plugs for the connections to the pump and the moisture sensor can be added to detach the components from one another.

Setting Up the Raspberry Pi Zero (Processing Unit)

As mentioned in the earlier steps the raspberry pi zero will be used as the processing unit for the automated irrigation system. The RPI will run both the control loop for the irrigation procedure as well as the webserver for hosting the UI website of the irrigation system. However, before we can execute any of the software components, the RPI first needs to be configured and properly set up.

Note: Any text typed in 'italics' are terminal commands inside the RPI terminal

Step 4.1: Flash the SD card (min 8GB) with the Raspberry Pi operating system (Link)

The official website provides an imager tool that allows you to flash the SD card without using any external application.

Step 4.2: Boot your Raspberry Pi and connect to the Internet.

To interface with the RPI, there are a few options such as using a standard mouse, screen, and keyboard connection, or by using an SSH connection. To enable SSH the first time you can simply create an empty SSH folder inside the boot directory of the flashed image. The following website gives a detailed overview of the steps involved.

If you are like me and only have limited accessories (No extra mouse, keyboard, or OTG Ethernet adapter) you might be wondering how you could connect to your RPI using SSH without having it connected to the ethernet. Well, thankfully you can create a headless wifi connection to your home router by configuring your boot directory on the SD card. Follow the guide on this website to create a WIFI connection.

Step 4.3: Once booted and connected run a quick updated 'sudo apt update' & 'sudo apt upgrade' in the terminal to fetch all the latest packages.

Step 4.4: Clone the provided git repository to your RPI: https://github.com/Patrick2428/P4U

Go to your home directory on the RPI and enter the following commands

'git init'

'git clone https://github.com/Patrick2428/P4U.git'

Note: feel free to remove all the document, CAD, and STL files as they will only take up unnecessary space

Step 4.5: Enable Interface configurations.

Enable GPIO and I2C:

'sudo raspi-config' -> Interface Options -> I2C -> Enable

'sudo raspi-config' -> Interface Options -> Remote GPIO -> Enable

In the next sections, the two different software components for the irrigation controller and the website will be discussed and set up for usage.

Running the Irrigation Control Software

irrigation control loop.png

The Irrigation control software was coded in python. Python is a great all-purpose scripting programming language that runs very well on raspberry pi processing units.

The irrigation control software is located in the cloned git repository: Planting4U/Code/Irrigation_system/

The main executable script is the 'irrigation_control_system.py' which will run the full irrigation control loop. However, before we can start the execution of the software there are a few additionals python packages we need to install:

(Note: pip is a package management system which should be installed by default'

1. 'pip install smbus2'

...

That seems to be it :)

You can now test out the irrigation control script and see if all your electrical connections are correct.

use the following command in the Irrigation_system directory: 'python irrigation_control_system.py'

If everything functions correctly the system will give you a start message and then output info every second.

For a detailed insight into past irrigation cycles or data you can view the log section: Planting4U/Code/Irrigation_system/logs/

Setting Up the Irrigation UI Website

login.png
homepage.png

The website that publishes essential data to the user is comprised out of two web pages, a login page and a homepage. These were both created from scratch using HTML, Javascript and CSS to define the layout, the design and the dynamic behaviour of the website.

The login page simply requires the user to fill in the correct credentials (view step 6.2) which will then navigate the user to the homepage. The homepage displays all relevant data of the irrigation system and allows for the user to change the moisture level setpoint (pump trigger level).

The software for the website is located in the cloned git repository: Planting4U/Code/Website/

To set up and publish the website from your Raspberry Pi to your local network follow these steps:

Step 6.1: Install Node.js for the server-side handler.

Node.js is an open-source javascript runtime environment that is used for running the server-side (backend) of the website. The server side is responsible for publishing and handling the client-side (frontend).

Note that only specific versions of node work on the RPI zero. Refer to the official node.js website for more details.

You can use the following installation guide to install the appropriate software version. The version used for this project was: 'wget https://nodejs.org/dist/latest-v10.x/node-v10.24.1-linux-armv6l.tar.xz'

Note that some older versions do not support the latest libraries used for the server-side code of the website applications.

Once the installation was completed, navigate back to the website directory Planting4U/Code/Website/.

Step 6.2: Install node bcrypt module

Before being able to execute the javascript software in the website/ directory, an additional node module has to be installed.

The bcrypt module is required for encryption of the password to prevent any data leakage. Simply run the following command

'npm i bcrypt'

Note that if there seem to be any additional module errors in the upcoming steps, refer to the README file in the website/ directory for an overview of all the modules that were installed beforehand and try reinstalling them.

Step 6.3: Create a username and password

The default username and password for the website are:

name: user

pass: 1234

These login credentials are stored inside the /data/user_info.json file. Because the password is encrypted you can't simply go into the file and change the variables as you wish. To change the login credentials you have to run the following javascript files using node:

'node create_new_user.js'

Follow the instructions displayed on the terminal and reset your username and password

Step 6.4: Install Apache Server

Apache HTTP Server is a free open source web server software that will be used for publishing the website. We simply proxy pass our created server handler (server_handler.js) to Apache to publish the Planting4U website on our RPI localhost.

To install the server software, use the following command:

'sudo apt install apache2 -y'

Once installed you can test the webserver. Simply browse 'http://<rpi_IP_address>' on a local machine.A default HTML file of apache should be depicted. If not try restarting the server.

Step 6.5: Set up apache and proxy pass our custom server handler

In order for apache to publish our website (client-side), we need to proxy-pass the port (port:3000) of our server handler (server-side). This will allow apache to access the custom webpage and post it to your local or public network

Proxy-pass backend server (localhost:3000) to apache2:

Firstly, navigate to /etc/apache2/sites-available/ in your RPI terminal. Then open the file 000-default.conf

'sudo nano 000-default.conf'

and enter the line: 'ProxyPass / http://localhost:3000/'

Secondly, enable proxy in apache2.

Navigate to /etc/apache2/sites-enabled/ and run the following command:

'sudo a2enmod'

This command allows you to enable all kinds of apache modules. The two modules to enable are proxy and proxy_http. Run a2enmod twice to enable both of the modules by following the instructions.

Thirdly, restart your apache server using the command:

'sudo /etc/init.d/apache2 restart'

Step 6.6: Start the server-handler script.

The last step is to start the server_handler.js script in the Planting4U/Code/Website/ directory

Simply enter the command: 'node server_handler.js' to start the javascript file. A text should pop up saying 'listening at 3000'

Your server is now running on your local internet connection, which means that all devices connected to your home router can connect to the website by browsing: http://<rpi_IP_Address> e.g. http://192.168.2

Note: To publish the website to a public network a few extra steps are required which will be discussed in the last step of this instructable.

Finalizing and Running the Automated Irrigation System

Planting4U Automated Irrigation System - Preview

In the previous steps, we have covered all the different hardware and software aspects of the system and provided a DIY run-through on how to set everything up. Now we have come to the point where we can finalize the system and execute all the software units as one integrated system.

In the current system state, you can execute the server_handler.js and the irrigation_control_system.py scripts individually and the system will run with both the website and the irrigation control loop interacting with one another. However, this becomes tedious and requires you to log into your RPI every single time you want to start your system. An alternative is to use Cron for starting and stopping the scripts automatically.

Cron is a software utility on the RPI operating system, which acts as a time-based scheduler. You can use cron to automatically start an application at the boot or at a specified date and time. For simplicity, we will configure cron such that the website and irrigation software is started when the RPI is booted.

open the crontab: 'crontab -e' and choose an editor such as nano.

below the comment section (#) enter the following two lines:

1: @reboot python <full path to file>/irrigation_control_system.py #starts irrigation control loop script

2: @reboot node <full path to file>/server_handler.js #starts the server handler script

after closing and saving the file you can double-check your commands using 'crontab -l'.

Now reboot your system and it should all be up and running. You can check the crontab logfile with the command: 'grep CRON /var/log/syslog'

The system is now fully set up and ready for use. Note that the website is still running on your local network and cannot be accessed from outside the home router connection. View the next additional section to incorporate publishing the website to the public network.

Conclusion

The Planting4U automated irrigation system is a startup prototype that can easily be modified and extended to your needs. The simplicity behind the design and the usage of limited resources is to encourage you to further develop the system.

With the current version, you are capable of controlling and observing the water cycle of your home plants using a website interface. The interface has a simplistic and user-friendly design onto which further functionalities can be added, such as data graphs, raw data overview, or even a webcam interface to see the state of your plants. For the system to control the water cycle of numerous plants, more sensing units and a more powerful pump are required. These extensions are easily applicable with only slight software and hardware changes.

Although many additional modifications could be added, in its current state it serves as a functioning system that can right away be used for controlling the irrigation procedure of your plants.

Additional - Porting the Website to a Public Network

There are a few alternative ways to publish your website to the public network. The main requirement for this is to set up a static IP address for your system, which you could obtain by contacting your internet service provider. However, since most people want to avoid such contact there are some workaround methods.

The following youtube video [5:42] gives a clear step-by-step tutorial on setting up such an IP address using No-IP, a dynamic DNS service provider. Google also offers their own DNS services, however, these can cost around 10€ per year.

Due to time constraints, this feature was not yet tested or implemented within this system. However, the video should provide a sufficient guide to setting up the website on a public network allowing for access from anywhere in the world.