Skip to content

Instantly share code, notes, and snippets.

@mjumbewu
Forked from benrules2/auto_water.py
Last active September 17, 2019 14:55
Show Gist options
  • Save mjumbewu/98cc88badd68a6a7016624d2b69eaf56 to your computer and use it in GitHub Desktop.
Save mjumbewu/98cc88badd68a6a7016624d2b69eaf56 to your computer and use it in GitHub Desktop.
import water
if __name__ == "__main__":
water.auto_water()
<!DOCTYPE html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>PLANT HELPLINE</h1>
<h2>The date and time on the server is: {{ time }}</h2>
<h2> {{ text }} </h2>
<a href="/auto/water/ON"><button>Turn ON Auto Watering</button></a>
<a href="/auto/water/OFF"><button>Turn OFF Auto Watering</button></a>
<a href="/sensor"><button>Check Soil Status</button></a> <br>
<a href="/water"><button>Water Once</button></a>
<a href="/last_watered"><button>Check Time Last Watered</button></a>
</body>
</html>
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