
Originally Posted by
Killg0re NL
How far are you? and willing to share the code?
I am quite far, (but not workings on the timing). i am working on making it UDP version independent, and to get the code working more efficiently.
Carflags 6 and 7 don't seem to work (ESP and TCS) and can't calculate the fuel lavel correctly.
now working with racestate and gamestate, but those bits also arn't what i am expecting of it.
am thinking of publishing the code, just to share and hopefully, to get some help/feedback.
after the post i stop the project... last week i was trying to use de UDP the ESP 32 and a CAN transeiver to use a car dashboard and it works =]
i can't find the esp, tcs and pit limit on the carflags.. ABS, ENGINE ACTIVE, ENGINE WARNING, LIGHTS is OK.
To calculate the fuel, you have to use the sFuelLevel [4 - use the same convertfloat as the speed] i'ts give you the % of fuel. mutiply by sFuelCapacity to have the Liters on the tank.
But i didn't sucess with laptime =[
if you want to email me, it is easier to help
Code:
/===============================================================
//= Libraries
//===============================================================
// ESP32 Wifi
#include <WiFi.h>
// Standard Wifi UDP Lib
#include <WiFiUdp.h>
//===============================================================
//= Setup Definitions
//===============================================================
unsigned int localPort = 5606; // porta udp do pcars
//===============================================================
//= Storage section
//===============================================================
int status = WL_IDLE_STATUS;
char ssid[] = " "; // your network SSID (name)
char pass[] = " "; // your network password (use for WPA, or use as key for WEP)
//char packetBuffer[255]; //buffer to hold incoming packet
char packetBuffer[1600]; //buffer to hold incoming packet
//float packetBuffer; //buffer to hold incoming packet
//char ReplyBuffer[] = "ok"; // a string to send back - NOT USED
int vGear;
word nome;
unsigned char sCarFlags;
//===============================================================
//= 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);
}
union {
byte uBytes[4];
float uFloat;
long uLong;
} uData;
union {
byte sBytes[4];
float sFloat;
long sLong;
} sData;
//===============================================================
//= 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);
Udp.read(packetBuffer, 1600);
//int len = Udp.read(packetBuffer);
//if (len > 0) packetBuffer[len] = 0;
//Serial.println("Contents:");
//Serial.println(packetBuffer);
//byte swing2 = packetBuffer[12];
//byte swing3 = packetBuffer[12];
//Serial.print("teste:");Serial.print(swing2);Serial.print(" - ");Serial.println(swing3);
if (packetSize == 559){
byte marcha = packetBuffer[45];
int marchaatual = marcha & 0xf;
//Serial.print("marcha: ");Serial.println(marchaatual); //GEAR INDICATOR
byte acel = map(packetBuffer[30],0,255,0,100);
//Serial.print("acel: ");Serial.println(acel); //Throttler %
unsigned char carflags = packetBuffer[17];
// Serial.print("pit: ");Serial.println(bitRead(carflags,0)); // 1 if the car is in the pitlane
byte bbias = packetBuffer[554];
int bia = (100-bbias/2.55) ;
// Serial.print("Bbias: ");Serial.println(bia ); //Brake Bias
byte swing1 = map(packetBuffer[371],0,255,0,100);
//Serial.print("swing1 :");Serial.println(swing1);
byte swing2 = packetBuffer[28];
//Serial.print("swing2 :");Serial.println(swing2);
sCarFlags =packetBuffer[17];
Serial.println (sCarFlags);
//float CurrentTime = (packetBuffer[10]) << 24 | (inBuffer[9] & 0xff) << 16 | (inBuffer[8] & 0xff) << 8 | (inBuffer[7] & 0xff);
//Serial.print("nome :");Serial.println();
//convertFloat();
}
}
}
void convertFloat() {
uData.uBytes[0] = packetBuffer[36];
uData.uBytes[1] = packetBuffer[37];
uData.uBytes[2] = packetBuffer[38];
uData.uBytes[3] = packetBuffer[39];
int velocidade = (uData.uFloat) *3.6;
Serial.print("speed :");Serial.println(velocidade); //Speed KMh
}
//===============================================================
//= 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