-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverRPi.py
67 lines (60 loc) · 1.98 KB
/
serverRPi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import socket
import os
from _thread import *
import RPi.GPIO as GPIO
# set up server side socket
ServerSideSocket = socket.socket()
ServerSideSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = '192.168.48.238'
port = 22000
ThreadCount = 0
#initialize RPi GPIO
button1 = 24
button2 = 22
GPIO.setmode(GPIO.BOARD)
GPIO.setup(button1, GPIO.OUT)
GPIO.setup(button2, GPIO.OUT)
try:
ServerSideSocket.bind((host, port))
except socket.error as e:
print(str(e))
print('Socket is listening..')
ServerSideSocket.listen(5)
# process connection from client
def multi_threaded_client(connection):
connection.send(str.encode('Server is working:'))
while True:
#receive data from client
data = connection.recv(2048)
cmd = data.decode()
print ("Received command: " + str(cmd) + " len: " + str(len(cmd)))
# server toggles button1 and button2 based on request from client
if cmd == "1":
if GPIO.input(button1) == GPIO.LOW:
GPIO.output(button1,1)
data = "Button1 pressed"
else:
GPIO.output(button1,0)
data = "Button1 released"
if cmd == "2":
if GPIO.input(button2) == GPIO.LOW:
GPIO.output(button2,1)
data = "Button2 pressed"
else:
GPIO.output(button2,0)
data = "Button2 released"
print (data)
response = 'Server message: ' + data
if not data:
break
#send message to client
connection.sendall(str.encode(response))
connection.close()
while True:
#accept connection from client and start a thread
Client, address = ServerSideSocket.accept()
print('Connected to: ' + address[0] + ':' + str(address[1]))
start_new_thread(multi_threaded_client, (Client, ))
ThreadCount += 1
print('Thread Number: ' + str(ThreadCount))
ServerSideSocket.close()