-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vending_Client.py
117 lines (111 loc) · 3.75 KB
/
Vending_Client.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
import RPi.GPIO as GPIO
import curses
import time
import random
from curses import wrapper
from websocket import create_connection
ws = create_connection("ws://163.18.2.157:8000/")
GELD_ONE=7
GELD_TWO=11
GELD_THREE=13
GELD_FOUR=15
GELD_FIVE=12
GELD_SIX=16
GELD_SEVEN=18
GELD_EIGHT=22
stdscr = curses.initscr()
stdscr.clear()
try:
while True:
web_token = ws.recv()
if web_token == "token":
pi_token = random.choice("@!~&$%^*")
ws.send(pi_token)
pi_start = ws.recv()
game_start = ws.recv()
if pi_start == pi_token and game_start == 'START' :
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(GELD_ONE,GPIO.OUT)
GPIO.setup(GELD_TWO,GPIO.OUT)
GPIO.setup(GELD_THREE,GPIO.OUT)
GPIO.setup(GELD_FOUR,GPIO.OUT)
GPIO.setup(GELD_FIVE,GPIO.OUT)
GPIO.setup(GELD_SIX,GPIO.OUT)
GPIO.setup(GELD_SEVEN,GPIO.OUT)
GPIO.setup(GELD_EIGHT,GPIO.OUT)
print "Setup GPIO"
while True:
print "Press button :"
message = ws.recv()
#message = stdscr.getkey()
if message=="GELD_ONE": #1
GPIO.output(GELD_ONE,1)
time.sleep(3)
GPIO.output(GELD_ONE,0)
print("ONE")
ws.send("ONE")
if message=="GELD_TWO": #2
GPIO.output(GELD_TWO,1)
time.sleep(3)
GPIO.output(GELD_TWO,0)
print("TWO")
ws.send("TWO")
if message=="GELD_THREE": #3
GPIO.output(GELD_THREE,1)
time.sleep(3)
GPIO.output(GELD_THREE,0)
print("THREE")
ws.send("THREE")
if message=="GELD_FOUR": #4
GPIO.output(GELD_FOUR,1)
time.sleep(3)
GPIO.output(GELD_FOUR,0)
print("FOUR")
ws.send("FOUR")
if message=="GELD_FIVE": #5
GPIO.output(GELD_FIVE,1)
time.sleep(3)
GPIO.output(GELD_FIVE,0)
print("FIVE")
ws.send("FIVE")
if message=="GELD_SIX": #6
GPIO.output(GELD_SIX,1)
time.sleep(3)
GPIO.output(GELD_SIX,0)
print("SIX")
ws.send("SIX")
if message=="GELD_SEVEN": #7
GPIO.output(GELD_SEVEN,1)
time.sleep(3)
GPIO.output(GELD_SEVEN,0)
print("SEVEN")
ws.send("SEVEN")
if message=="GELD_EIGHT": #8
GPIO.output(GELD_EIGHT,1)
time.sleep(3)
GPIO.output(GELD_EIGHT,0)
print("EIGHT")
ws.send("EIGHT")
if message=="END":
GPIO.output(GELD_ONE,0)
GPIO.output(GELD_TWO,0)
GPIO.output(GELD_THREE,0)
GPIO.output(GELD_FOUR,0)
GPIO.output(GELD_FIVE,0)
GPIO.output(GELD_SIX,0)
GPIO.output(GELD_SEVEN,0)
GPIO.output(GELD_EIGHT,0)
print("---END---")
ws.send("END")
break
# else:
# print("unknow type")
# print(message)
# break
# break
except KeyboardInterrupt:
print "Exception: KeyboardInterrupt"
finally:
ws.close()
GPIO.cleanup()