Giter VIP home page Giter VIP logo

ogarden's Introduction

Easy Garden Automation with the Raspberry Pi

Overview

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.

How Ogarden Works

  1. A Systemd service runs twice a day, initiating the following steps.
  2. The weather forecast is fetched from the US National Weather Service
  3. Current soil moisture is measured from a sensor in the garden soil
  4. 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)
  5. "rainfall" amount is applied to an irrigation system model to determine the required number of seconds of irrigation.
  6. A solenoid sprinkler valve is turned on for the required number of seconds.

Ogarden is Easy to Customize

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 is Modular

Ogarden has two separate (loosely coupled) processes

  1. The Python application with is executed on a schedule with Systemd.
  2. 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.

Code Organization

The application source code is meant to be well-organized, easy to understand, and easy to extend and customize for your garden.

Python Application

  • 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.

Web UI

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.

Installation

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

Python Application Installation

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

Install Python Virtual Environment and Dependencies

  1. Verify your Raspberry Pi OS version has Python 3 and which executable points to it (ie, python or python3)
  2. Install pip and venv with sudo apt install python3-pip python3-venv
  3. Verify you have access to pip with the command pip3 or pip
  4. 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).
  5. Activate the virtual environment with . venv/bin/activate (don't forget the leading period/dot, which is shorthand for source 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.
  6. 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.

Configuring Systemd to Run the Python Application Automatically

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

  1. Create a unit file which describes the service
  2. Tell Systemd to enable that service
  3. (Optionally) Provide Systemd a timer file which describes how to run the aforementioned service on a schedule.
Systemd Controls Two Parts of Ogarden
  1. The Python application
    • Unit file describing the Python application entrypoint (app.py).
    • Timer file describing how to run the Python applicaton service twice daily.
  2. 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.

Systemd Control of the Python Application

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

  1. Edit the WorkingDirectory field in the ogarden/systemd/ogarden.service file to indicate the location of your ogarden installation.
  2. Install the service unit file by creating a link with the command ln -s /path/to/ogarden/systemd/ogarden.service /etc/systemd/system
  3. Install the timer unit file by creating a link with the command ln -s /path/to/ogarden/systemd/ogarden.timer /etc/systemd/system
  4. 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.

Web Interface Installation

Install Yarn

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)

ogarden's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.