-
Notifications
You must be signed in to change notification settings - Fork 0
/
LEDServer.py
165 lines (147 loc) · 4.4 KB
/
LEDServer.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Created on July 10 2022
@author:
PiE Homework 4
Server to run on Raspberry Pi that detects switch inputs and outputs signals on connected LEDs
This code is designed to send updates to Client.py with swithc states every second
"""
import RPi.GPIO as gpio
import time
import threading
import socket
import subprocess
import os
HOST = subprocess.check_output(["hostname", "-I"]).decode()[0:-2]
PORT = 3454
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
rLED = 16
gLED = 18
SW1 = 8
SW2 = 10
SW3 = 12
gpio.setup(SW1, gpio.IN, pull_up_down = gpio.PUD_UP)
gpio.setup(SW2, gpio.IN, pull_up_down = gpio.PUD_UP)
gpio.setup(SW3, gpio.IN, pull_up_down = gpio.PUD_UP)
gpio.setup(rLED, gpio.OUT)
gpio.setup(gLED, gpio.OUT)
global sw1Status
global sw2Status
global gLEDTmr
global rLEDTmr
sw1Status = "Released"
sw2Status = "Released"
gLEDTmr = 0
rLEDTmr = 0
def SW1_callback(channel):
global sw1Status
global rLEDTmr
time.sleep(.05)
if gpio.input(SW1) == gpio.LOW:
rLEDTmr = 200
sw1Status = "Pressed"
elif gpio.input(SW1) == gpio.HIGH:
rLEDTmr = 0
gpio.output(rLED, False)
sw1Status = "Released"
def SW2_callback(channel):
global sw2Status
global gLEDTmr
time.sleep(.05)
if gpio.input(SW2) == gpio.LOW:
gLEDTmr = 100
sw2Status = "Pressed"
if gpio.input(SW2) == gpio.HIGH:
gLEDTmr = 0
gpio.output(gLED, False)
sw2Status = "Released"
def SW3_callback(channel):
i = 0
if gpio.input(SW3) == gpio.LOW:
for client in clients:
try:
client.sendall("SW3 pressed, server shutting down...".encode())
except:
print(str(client) + "disconnected")
clients.remove(client)
while(gpio.input(SW3) == gpio.LOW):
time.sleep(.1)
i += .1
if i >= 3:
try:
client.sendall("Server shutting down. Connection terminated.".encode())
except:
time.sleep(.1)
print("Shutting down...")
os.system("shutdown now -h")
time.sleep(1)
def clientThread():
while True:
status = "{SW1: " + sw1Status + ", SW2: " + sw2Status + "}"
status = status.encode()
for client in clients:
try:
client.sendall(status)
except:
print("Client disconnected")
clients.remove(client)
time.sleep(1)
def rLEDThread():
global rLEDTmr
while True:
if rLEDTmr > 0:
if rLEDTmr == 200:
gpio.output(rLED, True)
elif rLEDTmr == 100:
gpio.output(rLED, False)
rLEDTmr -= 1
if rLEDTmr == 0:
rLEDTmr = 200
time.sleep(.01)
def gLEDThread():
global gLEDTmr
while True:
if gLEDTmr > 0:
if gLEDTmr == 100:
gpio.output(gLED, True)
elif gLEDTmr == 50:
gpio.output(gLED, False)
gLEDTmr -= 1
if gLEDTmr == 0:
gLEDTmr = 100
time.sleep(.01)
gpio.add_event_detect(SW1, gpio.BOTH, callback=SW1_callback, bouncetime=10)
gpio.add_event_detect(SW2, gpio.BOTH, callback=SW2_callback, bouncetime=10)
gpio.add_event_detect(SW3, gpio.BOTH, callback=SW3_callback, bouncetime=10)
#rLEDPWM.start(0)
#gLEDPWM.start(0)
tRLED = threading.Thread(target = rLEDThread)
tRLED.start()
tGLED = threading.Thread(target = gLEDThread)
tGLED.start()
clients = list()
if HOST == "":
print("Warning, not connected to network. Running in offline mode")
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((HOST, PORT))
#s.bind(("", PORT))
print("Socket bound")
except socket.error as message:
print(message)
#s.connect(("8.8.8.8", 80))
print("IP address: " + HOST)
print("Port: " + str(PORT))
#print(s)
s.listen(5)
#s.close
t1 = threading.Thread(target = clientThread)
t1.start()
print("Connecting to clients...")
while(True):
conn, addr = s.accept()
conn.sendall("Connection successful, sending switch status...".encode())
print("Connected to " + addr[0] + " on socket " + str(addr[1]))
#print(conn)
clients.append(conn)