-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnector.py
More file actions
62 lines (54 loc) · 1.56 KB
/
Copy pathConnector.py
File metadata and controls
62 lines (54 loc) · 1.56 KB
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
import paho.mqtt.client as mqtt
import hid # To read input from PS3 Controller
import time
BROKER = "broker.emqx.io"
TOPIC = "IoT/group8"
# Callback while disconnected
def on_disconnect(client, userdata, rc):
print("Disconnected, trying to reconnect...")
while True:
try:
client.reconnect()
break
except:
time.sleep(5)
# Initiate MQTT Client
client = mqtt.Client()
client.on_disconnect = on_disconnect
client.connect(BROKER, 1883, 60)
client.loop_start() # start loop MQTT
# Initiate PS3 Controller
PS3_VENDOR_ID = 0x1949
PS3_PRODUCT_ID = 0x402
try:
gamepad = hid.device()
gamepad.open(PS3_VENDOR_ID, PS3_PRODUCT_ID)
gamepad.set_nonblocking(True)
print("PS3 Controller Connected!")
while True:
data = gamepad.read(64)
if data:
lx = data[3] # Joystick left (X-axis)
ly = data[4] # Joystick left (Y-axis)
if ly < 100:
print("Forward")
client.publish(TOPIC, "forward")
elif ly > 150:
print("Backward")
client.publish(TOPIC, "backward")
elif lx < 100:
print("Left")
client.publish(TOPIC, "left")
elif lx > 150:
print("Right")
client.publish(TOPIC, "right")
else:
print("Stop");
client.publish(TOPIC, "stop")
time.sleep(0.2)
except Exception as e:
print("Error:", e)
finally:
gamepad.close()
client.loop_stop()
client.disconnect()