How can I read data from a PS4 controller using an ESP32 and send it to an Arduino Mega?

For my project, I need an ESP32 that reads the data from a PS4 controller and then transmits this information to an Arduino Mega. Although I've already developed an initial approach, I'm lacking the necessary knowledge to program in this area. As a result, I attempted to seek assistance from AI, but it hasn't worked out as expected.

Mega2560 as I2C MASTER:

#include <Wire.h>

const byte SLAVEADDRESS = 8;

const byte BYTEZAHL = 2 * sizeof(int);
union {
  byte bytes[BYTEZAHL];
  struct {
    uint16_t Wert_1;
    uint16_t Wert_2;
  };
} tx;

void loop()
{
  uint32_t jetzt = millis();
  static uint32_t vorhin = jetzt;
  if (jetzt - vorhin >= 1000)
  {
    vorhin = jetzt;
    tx.Wert_1 = random(150, 300);
    tx.Wert_2 = random(0, 1000);
    Wire.beginTransmission(SLAVEADDRESS);
    Wire.write(tx.bytes, sizeof(tx));
    Wire.endTransmission();
    Serial.print(F("\ttx.Wert_1: ")); Serial.print(tx.Wert_1); Serial.print(F("\ttx.Wert_2: ")); Serial.println(tx.Wert_2);
  }
}

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  //Wire.setClock(400000L); //setzt die I²C Taktfrequenz auf 400kHz
  delay(1000);
  Serial.println(F("\nStart"));
  Serial.print(F("BYTEZAHL Byte für int: ")); Serial.println(sizeof(int));
}

ESP32 as I2C SLAVE:

#include <Wire.h>
const byte SLAVEADDRESS = 8;
const byte BYTEZAHL = 4;
union {
  byte bytes[BYTEZAHL];
  struct {
    int16_t Wert_1;
    int16_t Wert_2;
  };
} rx;

volatile int32_t rxWert_1 = 0;
volatile int32_t rxWert_2 = 0;
bool neu = false;

void setup()
{
  Wire.begin(SLAVEADDRESS);
  Wire.onReceive(receiveEvent);
  Serial.begin(115200);
  Serial.println(F("\nStart"));
}

void receiveEvent(int numBytes)
{
  if (BYTEZAHL == numBytes)
  {
    for (byte j = 0; j < numBytes; j++)
    {
      rx.bytes[j] = Wire.read();
    }
    rxWert_1 = rx.Wert_1;
    rxWert_2 = rx.Wert_2;
    neu = true;
  }
}

void loop()
{
  if (neu)
  {
    neu = false;
    for (byte j = 0; j < BYTEZAHL; j++)
    {
      Serial.print(rx.bytes[j], HEX); Serial.print(' ');
    }
    Serial.print(F("\trxWert_1: ")); Serial.print(rxWert_1); Serial.print(F("\trxWert_2: ")); Serial.println(rxWert_2);
  }
}

PS4 Code:

#include <PS4Controller.h>

unsigned long lastTimeStamp = 0;

void notify()
{
  char messageString[200];
  sprintf(messageString, "%4d,%4d,%4d,%4d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d",
  PS4.LStickX(),
  PS4.LStickY(),
  PS4.RStickX(),
  PS4.RStickY(),
  PS4.Left(),
  PS4.Down(),
  PS4.Right(),
  PS4.Up(),
  PS4.Square(),
  PS4.Cross(),
  PS4.Circle(),
  PS4.Triangle(),
  PS4.L1(),
  PS4.R1(),
  PS4.L2(),
  PS4.R2(),  
  PS4.Share(),
  PS4.Options(),
  PS4.PSButton(),
  PS4.Touchpad(),
  PS4.Charging(),
  PS4.Audio(),
  PS4.Mic(),
  PS4.Battery());

  //Only needed to print the message properly on serial monitor. Else we dont need it.
  if (millis() - lastTimeStamp > 50)
  {
    Serial.println(messageString);
    lastTimeStamp = millis();
  }
}

void onConnect()
{
  Serial.println("Connected!.");
}

void onDisConnect()
{
  Serial.println("Disconnected!.");    
}

void setup() 
{
  Serial.begin(115200);
  PS4.attach(notify);
  PS4.attachOnConnect(onConnect);
  PS4.attachOnDisconnect(onDisConnect);
  PS4.begin();
  Serial.println("Ready.");
}

void loop() 
{

}

I would be very happy if someone could help me.
Best regards, Viki.

Hi @vikigraef ,

welcometo the arduino-forum.

Well done to post your code as code-sections in your very first posting.

There are some aditional things that you should follow to speed up finishing your project.

Most AI's are probalitiy based. This means if it does not find a 100.00% match
it take the 99% match. Where the 1% missing canbe the biggest bug.

I'm pretty sure that you agree and will follow the way how to solve
your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

I suggest using bluepad32 for PS4 to esp32 over bluetooth.

Please be aware that the ESP32 is a 3.3V device and the Mega is a 5V device. You can damage your ESP32 when using direct connections; level shifting is necessary.

I have connected the 3.3 V connection of the Mega to the connection of the ESP32.

The IO-pins of the Mega have 5V.
Connecting a 5V IO-pin of the Arduino Mega to a 3.3V IO-pin of the ESP32 can damage the ESP32.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.