Hallo everyone,
i'm an absolute beginner to the world of Arduino and haven't any knowledge about programming. Because of my interest in SimRacing and DIY Stuff, I came up with the Idea of making my own Steeringwheel with a Display that monitors the telemetry of the Car. For now I just want to get it to work with the telemetry send via UDP from the game F1 2021 . The Arduino I am using is the WiFi uno Rev 2
With the Example "WiFi UDP Send and Receive String" from the WiFiNINA lib, I was able to wrote my first Sketch that is able to receive UDP. It receives the Packages from the game too. But I know, that I still have to decode/decrypt the Packages. And that's the point where I am stuck. I already searched for some examples and found a few helpful things but did not found any tutorial or explanation for the decoding of incoming UDP.
This is the Sketch I wrote, to receive the UDP-Packages. I already found some things for improvement, but for the beginn it's enough:
#include <WiFiNINA.h>
#include <WiFiUDP.h>
// Daten für Verbindung mit WLAN
int status = WL_IDLE_STATUS;
#include "arduino_secrets.h" // Eingabe von SSID und Passwort in tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // Netzwerk SSID (name)
char pass[] = SECRET_PASS; // Passwort
WiFiUDP Udp; // Festlegen von UDP Objekt
unsigned int localPort = 20777; // Sendender Port/Local Port (hier Xbox F1 21)
char packetBuffer[255]; // Speicherplatz (Buffer) für erhaltene Pakete
void setup()
{
Serial.begin(9600); // Startet den Seriellen Monitor
// Mit WiFi verbinden
WiFi.begin(ssid, pass); // WiFi initialisieren
while (status != WL_CONNECTED) // While-Schleife läuft so lange bis verbunden (WL_CONNECTED)
{
Serial.print("Verbindet mit SSID: "); // Status in Seriel zeigen
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000); // Warten auf verbindung (10Sek.)
}
Serial.println("Mit WiFi verbunden"); // Gibt im Seriellen Monitor aus ob verbunden ist
printWifiStatus();
Serial.println("Verbindet mit Local UDP Port ..."); // Gibt im S.M. aus, dass verbindung zu UPD Port beginnt
Udp.begin(localPort); // Initialisiert das empfangen von UDP
Serial.println("Verbunden");
}
void printWifiStatus() // Gibt im S.M. die genutzte IP Adresse und SSID aus
{
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Adresse: ");
Serial.println(ip);
}
void loop()
{
int packetSize = Udp.parsePacket(); // Empfängt das Paket und gibt im S.M. Größe, SenderIP und Senderport aus
if (packetSize)
{
Serial.print("Erhalt eines UDP-Pakets mit der Größe ");
Serial.println(packetSize);
Serial.print("von ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
}
int len = Udp.read(packetBuffer, 255); // Überführt UDP-Paket in den Buffer und ließst es aus
if (len > 0)
{
packetBuffer[len] = 0;
}
Serial.print("Inhalt: ");
Serial.println(packetBuffer);
}
This is a parser I found on GitHub, but its in TypeScript and I don't how to use it properly for the Arduino:
https://github.com/raweceek-temeletry/f1-2021-udp
The other Parser I found (https://github.com/thealexinator2904/Arduino_F1_UDP_Parser) is written for the Arduino. But when I try to compile it, the IDE gives me various errors. I know, that the Sketch was written for the ESP32, but another search said, that I just need to replace WiFi.h with the WiFiNINA.h lib. After this, it still gives out another error.
This is the example sketch where I switch the WiFi.h with the WiFiNINA.h:
#include <Arduino.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include <F1_UDP_Parser.h>
const char *SSID = "Your SSID";
const char *WiFiPassword = "YOur PW";
void ConnectToWiFi();
//The IP address that this arduino has requested to be assigned to.
IPAddress ip();
WiFiUDP Udp;
int flag = 0;
unsigned int localPort = 8888;
F1_UDP_Parser* parser;
void setup()
{
parser = new F1_UDP_Parser();
Serial.begin(115200);
ConnectToWiFi();
Udp.begin(localPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if(packetSize)
{
char packetBuffer[packetSize];
while(Udp.available())
{
Udp.read(packetBuffer, packetSize);
}
parser->push(packetBuffer);
Serial.print("Speed: ");
Serial.println(parser->packetCarTelemetryData()->m_carTelemetryData(0).m_speed);
}
}
void ConnectToWiFi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, WiFiPassword);
Serial.print("Connecting to "); Serial.println(SSID);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(500);
if ((++i % 16) == 0)
{
Serial.println(F(" still trying to connect"));
}
}
Serial.print(F("Connected. My IP address is: "));
Serial.println(WiFi.localIP());
}
And this is the error I get:
Arduino: 1.8.19 (Mac OS X), Board: "Arduino Uno WiFi Rev2, None (ATMEGA4809)"
In file included from /Users/------------/Documents/Arduino/libraries/Arduino_F1_UDP_Parser-main/src/F1_UDP_Parser.h:21:0,
from /Users/----------/Desktop/F1_2021_parser2/F1_2021_parser2.ino:4:
/Users/---------/Documents/Arduino/libraries/Arduino_F1_UDP_Parser-main/src/PacketEventData.h:18:9: fatal error: string: No such file or directory
#include<string>
^~~~~~~~
compilation terminated.
exit status 1
So, this is the Problem I have. I know, that this is a pretty deep dive into the topic without any knowledge. I am just asking for a hint or maybe some literature about the encoding of UDP. It would make me very happy, if there's someone, who could help me with this! Thank you all!