Plantomation (2/2)

Components

Note

Setup

import mariadb
import time
import sys
from datetime import datetime
import subprocess
import pprint
import json
time.sleep(35)try:
conn = mariadb.connect(
user="admin",
password="root",
host="localhost",
port=3306,
database="plant_monitoring",
autocommit=True
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)

while True:
try:
print("attempt to connect to plug to get state")
output = subprocess.Popen(["hs100", "192.168.0.48", "info"], stdout=subprocess.PIPE).stdout.readlines()
res = json.loads(output[0].decode("utf-8"))['system']['get_sysinfo']['on_time']
print(res)
cur = conn.cursor()
cur.execute("SELECT humidity FROM logger ORDER BY date desc LIMIT 1;")
humidity = cur.fetchone()[0]
date = datetime.now()
print(humidity, res, date)
cur.close()
if humidity > 55 and res > 0:
subprocess.run(["hs100", "192.168.0.48", "off"])
if humidity < 40 and res == 0:
subprocess.run(["hs100", "192.168.0.48", "on"])
time.sleep(30)
except (IndexError, mariadb.Error) as error:
print("couldn't eastablish connection to smart plug", error)
time.sleep(30)
continue
@reboot nohup python3 -u /home/pi/humidifer.py > /home/pi/humidifier_plug.txt 2>&1
import time
import mariadb
import RPi.GPIO as GPIO
import sys
from datetime import datetime
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
def turn_water_on(time_on):
print("Turn water-pump on")
GPIO.output(21, GPIO.LOW)
time.sleep(time_on)
def turn_water_off(time_off):
print("turn water-pump off")
GPIO.output(21, GPIO.HIGH)
time.sleep(time_off)
def start_watering_process(no_iterations, time_on, time_off):
for i in range(0, no_iterations):
turn_water_on(time_on)
turn_water_off(time_off)
def one_watering():
turn_water_on(6)
turn_water_off(5)

try:
conn = mariadb.connect(
user="root",
password="admin",
host="localhost",
port=3306,
database="plant_monitoring",
autocommit=True
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
cur = conn.cursor()
cur.execute("SELECT soil_moisture FROM logger ORDER BY date desc LIMIT 1;")
soil_moisture = cur.fetchone()[0]
date = datetime.now()
if soil_moisture < 19:
print(f"Plant needs watering at {date}, soil moisture is {soil_moisture}")
print("Starting watering process")
start_watering_process(no_iterations=5, time_on=6, time_off=5)
print("Self watering-process finished")
else:
print(f"Plant did not need watering at {date}, soil moisture was {soil_moisture}")
GPIO.cleanup()
0 * * * * nohup python3 /home/pi/self_watering.py >> /home/pi/self_watering_log 2>&1

Future plans

--

--

--

Data Scientist at Basefarm

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Announcing Flutter 1.20 Another Update On Google’s Flutter Stable Channel

CircleCI — The Modern Cloud-Based Platform of Pipeline Automation

Creating “.exe” Executable Setup for Flutter Desktop Apps — Windows 💻

Backup Concepts for Azure Data Lake

Best practices to Deploy Microservices in k8s

Kubernetes for Normal People

BUBBLE SORT ALGORITHM

Lambda LAN…Everyone’s invited.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
André Azam Siggesjö

André Azam Siggesjö

Data Scientist at Basefarm

More from Medium

Google amps up privacy features with Incognito Mode for Maps and much more

Power Tankings, Vol 13: Royal Rumble

The 3 Biggest Offseason Needs for the Philadelphia Eagles

Why hasn’t Sebastian Vettel Retired Yet?