me-no-dev / ESPAsyncUDP

Async UDP library for ESP8266
173 stars 56 forks source link

[HELP] Regular Udp-Code to AsyncUDP #43

Closed luxorbis closed 4 years ago

luxorbis commented 4 years ago

This may be a rather trivial question, but I don't know/understand what i have to change in my currently working code to get it to work with this library. The reason I'm considering this is because the current version is too slow(~23 packets per second). The data I'm receiving is from a game in form of structs, so the issue can't be there. After receiving I plan on sending the data over to another µC.

//#include <ESPAsyncUDP.h>

#include <ESP8266WiFi.h>   //connection to wifi
#include <WiFiUdp.h>       //UDP data transfer
#pragma pack(1)            //enable structure packing

#ifndef STASSI
#define STASSID "***********"
#define STAPSK  "*********"
#endif

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
const unsigned int localPort = 8687;

WiFiUDP Udp;
//=======Data Structure to Send======================
struct SendDataHighFrequency{
  char gear;
  word speedkmh;
  word engineRPM;
  byte drs;
  byte revLightsPercent;
}fastData;
//=========Data Structure toSend======================
//============Header============================
struct PacketHeader{
  uint16    m_packetFormat;         // 2018
  uint8     m_packetVersion;        // Version of this packet type, all start from 1
  uint8     m_packetId;             // Identifier for the packet type, see below
  uint64    m_sessionUID;           // Unique identifier for the session
  float     m_sessionTime;          // Session timestamp
  uint      m_frameIdentifier;      // Identifier for the frame the data was retrieved on
  uint8     m_playerCarIndex;       // Index of player's car in the array
};
//============Header===========================
//============Car Telemetry=======================
struct CarTelemetryData{ //Size: 1085bytes
  uint16    m_speed;                      // Speed of car in kilometres per hour
  uint8     m_throttle;                   // Amount of throttle applied (0 to 100)
  int8      m_steer;                      // Steering (-100 (full lock left) to 100 (full lock right))
  uint8     m_brake;                      // Amount of brake applied (0 to 100)
  uint8     m_clutch;                     // Amount of clutch applied (0 to 100)
  int8      m_gear;                       // Gear selected (1-8, N=0, R=-1)
  uint16    m_engineRPM;                  // Engine RPM
  uint8     m_drs;                        // 0 = off, 1 = on
  uint8     m_revLightsPercent;           // Rev lights indicator (percentage)
  uint16    m_brakesTemperature[4];       // Brakes temperature (celsius)
  uint16    m_tyresSurfaceTemperature[4]; // Tyres surface temperature (celsius)
  uint16    m_tyresInnerTemperature[4];   // Tyres inner temperature (celsius)
  uint16    m_engineTemperature;          // Engine temperature (celsius)
  float     m_tyresPressure[4];           // Tyres pressure (PSI)
};
struct PacketCarTelemetryData {
  PacketHeader        m_header;               // Header
  CarTelemetryData    m_carTelemetryData[20];
  uint32              m_buttonStatus;         // Bit flags specifying which buttons are being pressed currently - see appendices
};
//============Car Telemetry===============

void setup() {
  Serial.begin(250000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());
  Serial.printf("UDP server on port %d\n", localPort);
  Udp.begin(localPort);
}

void loop() {
int packetSize = Udp.parsePacket(); //Doesn't always trigger, around 40ms recovery Time
  if (packetSize) {   
      udpReceive(packetSize);
  }
}
void udpReceive(int packetSize) {
    int n = Udp.read(packetBuffer, packetSize);     // read the packet into packetBuffer
    byte packetId = (byte) ((PacketHeader*) &packetBuffer)->m_packetId;
    if (packetId == 6) {                        //rate specified in game
        byte playerIndex = (byte) ((PacketHeader*) &packetBuffer)->m_playerCarIndex;
        fastData.gear = (char) (((PacketCarTelemetryData*) &packetBuffer)->m_carTelemetryData[playerIndex]).m_gear;
        fastData.speedkmh = (word) (((PacketCarTelemetryData*) &packetBuffer)->m_carTelemetryData[playerIndex]).m_speed;
        fastData.engineRPM = (word) (((PacketCarTelemetryData*) &packetBuffer)->m_carTelemetryData[playerIndex]).m_engineRPM;
        fastData.drs = (byte) (((PacketCarTelemetryData*) &packetBuffer)->m_carTelemetryData[playerIndex]).m_drs;
        fastData.revLightsPercent = (byte) (((PacketCarTelemetryData*) &packetBuffer)->m_carTelemetryData[playerIndex]).m_revLightsPercent;                                  
      }   
}

Your help is much appreciated!!

luxorbis commented 4 years ago

Little Update: Now I can receive data from the app called "Packet Sender", but somehow the udp.onPacket()function doesn't trigger when I enable UDP ingame. UPDATE: everything works so far, I can retrieve data, but somehow the "packet per second"-rate did not improve over the regular WiFiUdp.h library(still around 23 Pps). Maybe this is due to the game bursting out all the packets at a time, or because of the packet-length (largest packet: 1341bytes).

stale[bot] commented 4 years ago

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.