DJ_Steve
DJ_Steve

Reputation: 61

python & GPIO scripting

im working on a python script using sockets and gpio inputs for a project on a RPi.

i am attempting to get the main code to get the hardware switches working first before looking into socket usage.

but i am getting a syntax error that i dont have a clue about;

i should say i have NO real idea how to code in python at all as my expertise is in php.

i have included my code below.

i would also be interested in some help on how best to use sockets to control the output lines OVERRIDING whatever the physical switch may be set as

please help folks this is driving me balmy now :)

import socket
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

r1=0
r2=0
r3=0
r4=0
r5=0
r6=0

#Input switches
i1=11
i2=5
i3=6
i4=13
i5=19
i6=26

#output relays
o1=2
o2=3
o3=4
o4=17
o5=27
o6=22

#setup GPIOs
GPIO.setup(i1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(o1, GPIO.OUT, initial=1)
GPIO.setup(i2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(o2, GPIO.OUT, initial=1)
GPIO.setup(i3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(o3, GPIO.OUT, initial=1)
GPIO.setup(i4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(o4, GPIO.OUT, initial=1)
GPIO.setup(i5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(o5, GPIO.OUT, initial=1)
GPIO.setup(i6, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(o6, GPIO.OUT, initial=1)

#setting up php socket link
s = socket.socket()
host = "localhost"
port = 12345
s.bind((host, port))

s.listen(5)
while True:
   1sta=GPIO.input(i1)
   2sta=GPIO.input(i2)
   3sta=GPIO.input(i3)
   4sta=GPIO.input(i4)
   5sta=GPIO.input(i5)
   6sta=GPIO.input(i6)

   c, addr = s.accept()
   data = c.recv(1024)

   if 1sta == False:
        GPIO.output(o1,0)
        print "1 On"
   if 1sta == True:
        GPIO.output(o1, 1)
        print "1 Off"
   if 2sta == False:
        GPIO.output(o2,0)
        print "2 On"
   if 2sta == True:
        GPIO.output(o2, 1)
        print "2 Off"
   if 3sta == False:
        GPIO.output(o3,0)
        print "3 On"
   if 3sta == True:
        GPIO.output(o3, 1)
        print "3 Off"
   if 4sta == False:
        GPIO.output(o4,0)
        print "4 On"
   if 4sta == True:
        GPIO.output(o4, 1)
        print "4 Off"
   if 5sta == False:
        GPIO.output(o5,0)
        print "5 On"
   if 5sta == True:
        GPIO.output(o5, 1)
        print "5 Off"
   if 6sta == False:
        GPIO.output(o6,0)
        print "6 On"
   if 6sta == True:
        GPIO.output(o6, 1)
        print "6 Off"



   c.close()

Upvotes: 0

Views: 88

Answers (1)

h4rry
h4rry

Reputation: 11

Python variables can't start with numbers! You need to change:

1sta=GPIO.input(i1)

to:

sta1=GPIO.input(i1)

Upvotes: 1

Related Questions