In this tutorial we are going to learn how to detect the connection of a PS4 controller to the ESP32 by using a callback function. We will be using the Arduino core and this library. The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.
Introduction
In this tutorial we are going to learn how to detect the connection of a PS4 controller to the ESP32 by using a callback function. We will be using the Arduino core and this library.
For an introductory tutorial on how to use the library, please check here. Note that, in order to use the library and support the connection of the controller to the ESP32, we need to get the Bluetooth address stored on the controller. The mentioned tutorial explains the whole procedure on how to do it and the software needed.
Note that in the introductory tutorial we also learned how to check if the controller is connected by resorting to polling. Nonetheless, in general, polling is inefficient and leads to wasting CPU cycles that could be used for other useful computation.
So, in this tutorial, we are going to learn how to handle the connection of a controller using events and a callback function, which avoids the need to periodically poll an object.
The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.
The code
We start by including the PS4Controller.h library, so we have access to the PS4 extern variable that allows us 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);
Then we are going to call the begin method on our PS4 extern variable, passing as input the Bluetooth MAC address registered on the controller (check in the previous tutorial the procedure to figure out this address).
This will take care of initializing the Bluetooth layer on the ESP32 and making it ready to receive an incoming PS4 controller
connection.
Note that this method returns a Boolean value indicating if the connection was successful or not. For this tutorial we are skipping the error handling but, in a real application scenario, you should handle it.
PS4.begin("yourDeviceMAC");
After that we are going to call the attachOnConnect method on the PS4 variable. This method allows us to register a callback function that will be executed when a controller connects to the ESP32.
This method receives as input our callback function. We are going to call it onConnect and check its implementation later.
PS4.attachOnConnect(onConnect);
You can check the full setup function below.
void setup()
{
Serial.begin(115200);
PS4.begin("yourDeviceMAC");
PS4.attachOnConnect(onConnect);
Serial.println("Initialization ready!");
}
Since we are going to work with callback functions, we won’t need to make use of the Arduino main loop. Thus, we will simply delete the corresponding FreeRTOS task by calling the vTaskDelete function and passing NULL as input.
void loop()
{
vTaskDelete(NULL);
}
To finalize, we are going to check the implementation of our onConnect function. It needs to follow a pre-defined signature: return void and receive no arguments.
void onConnect(){
// Callback code here
}
The implementation of our function will be very simple. We will just print the value of the isConnected method, to confirm that the controller is indeed connected to the ESP32.
void onConnect(){
Serial.print("Controller connected: ");
Serial.println(PS4.isConnected());
}
The final code can be seen below.
#include <PS4Controller.h>
void onConnect(){
Serial.print("Controller connected: ");
Serial.println(PS4.isConnected());
}
void setup()
{
Serial.begin(115200);
PS4.begin("yourDeviceMAC");
PS4.attachOnConnect(onConnect);
Serial.println("Initialization ready!");
}
void loop()
{
vTaskDelete(NULL);
}
Testing the code
To test the code, simply compile it and upload it to your ESP32 device using the Arduino core. Make sure to use the correct Bluetooth MAC address in the final code.
After that, open the Arduino IDE serial monitor and then turn the controller on by clicking the PS4 button.
You should get an output similar to figure 1. As can be seen, the callback is executed and the value 1 (which corresponds to true) is printed, thus indicating the controller is connected.
Figure 1 – Output of the program, showing the value printed by the callback function when the controller is connected.