0

$USD

$USD
TUTORIALS ESP32

ESP32: Connecting a PS4 Controller

DFRobot Mar 04 2020 3910

Tutorials Writer: TECHTUTORIALSX

In this tutorial we will learn how to connect a PS4 controller to the ESP32, using this library and the Arduino core.


Introduction

In this tutorial we will learn how to connect a PS4 controller to the ESP32, using this library and the Arduino core. The connection will be established using Bluetooth.

If you are looking for a similar tutorial but using a PS3 controller, please go here.

In this introductory tutorial we will focus on all the procedures we need to do before we can connect the controller to the ESP32. So, in terms of coding, we will just configure the ESP32 to receive a controller connection. We will cover more advanced functionalities in future tutorials.

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.


Installing the library

Installing the library is very simple. Basically, we just need to go to its GitHub page and download it by clicking on the “Clone or Download” button. You should get a .zip file on your computer.

After that, locate the folder where our Arduino libraries are installed. Mine is located in the path below, but it might differ in your machine

After this, simply extract the content of the .zip file to the Arduino libraries folder and name the top level folder PS4-esp32.

After this, you should be able to include the library in your Arduino sketches.


Preparing the controller

Before we can start using the controller, we need to consider that when a PS4 controller is paired with a console, it means that the controller has stored the Bluetooth MAC address of the console and this is the only device to which the controller will connect to [1].

So, before we can connect the ESP32 and the PS4 controller, we need to follow one of the following options [1]:

Find out what is the Bluetooth MAC address of the PS4 that is stored on the controller and set the ESP32 address to use that address;

Change the MAC address stored on the controller to the one from the ESP32.

Independently of which one we choose, the easiest way is to use a tool called SixaxisPairTool, which allows to both get the current MAC address stored on the controller and also set a new value.

In this tutorial we are going to use this tool but, in the documentation of PS4 controller library there’s also a mention to this open source tool as an alternative.

So, to install the mentioned tool, simply go to its website and download the executable (assuming that you are on Windows). The download link is available as shown in figure 1.

Note that there are also versions for Linux and MacOSX, although I haven’t tested any of them.

After downloading the executable, run it and install the program in your machine. After the installation is concluded, search for sixaxisPairTool in your programs and launch it. The program is just a tiny window like the one shown below at figure 2.
Figure 2 – Sixaxis Pair Tool UI.
Then, plug your controller to your computer using a USB cable. After a bit of time, in the “Current Master” text area, it should change to a MAC address, like shown in figure 3. Take note of this address since we will need it for the Arduino code.


Figure 3 – Sixaxis Pair Tool showing the controller Bluetooth MAC address.
Note that there is a “Change Master” option below that you can use to set the MAC address to a different value, such as the one from your ESP32.

In my case, I’ve opted to keep the address of my PS4 because it is really trivial to use a custom MAC address while using the library we have just installed. Basically, we just need to know that address and pass it as a string to an initialization method, as we will see below.
After this, we should be ready to connect the controller to the ESP32.



The code

We will start by including the installed library. This will make available an extern variable of class PS4Controller called PS4. This will be the variable that we will be using to interact with the controller.

#include <PS4Controller.h>
Moving on to the Arduino setup, we will start by opening a serial connection, so we can output the results of our program.

Serial.begin(115200);
Followed by that, we will perform the initialization of the PS4 instance by calling the begin method and passing as input the MAC address that is stored on the controller. It is passed as a string with the default format for representing a Bluetooth MAC address, like the arbitrary example below:

"00:11:22:33:ff:ee"

Note that this method call won’t connect the ESP32 to the controller because, at this point, the controller might not even be on.

Instead, it takes care of storing the MAC address passed as input, initializing the ESP32 Bluetooth layer and getting the Bluetooth services ready to listen for an incoming PS4 controller connection. You can check in detail the implementation of the method here.

Note that this method call returns a Boolean value indicating if the connection was successful or not but, for simplicity, we will assume everything is initialized correctly. Naturally, for a real application scenario, you should do the proper error handling.

PS4.begin("yourDeviceMAC");
Serial.println("Initialization ready!");
From this point onward the ESP32 should be able to receive a controller connection. So, in the Arduino main loop, we will check when a controller is connected by calling the isConnected method on the PS4 extern variable.

This method takes no arguments and returns a Boolean indicating if a controller is connected or not.

if(PS4.isConnected()) {
    Serial.println("Controller connected");
}
You can check the full code below. As can be seen, we have added a small 1 second delay between iterations of the main loop, so we are not constantly polling.

Note that there’s a method called attachOnConnect that we can leverage to register a callback that is invoked when a PS4 controller connects to the ESP32, but we are not going to cover it here.

#include <PS4Controller.h>
 
void setup()
{
  Serial.begin(115200);
 
  PS4.begin("yourDeviceMAC");
  Serial.println("Initialization ready!");
}
 
void loop()
{
  if(PS4.isConnected()) {
    Serial.println("Controller connected");
  }
 
  delay(1000);
}

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. Don’t forget to use the MAC address that is registered on your controller on the Arduino code.

After that, open the Arduino IDE serial monitor. You should get a “Initialization ready!” message printed to the monitor, indicating that the initialization procedure went fine.

Next, click the PS4 button in your controller to turn it on. You should start getting the “Controller connected” message periodically printed, as shown in figure 4, indicating a successful connect of the controller to the ESP32.

Figure 4 – Output of the program after a PS4 controller is connected.

References

[1] https://github.com/NURobotics/PS4-esp32