Skip to content

Instantly share code, notes, and snippets.

@badouralix
Last active June 24, 2020 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badouralix/b1fb6b0b081c06494d5c375672587990 to your computer and use it in GitHub Desktop.
Save badouralix/b1fb6b0b081c06494d5c375672587990 to your computer and use it in GitHub Desktop.
RPi Garden - small project to take care of Lily

RPi Garden

Lily is a succulent. She likes water, but not too much 🌵

rpi-garden is a raspberry pi project to take care of Lily.

# Build dependencies
FROM python:3.7-alpine AS build
RUN apk update && apk add build-base
COPY requirements.txt /tmp/requirements.txt
RUN pip install --install-option="--prefix=/requirements" --requirement /tmp/requirements.txt
# Build runtime image
FROM python:3.7-alpine
COPY --from=build /requirements /usr/local
COPY garden.py /usr/local/bin/garden
CMD [ "garden" ]
#!/usr/bin/env python
"""
Long life the plant!
"""
# Project dependencies
from datadog import api, initialize
from sys import exit
import RPi.GPIO as GPIO
import os
import time
# Global variables
INPUT_PIN = 17
SLEEP_DURATION = 60
WATER_PIN = 27
WATER_WAIT = 15
# Main function
def main():
# Init datadog and crash if api key is missing
options = {
"api_key": os.environ["DATADOG_API_KEY"],
}
initialize(**options)
# Init gpio
GPIO.setmode(GPIO.BCM)
GPIO.setup(INPUT_PIN, GPIO.IN)
GPIO.setup(WATER_PIN, GPIO.OUT)
# Init pump
timer = 0
# Read, refresh, loop
while True:
api.Metric.send(
metric="rpi.garden.moisture.heartbeat",
type="count",
interval=SLEEP_DURATION,
points=1,
host="rpi1",
tags=["version:3"],
)
value = GPIO.input(INPUT_PIN)
api.Metric.send(
metric="rpi.garden.moisture.digital",
type="count",
interval=SLEEP_DURATION,
points=value,
host="rpi1",
tags=["version:3"],
)
if value == 0 and timer == 0:
GPIO.output(WATER_PIN, True)
timer = WATER_WAIT
elif timer > 0:
GPIO.output(WATER_PIN, False)
timer -= 1
api.Metric.send(
metric="rpi.garden.moisture.timer",
type="count",
interval=SLEEP_DURATION,
points=timer,
host="rpi1",
tags=["version:3"],
)
print(f"{time.asctime()} - sent value={value} timer={timer}, going to sleep for {SLEEP_DURATION}s...")
time.sleep(SLEEP_DURATION)
if __name__ == "__main__":
try:
print(f"{time.asctime()} - starting garden now !")
main()
except (KeyboardInterrupt, SystemExit):
print(f"{time.asctime()} - stopping garden now !")
exit(0)
datadog==0.28.0
RPi.GPIO==0.6.5
[flake8]
exclude = .git
ignore = E501
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment