I am getting started with Python (and programming all together).
I am referencing eventtime against the current time to control an outside/semi enclosed herb garden using GPIO to control relays. I have everything responding the way I expect it to in current tests - including the hacky way of initiating water() but I'm fine with that for now until my skills improve.
I am having trouble with getting the lighton() function called after going to sleep. Below is my code and output...
import RPi.GPIO as GPIO
import time
import datetime
import os
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
TEMPHIGH = 23
TEMPOK = 17
LIGHTS = 18
WATER = 22
FAN = 24
HEATER = 25
GPIO.setup(TEMPHIGH,GPIO.OUT)
GPIO.setup(TEMPOK,GPIO.OUT)
GPIO.setup(LIGHTS,GPIO.OUT) # relay IN1
GPIO.setup(FAN,GPIO.OUT) # relay IN2
GPIO.setup(WATER,GPIO.OUT) # relay IN3
GPIO.setup(HEATER,GPIO.OUT) #relay IN4
timenow = time.strftime("%X")
awake = "21:06:00"
sleep = "21:30:00"
optimalroom = 24
optimalheat = 30
watering = "07:34:00"
water1 = "07:34:01"
water2 = "07:34:02"
water3 = "07:34:03"
water4 = "07:34:04"
water5 = "07:34:05"
watering1 = "21:34:00"
water11 = "21:34:01"
water12 = "21:34:02"
water13 = "21:34:03"
water14 = "21:34:04"
water15 = "21:34:05"
roomtemp_sensor = "/sys/bus/w1/devices/28-000007c0ab9b/w1_slave" #room temp sensor
heatertemp_sensor = "/sys/bus/w1/devices/28-000007c666b9/w1_slave" #heater temp sensor
def temphigh():
GPIO.output(TEMPOK, GPIO.HIGH)
GPIO.output(TEMPHIGH, GPIO.LOW)
def tempok():
GPIO.output(TEMPOK, GPIO.LOW)
GPIO.output(TEMPHIGH, GPIO.HIGH)
def fanon():
GPIO.output(FAN, GPIO.LOW)
def fanoff():
GPIO.output(FAN, GPIO.HIGH)
def lightson():
GPIO.output(LIGHTS, GPIO.LOW)
def lightsoff():
GPIO.output(LIGHTS, GPIO.HIGH)
def water():
GPIO.output(WATER, GPIO.LOW)
time.sleep(30)
def wateroff():
GPIO.output(WATER, GPIO.HIGH)
def heateron():
GPIO.output(HEATER, GPIO.LOW)
def heateroff():
GPIO.output(HEATER, GPIO.HIGH)
print ("Resetting Room")
GPIO.output(LIGHTS, GPIO.HIGH) # turn lights off
GPIO.output(WATER, GPIO.HIGH) # turn water off
GPIO.output(FAN, GPIO.HIGH) # turn fan off
GPIO.output(HEATER, GPIO.HIGH) # turn heater off
time.sleep(1)
while True:
timenow = time.strftime("%X")
time.sleep(1)
print (timenow)
#start water block
if timenow == watering or timenow == water1 or timenow == water2 or timenow == water3 or timenow == water4 or timenow == water5 or timenow == watering1 or timenow == water11 or timenow == water12 or timenow == water13 or timenow == water14 or timenow == water15:
print ("Watering Now")
water()
else:
wateroff()
# start lighting block
if timenow == sleep and timenow >= sleep:
print ("1Lights Off")
lightsoff()
elif timenow == awake and timenow >= awake:
print ("1Lights On")
lightson()
elif sleep <= timenow:
print ("2Lights Off")
lightsoff()
elif awake == timenow or awake < sleep and sleep > timenow:
print ("2Lights On")
lightson()
# Open temp sensors and check temps etc... but this is working fine.
My output below as it counts to awake = "21:05:00"
Resetting Room
21:05:41
2Lights On
Heat Source Temp: 16.062
Room Temp: 16.25
Temperature OK
Heater On
21:05:46
2Lights On
Heat Source Temp: 16.062
Room Temp: 16.25
Temperature OK
Heater On
21:05:50
2Lights On
Heat Source Temp: 16.125
Room Temp: 16.25
Temperature OK
Heater On
21:05:54
2Lights On
Heat Source Temp: 16.062
Room Temp: 16.25
Temperature OK
Heater On
21:05:58
2Lights On
Heat Source Temp: 16.062
Room Temp: 16.25
Temperature OK
Heater On
21:06:02
2Lights On
Heat Source Temp: 16.062
Room Temp: 16.25
Temperature OK
Heater On
21:06:06
2Lights On
Heat Source Temp: 16.062
Room Temp: 16.25
Temperature OK
Heater On
At current I'd hoped the lights be off as at 21:05:57 ... as 21:05:57 is before awake = "21:06:00"
I am going to set awake = "07:30:00" and let it run to see what happens then...
Any help is appreciated.
The reason why most of this code would fail is because youre comparing time in string with <
and >
. Try parsing these strings to DateTime objects or converting them to timestamps, before comparing them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.