Go Down

Topic: Interpreting a UDP stream from PS4 (Read 2907 times) previous topic - next topic

cyclicscooby

Hi Guys..
I'm stuck with a data format issue.

I'm using an ESP8266 module to connect to my Phone Hotspot (My only internet) and read a UDP stream from the PS4.
Its ProjectCars Telemetry im after..

I'm coding the ESP directly, planning to output the serial to a Teensy for manipulation later..
Presently, its just outputting to the serial mon, Bodged thru an UNO serial (FTDI cable in post)

I hacked 'ESP8266 Wifiscan' with standard 'WifiUDPsendrecievestring' and it appears i've connected to the stream, as i'm recieving packets of the correct size (1367), from the PS4's IP address.. :)
Note- I changed the original packetBuffer size from 255 to 1367.!! Is that OK..?



But now I need help.. How to interpret the data..



From what ive managed to find out, peeps are using a 'Struct' to move this stream to, but i just cannot get my head around how to use this within Arduino..
I kinda get how the struct works, and the naming convention (structname.field1, etc) but I quickly get out of my depth, especially how to use it in the actual code..

I've added the latest data_struct.h, if only to see the layout, but adding it as an include (as suggested) errors, so its hashed out at the mo..


I found a German site with 'Arduino Dash' inc code.
He's used 'String', which i've tried to utilise in my code, but it's just coming back empty (nothing matches)..
His bit of code is commented as 'NEW Arduino Dash', and I hopefully just addeded it after the existing print 'Packetbuffer'


I've been at this for a week. I've read many a tutorial on C++, but just get more confused..
( I used to be a COBOL mainframe programmer in a previous life, but now drive 44 ton trucks.. )


So.. Heres the Serial OP..


Received packet of size 1367
From 192.168.43.24, port 62761
Contents:
Šx

Received packet of size 1367
From 192.168.43.24, port 62761
Contents:
Š€


etc, etc...

WITH the 'Arduino-dash' bit added...

Received packet of size 1367
From 192.168.43.24, port 62761
Contents:
ŠŒ
stringOne = ŠŒ
converted values =
vRpm = 0
vGear = 0
vSpeed = 0
vTemp = 0
vAbs = 0
vFuel = 0
vEngineState = 0
vLevel = 0
vTime = 0
vSplit = 0
LEDSTATE = 0

Bit hopeful just adding it to the end, but had to try...




cyclicscooby

And the code..

Code: [Select]
//============================================================================
//= Libraries
//============================================================================
// ESP8266 Wifi
#include <ESP8266WiFi.h>

// Standard Wifi UDP Lib
#include <WiFiUdp.h>

// NEW Lib from arduino-dash - standard lib
#include <string.h>


// Data struct for Pcars
//#include "data_struct.h"
// Data struct for Pcars
//#include "data_struct2.h"

//============================================================================
//= Setup Definitions
//============================================================================
unsigned int localPort = 5606;        // Pcars UDP Port on all platforms

//============================================================================
//= Storage section
//============================================================================
int status = WL_IDLE_STATUS;
char ssid[] = "S5 Hotspot";           //  your network SSID (name)
char pass[] = "xxxxxxxxxx";           // your network password (use for WPA, or use as key for WEP)

//char packetBuffer[255];             //buffer to hold incoming packet
char packetBuffer[1367];              //buffer to hold incoming packet

// NEW fields from arduino-dash
int vRpm, vGear, vSpeed, vTemp = 0, vAbs = 0, vFuel = 0, vEngineState = 0, vLevel = 0, vTime = 0, vSplit = 0;
int LEDSTATE = 0;
char incomingByte = 0;
char data[50];
String sSpeed;
String sRpm;
int counter = 0;
int button = 1;
int state = 0;
String ver = "1.3.2";                 // version number
String module = "1";                  // module number

//char  ReplyBuffer[] = "acknowledged"; // a string to send back - NOT USED

//============================================================================
//= Setup objects
//============================================================================
WiFiUDP Udp; // Create Udp object

//============================================================================
//= Setup
//============================================================================
void setup() {
  //Initialize serial and wait for port to open:
  delay(2000);
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();                                // Just prints connection details

  Serial.println("Starting UDP");
  Udp.begin(localPort);

}

//============================================================================
//= Main Loop
//============================================================================
void loop() {

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
//    int len = Udp.read(packetBuffer, 255);
    int len = Udp.read(packetBuffer, 1367);
    if (len > 0) packetBuffer[len] = 0;
    Serial.println("Contents:");
    Serial.println(packetBuffer);

// Original Code ends here. Below is 'Arduino-dash' added after

// NEW from arduino-dash
    String stringOne = packetBuffer;

    if (stringOne.substring(0, 2) == "CC") {
        sRpm = stringOne.substring(2, 6);
        vRpm = sRpm.toInt();

        //here the right display for RPM,LAP,Water and Oil temp,position

    }
    else if (stringOne.substring(0, 3) == "VER") {
        //to version check
        Serial.println(ver + ";" + module + ";");

    }
    else if (stringOne.substring(0, 5) == "state") {
        //button state
        state = stringOne.substring(5, 6).toInt();

      if (state == 1) {
          String button_s = String(button);
          Serial.println(button_s + ":0:0:");
      }

    }
    else if (stringOne.substring(0, 2) == "DD") {
        sSpeed = stringOne.substring(2, 6);
        vSpeed = sSpeed.toInt();

        //here the speed display

    }
    else if (stringOne.substring(0, 2) == "BB") {
        vGear = stringOne.substring(2).toInt();
        //here the gear setup
        // -3 = clear
        // -2 = -
        // -1 = R
        // 0 = n
        // 1 - 6 = gear number

    }
    else if (stringOne.substring(0, 2) == "CH") {
        vLevel = stringOne.substring(2).toInt();

        //here the setup for Fule level data come in deziliter
        //fuel, oil, water pressure date come in Hektopascal

    }
    else if (stringOne.substring(0, 2) == "CA") {
        vTime = stringOne.substring(2).toInt();

        //here the setup for last, best and current time data come in Dezisekunde

    }
    else if (stringOne.substring(0, 2) == "CE") {
        vSplit = stringOne.substring(2).toInt();

        //here the setup for split times data come in ms

    }
    else if (stringOne.substring(0, 2) == "AA") {
        vTemp = stringOne.substring(2, 3).toInt(); //0 or 1 , 0 is off and 1 is on
        vAbs = stringOne.substring(3, 4).toInt(); //0 or 1
        vEngineState = stringOne.substring(4, 5).toInt(); //0 or 1
        vFuel = stringOne.substring(5, 6).toInt(); //0 or 1
        LEDSTATE = stringOne.substring(6, 7).toInt(); //0 - 8

        //here the LED setup
   }

   Serial.print("stringOne = "); Serial.println(stringOne);
   Serial.println("converted values = ");
   Serial.print("vRpm = "); Serial.println(vRpm);
   Serial.print("vGear = "); Serial.println(vGear);
   Serial.print("vSpeed = "); Serial.println(vSpeed);
   Serial.print("vTemp = "); Serial.println(vTemp);
   Serial.print("vAbs = "); Serial.println(vAbs);
   Serial.print("vFuel = "); Serial.println(vFuel);
   Serial.print("vEngineState = "); Serial.println(vEngineState);
   Serial.print("vLevel = "); Serial.println(vLevel);
   Serial.print("vTime = "); Serial.println(vTime);
   Serial.print("vSplit = "); Serial.println(vSplit);
   Serial.print("LEDSTATE = "); Serial.println(LEDSTATE);

  }
}

//============================================================================
//=  Serial print WiFi details                                                
//============================================================================
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

// END OF PROGRAM



I dont understand why it says its recieved 1367 bytes, then only displays a few characters.. ?!?!


Any help or advice will be gratefully recieved..



Luv
Chrisie.. x

cyclicscooby

Just a thought... Its not speed related is it..??
If the serial monitor is set to the wrong speed, you get similar random ASCII chars, just like i'm getting in the packet..??

Just read up on serial again, but it goes on about finding the end of the data, whereas, I presume the packet is the packet, wholesale and presented to me as 1367 bytes..?! Or am I completely wrong..??


Luv
Chrisie.. x

cyclicscooby

Got it sorted..

Finally got my head around pulling the 'bits' out..

Now "$ *" = Loads of useful data...


Luv
Chrisie.. xx

Tady

do you mind telling me how you did that? i am working on a similar project :)
thank you!

Maskmagog

Great project! I'm trying this myself, but need some help. How did you solve your issues?

Go Up