ESP32: checking if PS4 controller is charging

In this tutorial we will learn how to check if the PS4 controller is charging or not, using the ESP32. We will be using this library and the Arduino core. The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

In this tutorial we will learn how to check if the PS4 controller is charging or not, using the ESP32. We will be using this library and the Arduino core.

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

If you prefer a video version of this tutorial, please check my YouTube channel:

The code

As usual, the first thing we do is importing the PS4Controller.h library.

#include <PS4Controller.h>

Moving on to the Arduino setup, we will start by opening a serial connection. Then we will initialize the Bluetooth layer of the ESP32 by calling the begin method on the PS4 extern variable.

Recall that we need to pass as input of this method a string with the Bluetooth address stored on the PS4 controller. The procedure to obtain this address was already covered on this introductory tutorial.

The full setup function can be seen below and it already includes an error checking to make sure the previous initialization was performed successfully.

void setup()
{

  Serial.begin(115200);

  if(!PS4.begin("01:01:01:01:01:01")) {
    Serial.println("Initialization failed");
    return;
  }

  Serial.println("Initialization finished.");

}

We will write the rest of our code on the Arduino main loop. Here we will first call the isConnected method on the PS4 extern variable to check if a controller is connected to the ESP32.

Naturally it only makes sense to check if a controller is charging if it is already connected to the ESP32.

if (PS4.isConnected()){
   // check if controller is charging
}

Inside the previous conditional block we will then check if the controller is charging or not. To do it we need to access the data member of our PS4 extern variable. This is a struct of type ps4_t.

On this struct we will access the status element. This is also a struct of type ps4_status_t. Finally, on that struct, we can access the charging element, which indicates if the controller is charging or not.

if (PS4.isConnected()){
    
    if (PS4.data.status.charging) {
      Serial.println("The controller is charging");
    }else{
      Serial.println("The controller is not charging");      
    }
}

The complete Arduino loop can be seen below.

void loop()
{
  if (PS4.isConnected()){
    
    if (PS4.data.status.charging) {
      Serial.println("The controller is charging");
    }else{
      Serial.println("The controller is not charging");      
    }
  }

  delay(3000);
}

The final code can be seen below.

#include <PS4Controller.h>

void setup()
{

  Serial.begin(115200);

  if(!PS4.begin("01:01:01:01:01:01")) {
    Serial.println("Initialization failed");
    return;
  }

  Serial.println("Initialization finished.");

}

void loop()
{
  if (PS4.isConnected()){
    
    if (PS4.data.status.charging) {
      Serial.println("The controller is charging");
    }else{
      Serial.println("The controller is not charging");      
    }
  }

  delay(3000);
}

Testing the code

To test the code, first compile it and upload it to your ESP32 using the Arduino IDE. Once the procedure finishes, open the Serial Monitor.

After that, turn on your PS4 controller without having it charging. You should see the message indicating that it is not charging. Then plug it to charge. You should now see a different message indicating it is charging. Figure 1 illustrates the expected result.

Output of the program on the Arduino IDE Serial Monitor.
Figure 1 – Output of the program on the Arduino IDE Serial Monitor.

1 thought on “ESP32: checking if PS4 controller is charging”

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading