Skip to content

Instantly share code, notes, and snippets.

@benrules2
Last active March 14, 2024 23:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save benrules2/c4f3db455f4f2dfbe7d5b825b0b4ee36 to your computer and use it in GitHub Desktop.
Save benrules2/c4f3db455f4f2dfbe7d5b825b0b4ee36 to your computer and use it in GitHub Desktop.
import water
if __name__ == "__main__":
water.auto_water()
from flask import Flask, render_template, redirect, url_for
import psutil
import datetime
import water
import os
app = Flask(__name__)
def template(title = "HELLO!", text = ""):
now = datetime.datetime.now()
timeString = now
templateDate = {
'title' : title,
'time' : timeString,
'text' : text
}
return templateDate
@app.route("/")
def hello():
templateData = template()
return render_template('main.html', **templateData)
@app.route("/last_watered")
def check_last_watered():
templateData = template(text = water.get_last_watered())
return render_template('main.html', **templateData)
@app.route("/sensor")
def action():
status = water.get_status()
message = ""
if (status == 1):
message = "Water me please!"
else:
message = "I'm a happy plant"
templateData = template(text = message)
return render_template('main.html', **templateData)
@app.route("/water")
def action2():
water.pump_on()
templateData = template(text = "Watered Once")
return render_template('main.html', **templateData)
@app.route("/auto/water/<toggle>")
def auto_water(toggle):
running = False
if toggle == "ON":
templateData = template(text = "Auto Watering On")
for process in psutil.process_iter():
try:
if process.cmdline()[1] == 'auto_water.py':
templateData = template(text = "Already running")
running = True
except:
pass
if not running:
os.system("python3.4 auto_water.py&")
else:
templateData = template(text = "Auto Watering Off")
os.system("pkill -f water.py")
return render_template('main.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment