Connecting DS4 to Raspberry Pi over USB or BT

Connecting DS4 to Raspberry Pi

In the previous post here,  I was using python evdev module to capture the USB data.
This time I tried to connect gaming controoler over BT or USB
Connecting gaming controller to Raspberry Pi expands more application to play with.
I thought it would be very useful to make DS4 work with Raspberry Pi so that I can potentially controll various  things that I connected to Raspbery Pi, such as sensors, servos, and so on.
Since I have experienced evdev previously, I will use the same and obtain the input event data from DS4.

For this setup, I am using Raspberry Pi 3 Model B (not Plus).
To connect DS4 to Raspberry Pi, there are a few ways and I tried 2 method here which is using USB or BT. 
    *** You can also use Playstation USB adapter 

1. Using USB
This is the easiest method and you just need to connect DS4 to Raspberry Pi using micro USB cable.
After connecting DS4, you can check if the controller is recognized using "dmesg" command or something.
In the "/dev/input/" directory, you should see the new event number.

2. Using BT
Using BT is a little bit more complecated, and it might not be stable in some cases.
Here is what I did and it worked.
$ sudo apt-get update
$ sudo bluetoothctl
After running the bluetoothctl, you can turn on the agent and set it to the default.
agent on
Now you are ready to scan the BT devices, and prepare for DS4 to enter the pairing mode.
To enter the pairing mode, you press "Share" button and "PS" button at the same time and hold it until the front LED starts flashing.
Then type the following command in the bluetoothctl terminal.
scan on

Once your DS4 is discovered, you will see something like this in the terminal.
[NEW] Device XX:XX:XX:XX:XX:XX Wireless Controller

"XX:XX:XX:XX:XX:XX" is your controller MAC address and type the following command with the MAC address.

If DS4 stoped flashing by the time you run the above command, press "Share" and "PS" button again and do the same.
Once you see the message "Connection successful", your DS4 is connected, and last thing you may want to do is remember the device so that it automatically connects next time when you press "PS" button.

Now you are ready, and you can close the bluetoothctl by typing "quit".
To double check the connection, you should see the new event number in the "/dev/input/" directory.
You can disconnect the controller from upper right BT icon by selecting "Wireless Controller" > "Disconnect".
When you press "PS" button next time, it should automatically connected. 


3. Testing
Next thing is to make sure if the data is received properly. 
I used "joystick" package and here is how to install and use it.

Install joystick:
$ sudo apt-get install joystick
Run joystick software:
$ sudo jstest /dev/input/js0
Check input events:
$ ls /dev/input
If you do not know which event is mouse or keyboard, follow this.
Install input-utils:
$ sudo apt-get install input-utils
$ lsinput    

Python code for testing
I think most people want to actually write the code that can receive the data from the input event.
I believe there are multiple ways and I used evdev and python for this quick example.
This is the python example code.
    *** Depending on the python version, you may get different result and my python version is 2.7.13.

In the python script, you need the event numbers, and you can use the following commands to idendify the numbers.

Check input events:
$ ls /dev/input

If you do not know which event is mouse or keyboard, follow this.
Install input-utils:
$ sudo apt-get install input-utils
$ lsinput    

from evdev import InputDevice, categorize, ecodes, KeyEvent
gamepad = InputDevice('/dev/input/event4')

print (gamepad.capabilities(verbose=True))

for event in gamepad.read_loop():
    if event.type != ecodes.EV_SYN:
      print (categorize(event))

Crate a python script file as below and copy&paste the above code.
Run the python script.
Press any buttons on the DS4 and you will see the outputs in the terminal.

Raspberry Pi Power Monitor

Power Consumption Monitor using Raspberry Pi Sometimes I get tired of using test equipment to monitor the current and/or power...