Greenhouse Controller RPi Software

This code needs more documentation! I hope to add some in the fullness of time. Apologies for poor or inefficient coding! NB. This code plugin does nor wrap text. Use arrow keys, mouse, pad or scroll bars at the bottom to view text on extreme right.

# GUI to control RPI GPIO and monitor Feather
# 11.8.19
# by Julian Rogers
# now greenhouse7
# was feather_reset_etc20
# more data recieved from Feather in v21
# data displayed in v1
# 
# 
# 
# 
# 
#IP_FEATHER4 = "##.##.##.##" #remote ip address
IP_FEATHER4 = "##.##.##.##" #remote ip address - actually Feather 3
DEST_PORT_FEATHER4= ####
THIS_PORT = ####     #port on this computer
BACKGROUND = "light gray"
BUTTON_1 = "light yellow"       #buttons: advance, load, refresh
BUTTON_2 =  "cornflower blue"     #update buttons
global watchdog
watchdog = 0
global count1
count1 = 0
global count2
count2 = 0
global commnd
commnd = ""
global first
first = True
global wpress_hi
wpress_hi = 0
global wpress_lo
wpress_lo = 0
global rhumid_hi
rhumid_hi = 0
global rhumid_lo
rhumid_lo = 0
global intemp_hi
intemp_hi = 0
global intemp_lo
intemp_lo = 0
global outtemp_hi
outtemp_hi = 0
global outtemp_lo
outtemp_lo = 0
global theFirst
theFirst = True
global rst_press
rst_press = False
global rst_rh
rst_rh = False
global rst_in
rst_in = False
global rst_out
rst_out = False
global wpress_int
global rhumid_int
global intemp_int
global outtemp_int
global wpress_curr
global rh_curr
global in_curr
global out_curr
wpress_int = 0
rhumid_int = 0
intemp = 0
outtemp = 0
press_curr = 0
rh_curr = 0
in_curr = 0
out_curr = 0
from tkinter import *   
from meter import *
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)    #Feather reset pin - not used now. Feather reset by usb power-off
GPIO.setup(22, GPIO.OUT)    #not used
GPIO.setup(19, GPIO.OUT)    #reset hardware watchdog
GPIO.setup(26, GPIO.IN)     #Feather pin 16 - "watchdog"
GPIO.output(23, False)
GPIO.output(24, False)
GPIO.output(25, False)
GPIO.output(12, False)
GPIO.output(16, False)
GPIO.output(20, False)
GPIO.output(21, False)
GPIO.output(19, False)
import subprocess
import os
import datetime
import time
import socket           #UDP
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
from time import sleep
# create the root window
root = Tk()
# modify the window
root.title("9.8.19 V7")
root.geometry("900x700")
root.configure(bg = BACKGROUND)
#root.attributes('-fullscreen', True)   #eliminates the title bar
# the following sets the window full screen
#w, h = root.winfo_screenwidth(), root.winfo_screenheight()
#root.geometry("%dx%d+0+0" % (w, h))
# create a frame
app = Frame(root)
app.configure(bg = BACKGROUND)
app.grid()
title_lab = Label(app, text = "Greenhouse Control Central", font = ("Arial Bold", 20), fg = "maroon", bg = BACKGROUND)
title_lab.grid(row = 1, column = 1, columnspan = 8)
blank_lab = Label(app, bg = BACKGROUND)
blank_lab.grid(row = 2, column = 1)
sys_1_but = Button(app, text = " Water ", font = ("Arial", 12), fg = "maroon", bg = "light blue")
sys_1_but.grid(row = 3, column = 1)
def on_off_1():
but_col = sys_1_but.config('bg')[-1]
if but_col == "light blue":
but_col = "yellow"
GPIO.output(23, True)
else:
but_col = "light blue"
GPIO.output(23, False)
sys_1_but.config(bg = but_col)
sys_1_but.config(command = on_off_1)
sys_2_but = Button(app, text = "W/DG RST", font = ("Arial", 12), fg = "maroon", bg = "light blue")
sys_2_but.grid(row = 3, column = 2)
def on_off_2():
but_col = sys_2_but.config('bg')[-1]
if but_col == "light blue":
but_col = "yellow"
GPIO.output(19, True)
else:
but_col = "light blue"
GPIO.output(19, False)
sys_2_but.config(bg = but_col)
sys_2_but.config(command = on_off_2)
loop_ind = Button(app, text = "X", font = ("Arial", 8), fg = "white", bg = "white")
loop_ind.grid(row = 3, column = 4)
loop_lab = Label(app, text = "loop ind ", font = ("Arial", 12), fg = "maroon", bg = BACKGROUND)
loop_lab.grid(row = 4, column = 4)
sys_3_but = Button(app, text = "Rst Fthr", font = ("Arial", 12), fg = "maroon", bg = "pink")
sys_3_but.grid(row = 3, column = 3)
def reset_feather():
#GPIO.output(27, True)
#sleep(0.1)
#GPIO.output(27, False)
#now uses hub-ctrl to turn toggle usb power
os.system('sudo /home/pi/hub-ctrl -h 1 -P 2 -p 0')
sleep(0.1)
os.system('sudo /home/pi/hub-ctrl -h 1 -P 2 -p 1')
sys_3_but.config(command = reset_feather)
blank_lab1 = Label(app, text = "", fg = BACKGROUND, bg = BACKGROUND)
blank_lab1.grid(row = 4, column = 1)
blank_lab2 = Label(app, text = "", fg = BACKGROUND, bg = BACKGROUND)
blank_lab2.grid(row = 9, column = 1)
ind_lab = Label(app, text = "run ind", font = ("Arial", 12), fg = "maroon", bg = BACKGROUND)
ind_lab.grid(row = 6, column = 4)
run_ind = Button(app, text = "X", font = ("Arial", 8), fg = "white", bg = "white")
run_ind.grid(row = 5, column = 4)
watchdog_but = Button(app, text =" ", font = ("Arial", 8), fg = "black", bg = "white")
watchdog_but.grid(row = 1, column = 4)
time_lab = Label(app, text =" ", font = ("Arial", 14), fg = "maroon", bg = BACKGROUND)
time_lab.grid(row = 1, column = 1)
system_lab0 = Label(app, text ="Remote data: ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND)
system_lab0.grid(row = 7, column = 1)
system_lab = Button(app, text ="xx:xx:xx:xx:xx:xx:xx:xx:xx", font = ("Arial", 10), fg = "black", bg = "white")
system_lab.grid(row = 7, column = 2)
time_but = Button(app, text ="--:--", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND)
time_but.grid(row = 7, column = 3)
rg_reset_but = Button(app, text ="Rst R/G", font = ("Arial", 12), fg = "green", bg = "white")
rg_reset_but.grid(row = 8, column = 1)
def reset_raingauge():
but_col = rg_reset_but.config('bg')[-1]
if but_col == "white":
but_col = "yellow"
GPIO.output(24, True)
else:
but_col = "white"
GPIO.output(24, False)
rg_reset_but.config(bg = but_col)
rg_reset_but.config(command = reset_raingauge)
blank_lab3 = Label(app, text = "", fg = BACKGROUND, bg = BACKGROUND)
blank_lab3.grid(row = 11, column = 1)
text_lab = Label(app, text ="Command entry ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND)
text_lab.grid(row = 12 , column = 1)
entry1 = Entry(app, font = ("Arial", 16), fg = "black", bg = "white")
entry1.grid(row = 12, column = 2)
feedback_lab = Label(app, text ="", font = ("Arial", 16), fg = "blue", bg = BACKGROUND)
feedback_lab.grid(row = 13, column = 2)
conf_but = Button(app, text ="Confirm", font = ("Arial", 16), fg = "blue", bg = BACKGROUND)
conf_but.grid(row = 12, column = 3)
canc_but = Button(app, text ="Cancel ", font = ("Arial", 16), fg = "blue", bg = BACKGROUND)
canc_but.grid(row = 13, column = 3)
fb_lab = Label(app, text ="Last command: ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND)
fb_lab.grid(row = 13, column = 1)
test_lab = Label(app, text ="", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND)
test_lab.grid(row = 14, column = 1)
def press_rst():
global rst_press
rst_press = True
#meter to register water pressure
meter = Meter(app, width = 200)
meter.setrange(start = 0, end = 100)
meter.blob("red")
meter.set(0)
meter.grid(row = 15, column = 1)
meter.config(height = 200)
meter_but = Button(app, text =" Water Pressure ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
meter_but.grid(row = 16, column = 1)
meter_but.config(command = press_rst)
def rh_rst():
global rst_rh
rst_rh = True
#meter to register RH
meter1 = Meter(app, width = 200)
meter1.setrange(start = 0, end = 100)
meter1.blob("green")
meter1.set(0)
meter1.grid(row = 15, column = 2)
meter1.config(height = 200)
meter1_but = Button(app, text ="Relative Humidity", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
meter1_but.grid(row = 16, column = 2)
meter1_but.config(command = rh_rst)
def in_rst():
global rst_in
rst_in = True
#meter to register inside temp
meter2 = Meter(app, width = 200)
meter2.setrange(start = 0, end = 50)
meter2.blob("blue")
meter2.set(0)
meter2.grid(row = 15, column = 3)
meter2.config(height = 200)
meter2_but = Button(app, text ="   Inside Temp  ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
meter2_but.grid(row = 16, column = 3)
meter2_but.config(command = in_rst)
def out_rst():
global rst_out
rst_out = True
#meter to register outside temp
meter3 = Meter(app, width = 200)
meter3.setrange(start = 0, end = 50)
meter3.blob("yellow")
meter3.set(0)
meter3.grid(row = 15, column = 4)
meter3.config(height = 200)
meter_but3 = Button(app, text =" Outside Temp ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
meter_but3.grid(row = 16, column = 4)
meter_but3.config(command = out_rst)
press_lab = Label(app, text = "H: " + "L: ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
press_lab.grid(row = 17, column = 1)
rh_lab = Label(app, text = "H: " + "L: ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
rh_lab.grid(row = 17, column = 2) 
in_lab = Label(app, text = "H: " + "L: ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
in_lab.grid(row = 17, column = 3)
out_lab = Label(app, text = "H: " + "L: ", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
out_lab.grid(row = 17, column = 4)
rg_but = Button(app, text = "rain", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
rg_but.grid(row = 10, column = 1)
damp_but = Button(app, text = "damp", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
damp_but.grid(row = 10, column = 2)
settemp_but = Button(app, text = "set temp", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
settemp_but.grid(row = 10, column = 3)
fanset_but = Button(app, text = "set fan", font = ("Arial", 16), fg = "maroon", bg = BACKGROUND )
fanset_but.grid(row = 10, column = 4)
def confirm():
global commnd
commnd = entry1.get()
conf_but.config(command = confirm)
def cancel():
entry1.delete(0, "end")
canc_but.config(command = cancel)    
def get_data_remote():
global wpress_hi
global wpress_lo
global rhumid_hi
global rhumid_lo
global intemp_hi
global intemp_lo
global outtemp_hi
global outtemp_lo
global theFirst
global rst_press
global rst_rh
global rst_in
global rst_out
global wpress_int
global rhumid_int
global intemp_int
global outtemp_int
global wpress_curr
global rh_curr
global in_curr
global out_curr 
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sock.sendto(bytes("r", "utf-8") , (IP_FEATHER4, DEST_PORT_FEATHER4))
sock.settimeout(10)
#response = sock.recv(100)
response = sock.recv(200)
except socket.error:
#system_lab.config(text = "timed out!")
#response = bytes("----T---/", "utf-8")
response = bytes("--:--:--:--:--:--:--:--:--", "utf-8")
sock.close()
if response == bytes("r" , "utf-8"):       
#response = bytes("rrrrTrrr/", "utf-8")
response = bytes("##:##:##:##:##:##:##:##:##", "utf-8")
response = str(response, "utf-8")
system_lab.config(text = response)
try:                         
tim, intemp, settemp, outtemp, fansettemp, rhumid, tips, wpress, damp = response.split(":")
except ValueError:
tim = "0"
intemp = "0"
settemp = "0"
outtemp = "0"
fansettemp = "0"
rhumid = "0"
tips = "0"
wpress = "0"
damp = "0"
try:
tim_int = int(tim)
except ValueError:
tim_int = 0
clock_hrs = tim_int // 60
clock_mins = tim_int % 60
if clock_hrs < 10:
clock_hrs_str = "0" + str(clock_hrs)
else:
clock_hrs_str = str(clock_hrs)
if clock_mins < 10:
clock_mins_str = "0" + str(clock_mins)
else:
clock_mins_str = str(clock_mins)
time_but.config(text = clock_hrs_str + ":" + clock_mins_str) 
#RH is never zero, so this is quantity checked to see if data
#has been received or not - so needs to be checked first.
try:
rhumid_int = int(rhumid)
except ValueError:
rhumid_int = 0
if rhumid_int != 0:
rhumid_curr = rhumid_int
meter1.set(rhumid_int/10)
if rhumid == 0:     
meter1.set(rhumid_curr/10)
try:
wpress_int = int(wpress)
except ValueError:
wpress_int = 0
if rhumid_int != 0:
wpress_curr = wpress_int
meter.set(wpress_int)
if rhumid == 0:
meter.set(wpress_curr)
try:
intemp_int = int(intemp)
except ValueError:
intemp_int = 0
if rhumid_int != 0:
in_curr = intemp_int
meter2.set(intemp_int/10)
if rhumid == 0:     
meter2.set(in_curr/10)
try:
outtemp_int = int(outtemp)
except ValueError:
outtemp_int = 0
if rhumid_int != 0:
out_curr = outtemp_int
meter3.set(outtemp_int/10)
if rhumid == 0:     
meter3.set(out_curr/10)
try:
settemp_int = round(int(settemp)/10)
except ValueError:
settemp_int = 0    
try:
fansettemp_int = round(int(fansettemp)/10)
except ValueError:
fansettemp_int = 0
if wpress_int > wpress_hi:
wpress_hi = wpress_int
if theFirst == True and wpress_int != 0:
wpress_lo = wpress_int
elif wpress_int != 0 and wpress_int < wpress_lo:
wpress_lo = wpress_int
if rhumid_int > rhumid_hi:
rhumid_hi = rhumid_int
if theFirst == True and rhumid_int != 0:
rhumid_lo = rhumid_int
elif rhumid_int != 0 and rhumid_int < rhumid_lo:
rhumid_lo = rhumid_int
if intemp_int > intemp_hi:
intemp_hi = intemp_int
if theFirst == True and intemp_int != 0:
intemp_lo = intemp_int
elif intemp_int != 0 and intemp_int < intemp_lo:
intemp_lo = intemp_int
if outtemp_int > outtemp_hi:
outtemp_hi = outtemp_int
#NB outtemp_int could be legit zero deg C. Needs to be fixed
if theFirst == True and outtemp_int != 0:
outtemp_lo = outtemp_int
elif outtemp_int != 0 and outtemp_int < outtemp_lo:
outtemp_lo = outtemp_int
if rhumid_int > 0:
theFirst = False
press_lab.config(text = "Hi: " + str(wpress_hi) + " Lo: " + str(wpress_lo))
rh_lab.config(text = "Hi: " + str(rhumid_hi/10) + " Lo: " + str(rhumid_lo/10))
in_lab.config(text = "Hi: " + str(intemp_hi/10) + " Lo: " + str(intemp_lo/10))
out_lab.config(text = "Hi: " + str(outtemp_hi/10) + " Lo: " + str(outtemp_lo/10))
rg_but.config(text = "rain: " + tips)
damp_but.config(text = "soil damp: " + damp)
settemp_but.config(text = "heat set: " + str(settemp_int))
fanset_but.config(text = "fan set: " + str(fansettemp_int))
# calls a function after given time
def after(self, ms, func = None, *args):
"""call function after a given time"""
# updates screen every 1 seconds
def task():
global commnd
global wpress_hi
global wpress_lo
global rhumid_hi
global rhumid_lo
global intemp_hi
global intemp_lo
global outtemp_hi
global outtemp_lo
global theFirst
global rst_press
global rst_rh
global rst_in
global rst_out
global watchdog
global count1
global count2
global first
global wpress_int
global rhumid_int
global intemp_int
global outtemp_int
run_ind.config(fg = "red")
if commnd == "switch" or commnd == "heaton" or commnd == "heatoff" or commnd == "staton" or commnd == "statoff":
feedback_lab.config(text = commnd)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sock.sendto(bytes(commnd, "utf-8") , (IP_FEATHER4, DEST_PORT_FEATHER4))
sock.settimeout(5)
response = sock.recv(100)
entry1.delete(0,END)
commnd = ""
except socket.error:
system_lab.config(text = "timed out!")
sock.close()
elif len(commnd) == 3:
try:
int(commnd)
feedback_lab.config(text = commnd) 
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sock.sendto(bytes(commnd, "utf-8") , (IP_FEATHER4, DEST_PORT_FEATHER4))
sock.settimeout(5)
response = sock.recv(100)
entry1.delete(0,END)
commnd = ""
except socket.error:
system_lab.config(text = "timed out!")
sock.close()
except  ValueError:
entry1.delete(0,END)
commnd = ""
feedback_lab.config(text = "error")
#feedback_lab.config(text = commnd) 
count2 = count2 + 1
#loop_ind.config(text = count2)
if count2 % 2 == 0:
but_col = loop_ind.config('bg')[-1]
if but_col == "red":
loop_ind.config(bg = "white", fg = "white")
else:
loop_ind.config(bg = "red", fg = "red")
if count2 == 30:     
get_data_remote()
count2 = 0
#clear_feedback()
ind = GPIO.input(26)
if ind == 1:
watchdog = watchdog + 1
run_ind.config(fg = "red", bg = "red")
else:
run_ind.config(fg = "white", bg = "white")
count1 = count1 + 1
if count1 == 30:
count1 = 0
if watchdog < 2 or watchdog > 28:
watchdog_but.config(text = "FAIL", bg = "red")
#if auto_reset_but.config('bg')[-1] == "red":               
#reset_feather()
else:
watchdog_but.config(text = "OK", bg = "green")
watchdog = 0
first = False
if rst_press == True:
wpress_lo = wpress_int
wpress_hi = wpress_int
rst_press = False
if rst_rh == True:
rhumid_lo = rhumid_int
rhumid_hi = rhumid_int
rst_rh = False
if rst_in == True:
intemp_lo = intemp_int
intemp_hi = intemp_int
rst_in = False
if rst_out == True:
outtemp_lo = outtemp_int
outtemp_hi = outtemp_int
rst_out = False    
time_lab.config(text = "Comp time " + time.strftime('%H:%M'))        
root.after(1000, task)
# calls the screen update every 1 seconds
root.after(1000, task)
#---------------------------------------------------------------------------------------
# kick off the window's event-loop
root.mainloop()