I am using a Raspberry Pi with Raspbian a python script to control a relay. The relay is timed for 12/12 on/off for a lighting module.
The code:
The start and stop timers are created at class initialization:
self.timer_on = datetime.time(self.thresholds['TON'],0)
self.timer_off = datetime.time(self.thresholds['TOFF'], 0)
self.timer_on
and self.timer_off
is used in this function:
def timer_check(self):
now = datetime.datetime.now().time()
if self.timer_on <= now < self.timer_off:
GPIO.output(self.light_pin, False) #Turn the light on
else:
GPIO.output(self.light_pin, True) #Turn the light off
This is put into the following code:
def check_vs_thresholds(self):
'''
Determines when and if to turn on or off the heater, ac, and humidifier.
Also checks if the temps are high or low enough to warrant creating an alert.
'''
# turn heater on if temp is too low
if self.heat_pin:
self.heater_check()
# turn ac on if temp is too high
if self.ac_pin:
self.cooler_check()
#Turn humidifer on if humidity is low
if self.hum_pin:
self.humidity_check()
if self.light_pin:
self.timer_check()
which is then put into the run loop with some other activities:
def run(self):
while True:
self.update_readings_from_sensor()
self.check_vs_thresholds()
self.record()
The controller will not turn the light off consistently. I have the timer set to turn a light on at 10 am, and off at 10pm. This translates to 10 and 22.
The light always turns on at 10am without fail. The light will never turn off at 10pm. If I reset the script after 10pm it will recognize the light should be off and then turn off. The next morning it will turn on at 10am without fail. This means that it recognizes the on and off times.
To test that the chained comparison isn't an issue, I set times closer together. Of set to 12pm and 1pm, then it will function as expected and turn on/off at the proper times. I've tested many different time sets and they all work as long as the time is closer. However it has never worked for 10 and 22.
I receive no error message.