Ogarden is an easy-to-understand application for automating garden watering with a Raspberry Pi.
Ogarden combines the following data
- Internet weather forecasts for your area (US only, but easy to add another API)
- Measurments of current soil moisture content
and applies those conditions to a simple parameterized mathematical model of soil moisture to determine, and apply, the optimal amount of water to your garden.
- A Systemd service runs twice a day, initiating the following steps.
- The weather forecast is fetched from the US National Weather Service
- Current soil moisture is measured from a sensor in the garden soil
- Weather and soil conditions are applied to a soil moisture model to determine the required depth of uniform watering (similar to inches or millimeters of rainfall)
- "rainfall" amount is applied to an irrigation system model to determine the required number of seconds of irrigation.
- A solenoid sprinkler valve is turned on for the required number of seconds.
All model values are parameterized and can (and should) be tweaked for your
- Weather forecast location
- Soil composition and drainage
- Garden size
- Irrigation system throughput
- Hardware connections to solenoid sprinkler valve
Ogarden has two separate (loosely coupled) processes
- The Python application with is executed on a schedule with Systemd.
- The (optional) web UI, which is controlled by a separate Systemd process and presents source data from the log database generated by the Python application.
The application source code is meant to be well-organized, easy to understand, and easy to extend and customize for your garden.
- Each step in the How Ogarden Works process above is implemented in a separate Python file.
- Each file is only about 100 lines long (only about two pages of code).
- Each file can be run individually for self-test or to demonstrate the functionality.
Ogarden also contains a web interface which you can extend to your liking, or adapt to other projects.
The technology stack is based on Flask and React.
Ogarden does not require the web interface to function and can be omitted if desired.
After you configure your Raspberry Pi on your network and can SSH into it,
clone the ogarden repository from your home directory
git clone [email protected]:drewlio/ogarden.git
Let's address installation of the two modular components separately
- Python application
- Web UI
Modern Raspberry Pi OS versions come with Python 3.x. In addition, we'll need
to install pip
and venv
in order to install all the project dependencies in
a self-contained environment within the project folder.
The installation process follows
- Verify your Raspberry Pi OS version has Python 3 and which executable points to it (ie, python or python3)
- Install pip and venv with
sudo apt install python3-pip python3-venv
- Verify you have access to pip with the command pip3 or pip
- Create a virtual environment so all of our Python dependencies are
self-contained within the project. From the ogarden project directory, use
the command
python3 -m venv venv
. This uses the venv module to create a virtual environment called venv (this is a popular naming convention). - Activate the virtual environment with
. venv/bin/activate
(don't forget the leading period/dot, which is shorthand forsource venv/bin/activate
). This modifies Python-related paths to point to dependencies and executables in a self-contained virtual environment within the Ogarden project directory. - Use pip to automatically install the Python dependencies listed in the
file called requirements.txt. Note your virtual environment may have mapped
the command pip to pip3 so you can use the command
pip install -r requirements.txt
.
Now the Python application is ready to run. You can test individual components
with commands such as python soil.py
or python weather.py
or run the entire
app with python app.py
.
Now it's time to run the Python application automatically with Systemd.
Systemd is the daemon in Raspberry Pi OS (and virtually all Linux distributions) that handles starting processes (Systemd calls them services) at boot time and periodically on a schedule. (Note cron is another subsystem that can schedule processes to run, but in this example we'll use Systemd.)
The general process for configuring a Systemd service is as follows
- Create a unit file which describes the service
- Tell Systemd to enable that service
- (Optionally) Provide Systemd a timer file which describes how to run the aforementioned service on a schedule.
- The Python application
- Unit file describing the Python application entrypoint (app.py).
- Timer file describing how to run the Python applicaton service twice daily.
- The Web UI
- Unit file describing the web UI service.
Below we will configure the Systemd control of the Python application.
Systemd configuration of the Web UI control is below in the Web UI section.
The directory ogarden/systemd/ contains unit files for the Ogarden service and time. The service unit file describes how to launch the Python application. The timer unit file describes the schedule for launching the service.
The steps for this configuration are
- Edit the
WorkingDirectory
field in the ogarden/systemd/ogarden.service file to indicate the location of your ogarden installation. - Install the service unit file by creating a link with the command
ln -s /path/to/ogarden/systemd/ogarden.service /etc/systemd/system
- Install the timer unit file by creating a link with the command
ln -s /path/to/ogarden/systemd/ogarden.timer /etc/systemd/system
- Enable the timer with the command
systemctl enable ogarden.timer
.
Note we do not enable the service ogarden.service because we don't want the Python application to run automatically upon boot. We only want the Python application to run according to the timer.
follow directions from Yarn https://classic.yarnpkg.com/en/docs/install/#debian-stable which are to add the repository and then install with apt
commands: curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt update && sudo apt install yarn yarn --version
yarn install (5+ minutes) yarn build (5 minutes)