Toggling rpi3 GPIO pins with a Python script - python

I am a novice programmer. I learned to code in Basic back in the Commodore 64 days. Recently, I purchased a Raspberry Pi 3 and an 8-channel relay board and am writing a script in Python 3.x to control the relay board via the GPIO pins. The script I have written works but I suspect it is not very efficient. Any critiques or advice would be appreciated.
That being said, I would like to alter the script to toggle the pin's state when it's number is input. Currently, when the user inputs a number, the pin becomes active for a few seconds then shuts down. I would like for the pin to remain active until the user chooses the same number again. I am fairly certain I could code something that will work but, my initial thoughts would be to start another loops that would turn it off again. Not sure if that makes sense but I AM sure it would not be an elegant or efficiently coded solution.
Any suggestions or help would be appreciated.
import RPi.GPIO as GPIO # import the GPIO Library
import time # import system time
GPIO.setmode(GPIO.BCM) # set pin numbering
PinList = [2, 3, 4, 17, 27, 22, 10, 9] # init list with pin numbers
SleepTime = 4 # set sleep delay variable
for i in PinList: # loop through pins and set mode and state to 'low'
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
# Get user input, turn on the appropriate GPIO pin, pause, then turn it off.
while True:
choice = input("\nEnter a number between 1 and 8.\nEnter 0 to exit: ")
if choice == '0':
break
if choice == '1':
print("You chose 1")
GPIO.output(9, GPIO.LOW)
time.sleep(SleepTime)
GPIO.output(9, GPIO.HIGH)
elif choice == '2':
print("You chose 2")
GPIO.output(10, GPIO.LOW)
time.sleep(SleepTime)
GPIO.output(10, GPIO.HIGH)
elif choice == '3':
print("You chose 3")
GPIO.output(22, GPIO.LOW)
time.sleep(SleepTime)
GPIO.output(22, GPIO.HIGH)
elif choice == '4':
print("You chose 4")
GPIO.output(27, GPIO.LOW)
time.sleep(SleepTime)
GPIO.output(27, GPIO.HIGH)
elif choice == '5':
print("You chose 5")
GPIO.output(17, GPIO.LOW)
time.sleep(SleepTime)
GPIO.output(17, GPIO.HIGH)
elif choice == '6':
print("You chose 6")
GPIO.output(4, GPIO.LOW)
time.sleep(SleepTime)
GPIO.output(4, GPIO.HIGH)
elif choice == '7':
print("You chose 7")
GPIO.output(3, GPIO.LOW)
time.sleep(SleepTime)
GPIO.output(3, GPIO.HIGH)
elif choice == '8':
print("You chose 8")
GPIO.output(2, GPIO.LOW)
time.sleep(SleepTime)
GPIO.output(2, GPIO.HIGH)
else:
print("\nThat is not a valid input.")
print ("\n Quit")
GPIO.cleanup() # Reset GPIO pin settings

I don't use Python for my Pi coding, but I am quite familiar with the language. Here's a quickly drummed up example that cleans up your code quite a bit, and gets you the desired action; the pin will remain in it's last state until selected a second time, which will toggle it to it's opposite state.
Instead of using a bunch of if/else statements, we simply map all of the pins to the selection number in a dictionary, and throw the user's selection into the dict as a key to get back out the BCM pin number and act on it directly. You'll almost certainly want to add in further error checking (if a user sends in an alpha char for example):
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
pins = {
1: 9,
2: 10,
3: 22,
4: 27,
5: 17,
6: 4,
7: 3,
8: 2
}
for pin in pins.values():
GPIO.setup(pin, GPIO.OUT)
while True:
choice = int(input("\nenter a pin number (0 to exit): "))
if choice == 0:
exit()
if choice not in pins.keys():
print("\ninvalid selection...\n")
continue
pin = pins[choice]
print("working on pin {}\n".format(pin))
GPIO.output(pin, not GPIO.input(pin))
print("pin {}: status {}\n".format(pin, GPIO.input(pin)))
GPIO.cleanup()

Related

Is there a way to have the red LED turn on first upon pressing button, then have blue LED turn on and lastly green LED turn on?

I am working through some LED tasks on my raspberry pi 4. I have the following code and I am getting the output on the breadboard wrong, which mean the code is faulty. Since I set the 'previous_button_state = 0', this would mean only the first 'if' would be executed (red LED). Since the previous_button_state is assigned a 1 in that 'if' statement, it should not be turning the red LED on again until the code gets to the 'elif', where the previous_button_state is reassigned the number 0 again. Instead, all three LED keep turning on every time I press the button.
import RPi.GPIO as GPIO
import time
previous_button_state = 0
LED_PIN3 = 22
LED_PIN2 = 27
LED_PIN = 17
BUTTON_PIN = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(LED_PIN2, GPIO.OUT)
GPIO.setup(LED_PIN3, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.output(LED_PIN2, GPIO.LOW)
GPIO.output(LED_PIN3, GPIO.LOW)
while True:
if (GPIO.input(BUTTON_PIN) == GPIO.HIGH) and previous_button_state == 0:
GPIO.output(LED_PIN, GPIO.HIGH)
GPIO.output(LED_PIN2, GPIO.LOW)
GPIO.output(LED_PIN3, GPIO.LOW)
previous_button_state = 1
elif (GPIO.input(BUTTON_PIN) == GPIO.HIGH) and previous_button_state == 1:
GPIO.output(LED_PIN2, GPIO.HIGH)
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.output(LED_PIN3, GPIO.LOW)
previous_button_state = 2
elif (GPIO.input(BUTTON_PIN) == GPIO.HIGH) and previous_button_state == 2:
GPIO.output(LED_PIN3, GPIO.HIGH)
GPIO.output(LED_PIN2, GPIO.LOW)
GPIO.output(LED_PIN, GPIO.LOW)
previous_button_state = 0
else:
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.01)
GPIO.cleanup()

Wait for input during several iterations of a loop in python

I'm trying to write a python code to control the voltage of the GPIO Pins.
The idea behind it is that in a frequency of 1KHz if half of the time the pin is off and half of the time it is on than the voltage will be 50%.
I need to keep the while loop running while waiting for user input so that I can change the speed of the motor.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
# The higher the number for speed the slower the motor will rotate
def motor(speed):
i = 0
run = True
# Runs until user tells it to stop
while run == True:
# Pins should vary at every second
time.sleep(0.001)
i += 1
if i % speed == 0:
GPIO.output(4, GPIO.HIGH)
else:
GPIO.output(4, GPIO.LOW)
# This block is causing the issue
# I need the loop to keep running while waiting for the input
if i % speed * 10 == 0:
com = int(input("Enter 0 to quit"))
if com == -1:
GPIO.output(4, GPIO.LOW)
break
GPIO.output(4, GPIO.LOW)
run = True;
while (run):
speed = int(input("Select Speed"))
if speed == 0:
run = False
else:
motor(speed)
GPIO.cleanup()

Python+ Raspi: change LED color based on # of button presses

I wrote the following code to change the color of an LED on my breadboard from blue to violet to red, and then flash. When the program is run, the LED turns on and stays blue the entire time, regardless of the button presses.
import RPi.GPIO as GPIO
import time
limit = 10
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Rpin = 11
Bpin = 13
flow = 0
try:
while True:
if GPIO.input(15) == False:
flow += 1
print flow
time.sleep(0.1)
if flow >= limit:
False
if flow < 6*limit//10:
print("blue")
GPIO.setup(Bpin, GPIO.OUT)
GPIO.output(Bpin, GPIO.HIGH)
elif flow in range(6*limit//10, 8*limit//10):
print("violet")
GPIO.setup(Rpin, GPIO.OUT)
GPIO.output(Rpin, GPIO.HIGH)
elif flow in range(8*limit//10, limit):
print("red")
GPIO.output(Bpin, GPIO.LOW)
else:
print("flashing red...")
GPIO.output(Rpin, GPIO.LOW)
time.sleep(.2)
GPIO.output(Rpin, GPIO.HIGH)
time.sleep(.2)
GPIO.output(Rpin, GPIO.LOW)
time.sleep(.2)
GPIO.output(Rpin, GPIO.HIGH)
time.sleep(.2)
GPIO.output(Rpin, GPIO.LOW)
break
except KeyboardInterrupt:
GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)
GPIO.cleanup()
I can make the LED work correctly with key presses, and can count the button presses and print that value, but the two do not work in conjunction. I'm fairly certain that this is not a wiring issue, as other code can make these parts work on their own. I have read related posts on Stack and other websites and haven't found any code similar to mine, where an increment changes the color of the LED. As a relatively new python user, I suspect there is a problem in the code. Any help is greatly appreciated!
import RPi.GPIO as GPIO
import time #added for sleep mode
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Rpin = 11
Bpin = 13
flow = 0
GPIO.setup(Bpin, GPIO.OUT)
GPIO.setup(Rpin, GPIO.OUT)
GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)
limit = int(raw_input("How many gallons of water would you like to use for your shower? "))
print("You may begin showering")
print limit
def flowmeter(f, l):
if f == 0:
print("off")
GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)
GPIO.cleanup()
if f < 6*l//10:
print("blue")
GPIO.output(Bpin, GPIO.HIGH)
elif f in range(6*l//10, 8*l//10):
print("violet")
GPIO.output(Rpin, GPIO.HIGH)
elif f in range(8*l//10, l):
print("red")
GPIO.output(Bpin, GPIO.LOW)
else:
print("flashing red... time's up!")
GPIO.output(Rpin, GPIO.LOW)
time.sleep(.5)
GPIO.output(Rpin, GPIO.HIGH)
time.sleep(.5)
GPIO.output(Rpin, GPIO.LOW)
time.sleep(.5)
GPIO.output(Rpin, GPIO.HIGH)
time.sleep(.5)
GPIO.output(Rpin, GPIO.LOW)
print("cleaning GPIO")
GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)
GPIO.cleanup()
while True:
if GPIO.input(15) == False:
flow += 1
print("flow = %s" %flow)
time.sleep(0.1) #prevents infinite pushes if held down for a few seconds
flowmeter(flow,limit)
A debugging tip: if your code is never executing the code in an if statement, it's because the if test is always evaluating to false, and that's what you should investigate.
You need to read the python docs about the built-in range() function - it doesn't do what you are trying to do.
Instead of:
f in range(6*l//10, 8*l//10):
use comparisons e.g.
f >= 6*l//10 and f < 8*l//10

Raspberry Webiopi GPIO one time Email Trigger

I have been trying to make this code I have to only email once after the Door pin has been tripped. I am newish to python and have been reading the posts with similar question and trying what I have seen to my Script. But can't seem to get it to only send the email one time.
enter code here
import webiopi
import datetime
import smtplib
import subprocess
import os
GPIO = webiopi.GPIO
ARM = 7 # GPIO pin using BCM numbering
FLASH = 8
START = 25 # relay
DOOR = 11
HOUR_ON = 8 # Turn Light ON at 08:00
HOUR_OFF = 18 # Turn Light OFF at 18:00
def setup():
GPIO.setFunction(ARM, GPIO.OUT)
GPIO.setFunction(FLASH, GPIO.OUT)
GPIO.setFunction(START, GPIO.OUT)
GPIO.setup(DOOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
now = datetime.datetime.now()
# test if we are between ON time and tun the light ON
if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
GPIO.digitalWrite(ARM, GPIO.HIGH)
GPIO.digitalWrite(FLASH, GPIO.HIGH)
def loop():
# retrieve current datetime
now = datetime.datetime.now()
# toggle light ON all days at the correct time
if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.LOW):
GPIO.digitalWrite(ARM, GPIO.HIGH)
# toggle light OFF
if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(ARM, GPIO.LOW)
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(FLASH, GPIO.HIGH)
webiopi.sleep(.5)
GPIO.digitalWrite(FLASH, GPIO.LOW)
if (GPIO.digitalRead(DOOR) == GPIO.HIGH):
SendText()
# gives CPU some time before looping again
webiopi.sleep(1)
def SendText():
os.system("python /home/pi/html/myemail.py")
# destroy function is called at WebIOPi shutdown
def destroy():
GPIO.digitalWrite(ARM, GPIO.LOW)
GPIO.digitalWrite(START, GPIO.LOW)
#webiopi.macro
def getLightHours():
return "%d;%d" % (HOUR_ON, HOUR_OFF)
#webiopi.macro
def setLightHours(on, off):
global HOUR_ON, HOUR_OFF
HOUR_ON = int(on)
HOUR_OFF = int(off)
return getLightHours()
You can set a variable, send_email, to track if it is time to send an email and if that email has been sent.
Create a global variable, send_email = False, because the variable will go inside the loop method and needs to be initialized.
Add to the SendText() if statement and send_email
After the SendText() line set send_email = False
Add another if statement to check if GPIO.LOW and change the send_email to True
I have made the suggested changes to your code. I put * around all the text I added. Make sure to remove all the * from the code for it to work.
import webiopi
import datetime
import smtplib
import subprocess
import os
GPIO = webiopi.GPIO
ARM = 7 # GPIO pin using BCM numbering
FLASH = 8
START = 25 # relay
DOOR = 11
HOUR_ON = 8 # Turn Light ON at 08:00
HOUR_OFF = 18 # Turn Light OFF at 18:00
*send_email = False*
def setup():
GPIO.setFunction(ARM, GPIO.OUT)
GPIO.setFunction(FLASH, GPIO.OUT)
GPIO.setFunction(START, GPIO.OUT)
GPIO.setup(DOOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
now = datetime.datetime.now()
# test if we are between ON time and tun the light ON
if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
GPIO.digitalWrite(ARM, GPIO.HIGH)
GPIO.digitalWrite(FLASH, GPIO.HIGH)
def loop():
# retrieve current datetime
now = datetime.datetime.now()
# toggle light ON all days at the correct time
if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.LOW):
GPIO.digitalWrite(ARM, GPIO.HIGH)
# toggle light OFF
if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(ARM, GPIO.LOW)
if (GPIO.digitalRead(ARM) == GPIO.HIGH):
GPIO.digitalWrite(FLASH, GPIO.HIGH)
webiopi.sleep(.5)
GPIO.digitalWrite(FLASH, GPIO.LOW)
if (GPIO.digitalRead(DOOR) == GPIO.HIGH) *and send_email*:
SendText()
*send_email = False*
*if (GPIO.digitalRead(DOOR) == GPIO.LOW) and not send_email*:
*send_email = True*
# gives CPU some time before looping again
webiopi.sleep(1)
def SendText():
os.system("python /home/pi/html/myemail.py")
# destroy function is called at WebIOPi shutdown
def destroy():
GPIO.digitalWrite(ARM, GPIO.LOW)
GPIO.digitalWrite(START, GPIO.LOW)
#webiopi.macro
def getLightHours():
return "%d;%d" % (HOUR_ON, HOUR_OFF)
#webiopi.macro
def setLightHours(on, off):
global HOUR_ON, HOUR_OFF
HOUR_ON = int(on)
HOUR_OFF = int(off)
return getLightHours()

LED control on Raspberry Pi by GPIO with Python

I am using momentary switches wired to GPIO pins on a Raspberry Pi to control 4 LEDs.
I have five buttons wired up.
The first 4 buttons when pressed toggle the state of a connected LED from on to off or off to on depending on the current state.
The fifth button turns on all 4 LEDs or turns off all 4 LEDs based on the on/off state of GPIO 18.
I also have an extra LED wired to GPIO pin 18, this simply turns on or off based on the state of pin 18.
The idea of this project is to be able to independently control the LEDs and have a master control button as well. The LED attached to pin 18 is a monitoring LED, it should be on if ANY of the 4 LEDs is on and it should only be off if all 4 of the LEDs are off simultaneously.
This is all based on python scripts that monitor for button presses and act accordingly.
The code I've written to control all LEDs at once looks like this:
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
chan_list = (4,17,22,27)
GPIO.setup(chan_list,GPIO.OUT)
GPIO.output(chan_list,0)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,0)
while True:
input_state = GPIO.input(26)
if input_state == False:
chan_list = (4,17,22,27)
GPIO.output(18, not GPIO.input(18))
time.sleep(0.1)
GPIO.output(chan_list, GPIO.input(18))
time.sleep(0.4)
This code appears to operate perfectly. As you can see it toggles the current state of pin 18 and then applies that state to pins 4,17,22 and 27 which are the pins the LEDs are connected to.
The code I've written to control the individual LEDs is a bit more complicated and it looks like this:
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,0)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
while True:
input_state = GPIO.input(5)
if input_state == False:
if GPIO.output(17,0) == False:
GPIO.output(4, not GPIO.input(4))
elif GPIO.output(22,0) == False:
GPIO.output(4, not GPIO.input(4))
elif GPIO.output(27,0) == False:
GPIO.output(4, not GPIO.input(4))
else:
GPIO.output(4, not GPIO.input(4))
time.sleep(0.1)
GPIO.output(18, GPIO.input(4))
time.sleep(0.4)
There are 4 versions of this with the pins adjusted, one version for each LED, This version uses pin 5 as an input to detect button presses and pin 4 as an output to activate the LED. What I want is to do is this:
When pressed (if LED1 is off)
- Toggle LED1 on, also toggle pin 18 on to activate the indicator light.
- Take no further action.
When pressed (if LED1 is on)
- Check if pin 17 is on or off;
- If pin 17 is on toggle LED1 off and take no further action.
- If pin 17 is off check if pin 22 is on or off;
- If pin 22 is on toggle LED1 off and take no further action.
- If pin 22 is off check if pin 27 is on or off;
- If pin 27 is on toggle LED1 off and take no further action.
- If pin 27 is off toggle LED1 off then set pin 18 to the current status of pin 4 (LED1).
However, what is actually happening is this:
When pressed (if LED1 is off)
- Turns off all of the other 3 LEDs then toggles LED1 on and toggles pin 18 on.
When pressed (if LED1 is off)
- Turns off all LEDs and toggles pin 18 off.
I can not for the life of me figure this out.
Your help is hugely appreciated.
P.S. Pardon my ignorance, I started learning python yesterday and have no prior programming experience at all. I'm sure it's something simple but I can't seem to solve it.
Sounds like you have 3 logical blocks:
Individual LED control
Master LED control
Monitor-LED indicator control
Decouple your code into functions, let's name them checkIndividualButton, checkMasterButton and updateMonitorLed, one for each logical block, and call them from your main loop
import RPi.GPIO as GPIO
import time
# our 3 functions will go here, yet to be written
# setup pins here
all_leds = [???,???,???,???]
GPIO.setup blah blah blah
while True:
checkIndividualButton(button_pin=17, led_pin=4) # assuming button wired to pin 17 controls LED on pin 4
checkIndividualButton(????, ????) # fill this in
checkIndividualButton(????, ????) # fill this in
checkIndividualButton(????, ????) # fill this in
checkMasterButton(master_button_pin=26, monitor_led_pin=18, all_leds) # notice reference to all_leds which we setup above
updateMonitorLed(all_leds, monitor_led_pin=18)
Now all you have to do is implement individual functions, each doing Just One Job(TM):
def checkIndividualButton(button_pin, led_pin):
is_pressed = GPIO.input(button_pin)
if is_pressed:
GPIO.output(led_pin, not GPIO.input(led_pin))
def checkMasterButton(master_button_pin, monitor_led_pin, all_led_pins):
is_pressed = GPIO.input(master_button_pin)
if is_pressed:
GPIO.output(monitor_led_pin, not GPIO.input(monitor_led_pin))
time.sleep(0.1)
GPIO.output(all_led_pins, GPIO.input(monitor_led_pin))
time.sleep(0.4)
def updateMonitorLed(all_leds_pins, monitor_led_pin):
is_any_led_on = False
for led_pin in all_leds_pins:
if GPIO.input(led_pin):
is_any_led_on = True
GPIO.output(monitor_led_pin, is_any_led_on)
time.sleep(0.1)
Paste this block of functions into the right place in your main program.
DISCLAIMER: I have not tested this. There are ways to optimize and cleanup the code further, happy to guide you in comments if you'd like.
Thank you #pbkhrv for an incredibly helpful answer.
I learned a lot from it and managed to get a piece of code that works perfectly for my needs.
In the end I have 2 python scripts running, one which changes the pin status on the press of a button:
import RPi.GPIO as GPIO
import webiopi
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
chan_list = (4,17,22,27)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,0)
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,0)
GPIO.setup(22,GPIO.OUT)
GPIO.output(22,0)
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,0)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,0)
while True:
#This portion is pin 4 control from physical switches
if GPIO.input(5) == False:
GPIO.output(4, not GPIO.input(4))
time.sleep(0.3)
#This portion is pin 17 control from physical switches
if GPIO.input(6) == False:
GPIO.output(17, not GPIO.input(17))
time.sleep(0.3)
#This portion is pin 22 control from physical switches
if GPIO.input(19) == False:
GPIO.output(22, not GPIO.input(22))
time.sleep(0.3)
#This portion is pin 27 control from physical switches
if GPIO.input(13) == False:
GPIO.output(27, not GPIO.input(27))
time.sleep(0.3)
#This portion is pins 4,17,22,27 as one control from physical switches. Toggles all on/off
# based on the current state of pin 18.
if GPIO.input(26) == False:
chan_list = (4,17,22,27)
GPIO.output(18, not GPIO.input(18))
# time.sleep(0.01)
GPIO.output(chan_list, GPIO.input(18))
time.sleep(0.3)
and this which looks after the indicator LED:
import webiopi
import time
GPIO = webiopi.GPIO
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,0)
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,0)
GPIO.setup(22,GPIO.OUT)
GPIO.output(22,0)
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,0)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,0)
# loop function is repeatedly called by WebIOPi
while True:
#Block to control pin 18 state by pin 4 state
if (GPIO.digitalRead(4) == GPIO.HIGH):
#webiopi.sleep(0.1)
GPIO.digitalWrite(18, GPIO.HIGH)
if (GPIO.digitalRead(4) == GPIO.LOW):
webiopi.sleep(0.01)
if (GPIO.digitalRead(17) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(22) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(27) == GPIO.HIGH):
webiopi.sleep(0.01)
else:
GPIO.digitalWrite(18, GPIO.LOW)
webiopi.sleep(0.01)
#Block to control pin 18 state by pin 17 state
if (GPIO.digitalRead(17) == GPIO.HIGH):
#webiopi.sleep(0.1)
GPIO.digitalWrite(18, GPIO.HIGH)
if (GPIO.digitalRead(17) == GPIO.LOW):
webiopi.sleep(0.01)
if (GPIO.digitalRead(4) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(22) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(27) == GPIO.HIGH):
webiopi.sleep(0.01)
else:
GPIO.digitalWrite(18, GPIO.LOW)
webiopi.sleep(0.01)
#Block to control pin 18 state by pin 22 state
if (GPIO.digitalRead(22) == GPIO.HIGH):
#webiopi.sleep(0.1)
GPIO.digitalWrite(18, GPIO.HIGH)
if (GPIO.digitalRead(22) == GPIO.LOW):
webiopi.sleep(0.01)
if (GPIO.digitalRead(4) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(17) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(27) == GPIO.HIGH):
webiopi.sleep(0.01)
else:
GPIO.digitalWrite(18, GPIO.LOW)
webiopi.sleep(0.01)
#Block to control pin 18 state by pin 27 state
if (GPIO.digitalRead(27) == GPIO.HIGH):
#webiopi.sleep(0.1)
GPIO.digitalWrite(18, GPIO.HIGH)
if (GPIO.digitalRead(27) == GPIO.LOW):
webiopi.sleep(0.01)
if (GPIO.digitalRead(4) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(17) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(22) == GPIO.HIGH):
webiopi.sleep(0.01)
else:
GPIO.digitalWrite(18, GPIO.LOW)
webiopi.sleep(0.01)
These two python scripts launch at the boot of the raspberry pi as well as webiopi which gives me a web UI that can control the LEDs as well.
These 3 things together give me exactly what I was after and a web interface that live updates the button for each LED based on it's current HIGH or LOW status.
This has been a proof of concept, THE LEDs will shortly be replaced by relays which will turn on or off connected sets of speakers in different rooms of my house, Raspberry Pi multiroom audio controller. The raspberry pi will also be the source of the audio to the connected zones with airplay and bluetooth streaming.

Categories