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 |