I have a rather curious scripting challenge. I have an extraction unit I am trying to control via a Python script and a regular relay. The relay is connected to my RPi 3 Model B using GPIO BCM pin 15, it is grounded and it receives 5V from the pi's VCC pin. So far so good. I have tested the relay and it closes and opens as expected.
The script I have written has a section that defines the length of time it must pause for before proceeding to the next operation. Each operation is given a name "closed and open". Both operations must last for 21600 seconds (6 hours). When the closed operation (extractor on) has concluded, the open operation (extractor off) begins and it is there that I want to try get clever with the script.
What I would like to do is maintain the "open" operation's counter while I change the state of pin 15. So, while the open operation counts up to 21600 seconds, I would like the extractor to switch on for 60 seconds (1 minute), then it must switch off again for 300 seconds (5 minutes). I have tried to figure it out but it seems like any time I change the state of the pin, it stops the counter of the open operation and the script stops running or fails.
I cannot seem to figure out how to keep the timer counting up to 21600 seconds, in the open operation, while I change the state of pin 15. Please see below, this is the script I've been running for some months. It works well, but I'd now like to improve it by doing the above.
So that you are aware, the pi runs headless in an electricity box with a plexi-window so that I can see relay status. I have setup the script to run automatically at startup using rc.local which works perfectly. The first script that runs automatically is a GPIO cleanup script that resets the state of the relay to open (no current flow), in case for some reason it is closed (generally not). The extractor script then runs and the rest is history. I do not restart the pi ever unless the relays get confused (which sometimes they do) and they start behaving weirdly like staying closed even though the open operation has begun, meaning the state of pin 15 will have changed telling the relay to open (haven figured that out) but it only happens once every few weeks but that is a conversation for another day. For now I'd like to understand how I can edit this script to switch the extractor unit on (1 minute) and off (5 minutes) while the open operation counter keeps going.
When the closed operation starts up again, then the extractor unit can run for the full 6 hours. Any assistance from the community will be greatly appreciated. Thanks All.
# !/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# init list with pin numbers
pinList = [15]
# Loop through pins and set mode and state to 'high'.
for i in pinList:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, 1)
# time to sleep between operations in the main loop
Closed = 21600
Open = 21600
# main loop
while True:
GPIO.output(15, 0)
print "Closed"
time.sleep(Closed);
GPIO.output(15, 1)
print "Open"
time.sleep(Open);
PS...I understand that pin state 'low or 0' means no current is flowing, so the relay should be open. Conversely, pin state 'high or 1' means current is flowing so the relay should be closed. This is not the case here. With this relay, low means closed and high means open. That confuses me, although I may have misunderstood what low and high actually means.
You have two options, two process signals while waiting for some time to continue with another operation:
1 nested loops
try doing something like
while True:
print "Closed"
for ii in range(0,Closed):
GPIO.output(15, 0)
time.sleep(1);
#if HERE COMES YOUR CHECK FOR WHATEVER YOU WANT EXACTLY:
# DO SOMETHING
print "Open"
for ii in range(0,Open):
GPIO.output(15, 1)
time.sleep(1);
#if HERE COMES YOUR OTHER CHECK FOR WHATEVER YOU WANT EXACTLY:
# DO SOMETHING
2 threads
Threads are some background processes, which run automatically. Check out some tutorial for threads like this one.
You woud introduce a thread then, which
This thread is started once and your main loop can care about other whings then.
side remark
There is another interesting concept called interrupts. You can basically define a function, which is executed when a certain event happens (for example, when the status of a reading pin is changed = button press). This might be a good example to get started with interrups.