Controlling a Windows PC From the Raspberry Pi GPIO Bus
In this post I'm going to talk about controlling the Bluetooth keyboard emulator I discussed previously to send a key to the host based on the status of a GPIO pin. This will enable a Raspberry Pi to control a PC using simple GPIO inputs.
Once you have this set up, the sky's the limit for what you can achieve. Anything you can do on the PC using the keyboard you can also control from the Pi.
For this demo I am going to show how you use the Pi and the Bluetooth keyboard emulator to create an alternative input for a PC game.
Bluetooth Keyboard EmulatorThis demo assumes that you have successfully setup the Bluetooth Keyboard emulator in my previous post.
The emulator is written to run as a D-Bus service, which means that once it is set up we can create different clients that consume this service without having to modify the original server code. This is a separation of concerns that hopefully makes the client code easier to maintain and understand.
Install DependenciesThe GPIO keyboard client uses the RPi.GPIO python module to interact with the GPIO pins. It is installed by default in most Raspbian distributions, but just in case it isn't you can install it with the following command
> sudo apt-get install python-rpi.gpio
Obtain Source CodeIf you haven't already cloned a copy of the github repository for this blog, you really should go back and read the previous article
If you have already cloned a copy, then change into the BlogCode directory and get the latest version
> git pull origin master
Review Source CodeChange into the BlogCode/btk_gpio folder and use a python editor such as Idle to view the gpio_btkclient.py file
> idle btk_gpio_client &
It uses the RPi.GPIO library to monitor a single pin on the GPIO bus, then updates a keyboard state structure and sends that structure to the emulator to generate a key event,
The main loop monitors the state of the input pin and generates key up and key down events accordingly,
At the top of the class are some constants that can be modified to change how the program behaves:
- PIN sets the physical pin number. of the GPIO pin to monitor. By default this is pin # 37, which corresponds to GPIO 26
- KEYCODE sets the USB scan code of the key to send. This is set to 'W' by default, which is the forward key for many games.
- REPEAT_KEY controls whether the program will send a single key stroke or multiple keystrokes per switch press
- MIN_KEY_TIME sets the minimum delay between sending a key down event and a key up event. This seems to be a least 0.001 seconds for a keystroke to register
- REPEAT_KEY_DELAY sets the delay between sending repeated keys. This may need to be tuned for different applications
Connect a switch to the GPIO
To test the demo, connect a switch between a ground pin and the pin defined in the PIN constant. If using the default pin 39, then pin 40 can be used as a convenient ground
In my setup I am using a breadboard and an adafruit T-Cobbler to connect a micro switch.
Run the Emulator Server
Follow the steps from the previous article to run the Bluetooth Keyboard Emulator server
If all goes well it should look something like this
Run the Client
Open a new terminal window and change into t BlogCode\btk_gpio directory,
Use the following command to run the client
> sudo python gpio_btkclient.py
Control a PC Program
Run a program that you want to control with the switch and give it the mouse focus.
If all goes well, when you press the switch the client will display the switch events and the PC program will receive key events from your Raspberry Pi!
This is obviously just scratching the surface of what you can achieve. You can extend this beyond simple switches to control a PC using any sensor you can read through the GPIO
This method could also be easily extended to use sensors on the I2C bus to control a PC.
Here is a video showing how this setup can can be used to control Minecraft on the PC.