Python help coding
Posted: Mon Feb 29, 2016 10:30 am
by birdman1984
Would like some help coding a python script. I have the raspberry pi 2 board. Through web page submit that runs a python script via php, i have made a light controller for yard lights. What i am after is coding that i can wire a light switch to the board to activate lights with out use of the web page. You could say i want a manual overide switch because not everyone has a smartphone/tablet to turn these lights on. I know a gpio needs to be setup as a input.
This is what i am after for this control
Switch in the off position (input gpio is open) runs through 5 gpio pins and turns them to high with .05 sleep delay and when the switch is in the on position (input gpio is closed) runs through the same 5 gpios and turns them low with .05 sleep delay. This code will be loaded during boot. All the output gpios will be shared with the already inplace web controler.
Thanks for your help.
I tried to use this code from
http://www.instructables.com/id/Raspber ... -the-GPIO/ but the input would trigger the output without even grounding the switch. My finger touching the input pin would turn the light on.
Code: Select all
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(22,GPIO.IN)
# input of the switch will change the state of the LED
while True:
GPIO.output(4,GPIO.input(22))
time.sleep(0.05)
Re: Python help coding
Posted: Tue Mar 01, 2016 7:23 am
by birdman1984
thanks but when i run the script and flip the switch the script stops, it returns me to the terminal and then it wount run again unless i change pins.
Re: Python help coding
Posted: Tue Mar 01, 2016 11:02 am
by Major Tom
I can see two problems. First, you don't have a pull-up on the input pin, so it will float and will be affected by nearby electrical interference. Second, in the second version you don't have a loop anymore, so the code will execute until the switch is pressed and then finish.
Try this. It's your original code with a pull-up turned on.
Code: Select all
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# input of the switch will change the state of the LED
while True:
GPIO.output(4,GPIO.input(22))
time.sleep(0.05)
Re: Python help coding
Posted: Wed Mar 02, 2016 7:21 am
by birdman1984
Thanks major tom. Now I wish i new even more about coding so i could run all of this together because the light switch code runs in a loop, it overrides the web page script. The simple solution would be 2 sets of relays and wire them like a 3 way switch.
If anyone wants to take this on I would appreciate it. If you take this on you will notice in my code that 5 relays are coded for an easy all on or off plus on their own along with other relays on their own. the light switch code needs to work with the all lights code in the web page everything else will be done via the web page. Thanks again.
index.php for web interface
[code]<?php
$ctl = $_POST['ctl'];
$command = "sudo python lights_c.py $ctl";
$result = shell_exec($command);
echo($result);
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<title>Arena light control</title>
<body><p class="head">Arena light system!</p>
<p class="title"> Control the lights from here </p>
<table align="center" border="1">
<tr>
<td>
<form action="" method="POST" name="form">
<p class="title">All Lights </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="1">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="0">
</form>
</td>
<td>
<form action="" method="POST" name="form">
<p class="title"> NE Lights </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="11">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="22">
</form>
</td>
<td>
<form action="" method="POST" name="form">
<p class="title"> NW Lights </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="33">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="44">
</form>
</td>
</tr>
<tr>
<td>
<form action="" method="POST" name="form">
<p class="title">Gate Lights </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="55">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="66">
</form>
</td>
<td>
<form action="" method="POST" name="form">
<p class="title"> SE Lights </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="77">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="88">
</form>
</td>
<td>
<form action="" method="POST" name="form">
<p class="title"> SW Lights </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="99">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="1010">
</form>
</td>
</tr>
<tr>
<td>
<form action="" method="POST" name="form">
<p class="title"> Cattle Lights </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="1020">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="1030">
</form>
</td>
<td>
<form action="" method="POST" name="form">
<p class="title"> Saloon Lights </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="1040">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="1050">
</form>
</td>
<td>
<form action="" method="POST" name="form">
<p class="title"> Crows Nest </p>
<p class="form">
<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl" value="1060">
</form></p>
<form action="" method="POST" name="form">
<p class="lol">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl" value="1070">
</form>
</td>
</tr>
</table>
</p>
</body></html>
[/code]
script that index reads
[code]#!/usr/bin/python
import sys
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
args = sys.argv
pin = [11, 12, 13, 15, 16, 18, 22, 7, 3, 5, 24, 26, 19, 21, 23, 29]
#relay 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#location
#NE= 11 NW=12 Gate=13 SE=15 SW=16
ctl = args[1] #Argument 1 for ON/OFF
if (int(ctl) == 1):
GPIO.setup(11, GPIO.OUT)
GPIO.output(11,GPIO.LOW)
time.sleep(.5)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12,GPIO.LOW)
time.sleep(.5)
GPIO.setup(13, GPIO.OUT)
GPIO.output(13,GPIO.LOW)
time.sleep(.5)
GPIO.setup(15, GPIO.OUT)
GPIO.output(15,GPIO.LOW)
time.sleep(.5)
GPIO.setup(16, GPIO.OUT)
GPIO.output(16,GPIO.LOW)
if (int(ctl) == 0):
GPIO.setup(16, GPIO.OUT)
GPIO.output(16,GPIO.HIGH)
time.sleep(.5)
GPIO.setup(15, GPIO.OUT)
GPIO.output(15,GPIO.HIGH)
time.sleep(.5)
GPIO.setup(13, GPIO.OUT)
GPIO.output(13,GPIO.HIGH)
time.sleep(.5)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12,GPIO.HIGH)
time.sleep(.5)
GPIO.setup(11, GPIO.OUT)
GPIO.output(11,GPIO.HIGH)
if (int(ctl) == 11):
GPIO.setup(11, GPIO.OUT)
GPIO.output(11,GPIO.LOW)
if (int(ctl) == 22):
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, GPIO.HIGH)
if (int(ctl) == 33):
GPIO.setup(12, GPIO.OUT)
GPIO.output(12,GPIO.LOW)
if (int(ctl) == 44):
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, GPIO.HIGH)
if (int(ctl) == 55):
GPIO.setup(13, GPIO.OUT)
GPIO.output(13,GPIO.LOW)
if (int(ctl) == 66):
GPIO.setup(13, GPIO.OUT)
GPIO.output(13, GPIO.HIGH)
if (int(ctl) == 77):
GPIO.setup(15, GPIO.OUT)
GPIO.output(15,GPIO.LOW)
if (int(ctl) == 88):
GPIO.setup(15, GPIO.OUT)
GPIO.output(15, GPIO.HIGH)
if (int(ctl) == 99):
GPIO.setup(16, GPIO.OUT)
GPIO.output(16,GPIO.LOW)
if (int(ctl) == 1010):
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, GPIO.HIGH)
if (int(ctl) == 1020):
GPIO.setup(18, GPIO.OUT)
GPIO.output(18,GPIO.LOW)
if (int(ctl) == 1030):
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
if (int(ctl) == 1040):
GPIO.setup(22, GPIO.OUT)
GPIO.output(22,GPIO.LOW)
if (int(ctl) == 1050):
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, GPIO.HIGH)
if (int(ctl) == 1060):
GPIO.setup(7, GPIO.OUT)
GPIO.output(7,GPIO.LOW)
if (int(ctl) == 1070):
GPIO.setup(7, GPIO.OUT)
GPIO.output(7, GPIO.HIGH)
[/code]
and the above code for the light switch
[code]import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(37,GPIO.IN, pull_up_down=GPIO.PUD_UP)
# input of the switch will change the state of the LED
while True:
time.sleep(.5)
GPIO.output(11,GPIO.input(37))
time.sleep(.5)
GPIO.output(12,GPIO.input(37))
time.sleep(.5)
GPIO.output(13,GPIO.input(37))
time.sleep(.5)
GPIO.output(15,GPIO.input(37))
time.sleep(.5)
GPIO.output(16,GPIO.input(37))
time.sleep(.5)
[/code]
Re: Python help coding
Posted: Thu Mar 03, 2016 7:43 am
by birdman1984
would something like this work i wounder. not sure how to exactly code. My thinking would have it read another py file like the one i use for my webpage interface
Code: Select all
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(37,GPIO.IN, pull_up_down=GPIO.PUD_UP)
# input of the switch will change the state of the LED
if GPIO.input(37) = true
execfile("lights_on.py")
else
execfile("lights_off.py")