I created a game controller with the RaspberryPi Zero W using websockets in Python and the GPIOs connected to switches. I chose this setup as the PiZero offers a terminal to run shell commands, can be accessed headless via ssh and offers the possibility to run Python scripts. It is also very portable and can be mounted on a small controller.
The connection to the game server via sockets was very smooth and reliable, the GPIO connection with the switches worked well - after quite a few hickups regarding the wiring of the switches: I initially wired all switches directly into one power source without separating them via resistors. As a result all switches fired with each other once one was triggered. After a few hours of hardware debugging (and de-soldering …), I mounted the resistors in between each power connection and the switch and everything worked smoothly.
I finally added a LED to indicate a successful socket connection to the server.
Here the code for the socket connection and GPIO wiring in Python:
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library import socket import time s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect(('Server-IP',Port)) GPIO.setwarnings(False) # Ignore warning for now GPIO.setmode(GPIO.BOARD) # Use physical pin numbering GPIO.setup(35, GPIO.OUT) GPIO.output(35, GPIO.LOW) #check connection data=s.recv(1024) if len(data) > 0: print('yay, we are connected') GPIO.output(35, GPIO.HIGH) msg1='l' msg2='r' byt1=msg1.encode() byt2=msg2.encode() msg3='l' msg4='r' byt3=msg3.encode() byt4=msg4.encode() #for i in range(0,50): # s.send(byt2) # time.sleep(2) # s.send(byt1) # time.sleep(2) #s.close() def button_callback_l(channel): print("go left!") s.send(byt1) def button_callback_r(channel): print("go right!") s.send(byt2) def button_callback_u(channel): print("go up!") s.send(byt3) def button_callback_d(channel): print("go down!") s.send(byt4) GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set ini$ GPIO.add_event_detect(11,GPIO.FALLING,bouncetime=500, callback=button_callback_l) # Setup event o$ GPIO.setup(7,GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(7, GPIO.FALLING,bouncetime=500, callback=button_callback_r) GPIO.setup(13,GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(13, GPIO.FALLING,bouncetime=500, callback=button_callback_u) GPIO.setup(15,GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(15, GPIO.FALLING,bouncetime=500, callback=button_callback_d) message = input("Press enter to quit\n\n") # Run until someone presses enter GPIO.cleanup() # Clean up