Page 1 of 1

I am trying to turn on LED via LAN

Posted: Sat Nov 24, 2018 1:24 pm
by tobiastakao
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)

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()
Best regards,
tobi

Re: I am trying to turn on LED via LAN

Posted: Sat Nov 24, 2018 2:35 pm
by ghp
Hello,
the decode-method should be written as
data = data.decode('utf-8')
This makes the code work. What is helpful here to write the received data to console also like

Code: Select all

                while True:
                    data = conn.recv(BUFSIZ) # Receive data sent from the client
                    data = data.decode('utf-8')  # write the result of decode into same variable
                    print("data", data)

Re: I am trying to turn on LED via LAN

Posted: Sat Nov 24, 2018 4:35 pm
by PhatFil
May i suggest you take a different approach and check out MQTT Brokers, you can host mosquitto on the pi and then publish mqtt messages from anywhere on your network and in order to respond just run a daemon (script) to subscribe to the relevant Topic and react accordingly (turn on led for example) mqtt messages have a Topic (think address or key) and a payload (think data, set or record)

if you also look at hosting node red on your pi you could also use that to subscribe and respond to the mqtt led messages from other network clients
this guy uses a TWATer a/c rather than a mqtt message to trigger the led on/off but it illustrates how easy the 'programming' can be once you get used to the node red idea of data flows rather than execution flow being germain.
https://www.youtube.com/watch?v=5Y5yHONKFt4
And its very short ;)
Lots of other utubes on mqtt/node-red worth a look too if this seems like a fruitful way forward?

Re: I am trying to turn on LED via LAN

Posted: Sun Nov 25, 2018 12:37 am
by tobiastakao
Thank you ghp, it works perfectly.

Thank you PhatFil, watched the youtube, it is interested,
alternatively, there is another IOT gagget for your reference:

Blynk
https://www.youtube.com/watch?v=hipyrhJIgGk

Blynk an LED on Raspberry Pi
https://www.youtube.com/watch?v=LJ3ic8C8CcA

For Blynk installation part, I have referred following site (please translate) as this is simpler:
https://makerslove.com/10051.html