me myself I was only using code similar to this simple udp-send-receive-demo
#include <DebugTxtVar.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// Set WiFi credentials
#define WIFI_SSID "TheOtherESP"
#define WIFI_PASS "flashmeifyoucan"
char buttonState = 1;
int packetsize = 0;
// UDP Buffer
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
// UDP
WiFiUDP UDP;
IPAddress remote_IP(192,168,4,1);
#define UDP_PORT 4210
void PrintFileNameDateTime() {
Serial.println("Code running comes from file ");
Serial.println(__FILE__);
Serial.print(" compiled ");
Serial.print(__DATE__);
Serial.print(" ");
Serial.println(__TIME__);
}
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
const byte OnBoard_LED = 2;
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
digitalWrite(IO_Pin,!digitalRead(IO_Pin) );
}
}
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
PrintFileNameDateTime();
// Begin WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
WiFi.mode(WIFI_STA);
// Connecting to WiFi...
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
// Loop continuously while WiFi is not connected
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
// Connected to WiFi
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// Begin UDP port
UDP.begin(UDP_PORT);
Serial.print("Opening UDP port ");
Serial.println(UDP_PORT);
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED,100);
if ( TimePeriodIsOver(MyTestTimer,2000) ) {
if (buttonState == 1) {
buttonState = 0;
Serial.print("buttonState =");
Serial.println(buttonState);
}
else {
buttonState = 1;
Serial.print("buttonState =");
Serial.println(buttonState);
}
// Send Packet
UDP.beginPacket(remote_IP, UDP_PORT);
UDP.write(buttonState);
UDP.endPacket();
Serial.println("UDP packed sended");
delay(100);
}
// Receive packet
packetsize = UDP.parsePacket();
UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
if (packetsize){
Serial.print("UDP-packet #");
Serial.print(packetBuffer);
Serial.print("# from IP");
Serial.println(UDP.remoteIP() );
}
}
it is written for ESP8266 but the udp-part will be the same for ESP32
these are the commands for sending a string
Udp.beginPacket(remoteIP, remotePort);
Udp.write((const uint8_t*)UDP_Msg_SS.c_str(), UDP_Msg_SS.length() );
Udp.endPacket();
as you can see for using the udp.write-function the string has to be type-casted into uint8_t anyway. So sending a few bytes with hexadecimal values should be pretty straight forward
best regards Stefan