Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from inputs import get_gamepad
- import socket
- import time
- # 🧠 Replace with your ESP32's actual IP address
- ESP32_IP = '192.x.x.x' #replace with your ESP32 ip address printed in esp32 program
- PORT = 1234
- def connect_socket():
- while True:
- try:
- sock = socket.socket()
- sock.connect((ESP32_IP, PORT))
- print("✅ Connected to ESP32 over Wi-Fi")
- return sock
- except Exception as e:
- print(f"🔌 Failed to connect: {e}")
- print("🔄 Retrying in 3 seconds...")
- time.sleep(3)
- sock = connect_socket()
- try:
- while True:
- events = get_gamepad()
- for event in events:
- # 🔍 Show all incoming gamepad signals (great for mapping new buttons)
- print(f"Code: {event.code} | Type: {event.ev_type} | State: {event.state}")
- # 🎯 Only respond to key/button press events
- if event.ev_type == 'Key' and event.state == 1:
- msg = f"BTN:{event.code}\n"
- try:
- sock.sendall(msg.encode())
- print(f"📤 Sent → {msg.strip()}")
- time.sleep(0.05) # 💤 Prevent flooding the ESP32
- except Exception as e:
- print(f"⚠️ Connection lost: {e}")
- print("🔁 Reconnecting...")
- sock.close()
- sock = connect_socket()
- break # Restart the loop after reconnect
- except KeyboardInterrupt:
- print("👋 Bridge manually stopped.")
- sock.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement