I am trying to turn on LED via LAN
Posted: Sat Nov 24, 2018 1:24 pm
Hi I am seeking advise for following issue:
Following code can connect to Raspberry pi 3 (with Python3) but always give me "error cmd!" message when I input ON at client side, please provide advise, thank you.
(Server Side)
(Client Side)
Best regards,
tobi
Following code can connect to Raspberry pi 3 (with Python3) but always give me "error cmd!" message when I input ON at client side, please provide advise, thank you.
(Server Side)
Code: Select all
import socket
from time import ctime
import RPi.GPIO as GPIO
LedPin = 11
HOST = ''
PORT = 5710
BUFSIZ = 1024 # Size of the buffer
ADDR = (HOST, PORT)
ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create a socket
ss.bind(ADDR) # Bind the IP address and port number
ss.listen(5)
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def loop():
while True:
print ('Waiting for connection...')
conn, addr = ss.accept()
print ('...connected from :', addr) # Print the IP address of the client connected with the server
while True:
data = conn.recv(BUFSIZ) # Receive data sent from the client
data.decode('utf-8')
if data == 'ON':
GPIO.output(LedPin, GPIO.LOW)
print ('led on')
elif data == 'OFF':
GPIO.output(LedPin, GPIO.HIGH)
print ('led off')
else:
print ('error cmd !')
tcpSerSock.close()
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
tcpSerSock.close()
(Client Side)
Code: Select all
import socket
HOST = '192.168.1.113' # Server(Raspberry Pi) IP address
PORT = 5710
BUFSIZ = 1024 # buffer size
ADDR = (HOST, PORT)
cs = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create a socket
cs.connect(ADDR) # Connect with the server
def loop():
while True:
st = input('input cmd : ')
byt = st.encode()
cs.send(byt)
if __name__ == '__main__':
try:
loop()
except KeyboardInterrupt:
tcpCliSock.close()
tobi