Results 1 to 4 of 4

Thread: Modular DIY Buttonbox - Outputs : V1b

  1. #1
    Superkart Pilot Killg0re NL's Avatar
    Join Date
    Sep 2017
    Location
    Netherlands
    Posts
    134
    Platform
    PS4 Pro

    Modular DIY Buttonbox - Outputs : V1b

    For now the code works on a ESP8266 with an Arduino to process, special thanks to Cyclicscooby
    But i am thinking to move over to the ESP32 (more memory, and in/outputs available to assign, saves one design, and easier programming)

    a link to download both sketches at once:DOWNLOAD LINK


    For the inputs TO the game check THIS LINK


    The code is able to:
    Show
    • WiFi Connected (ESP8266)
    • ABS (Arduino)
    • TCS (Arduino)
    • ESP (Arduino)
    • Light (Arduino)


    OUTPUT LED
    • ledPin = 2; // GPIO2 on ESP8266 for status WiFi connected


    The Code consists out of the sketch, and two additional files
    • _PC2_ESP8266_V1b.ino
    • data_out.h
    • info


    ESP8266 Code

    File: _PC2_ESP8266_V1b.ino
    Code:
    /* 
     ===============================
     = Pcars_udp_rx_v5.3r          =
     = See info tab, for more info =
     = data_out.h file needed      =
     = info file optional          =
     ===============================
    */
    
    //============================================================================
    //= Libraries
    //============================================================================
    // ESP8266 Wifi
    #include <ESP8266WiFi.h>
    
    // Standard Wifi UDP Lib
    #include <WiFiUdp.h>
    
    // Data from Pcars - Cut down to required fields
    //#include "Data_Struct_Cut.h" Now there, but needed?
    
    // Data for Serial - Cut down to required fields
    #include "data_out.h"
    
    //============================================================================
    //= Setup Definitions
    //============================================================================
    unsigned int localPort = 5606;              // Pcars UDP Port on all platforms
    char sketchName[]= "_PC2_ESP8266_V1b.ino";  // SketchName
    char sketchDate[]= "9 september 2018";      // SketchDate
    byte mac[6];                                // the MAC address of your Wifi shield
    
    WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;
    
    /*
    // For connection status
    const char* wl_status_to_string(wl_status_t status) {
      switch (status) {                                       // Corresponds with Wifi Status Code
        case WL_NO_SHIELD: return "WL_NO_SHIELD";             // 255
        case WL_IDLE_STATUS: return "WL_IDLE_STATUS";         // 0
        case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL";     // 1
        case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED";   // 2
        case WL_CONNECTED: return "WL_CONNECTED";             // 3
        case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED";   // 4
        case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST"; // 5
        case WL_DISCONNECTED: return "WL_DISCONNECTED";       // 6
      }
    }
    */
    //============================================================================
    //= Storage section
    //============================================================================
    // WiFi fields
    int status = WL_IDLE_STATUS;
    int tries = 0;
    
    char ssid[] = "ssid";              // your network SSID (name)
    char pass[] = "password";        // your network password (use for WPA, or use as key for WEP)
    
    char ssid2[] = "SSID";             // your network SSID (name)
    char pass2[] = "PASSWORD";       // your network password (use for WPA, or use as key for WEP)
    
    // Incoming UDP packet fields
    byte packetBuffer[1367];             // buffer to hold incoming packet
    
    unsigned int PartInfo = 1347;        // Is PartInfo packetsize - packet size match
    unsigned int Telemetry = 1367;       // Is Telemetry packetsize
    
    //bool inMenu = true;
    bool inGame = false;
    bool practiceStart = false;
    bool raceStart = false;
    byte raceLoading = 81;
    bool needPartInfo = false;
    bool needSetup = false;
    bool firstRun = true;
    
    // Timers
    uint32_t D2oldtime = millis();
    uint32_t D2delay = 1000; // 3k for testing - set to 1000
    uint32_t D3oldtime = millis();
    uint32_t D3delay = 1000; // 3k for testing - set to  or 2010?
    
    
    // Wifi Status led
    //boolean status = false; // status is allready in use
    boolean ledState = true;
    int ledPin = 2; // GPIO2 on ESP8266
    int interval = 1000;
    long previousMillis = 0;
    long currentMillis;
    
    //============================================================================
    //= Setup objects
    //============================================================================
    
    // Create Udp object
    WiFiUDP Udp;
    
    //============================================================================
    //= Setup
    //============================================================================
    void setup() {
    pinMode(ledPin,OUTPUT);
    
    //  WiFi.mode(WIFI_STA);  // Let ESP8266 Function as Client only
    //  WiFi.setAutoConnect (true); // Test
    //  WiFi.setAutoReconnect (true); // Test
      //Initialize serial and wait for port to open:
      Serial.begin(115200);
      
      gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event)
      {
    //    Serial.print("Station connected, IP: ");
    //    Serial.println(WiFi.localIP());
      });
    
      disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event)
      {
        Serial.println("Station disconnected");
      });
    
    //  Serial.printf("Connecting to %s ...\n", ssid); 
       
      delay(2000);
      Serial.println();
      Serial.println("************************************************************");
      Serial.print("Sketch name: ");  Serial.println(sketchName);
      Serial.print("Sketch date: ");  Serial.println(sketchDate);
      Serial.println("************************************************************"); 
    
    // next three lines, Leonardo only
    //while (!Serial) {; // wait for serial port to connect. Needed for Leonardo only
    //  }
    //  delay(1000);
    
    // Set OutBuffer Headers
      // Set 'startOfPacket' - Byte0
      C0[0] = P0[0] = P1[0] = P2[0] = D0[0] = D1[0] = D2[0] =  
      D3[0] = D9[0] =  S1[0] = M1[0] = startOfPacket;
    
      // Set Packet Type - Byte 1
      C0[1] = packetIsConnectInfo;
      P0[1] = P1[1] = P2[1] = packetIsPartInfo;
      D0[1] = D1[1] = D2[1] = D3[1] = D9[1] = packetIsDash;
      S1[1] = packetIsSusp;
      M1[1] = packetIsMotion;
    
      // Set Packet Number - Byte 2
      P0[2] = D0[2] = C0[2] = packetNum0;
      P1[2] = D1[2] = S1[2] = M1[2] = packetNum1;
      P2[2] = D2[2] = packetNum2;
      D3[2] = packetNum3;
      D9[2] = packetNum9;
    
      // Set 'endOfPacket' - Byte variable
      C0[3] = P0[67] = P1[67] = P2[67] = 
      D0[7] = D1[31] = D2[25] = D3[15] = D9[14] = S1[83] = M1[123] = endOfPacket;
    
      C0[1] = 'C';
      C0[2] = C0Disconnect;
      Serial.write(C0, 4);
      delay(500);
    
    // attempt to connect to Wifi network:
    while ( status != WL_CONNECTED){ // atm. status doesn't get updated
    //while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
            
        C0[2] = C0Attempt1;
        Serial.write(C0, 4);
          status = WiFi.begin(ssid, pass);
          WifiNotConnected(); // Execute WiFi Disconnected
          // wait for retry connection attempt:
          delay(4000);
        
    // 2nd network attempt  
        tries++;
        if (tries == 10 ) {
    //      Serial.println("Failed to Connect to First Network, Trying Next");
          break;
        }
      }
      if (tries == 10 ) {
    while ( status != WL_CONNECTED){
    //while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
              
          C0[2] = C0Attempt2;
          Serial.write(C0, 4);
            status = WiFi.begin(ssid2, pass2);
            WifiNotConnected(); // Execute WiFi Disconnected
            // wait for retry connection attempt:
            delay(4000);
        }
    //End 2nd network attempt
    
      }  
    
    // WiFi Successfully connected. Send connect packet
      printWifiStatus(); // serialPrint WiFi details
        C0[2] = C0Connect;
        Serial.write(C0, 4);
    
    // Start UDP
    //    Serial.println("UDP Begin");
      Udp.begin(localPort);
    }
    
    
    void WifiNotConnected(){
              currentMillis = millis();
              if (currentMillis - previousMillis > interval) 
              {
                previousMillis = currentMillis;
                ledState = !ledState; 
                digitalWrite(ledPin, ledState);    
              }       
         }
    
    //============================================================================
    //= Main Loop
    //============================================================================
    void loop() {
      if ( status != WL_CONNECTED) {
    
    //    Serial.println("Connected");  // DEBUG
        //Send ConnectPacket =Disconnect
        C0[1] = C0Disconnect;
        Serial.write(C0, 4);
         
      }  
      
    WifiConnected();  
      
      // If data available, read a packet
      int packetSize = Udp.parsePacket();
      if (packetSize) {
        if (packetSize == Telemetry) {
    //      Serial.println("***** Received Telemetry ***** ");
          Udp.read(packetBuffer, 1367);
    // debug
    //      PacketNum = packetBuffer[2];
    //      GameState = packetBuffer[3];
    //      RaceState = packetBuffer[10];
    //      CarFlags = packetBuffer[110];
    //      int carbit3 = bitRead(packetBuffer[110], 3);
    //      Serial.print("TelemetryPacketNum : "); Serial.println(TelemetryPacketNum);
    //      Serial.print("GameSessionState : "); Serial.println(GameSessionState);
    //      Serial.print("RaceStateFlags : "); Serial.println(RaceStateFlags);
    //      Serial.print("CarFlags : "); Serial.println(CarFlags);
    //      Serial.print("carbit3 : "); Serial.println(carbit3);
    //      Serial.println(" ");
    // end debug
              
       // Set gameState flags
          if (packetBuffer[3] == 1) {  // In Menu - NOT driving
            inGame = false;
            practiceStart = false;
            raceStart = false;
            needSetup = true;
            needPartInfo = true;
            firstRun = true;
            }
          else {
               inGame = true;          // We are InGame
               if (firstRun) {
                  needSetup = true;    // Added because Race mode not sending D0.?
                  firstRun = false;
               }
               if (packetBuffer[3] == 18 && bitRead(packetBuffer[110], 3) == 1) {
               practiceStart = true;
               }
              else { 
                practiceStart = false;
              }
                if (packetBuffer[3] == 82) {
                  raceStart = true;
                }
            else {
              raceStart = true; // Was Originally false by Cyclicscooby
              }
          }
          
          // Now Flags are set. Decide when to send data, ONLY when driving
          if (inGame) {
            if (practiceStart || raceStart) {
              if (needSetup) {
                needPartInfo = true;
                sendDashSetup();      // packet D0
                needSetup = false;             
              }
              sendDash(); // This will decide if it sends D1/2/3/9
    //          sendSuspension(); // sends S1
    //          sendMotion(); // sends M1
            }
          }
        } // end of If Telemetry
    
        else if (packetSize == PartInfo) { // Packet IS PartInfo
    //      Serial.println("***** Received PartInfo ***** ===  = = = = = = = = = = = = = = = = = = =");
          Udp.read(packetBuffer, 1347);
          if (needPartInfo) {
            sendPartInfo();
            needPartInfo = false;    
          }
        } // END OF IF PACKET T or P
       
      } // END OF IF PACKET AVAILABLE
    
    //raceStatus();            //Debug to show status of the Flags
    
    
    } // END OF MAIN LOOP
    
    //============================================================================
    //=  Send DashSetup D0 packet                                                 
    //============================================================================
    void sendDashSetup() {
    //  Serial.println("*************************************  Sending DashSetup D0");
      // Prepare D0
      D0[3] = packetBuffer[11];     // LapsInEvent
      D0[4] = packetBuffer[111];    // FuelCapacity
      D0[5] = packetBuffer[126];    // MaxRpm 1st
      D0[6] = packetBuffer[127];    // MaxRpm 2nd  
    
      // Send D0
      delay(1);
      Serial.write(D0, 8);
    }
    
    //============================================================================
    //=  Send a Dash 1/2/3/9 packet -                                                 
    //============================================================================
    void sendDash() {
    //  Serial.println("*************************************  Sending Dash D1");
      // Prepare D1 Dash packet
      D1[3] = packetBuffer[2];      // PacketNo
      D1[4] = packetBuffer[3];      // GameState
      D1[5] = packetBuffer[10];     // RaceState
      D1[6] = packetBuffer[110];    // CarFlags  
      D1[7] = packetBuffer[20];     // CurrentTime 1st
      D1[8] = packetBuffer[21];     // CurrentTime 2nd
      D1[9] = packetBuffer[22];     // CurrentTime 3rd
      D1[10] = packetBuffer[23];    // CurrentTime 4th
      D1[11] = packetBuffer[120];   // Speed 1st
      D1[12] = packetBuffer[121];   // Speed 2nd 
      D1[13] = packetBuffer[122];   // Speed 3rd 
      D1[14] = packetBuffer[123];   // Speed 4th  
      D1[15] = packetBuffer[124];   // Rpm 1st   
      D1[16] = packetBuffer[125];   // Rpm 2nd
      D1[17] = packetBuffer[128];   // GearNumGears 
      D1[18] = packetBuffer[129];   // Boost 
      D1[19] = packetBuffer[448];   // EngineSpeed 1st   
      D1[20] = packetBuffer[449];   // EngineSpeed 2nd  
      D1[21] = packetBuffer[450];   // EngineSpeed 3rd
      D1[22] = packetBuffer[451];   // EngineSpeed 4th
      D1[23] = packetBuffer[452];   // Torque 1st 
      D1[24] = packetBuffer[453];   // Torque 2nd  
      D1[25] = packetBuffer[454];   // Torque 3rd  
      D1[26] = packetBuffer[455];   // Torque 4th
      D1[27] = packetBuffer[280];   // TyreGrip[0] 
      D1[28] = packetBuffer[281];   // TyreGrip[1]
      D1[29] = packetBuffer[282];   // TyreGrip[2]  
      D1[30] = packetBuffer[283];   // TyreGrip[3]  
    
      // Send D1
      delay(1);
      Serial.write(D1, 32);
    
      // D2 Dash packet Timer  
      unsigned long currentMillisD2 = millis();
      if (currentMillisD2 - D2oldtime > D2delay) {
        D2oldtime = currentMillisD2;
    //    Serial.println("*************************************                 Sending Dash D2");
        // Prepare D2 Dash packet
        D2[3] = packetBuffer[100];    // OilTemp 1st
        D2[4] = packetBuffer[101];    // OilTemp 2nd
        D2[5] = packetBuffer[102];    // OilPress 1st
        D2[6] = packetBuffer[103];    // OilPress 2nd  
        D2[7] = packetBuffer[104];    // WaterTemp 1st
        D2[8] = packetBuffer[105];    // Watertemp 2nd
        D2[9] = packetBuffer[106];    // WaterPress 1st
        D2[10] = packetBuffer[107];   // WaterPress 2nd
        D2[11] = packetBuffer[108];   // FuelPress 1st
        D2[12] = packetBuffer[109];   // FuelPress 2nd 
        D2[13] = packetBuffer[116];   // FuelLevel 1st 
        D2[14] = packetBuffer[117];   // FuelLevel 2nd  
        D2[15] = packetBuffer[118];   // FuelLevel 3rd   
        D2[16] = packetBuffer[119];   // FuelLevel 4th
        D2[17] = packetBuffer[130];   // EnforcedPit 
        D2[18] = packetBuffer[131];   // CrashState 
        D2[19] = packetBuffer[132];   // OdoKM 1st   
        D2[20] = packetBuffer[133];   // OdoKM 2nd  
        D2[21] = packetBuffer[134];   // OdoKM 3rd
        D2[22] = packetBuffer[135];   // OdoKM 4th
        D2[23] = packetBuffer[456];   // AeroDamage 
        D2[24] = packetBuffer[457];   // EngineDamage  
      
        // Send D2
        delay(1);
        Serial.write(D2, 26);
      }
    
      // D3 Dash packet Timer  
      unsigned long currentMillisD3 = millis();
      if (currentMillisD3 - D3oldtime > D3delay) {
        D3oldtime = currentMillisD3;
    //    Serial.println("*************************************                                   Sending Dash D3");
        // Prepare D3 Dash packet
        D3[3] = packetBuffer[12];      // BestLapTime 1st
        D3[4] = packetBuffer[13];      // BestLapTime 2nd
        D3[5] = packetBuffer[14];      // BestLapTime 3rd
        D3[6] = packetBuffer[15];      // BestLapTime 4th  
        D3[7] = packetBuffer[16];      // LastLapTime 1st
        D3[8] = packetBuffer[17];      // LastLapTime 2nd
        D3[9] = packetBuffer[18];      // LastLapTime 3rd
        D3[10] = packetBuffer[19];     // LastLapTime 4th
        D3[11] = packetBuffer[40];     // FastestLapTime 1st
        D3[12] = packetBuffer[41];     // FastestLapTime 2nd 
        D3[13] = packetBuffer[42];     // FastestLapTime 3rd 
        D3[14] = packetBuffer[43];     // FastestLapTime 4th  
    
        // Send D3
        delay(1);
        Serial.write(D3, 16);
      }
    
    }
    
    //============================================================================
    //=  Send a P0, P1 and P2 packet - PartInfo                                                
    //============================================================================
    void sendPartInfo() {
    //  Serial.println("*************************************************************========================  Sending PartInfo P0");
      if (inGame) {
      // P0 - CarName. in[3] out[3]
        for (int incount = 3; incount < 67; incount++) {
          P0[incount] = packetBuffer[incount];
        }  
        Serial.write(P0, 68);
    
      // P1 - Class. in[67] out[3]
        for (int incount = 3; incount < 67; incount++) {
          P1[incount] = packetBuffer[(incount+64)];
        }  
        Serial.write(P1, 68);
      
      // P2 - Track. in[131] out[3]
        for (int incount = 3; incount < 67; incount++) {
          P2[incount] = packetBuffer[(incount+128)];
        }  
        Serial.write(P2, 68);
      } 
    }
    
    
    //============================================================================
    //=  Serial print WiFi details                                                
    //============================================================================
    void printWifiStatus() {
    //  Serial.print("Network Status: ");    Serial.println(wl_status_to_string(WiFi.status()));
    //  Serial.print("'Status': ");          Serial.println(status);
    
    // 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 your WiFi shield's MAC address:
      WiFi.macAddress(mac);
      Serial.print("MAC: ");
      Serial.print(mac[0],HEX);
      Serial.print(":");
      Serial.print(mac[1],HEX);
      Serial.print(":");
      Serial.print(mac[2],HEX);
      Serial.print(":");
      Serial.print(mac[3],HEX);
      Serial.print(":");
      Serial.print(mac[4],HEX);
      Serial.print(":");
      Serial.println(mac[5],HEX);
      
      // print the received signal strength:
      long rssi = WiFi.RSSI();
      Serial.print("signal strength (RSSI):");  Serial.print(rssi);  Serial.println(" dBm");
    }
    
    //============================================================================
    //=  Serial print Racestate                                                
    //============================================================================
    void raceStatus() {
    
      Serial.println("*********************************************************");
      Serial.print("inGame:            "); Serial.println(inGame);
      Serial.print("practiceStart:     "); Serial.println(practiceStart);
      Serial.print("raceStart:         "); Serial.println(raceStart);
      Serial.print("needSetup:         "); Serial.println(needSetup);
      Serial.print("needPartInfo:      "); Serial.println(needPartInfo);
      Serial.print("firstRun:          "); Serial.println(firstRun);
      
      Serial.print("packetBuffer[3]:   "); Serial.println(packetBuffer[3]);
      Serial.print("packetBuffer[110]: "); Serial.println(packetBuffer[110]);
    }
    void WifiConnected() {
       digitalWrite(ledPin, HIGH);  
         }
    
    
    // END OF PROGRAM



    File: data_out.h
    Code:
    // Project Cars UDP data structure
    /*
    Output fields
    P0 = PartInfo - Sent ONLY once, at start of race.
    D1, D2, D3, D9, D0 = Dashboard - 1=Every Packet, 2= 1 in 5/10
                                     3= 1 in 50, 9=ChangeState, 0=Start only(P1,P2)
    S1 = Suspension Data - Sent EVERY packet
    
    M1,M2,MX, = Motion - XYZ data - 1=Every packet, 2= 1 in 5/10, 9=Change                                  
    
    */
    
    char packetIsConnectInfo = 'C';
    char packetIsPartInfo = 'P';
    char packetIsDash = 'D';
    char packetIsSusp = 'S';
    char packetIsMotion = 'M';
    
    //char startOfPacket = '<';  // 60 = '<'
    //char endOfPacket = '>';    // 62 = '>'
    byte startOfPacket = 60;     // 60 = '<'
    byte endOfPacket = 62;       // 62 = '>'
    
    // Used to Write Packet Number Out
    byte packetNum0 = 0;   // Startup Packet - Only sent at start of Race
    byte packetNum1 = 1;   // Primary data stream. Sent Every packet
    byte packetNum2 = 2;   // Secondary stream. Sent 1 in 5/10 times
    byte packetNum3 = 3;   // 3rd stream. Sent 1 in 50/100 times
    byte packetNum9 = 9;   // Change - Sent on ChangeState
    // Connection Packet byte 2
    byte C0Disconnect = 0;
    byte C0Attempt1 = 1;
    byte C0Attempt2 = 2;
    byte C0Connect = 3;
     
    
    //===============================
    // C0 - Connection Info
    byte C0[4];
    //char startOfPacket = <             // 0
    //char ConnectionC0                  // 1
    //byte ConnectionC0Num               // 2
    //char endOfPacket = >               // 3
    // 4 bytes
    //===============================
    // P0 - PartInfo_0 CarName -  Have to send in 3 packets,cuz serial.write will overflow 128 buffer
    byte P0[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '0'
    //char  CarName[64];                 // 3   // 3
    //char endOfPacket = >               // 67 
    // 68 byte
    
    //===============================
    // P1 - PartInfo_1 Class
    byte P1[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '1'
    //char  CarClassName[64];            // 3   // 67
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // P2 - PartInfo_2 Track
    byte P2[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '2'
    //char  TrackLocation[64];           // 3   // 131
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // D0 - Dashboard_0 - Startup parameters - Send ONCE at start of race
    byte D0[8];
    //char startOfPacket = <             // 0
    //char  Dash_D0;                     // 1   // 0 = 'D'
    //byte  Dash_D0Num                   // 2   // 1 = '0'
    //byte LapsInEvent;                  // 3   // 11   
    //byte FuelCapacity;                 // 4   // 111  
    //unsigned int MaxRpm;               // 5   // 126  
    //char endOfPacket = >               // 7
    // 8 bytes
    
    //===============================
    // D1 - Dashboard_1
    byte D1[32];
    //char startOfPacket = <             // 0
    //char  Dash_D1;                     // 1   // 0 = 'D'
    //byte  Dash_D1Num                   // 2   // 1 = '1'
    //byte PacketNum;                    // 3   // 2    
    //byte GameState;                    // 4   // 3    
    //byte RaceState;                    // 5   // 10
    //byte CarFlags;                     // 6   // 110
    //float CurrentTime;                 // 7   // 20   
    //float Speed;                       // 11  // 120  
    //unsigned int Rpm;                  // 15  // 124  
    //byte GearNumGears;                 // 17  // 128  
    //byte BoostAmount;                  // 18  // 129  
    //float EngineSpeed;                 // 19  // 448  
    //float EngineTorque;                // 23  // 452  
    //byte TyreGrip[4];                  // 27  // 280  
    //char endOfPacket = >               // 31
    // 32 bytes
    
    //===============================
    // D2 - Dashboard_2
    byte D2[26];
    //char startOfPacket = <             // 0
    //char  Dash_D2;                     // 1    // 0 = 'D'
    //byte  Dash_D2Num                   // 2    // 1 = '2'
    //int OilTempCelsius;                // 3    // 100  
    //unsigned int OilPressureKPa;       // 5    // 102  
    //int WaterTempCelsius;              // 7    // 104  
    //unsigned int WaterPressureKpa;     // 9    // 106  
    //unsigned int FuelPressureKpa;      // 11   // 108  
    //float FuelLevel;                   // 13   // 116  
    //char EnforcedPitStopLap;           // 17   // 130  
    //byte CrashState;                   // 18   // 131  
    //float OdometerKM;                  // 19   // 132 
    //byte AeroDamage;                   // 23   // 456 
    //byte EngineDamage;                 // 24   // 457 
    //char endOfPacket = >               // 25
    // 26 bytes
    
    //===============================
    // D3 - Dashboard_3
    byte D3[16];
    //char startOfPacket = <             // 0 
    //char  Dash_D3;                     // 1   // 0 = 'D'
    //byte  Dash_D3Num                   // 2   // 1 = '3'
    //float BestLapTime;                 // 3   // 12   
    //float LastLapTime;                 // 7   // 16   
    //float PersonalFastestLapTime;      // 11  // 40   
    //char endOfPacket = >               // 15
    //16 bytes
    
    //===============================
    // D9 - Dashboard_9 - OnChange - Send if data changes
    byte D9[15];
    //char startOfPacket = <             // 0
    //char  Dash_D9;                     // 1   // 0 = 'D'
    //byte  Dash_D9Num                   // 2   // 1 = '9'
    //byte BrakeDamage[4];               // 3   // 320  
    //byte SuspensionDamage[4];          // 7   // 324  
    //byte DPad;                         // 11  // 1366     
    //unsigned int JoyPad;               // 12  // 96   
    //char endOfPacket = >               // 14
    // 15bytes
    
    //===============================
    // S1 - Suspension - Send EVERY packet
    byte S1[84];
    //char startOfPacket = <             // 0
    //char  Dash_S1;                     // 1   // 0 = 'S'
    //byte  Dash_S1Num                   // 2   // 1 = '1'
    //float TyreHeightAboveGround[4];    // 3   // 284  
    //float WheelLocalPositionY[4];      // 19  // 376  
    //float RideHeight[4];               // 35  // 392  
    //float SuspensionTravel[4];         // 51  // 408  
    //float SuspensionVelocity[4];       // 67  // 424  
    //char endOfPacket = >               // 83
    // 84 bytes
    
    //===============================
    // M1 - Motion - Send EVERY packet (WHEN READY i.e. building new seat)
    byte M1[124];
    //char startOfPacket = <             // 0
    //char  Dash_M1;                     // 1   // 0 = 'M'
    //byte  Dash_M1Num                   // 2   // 1 = '1'
    //float Orientation[3];              // 3   // 136  
    //float LocalVelocity[3];            // 15  // 148  
    //float WorldVelocity[3];            // 27  // 160  
    //float AngularVelocity[3];          // 39  // 172  
    //float LocalAcceleration[3];        // 51  // 184  
    //float WorldAcceleration[3];        // 63  // 196  
    //float ExtentsCentre[3];            // 75  // 208  
    //byte Terrain[4];                   // 87  // 224  
    //float TyreSlipSpeed[4];            // 91  // 260  
    //float TyreY[4];                    // 107 // 228  
    //char endOfPacket = >               // 123
    // 124 bytes
    
    
    //====================================================
    // Left over fields from orig 'Wanted'
    // Unfiltered input
    //byte UnfilteredThrottle;             // 6    - 4
    //byte UnfilteredBrake;                // 7    - 5
    //byte UnfilteredSteering;             // 8    - 6
    //byte UnfilteredClutch;               // 9    - 7
    // Flags
    //byte HighestFlag;                    // 98   - 28
    // Pit info
    //byte PitModeSchedule;             // 99   - 29
    // Car state
    //byte Brake;                       // 112  - 42
    //byte Throttle;                    // 113  - 43
    //byte Clutch;                      // 114  - 44
    //char Steering;                    // 115  - 45
    // Wheels / Tyres
    //byte TyreFlags[4];                // 220  - 152
    //float TyreRPS[4];                 // 244  - 176
    //float TyreLateralStiffness[4];    // 300  - 228
    //byte TyreWear[4];                 // 316  - 244
    //unsigned int AirPressure[4];      // 440  - 320
    // 339                                  // 1368 struct is padded to
    //===============================================================

    File: info
    Code:
    /*
    
    Test Versie met led (220 ohm) status op GPIO2
    
    http://forum.projectcarsgame.com/showthread.php?43111-How-to-import-data-from-wifi-into-arduino&highlight=Esp8266 Post 15
      Version Control
      ===============
      V1 - Copy of esp8266/WiFiscan & WiFiUDPsendrecievestring merged.. 
           scan for finding Hotspot, udp for UDP.
           Recieves packet of size 1367 (Pcars packet size)
      V1.1 Tidy code & reformat.
           Changed packet size from 255 to 1367.
           
     *V2 - Retrieve packets and move to struct for analysis.
      V2.1 Removed Arduino-dash code.. 
      V2.2 Decode ParticipantInfoString
           * NOTE - ESP 44% Full...      
      V3.0 Code cleaned up. All remains or original code removed (udp send, etc)
           Tried various way to get fastestlap. serial printed all steps, but always zero... 
      V3.1 De-code first part of Telemetry to see if gamestate or similar is used to detect if 
           actually driving, cuz telemetry stream is present all the time.. 
           Only need to write a packet IF driving... 
      V3.2 Add Multi Network option - For S5 AND Paul Hotspot
           Tried MultiAP, but had stack overflow..!!  
           My version tries S5 5 times, if still no connection, goto next network (Paul)
      V3.3 Prepare for serial connection to Teensy
           Remove ALL serial prints, and serial.write packetbuffer directl after read.     
      V4.0 Use SerialStream and Struct....!?!
    
      V5.0 Define smaller packets
           Splits data to smaller packets for Serial to Teensy.
           GameState and counts done here. 
           Type 0 = Startup ONLY - Just once at beginning of Race/PitBox
           Type 1 = Every Packet        
           Type 2 = 1 in 5/10 packets - depends on speed
           Type 3 = 1 on 20/50 packets - " ditto "
           Type 9 = OnChange - (Brk & Sus damage + Dpad & Joypad + ?flags)
      V5_1 Decide GameState
             If GameState == 17 OR 81
                Setup/Prepare for Driving
                Send P0 and D0
             Else if GameState == 18 & CF Bit 3 = 1
                Start Dash - Practice
             else if GameState == 82 
                Start Dash - Race      
    
           Added Packet type C for Connection only       
      V5_2 Testing and fixing Start/Stop data options.
           'raceStart' not setting 'needSetup', so had to add 'firstRun'
           Practice and Solo QuickRace working. Will probably need to add Career flags later
      V5_3_1
           'write' packets out.
           Connection
           PartInfo
           D0/D1/D2/D3
    
      
      Program Desciption
      ==================
      Connects to Hotspot (my S5 or Pauls). 
      Reads UDP stream on Port 5606. Port 5606 is the default port for Pcars.
      
      I/Os 
      ==== 
      Just ESP8266 at mo.
      Rx/Tx - serial (Tx > Tx / Rx > Rx on UNO) - Teensy probably opposite..
      ***  Move GPIO 0 to Gnd "BEFORE" rebooting, for flash mode.
      CH_PD & RST & GPIO2 pulled to VCC with 3k3.
    */
    Arduino code (Works on any arduino) this is (currently for DUE)
    This wil be in de 2nd post.
    Last edited by Killg0re NL; 06-10-2018 at 13:52. Reason: adding details
    The following 2 users likes this Post: Anguiano, VelvetTorpedo


  2. #2
    Superkart Pilot Killg0re NL's Avatar
    Join Date
    Sep 2017
    Location
    Netherlands
    Posts
    134
    Platform
    PS4 Pro
    Arduino code


    LEDs:
    • Light A12 // Head light
    • ABS A13 // ABS light
    • ESP A14 // ESP light
    • TCS A15 // TCS light

    Change this to the position where you put the lights on.


    Sketchfile: _PC2_Arduino_V1
    Code:
    /*
     ======================================
     = Teensy RX V5.3r (Pcars_udp_rx_v5.3) =
     ======================================
    */
    
    //============================================================================
    //= Libraries
    //============================================================================
    // Included so 'byte' stops getting 'does not name a type' errors - (Only required because i'm using a Teensy)
    //#include <arduino.h>
    
    // Serial Data in - Cut down to required fields
    #include "20_Data_in.h"
    
    // Data Extract - fields that Data-in is moved to
    #include "20_Data_Extract.h"
    
    // Extra flags not received. For Dashboard setup/control
    #include "20_extra_flags.h"
    
    //============================================================================
    //= Setup Definitions
    //============================================================================
    char sketchName[]= "_PC2_Arduino_V1.ino";      // SketchName
    char sketchDate[]= "1 july 2018";              // SketchDate
    
    //============================================================================
    //= Storage section
    //============================================================================
    // Incoming Buffer
    byte inBuffer[128];          //buffer to hold incoming packet
    byte inByte;
    char inChar;
    bool newPacket = false;
    
    //============================================================================
    //= Setup objects
    //============================================================================
    
    // Controllable LED's
    //#define ledpinRBTN A4    // Encoder Function (not in here yet)
    //#define ledpinFuel A11   // FUEL light
    #define ledpinLight A12    // Head light (temporarily on A12 instead of A5
    #define ledpinABS A13      // ABS light
    #define ledpinESP A14      // ESP light
    #define ledpinTCS A15      // TCS light
    #define ledpinTest 13      // Test light
    int ledValOn = 255;         // Led brightness Range from 0 to 255
    
    //============================================================================
    //= Setup
    //============================================================================
    void setup() {
    //  pinMode(ledpinRBTN,OUTPUT);
    //  pinMode(ledpinFuel,OUTPUT);
      pinMode(ledpinLight,OUTPUT);
      pinMode(ledpinABS,OUTPUT);
      pinMode(ledpinESP,OUTPUT);
      pinMode(ledpinTCS,OUTPUT);
      pinMode(ledpinTest,OUTPUT);
              
      // initialize serial: For Serial monitor
      Serial.begin(115200);
      delay(1000);
      // initialize serial2: Incoming Data
      Serial2.begin(115200);
      delay(1000);
    
    //  Serial.println();
      Serial.println("************************************************************");
      Serial.print("Sketch name: ");  Serial.println(sketchName);
      Serial.print("Sketch date: ");  Serial.println(sketchDate);
      Serial.println("************************************************************"); 
    }
    
    //===============
    // first attempt at a union
    union {
      byte uBytes[4];
      float uFloat;
      long uLong;
    } uData;
    
    union {
      byte sBytes[4];
      float sFloat;
      long sLong;
    } sData;
    
    //============================================================================
    //= Main Loop
    //============================================================================
    void loop() {
      
    // Input serial information:
      if (Serial2.available() > 0){
        Serial2.readBytesUntil(endOfPacket, inBuffer, 128);
        inChar = char(inBuffer[1]);
        Serial.print(inChar);
        Serial.println(inBuffer[2]);
        switch (inChar) {
          case 'C':
            unpackConnectInfo();  // In rx_Connect.ino
            // 
            break;
          case 'P': // Received a PartInfo packet
            if (inBuffer[2] < 3) { // Stop spurious P97 packet??
              unpackPartInfo();    // In rx_CPartInfo.ino
              clearInBuffer();
            }
            break;
          case 'D':
            unpackDash();
            clearInBuffer();
            break;
          case 'S':
            unpackSusp();
            break;
          case 'M':
            unpackMotion();
            break;
        }        
        // clear inBuffer 'PacketType' - see if it stops spurious 8097 packet
    //    inBuffer[1] = 0;
    //    inBuffer[2] = 0;
      }
    }    
        
    
    //============================================================================
    //= Unpack Dashboard
    //============================================================================
    void unpackDash() {
    
      switch (inBuffer[2]) { // packetNum
        case 0: // Setup D0
          Serial.println("");
          LapsInEvent = inBuffer[3];
          FuelCapacity = inBuffer[4];
          MaxRpm = word(inBuffer[6], inBuffer[5]);
          Serial.print("LapsInEvent : "); Serial.println(LapsInEvent);
          Serial.print("FuelCapacity : "); Serial.println(FuelCapacity);
          Serial.print("MaxRpm : "); Serial.println(MaxRpm);
          Serial.println("");
          break;
        case 1: // Main D1
          Serial.println("++");
          PacketNum = inBuffer[3];
          GameState = inBuffer[4];
          RaceState = inBuffer[5];
          CarFlags = inBuffer[6];
          //Set Flags from Bits..
          if (bitRead(CarFlags, 0)) cfLights = true;
          else cfLights = false;
          if (bitRead(CarFlags, 4)) cfAbs = true;
          else cfAbs = false;
          if (bitRead(CarFlags, 6)) cfStab = true;
          else cfStab = false;
          if (bitRead(CarFlags, 7)) cfTrac = true;
          else cfTrac = false;
          Rpm = word(inBuffer[16], inBuffer[15]);
          GearNumGears = inBuffer[17];
          currentGear = GearNumGears & 0xf; // & GearNumGears with 00001111(15) to get value of Low4 bits 
          maxGears = GearNumGears >> 4;     // Shift GearNumGears 4 bits Right. Shift out lower 4, leaving only Top 4 bits
         
          BoostAmount = inBuffer[18];
          TyreGrip[0] = inBuffer[27];      
          TyreGrip[1] = inBuffer[28];      
          TyreGrip[2] = inBuffer[29];      
          TyreGrip[3] = inBuffer[30];      
    
    //debug time
    //      Serial.print("inBuffer[7] : "); Serial.print(inBuffer[7],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[7],BIN);
    //      Serial.print("inBuffer[8] : "); Serial.print(inBuffer[8],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[8],BIN);
    //      Serial.print("inBuffer[9] : "); Serial.print(inBuffer[9],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[9],BIN);
    //      Serial.print("inBuffer[10] : "); Serial.print(inBuffer[10],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[10],BIN);
    
          // Move 4 float fields
          CurrentTime = (inBuffer[10]) << 24 | (inBuffer[9] & 0xff) << 16 | (inBuffer[8] & 0xff) << 8 | (inBuffer[7] & 0xff);
          Speed = (inBuffer[14]) << 24 | (inBuffer[13] & 0xff) << 16 | (inBuffer[12] & 0xff) << 8 | (inBuffer[11] & 0xff);
          EngineSpeed = (inBuffer[22]) << 24 | (inBuffer[21] & 0xff) << 16 | (inBuffer[20] & 0xff) << 8 | (inBuffer[19] & 0xff);
          EngineTorque = (inBuffer[26]) << 24 | (inBuffer[25] & 0xff) << 16 | (inBuffer[24] & 0xff) << 8 | (inBuffer[23] & 0xff);
    // debug speed
    //      Serial.print("inBuffer[11] : "); Serial.print(inBuffer[11],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[11],BIN);
    //      Serial.print("inBuffer[12] : "); Serial.print(inBuffer[12],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[12],BIN);
    //      Serial.print("inBuffer[13] : "); Serial.print(inBuffer[13],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[13],BIN);
    //      Serial.print("inBuffer[14] : "); Serial.print(inBuffer[14],DEC);
    //      Serial.print(" ");Serial.println(inBuffer[14],BIN);
    
    // reverse the bits in bytes 11-14, then feed to union
          byte byte11Rev = 0;
          if (bitRead(inBuffer[11], 0)) bitSet(byte11Rev, 7);
          if (bitRead(inBuffer[11], 1)) bitSet(byte11Rev, 6);
          if (bitRead(inBuffer[11], 2)) bitSet(byte11Rev, 5);
          if (bitRead(inBuffer[11], 3)) bitSet(byte11Rev, 4);
          if (bitRead(inBuffer[11], 4)) bitSet(byte11Rev, 3);
          if (bitRead(inBuffer[11], 5)) bitSet(byte11Rev, 2);
          if (bitRead(inBuffer[11], 6)) bitSet(byte11Rev, 1);
          if (bitRead(inBuffer[11], 7)) bitSet(byte11Rev, 0);
          byte byte12Rev = 0;
          if (bitRead(inBuffer[12], 0)) bitSet(byte12Rev, 7);
          if (bitRead(inBuffer[12], 1)) bitSet(byte12Rev, 6);
          if (bitRead(inBuffer[12], 2)) bitSet(byte12Rev, 5);
          if (bitRead(inBuffer[12], 3)) bitSet(byte12Rev, 4);
          if (bitRead(inBuffer[12], 4)) bitSet(byte12Rev, 3);
          if (bitRead(inBuffer[12], 5)) bitSet(byte12Rev, 2);
          if (bitRead(inBuffer[12], 6)) bitSet(byte12Rev, 1);
          if (bitRead(inBuffer[12], 7)) bitSet(byte12Rev, 0);
          byte byte13Rev = 0;
          if (bitRead(inBuffer[13], 0)) bitSet(byte13Rev, 7);
          if (bitRead(inBuffer[13], 1)) bitSet(byte13Rev, 6);
          if (bitRead(inBuffer[13], 2)) bitSet(byte13Rev, 5);
          if (bitRead(inBuffer[13], 3)) bitSet(byte13Rev, 4);
          if (bitRead(inBuffer[13], 4)) bitSet(byte13Rev, 3);
          if (bitRead(inBuffer[13], 5)) bitSet(byte13Rev, 2);
          if (bitRead(inBuffer[13], 6)) bitSet(byte13Rev, 1);
          if (bitRead(inBuffer[13], 7)) bitSet(byte13Rev, 0);
          byte byte14Rev = 0;
          if (bitRead(inBuffer[14], 0)) bitSet(byte14Rev, 7);
          if (bitRead(inBuffer[14], 1)) bitSet(byte14Rev, 6);
          if (bitRead(inBuffer[14], 2)) bitSet(byte14Rev, 5);
          if (bitRead(inBuffer[14], 3)) bitSet(byte14Rev, 4);
          if (bitRead(inBuffer[14], 4)) bitSet(byte14Rev, 3);
          if (bitRead(inBuffer[14], 5)) bitSet(byte14Rev, 2);
          if (bitRead(inBuffer[14], 6)) bitSet(byte14Rev, 1);
          if (bitRead(inBuffer[14], 7)) bitSet(byte14Rev, 0);
    
          
    //      Serial.print("byte11Rev      : "); Serial.println(byte11Rev,BIN);
    //      Serial.print("byte12Rev      : "); Serial.println(byte12Rev,BIN);
    //      Serial.print("byte13Rev      : "); Serial.println(byte13Rev,BIN);
    //      Serial.print("byte14Rev      : "); Serial.println(byte14Rev,BIN);
    
          
          uData.uBytes[0] = inBuffer[11];
          uData.uBytes[1] = inBuffer[12];
          uData.uBytes[2] = inBuffer[13];
          uData.uBytes[3] = inBuffer[14];
    
          sData.sBytes[0] = inBuffer[19];
          sData.sBytes[1] = inBuffer[20];
          sData.sBytes[2] = inBuffer[21];
          sData.sBytes[3] = inBuffer[22];
    
    
    //      uData.uBytes[0] = byte14Rev;
    //      uData.uBytes[1] = byte13Rev;
    //      uData.uBytes[2] = byte12Rev;
    //      uData.uBytes[3] = byte11Rev;
    
    //      Serial.print("uBytes  [0]-[11] : "); Serial.println(uData.uBytes[0]);
    //      Serial.print("uBytes  [1]-[12] : "); Serial.println(uData.uBytes[1]);
    //      Serial.print("uBytes  [2]-[13] : "); Serial.println(uData.uBytes[2]);
    //      Serial.print("uBytes  [3]-[14] : "); Serial.println(uData.uBytes[3]);
    //      Serial.print("uFloat      : ");      Serial.println(uData.uFloat,3);
    //      Serial.print("uLong       : ");      Serial.println(uData.uLong);
    
    //      Serial.print("sBytes  [0]-[19] : "); Serial.println(sData.sBytes[0]);
    //      Serial.print("sBytes  [1]-[20] : "); Serial.println(sData.sBytes[1]);
    //      Serial.print("sBytes  [2]-[21] : "); Serial.println(sData.sBytes[2]);
    //      Serial.print("sBytes  [3]-[22] : "); Serial.println(sData.sBytes[3]);
    //      Serial.print("sfloat      : ");      Serial.println(sData.sFloat,3);
    //      Serial.print("sLong       : ");      Serial.println(sData.sLong);
    
    // debug
    //      Serial.print("PacketNum : "); Serial.println(PacketNum);
    //      Serial.print("GameState : "); Serial.println(GameState);
    //      Serial.print("RaceState : "); Serial.println(RaceState);
    //      Serial.print("CarFlags : "); Serial.println(CarFlags);
    
          if (cfAbs) Serial.println("                                 ABS");
          if (cfStab) Serial.println("                                         ESP");
          if (cfTrac) Serial.println("                                                  TCS");
         
    //      Serial.print("Rpm : ");          Serial.println(Rpm);             // Currently not working?
    //      Serial.print("GearNumGears : "); Serial.println(GearNumGears);    // Currently not working?
    //      Serial.print("BoostAmount : ");  Serial.println(BoostAmount);     // Currently not working?
    //      Serial.print("TyreGrip[0] : ");  Serial.println(TyreGrip[0]);     // Currently not working?
    //      Serial.print("TyreGrip[1] : ");  Serial.println(TyreGrip[1]);     // Currently not working?
    //      Serial.print("TyreGrip[2] : ");  Serial.println(TyreGrip[2]);     // Currently not working?
    //      Serial.print("TyreGrip[3] : ");  Serial.println(TyreGrip[3]);     // Currently not working?
    //      Serial.print("CurrentTime : ");  Serial.println(CurrentTime,3);   // Currently not working?
    //      Serial.print("Speed : ");        Serial.println(Speed,3);         // Currently not working?
    //      Serial.print("EngineSpeed : ");  Serial.println(EngineSpeed,3);   // Currently not working?
    //      Serial.print("EngineTorque : "); Serial.println(EngineTorque,3);  // Currently not working?
    //      Serial.print("maxGears : ");     Serial.println(maxGears);
    //      Serial.print("currentGear : ");  Serial.println(currentGear);
    //      Serial.print("FuelCapacity : "); Serial.println(FuelLevel);       // Currently not working?
    
    // Status Lights
    
    /* Turn leds off when NOT racing (in BETA) GameState X is:
     *  17 is exiting Practice (Offline & Online)
     *  49 is exiting Qualification (Online, and Offline) 
     *  81 is exiting Replay, and exiting online Race
     *  
     *  97 is exiting (Offline) TimeTrial, 
     *  but there must be more
     *  FYI:
     *  18 is racing (Online and Offline) practice
     *  50 is racing (Online and Offline) qualifing
     *  82 is racing (Online abd Offline) Race
    */
    
    if (GameState == 0 || GameState == 17 || GameState == 49 || GameState == 81 || GameState == 97)  {
    //   Serial.println("Turn; Light, ABS, ESP and TCS Off"); //Debug Option
       analogWrite(ledpinLight, LOW);                   // turn Light LED off
       analogWrite(ledpinABS, LOW);                     // turn ABS LED off 
       analogWrite(ledpinESP, LOW);                     // turn ESP LED off
       analogWrite(ledpinTCS, LOW);                     // turn TCS LED off
       analogWrite(ledpinTest, LOW);                     // turn TCS LED off
    } 
    else{
    
    // Lights
    if (cfLights == true) {
     Serial.println("Light ON");                       // Debug option
       analogWrite(ledpinLight, ledValOn);               // turn Light LED on
       analogWrite(ledpinTest, ledValOn);               // turn Light LED on
    }
    else  {
     Serial.println("Light OFF");                      // Debug option
       digitalWrite(ledpinLight, LOW);                    // turn Light LED off
       digitalWrite(ledpinTest, LOW);                    // turn Light LED off
      }
    
    // ABS  
    if (cfAbs == true) {
    // Serial.println("ABS ON");                         // Debug option
       analogWrite(ledpinABS, ledValOn);                 // turn ABS LED on
    }
    else  {
    // Serial.println("ABS OFF");                        // Debug option
       analogWrite(ledpinABS, LOW);                      // turn ABS LED off
      }  
    
    // ESP 
    if (cfStab == true) {
    // Serial.println("ESP ON");                         // Debug option
       analogWrite(ledpinESP, ledValOn);                 // turn ESP LED on
    }
    else  {
    // Serial.println("ESP OFF");                        // Debug option
       analogWrite(ledpinESP, LOW);                      // turn ESP LED off
      }  
    // TCS  
    if (cfTrac == true) {
    // Serial.println("TCS ON");                         // Debug option
       analogWrite(ledpinTCS, ledValOn);                 // turn TCS LED on
    }
    else  {
    // Serial.println("TCS OFF");                        // Debug option
       analogWrite(ledpinTCS, LOW);                     // turn TCS LED off
      }  
    }
            
          Serial.println("");
          break;
    //    case 2:
    //      Serial.println("++++Received D2 ");
    //      break;
    //    case 3:
    //      Serial.println("++++Received D3 ");
    //      break;
      }
    }
    
    
    
    
    //============================================================================
    //= Unpack Suspension
    //============================================================================
    void unpackSusp() {
    //   Serial.println("void unpackSusp"); // Debug
    
    }
    
    //============================================================================
    //= Unpack Motion
    //============================================================================
    void unpackMotion() {
    //   Serial.println("                void unpackMotion"); // Debug
    
    
    }
    
    //============================================================================
    //= Unpack Motion
    //============================================================================
    void clearInBuffer() {
      for (int countCLR = 0; countCLR < 128; countCLR++) {
        inBuffer[countCLR] = 0;
    //   Serial.println("                                   void clearInBuffer"); // Debug
      }
    
    }
    
    // END OF CODE ================================================
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  
    //  = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    file: 20_Data_Extract.h
    Code:
    //===============================
    // C0 - Connection Info
    //byte C0[4];
    //char startOfPacket = <             // 0
    //char ConnectionC0                  // 1
    //byte ConnectionC0Num               // 2
    //char endOfPacket = >               // 3
    // 4 bytes
    //===============================
    // P0 - PartInfo_0 CarName -  Have to send in 3 packets,cuz serial.write will overflow 128 buffer
    //byte P0[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '0'
    char  CarName[64];                   // 3   // 3
    //char endOfPacket = >               // 67 
    // 68 byte
    
    //===============================
    // P1 - PartInfo_1 Class
    //byte P1[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '1'
    char  CarClassName[64];              // 3   // 67
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // P2 - PartInfo_2 Track
    //byte P2[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '2'
    char  TrackLocation[64];             // 3   // 131
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // D0 - Dashboard_0 - Startup parameters - Send ONCE at start of race
    //byte D0[8];
    //char startOfPacket = <             // 0
    //char  Dash_D0;                     // 1   // 0 = 'D'
    //byte  Dash_D0Num                   // 2   // 1 = '0'
    byte LapsInEvent;                    // 3   // 11   
    byte FuelCapacity;                   // 4   // 111  
    unsigned int MaxRpm;                 // 5   // 126  
    //char endOfPacket = >               // 7
    // 8 bytes
    
    //===============================
    // D1 - Dashboard_1
    //byte D1[32];
    //char startOfPacket = <           // 0
    //char  Dash_D1;                   // 1   // 0 = 'D'
    //byte  Dash_D1Num                 // 2   // 1 = '1'
    byte PacketNum;                    // 3   // 2    
    byte GameState;                    // 4   // 3    
    byte RaceState;                    // 5   // 10
    byte CarFlags;                     // 6   // 110
    float CurrentTime;                 // 7   // 20   
    float Speed;                       // 11  // 120  
    unsigned int Rpm;                  // 15  // 124  
    byte GearNumGears;                 // 17  // 128  
    byte BoostAmount;                  // 18  // 129  
    float EngineSpeed;                 // 19  // 448  
    float EngineTorque;                // 23  // 452  
    byte TyreGrip[4];                  // 27  // 280  
    //char endOfPacket = >             // 31
    // 32 bytes
    
    //===============================
    // D2 - Dashboard_2
    //byte D2[26];
    //char startOfPacket = <           // 0
    //char  Dash_D2;                   // 1    // 0 = 'D'
    //byte  Dash_D2Num                 // 2    // 1 = '2'
    int OilTempCelsius;                // 3    // 100  
    unsigned int OilPressureKPa;       // 5    // 102  
    int WaterTempCelsius;              // 7    // 104  
    unsigned int WaterPressureKpa;     // 9    // 106  
    unsigned int FuelPressureKpa;      // 11   // 108  
    float FuelLevel;                   // 13   // 116  
    char EnforcedPitStopLap;           // 17   // 130  
    byte CrashState;                   // 18   // 131  
    float OdometerKM;                  // 19   // 132 
    byte AeroDamage;                   // 23   // 456 
    byte EngineDamage;                 // 24   // 457 
    //char endOfPacket = >             // 25
    // 26 bytes
    
    //===============================
    // D3 - Dashboard_3
    //byte D3[16];
    //char startOfPacket = <           // 0 
    //char  Dash_D3;                   // 1   // 0 = 'D'
    //byte  Dash_D3Num                 // 2   // 1 = '3'
    float BestLapTime;                 // 3   // 12   
    float LastLapTime;                 // 7   // 16   
    float PersonalFastestLapTime;      // 11  // 40   
    //char endOfPacket = >             // 15
    //16 bytes
    
    //===============================
    // D9 - Dashboard_9 - OnChange - Send if data changes
    //byte D9[15];
    //char startOfPacket = <           // 0
    //char  Dash_D9;                   // 1   // 0 = 'D'
    //byte  Dash_D9Num                 // 2   // 1 = '9'
    byte BrakeDamage[4];               // 3   // 320  
    byte SuspensionDamage[4];          // 7   // 324  
    byte DPad;                         // 11  // 1366     
    unsigned int JoyPad;               // 12  // 96   
    //char endOfPacket = >             // 14
    // 15bytes
    
    //===============================
    // S1 - Suspension - Send EVERY packet
    //byte S1[84];
    //char startOfPacket = <           // 0
    //char  Dash_S1;                   // 1   // 0 = 'S'
    //byte  Dash_S1Num                 // 2   // 1 = '1'
    float TyreHeightAboveGround[4];    // 3   // 284  
    float WheelLocalPositionY[4];      // 19  // 376  
    float RideHeight[4];               // 35  // 392  
    float SuspensionTravel[4];         // 51  // 408  
    float SuspensionVelocity[4];       // 67  // 424  
    //char endOfPacket = >             // 83
    // 84 bytes
    
    //===============================
    // M1 - Motion - Send EVERY packet (WHEN READY i.e. building new seat)
    //byte M1[124];
    //char startOfPacket = <           // 0
    //char  Dash_M1;                   // 1   // 0 = 'M'
    //byte  Dash_M1Num                 // 2   // 1 = '1'
    float Orientation[3];              // 3   // 136  
    float LocalVelocity[3];            // 15  // 148  
    float WorldVelocity[3];            // 27  // 160  
    float AngularVelocity[3];          // 39  // 172  
    float LocalAcceleration[3];        // 51  // 184  
    float WorldAcceleration[3];        // 63  // 196  
    float ExtentsCentre[3];            // 75  // 208  
    byte Terrain[4];                   // 87  // 224  
    float TyreSlipSpeed[4];            // 91  // 260  
    float TyreY[4];                    // 107 // 228  
    //char endOfPacket = >             // 123
    // 124 bytes
    file: 20_Data_in.h
    Code:
    // Project Cars UDP data structure
    /*
    Output fields
    P0 = PartInfo - Sent ONLY once, at start of race.
    D1, D2, D3, D9, D0 = Dashboard - 1=Every Packet, 2= 1 in 5/10
                                     3= 1 in 50, 9=ChangeState, 0=Start only(P1,P2)
    S1 = Suspension Data - Sent EVERY packet
    
    M1,M2,MX, = Motion - XYZ data - 1=Every packet, 2= 1 in 5/10, 9=Change                                  
    
    */
    
    //char packetType; // Used to Write Packet Type Out
    char packetIsConnectInfo = 'C';
    char packetIsPartInfo = 'P';
    char packetIsDash = 'D';
    char packetIsSusp = 'S';
    char packetIsMotion = 'M';
    
    //char startOfPacket = '<';  // 60 = '<'
    //char endOfPacket = '>';    // 62 = '>'
    byte startOfPacket = 60;     // 60 = '<'
    byte endOfPacket = 62;       // 62 = '>'
    
    // Used to Write Packet Number Out
    byte packetNum0 = 0;   // Startup Packet - Only sent at start of Race
    byte packetNum1 = 1;   // Primary data stream. Sent Every packet
    byte packetNum2 = 2;   // Secondary stream. Sent 1 in 5/10 times
    byte packetNum3 = 3;   // 3rd stream. Sent 1 in 50/100 times
    byte packetNum9 = 9;   // Change - Sent on ChangeState
    // Connection Packet byte 2
    byte C0Disconnect = 0;
    byte C0Attempt1 = 1;
    byte C0Attempt2 = 2;
    byte C0Connect = 3;
     
    
    //===============================
    // C0 - Connection Info
    byte C0[4];
    //char startOfPacket = <             // 0
    //char ConnectionC0                  // 1
    //byte ConnectionC0Num               // 2
    //char endOfPacket = >               // 3
    // 4 bytes
    //===============================
    // P0 - PartInfo_0 CarName -  Have to send in 3 packets,cuz serial.write will overflow 128 buffer
    byte P0[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '0'
    //char  CarName[64];                 // 3   // 3
    //char endOfPacket = >               // 67 
    // 68 byte
    
    //===============================
    // P1 - PartInfo_1 Class
    byte P1[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '1'
    //char  CarClassName[64];            // 3   // 67
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // P2 - PartInfo_2 Track
    byte P2[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '2'
    //char  TrackLocation[64];           // 3   // 131
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // D0 - Dashboard_0 - Startup parameters - Send ONCE at start of race
    byte D0[8];
    //char startOfPacket = <             // 0
    //char  Dash_D0;                     // 1   // 0 = 'D'
    //byte  Dash_D0Num                   // 2   // 1 = '0'
    //byte LapsInEvent;                  // 3   // 11   
    //byte FuelCapacity;                 // 4   // 111  
    //unsigned int MaxRpm;               // 5   // 126  
    //char endOfPacket = >               // 7
    // 8 bytes
    
    //===============================
    // D1 - Dashboard_1
    byte D1[32];
    //char startOfPacket = <             // 0
    //char  Dash_D1;                     // 1   // 0 = 'D'
    //byte  Dash_D1Num                   // 2   // 1 = '1'
    //byte PacketNum;                    // 3   // 2    
    //byte GameState;                    // 4   // 3    
    //byte RaceState;                    // 5   // 10
    //byte CarFlags;                     // 6   // 110
    //float CurrentTime;                 // 7   // 20   
    //float Speed;                       // 11  // 120  
    //unsigned int Rpm;                  // 15  // 124  
    //byte GearNumGears;                 // 17  // 128  
    //byte BoostAmount;                  // 18  // 129  
    //float EngineSpeed;                 // 19  // 448  
    //float EngineTorque;                // 23  // 452  
    //byte TyreGrip[4];                  // 27  // 280  
    //char endOfPacket = >               // 31
    // 32 bytes
    
    //===============================
    // D2 - Dashboard_2
    byte D2[26];
    //char startOfPacket = <             // 0
    //char  Dash_D2;                     // 1    // 0 = 'D'
    //byte  Dash_D2Num                   // 2    // 1 = '2'
    //int OilTempCelsius;                // 3    // 100  
    //unsigned int OilPressureKPa;       // 5    // 102  
    //int WaterTempCelsius;              // 7    // 104  
    //unsigned int WaterPressureKpa;     // 9    // 106  
    //unsigned int FuelPressureKpa;      // 11   // 108  
    //float FuelLevel;                   // 13   // 116  
    //char EnforcedPitStopLap;           // 17   // 130  
    //byte CrashState;                   // 18   // 131  
    //float OdometerKM;                  // 19   // 132 
    //byte AeroDamage;                   // 23   // 456 
    //byte EngineDamage;                 // 24   // 457 
    //char endOfPacket = >               // 25
    // 26 bytes
    
    //===============================
    // D3 - Dashboard_3
    byte D3[16];
    //char startOfPacket = <             // 0 
    //char  Dash_D3;                     // 1   // 0 = 'D'
    //byte  Dash_D3Num                   // 2   // 1 = '3'
    //float BestLapTime;                 // 3   // 12   
    //float LastLapTime;                 // 7   // 16   
    //float PersonalFastestLapTime;      // 11  // 40   
    //char endOfPacket = >               // 15
    //16 bytes
    
    //===============================
    // D9 - Dashboard_9 - OnChange - Send if data changes
    byte D9[15];
    //char startOfPacket = <             // 0
    //char  Dash_D9;                     // 1    // 0 = 'D'
    //byte  Dash_D9Num                   // 2    // 1 = '9'
    //byte BrakeDamage[4];               // 3    // 320  
    //byte SuspensionDamage[4];          // 7    // 324  
    //byte DPad;                         // 11   // 1366     
    //unsigned int JoyPad;               // 12   // 96   
    //char endOfPacket = >               // 14
    // 15bytes
    
    //===============================
    // S1 - Suspension - Send EVERY packet
    byte S1[84];
    //char startOfPacket = <             // 0
    //char  Dash_S1;                     // 1   // 0 = 'S'
    //byte  Dash_S1Num                   // 2   // 1 = '1'
    //float TyreHeightAboveGround[4];    // 3   // 284  
    //float WheelLocalPositionY[4];      // 19  // 376  
    //float RideHeight[4];               // 35  // 392  
    //float SuspensionTravel[4];         // 51  // 408  
    //float SuspensionVelocity[4];       // 67  // 424  
    //char endOfPacket = >               // 83
    // 84 bytes
    
    //===============================
    // M1 - Motion - Send EVERY packet (WHEN READY i.e. building new seat)
    byte M1[124];
    //char startOfPacket = <             // 0
    //char  Dash_M1;                     // 1   // 0 = 'M'
    //byte  Dash_M1Num                   // 2   // 1 = '1'
    //float Orientation[3];              // 3   // 136  
    //float LocalVelocity[3];            // 15  // 148  
    //float WorldVelocity[3];            // 27  // 160  
    //float AngularVelocity[3];          // 39  // 172  
    //float LocalAcceleration[3];        // 51  // 184  
    //float WorldAcceleration[3];        // 63  // 196  
    //float ExtentsCentre[3];            // 75  // 208  
    //byte Terrain[4];                   // 87  // 224  
    //float TyreSlipSpeed[4];            // 91  // 260  
    //float TyreY[4];                    // 107 // 228  
    //char endOfPacket = >               // 123
    // 124 bytes
    file: 20_extra_flags.h
    Code:
    //=================================
    // DashBoard Extra flags. Not received, just for processing here
    bool ignitionOn = false;    // Read RPM - If last rpm was 0, now >0  Ign ON
    bool startUp = false;       // Use to run dash startup seq
    bool lightsOn = false;      // read from carFlags
    bool absOn = false;         // read from carFlags - 3 bits in carFlags change  
    bool tractionOn = false;    // read from carFlags - find out which is which
    bool stabilityOn = false;   // read from carFlags - if its there..
    
    // RPM = int, enginespeed = float. Not sure which yet
    // float lastRPM = 0;          // Hold last RPM value
    // int lastRPM = 0;            // Hold last RPM value
    
    //=================================
    // wifiLED status. No connect=Off, Attempt=Flash, Connected=Solid
    bool wifiLEDattempt = false; // WiFi Status - attempting - Flash
    bool wifiLEDconnect = false; // WiFi Status - connected - Solid
    bool connectS5 = false;
    bool connectPaul = false;
    
    long currentTimeLong = 0;
    long speedLong = 0;
    long engineSpeedLong = 0;
    long engineTorqueLong = 0;
    
    byte maxGears;
    byte currentGear;
    
    bool cfLights = false;
    bool cfAbs = false;
    bool cfStab = false;
    bool cfTrac = false;
    file: data_out.h
    Code:
    // Project Cars UDP data structure
    /*
    Output fields
    P0 = PartInfo - Sent ONLY once, at start of race.
    D1, D2, D3, D9, D0 = Dashboard - 1=Every Packet, 2= 1 in 5/10
                                     3= 1 in 50, 9=ChangeState, 0=Start only(P1,P2)
    S1 = Suspension Data - Sent EVERY packet
    
    M1,M2,MX, = Motion - XYZ data - 1=Every packet, 2= 1 in 5/10, 9=Change                                  
    
    */
    
    //char packetType; // Used to Write Packet Type Out
    char packetIsConnectInfo = 'C';
    char packetIsPartInfo = 'P';
    char packetIsDash = 'D';
    char packetIsSusp = 'S';
    char packetIsMotion = 'M';
    
    //char startOfPacket = '<';  // 60 = '<'
    //char endOfPacket = '>';    // 62 = '>'
    byte startOfPacket = 60;     // 60 = '<'
    byte endOfPacket = 62;       // 62 = '>'
    
    // Used to Write Packet Number Out
    byte packetNum0 = 0;   // Startup Packet - Only sent at start of Race
    byte packetNum1 = 1;   // Primary data stream. Sent Every packet
    byte packetNum2 = 2;   // Secondary stream. Sent 1 in 5/10 times
    byte packetNum3 = 3;   // 3rd stream. Sent 1 in 50/100 times
    byte packetNum9 = 9;   // Change - Sent on ChangeState
    // Connection Packet byte 2
    byte C0Disconnect = 0;
    byte C0Attempt1 = 1;
    byte C0Attempt2 = 2;
    byte C0Connect = 3;
     
    
    //===============================
    // C0 - Connection Info
    byte C0[4];
    //char startOfPacket = <             // 0
    //char ConnectionC0                  // 1
    //byte ConnectionC0Num               // 2
    //char endOfPacket = >               // 3
    // 4 bytes
    //===============================
    // P0 - PartInfo_0 CarName -  Have to send in 3 packets,cuz serial.write will overflow 128 buffer
    byte P0[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '0'
    //char  CarName[64];                 // 3   // 3
    //char endOfPacket = >               // 67 
    // 68 byte
    
    //===============================
    // P1 - PartInfo_1 Class
    byte P1[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '1'
    //char  CarClassName[64];            // 3   // 67
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // P2 - PartInfo_2 Track
    byte P2[68];
    //char startOfPacket = <             // 0
    //char  PartInfoP0;                  // 1   // 0 = 'P'
    //byte  PartInfoP0Num                // 2   // 1 = '2'
    //char  TrackLocation[64];           // 3   // 131
    //char endOfPacket = >               // 67
    // 68 byte
    
    //===============================
    // D0 - Dashboard_0 - Startup parameters - Send ONCE at start of race
    byte D0[8];
    //char startOfPacket = <             // 0
    //char  Dash_D0;                     // 1   // 0 = 'D'
    //byte  Dash_D0Num                   // 2   // 1 = '0'
    //byte LapsInEvent;                  // 3   // 11   
    //byte FuelCapacity;                 // 4   // 111  
    //unsigned int MaxRpm;               // 5   // 126  
    //char endOfPacket = >               // 7
    // 8 bytes
    
    //===============================
    // D1 - Dashboard_1
    byte D1[32];
    //char startOfPacket = <             // 0
    //char  Dash_D1;                     // 1   // 0 = 'D'
    //byte  Dash_D1Num                   // 2   // 1 = '1'
    //byte PacketNum;                    // 3   // 2    
    //byte GameState;                    // 4   // 3    
    //byte RaceState;                    // 5   // 10
    //byte CarFlags;                     // 6   // 110
    //float CurrentTime;                 // 7   // 20   
    //float Speed;                       // 11  // 120  
    //unsigned int Rpm;                  // 15  // 124  
    //byte GearNumGears;                 // 17  // 128  
    //byte BoostAmount;                  // 18  // 129  
    //float EngineSpeed;                 // 19  // 448  
    //float EngineTorque;                // 23  // 452  
    //byte TyreGrip[4];                  // 27  // 280  
    //char endOfPacket = >               // 31
    // 32 bytes
    
    //===============================
    // D2 - Dashboard_2
    byte D2[26];
    //char startOfPacket = <             // 0
    //char  Dash_D2;                     // 1   // 0 = 'D'
    //byte  Dash_D2Num                   // 2   // 1 = '2'
    //int OilTempCelsius;                // 3   // 100  
    //unsigned int OilPressureKPa;       // 5   // 102  
    //int WaterTempCelsius;              // 7   // 104  
    //unsigned int WaterPressureKpa;     // 9   // 106  
    //unsigned int FuelPressureKpa;      // 11   // 108  
    //float FuelLevel;                   // 13   // 116  
    //char EnforcedPitStopLap;           // 17   // 130  
    //byte CrashState;                   // 18   // 131  
    //float OdometerKM;                  // 19   // 132 
    //byte AeroDamage;                   // 23   // 456 
    //byte EngineDamage;                 // 24   // 457 
    //char endOfPacket = >               // 25
    // 26 bytes
    
    //===============================
    // D3 - Dashboard_3
    byte D3[16];
    //char startOfPacket = <             // 0 
    //char  Dash_D3;                     // 1   // 0 = 'D'
    //byte  Dash_D3Num                   // 2   // 1 = '3'
    //float BestLapTime;                 // 3   // 12   
    //float LastLapTime;                 // 7   // 16   
    //float PersonalFastestLapTime;      // 11  // 40   
    //char endOfPacket = >               // 15
    //16 bytes
    
    //===============================
    // D9 - Dashboard_9 - OnChange - Send if data changes
    byte D9[15];
    //char startOfPacket = <             // 0
    //char  Dash_D9;                     // 1   // 0 = 'D'
    //byte  Dash_D9Num                   // 2   // 1 = '9'
    //byte BrakeDamage[4];               // 3   // 320  
    //byte SuspensionDamage[4];          // 7   // 324  
    //byte DPad;                         // 11  // 1366     
    //unsigned int JoyPad;               // 12   // 96   
    //char endOfPacket = >               // 14
    // 15bytes
    
    //===============================
    // S1 - Suspension - Send EVERY packet
    byte S1[84];
    //char startOfPacket = <             // 0
    //char  Dash_S1;                     // 1   // 0 = 'S'
    //byte  Dash_S1Num                   // 2   // 1 = '1'
    //float TyreHeightAboveGround[4];    // 3   // 284  
    //float WheelLocalPositionY[4];      // 19  // 376  
    //float RideHeight[4];               // 35  // 392  
    //float SuspensionTravel[4];         // 51  // 408  
    //float SuspensionVelocity[4];       // 67  // 424  
    //char endOfPacket = >               // 83
    // 84 bytes
    
    //===============================
    // M1 - Motion - Send EVERY packet (WHEN READY i.e. building new seat)
    byte M1[124];
    //char startOfPacket = <             // 0
    //char  Dash_M1;                     // 1   // 0 = 'M'
    //byte  Dash_M1Num                   // 2   // 1 = '1'
    //float Orientation[3];              // 3   // 136  
    //float LocalVelocity[3];            // 15  // 148  
    //float WorldVelocity[3];            // 27  // 160  
    //float AngularVelocity[3];          // 39  // 172  
    //float LocalAcceleration[3];        // 51  // 184  
    //float WorldAcceleration[3];        // 63  // 196  
    //float ExtentsCentre[3];            // 75  // 208  
    //byte Terrain[4];                   // 87  // 224  
    //float TyreSlipSpeed[4];            // 91  // 260  
    //float TyreY[4];                    // 107 // 228  
    //char endOfPacket = >               // 123
    // 124 bytes
    
    
    //====================================================
    // Left over fields from orig 'Wanted'
    // Unfiltered input
    //byte UnfilteredThrottle;             // 6    - 4
    //byte UnfilteredBrake;                // 7    - 5
    //byte UnfilteredSteering;             // 8    - 6
    //byte UnfilteredClutch;               // 9    - 7
    // Flags
    //byte HighestFlag;                    // 98   - 28
    // Pit info
    //byte PitModeSchedule;             // 99   - 29
    // Car state
    //byte Brake;                       // 112  - 42
    //byte Throttle;                    // 113  - 43
    //byte Clutch;                      // 114  - 44
    //char Steering;                    // 115  - 45
    // Wheels / Tyres
    //byte TyreFlags[4];                // 220  - 152
    //float TyreRPS[4];                 // 244  - 176
    //float TyreLateralStiffness[4];    // 300  - 228
    //byte TyreWear[4];                 // 316  - 244
    //unsigned int AirPressure[4];      // 440  - 320
    // 339                                  // 1368 struct is padded to
    //===============================================================
    file: info
    Code:
    /*
    ANALOG IN/OUTPUT:
    A0      - i - Keypad Column 1
    A1      - i - Keypad Column 2
    A2      - i - Keypad Column 3
    A3      - i - Keypad Column 4
    A4      - o - Led Encoder Function
    A5      - o - Led Headlight
    A6 (DUE)- o - Led ABS
    A7 (DUE)- o - Led ESP
    A8 (DUE)- o - Led TCS
    A9 (DUE)- o - Led Fuel
    A10(DUE)- i/o - 
    A11(DUE)- i/o - 
    
    DIGITAL IN/OUTPUT
    0 (RX)  - i - Reserved for serial(0) interface
    1 (TX)  - i - Reserved for serial(0) interface
    2       - i - Keypad Row 1
    3       - i - Keypad Row 2
    4       - i - Keypad Row 3
    5       - i - Keypad Row 4
    6       - i - Encoder 1a
    7       - i - Encoder 1b
    8       - i - Encoder 2a
    9       - i - Encoder 2b
    10      - i - Encoder 3a
    11      - i - Encoder 3b
    12      - i - Encoder 4a
    13      - i - Encoder 4b
    14(DUE) - i/o - Reserved for serial(3)
    15(DUE) - i/o - Reserved for serial(3)
    16(DUE) - i/o - Reserved for serial(2)
    17(DUE) - i/o - Reserved for serial(2)
    18(DUE) - i/o - Reserved for serial(1)
    19(DUE) - i/o - Reserved for serial(1)
    20(DUE) - i/o - Reserved for SDA
    21(DUE) - i/o - Reserved for SCL
    22(DUE) - i/o -
    23(DUE) - i/o -
    24(DUE) - i/o -
    25(DUE) - i/o -
    26(DUE) - i/o -
    27(DUE) - i/o -
    28(DUE) - i/o -
    29(DUE) - i/o -
    30(DUE) - i/o -
    and 31 to 53
      
      Version Control
      ===============
      V1 - Copy of serialEvent example, but completely changed.
      V4.01 - Waits for 'startofpacket', then reads next byte (hijacked)
              Then, now on byte 2, reads fixed length buffer into 
              temp buffer (one less than original, cuz its read twice
              Then.. Moves temp buffer to full buffer
      V5.3 - Use ReadbyteUntil        
    
    
            
      Program Desciption
      ==================
      Reads Serial2 for startofpacket ("<")
      Moves next 1367 bytes to inBuffer, unless EOF (">")
      Once a full packet is received
        Test for packet type using hijacked byte [0].
        "P" = PartInfo
        "T" = Telemetry
      
      I/Os 
      ==== 
      ESP8266 incoming on Serial2 (Tx=10,Rx=9).
    */
    file: rx_Connect
    Code:
    //============================================================================
    //= Unpack Connection Info
    //============================================================================
    // Future- Add LED to Teensy or Display on screen
    void unpackConnectInfo() {
       
      Serial.print(inBuffer[1]); Serial.println(inBuffer[2]);
    
      // If NO connection (byte 2 == 0)
      if (inBuffer[2] == 0) {
        Serial.println("No Connection");
        connectS5 = false;
        connectPaul = false;
      }
    
      // If Attempting SSID 1 (byte 2 == 1)
      else if (inBuffer[2] == 1) {
        Serial.println("Attempting to connect to first Network");
        connectS5 = true; // Set 
        connectPaul = false;
      }
    
      // If Attempting SSID 2 (byte 2 == 2)
      else if (inBuffer[2] == 2) {
        Serial.println("Attempting to connect to second Network");
        connectS5 = false;
        connectPaul = true;
      }
      // If Connection Successful (byte 2 == 3) use connect flags to show which
      else if (inBuffer[2] == 3) {
        if (connectS5) Serial.println("Successfully Connected to TOFNET");
        if (connectPaul) Serial.println("Successfully Connected to TOFNET-5G");
      }
        
    } // END OF unpackConnectInfo
    file: rx_PartInfo
    Code:
    //============================================================================
    //= Unpack PartInfo
    //============================================================================
    void unpackPartInfo() {
    
      // debug - print buffer code
      Serial.print(inBuffer[1]); Serial.println(inBuffer[2]);
    
      switch (inBuffer[2]) {
        case 0:
          for (int count = 0; count < 64; count++) {
            CarName[count] = inBuffer[(count+3)];
          }
          Serial.print("CarName : "); Serial.println(CarName);
          break;
        case 1:
          for (int count = 0; count < 64; count++) {
            CarClassName[count] = inBuffer[(count+3)];
          }
          Serial.print("CarClassName : "); Serial.println(CarClassName);
          break;
        case 2:
          for (int count = 0; count < 64; count++) {
            TrackLocation[count] = inBuffer[(count+3)];
          }
          Serial.print("TrackLocation : "); Serial.println(TrackLocation);
          break;
      }  
    }  // END OF unpackPartInfo
    Last edited by Killg0re NL; 06-10-2018 at 13:40. Reason: Adding info
    The following 2 users likes this Post: Anguiano, Y22 Rydr


  3. #3
    Superkart Pilot Killg0re NL's Avatar
    Join Date
    Sep 2017
    Location
    Netherlands
    Posts
    134
    Platform
    PS4 Pro
    1st post contains the link an code for the ESP8266
    2nd post contains the code for the Arduino

    Future plans are to put this code straight on a single ESP32
    And this can be used together with the Buttonbox LINK
    Last edited by Killg0re NL; 06-10-2018 at 13:54.
    The following 3 users likes this Post: Anguiano, VelvetTorpedo, Y22 Rydr


  4. #4
    Superkart Pilot VelvetTorpedo's Avatar
    Join Date
    Jun 2015
    Location
    Philadelphia area suburbs
    Posts
    656
    Platform
    PC
    Eagerly waiting for esp32 version
    * HTC VIVE * i5-6600k * ASUS GTX1080 8GB TURBO * Crucial 16GB DDR4-2400 * MSI Z170A M5 * Fanatec CSL Elite w/P1 wheel * Fanatec CS Pedals v3 * Fanatec CS Shifter SQ v1.5 * TH8A analog handbrake * Playseat Evolution *
    PC2Tuner is amazing * Panther Racing Division

Similar Threads

  1. Replies: 7
    Last Post: 13-10-2018, 13:39
  2. Fanatec 's new modular Hub and Porsche rim
    By BigDad in forum Off Topic Discussion
    Replies: 20
    Last Post: 12-10-2018, 00:00
  3. Buttonbox
    By Niki Lauda DK in forum Project CARS 2 on XBOX One
    Replies: 114
    Last Post: 01-05-2018, 11:07
  4. PS4 : UDP and buttonbox / keyboard
    By vince34750 in forum Project CARS 2 on Playstation 4
    Replies: 8
    Last Post: 12-10-2017, 21:31
  5. Buttonbox ps4
    By Sanderski in forum Project CARS 2 on Playstation 4
    Replies: 1
    Last Post: 02-10-2017, 12:48

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •