Problem sending UDP message

Hello,

This is my first time with wireless networking. I want to send a UDP message to my receiver any time my esp32 wakes up. My problem is that, i have checked that all the programming statements are working properly but no message is being send.

I woud like to know why isn't working and how to solve this problem. Any orientation is welcomed. Thank you for your time and assistance.

This is my code:

#include <WiFi.h>
#include <WiFiUdp.h>
#define BUTTON_PIN_BITMASK 0x200002000

const char* ssid = "*****";
const char* password = "*****";

/*const char* ssid = "*****";
const char* password = "*****";*/

WiFiUDP udp;
const char * udpAddress = "***.***.***.255";
const int udpPort = 3333;

int MY_PIN = 13;
int MY_PIN2 = 33;
int MY_PIN3 = 15;

void setup(){
    Serial.begin(115200);

    pinMode(MY_PIN, INPUT_PULLUP);
    pinMode(MY_PIN2, INPUT_PULLUP);
    pinMode(MY_PIN3, OUTPUT);

    digitalWrite(MY_PIN3, HIGH);

    Serial.println("Booting");
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to wifi");

    Serial.println("Ready");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    udp.begin(udpPort);
    uint8_t message[50] = "hello world";
    udp.beginPacket(udpAddress, udpPort);
    udp.write(message,11);
    udp.endPacket();
    Serial.println("UDP send to: ");
    Serial.println(udp.remoteIP());
    Serial.println(udp.remotePort());
    Serial.println("          ");
    Serial.println(WiFi.localIP());

    int reading = digitalRead(MY_PIN);
    int reading2 = digitalRead(MY_PIN2);

    if(reading == 1 || reading2 == 1) {
        esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ALL_LOW);;
    }
    else {
        esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);;
    }

    Serial.println("Going to sleep now");
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH,   ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,         ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_MAX,          ESP_PD_OPTION_OFF);
    esp_deep_sleep_start();

    Serial.println("check_sleep");
}

void loop(){
  Serial.println("check_sleep2");
}

how do you know that it is not sent?
you can check the return value of endPacket

I've check the value and theorically is sending the packet. But when i use wireshark to check the network, the udp message doesn't appear and also it didn't appear in the receiver.

In wireshark appear this message: "Standard query 0xf3d6 PTR _arduino._tcp.local, "QM" question" but not the UDP message that says "hello world"

It looks like you're trying to broadcast your message. It might be worth trying to send it to an explicit IP instead, at least as a test.

I suspect there is something going on here.

udp.beginPacket(udpAddress, udpPort);

udpAddress is a const char *, but i actually think that the argument is expecting a pointer to
'IPAddress' which is a 4 byte struct, and the function is overloaded for a 4 byte array. I don't think the compiler will complain because of that.
try and define your IP address like this

const IPAddress udpAddress = IPAddress (xxx, xxx, xxx, 255);   // for broadcast it's 255 as wildbill says

because as it is you will be sending it to whatever the first 4 bytes of the char * are, which is probably not a valid ip address on your network.

I've try also and it continues doesn't working.

When i try it now appears this message on wireshark: "255.255.255.255 UDP 214 59727 → 6667 Len=172". I don't know what does it means.

i've also try this and doesn't work

Please show us the code and show the full IP address (it's no secret !) as well as the Serial monitor output please

Long, long ago: my project to send UDP (ESP8266) to PC running Processing:
GPS UDP Broadcaster - Everything ESP8266

/*
   UDP listiner for Tardis Time Server by: M. Ray Burnette 20151008
   Time Portal sends a UDP broadcast to all listeners on a random port (usually 4097)
   Compiled under Linux Mint 17.3

   Compiled under Arduino.cc IDE 1.6.8 on Linux Mint 17.3 on 20160206
   ESP8266 core: http://arduino.esp8266.com/staging/package_esp8266com_index.json
    Sketch uses 242,732 bytes (48%) of program storage space. Maximum is 434,160 bytes.
    Global variables use 41,545 bytes (50%) of dynamic memory, leaving 40,775 bytes for local variables. Maximum is 81,920 bytes.
    NOTE: After power-up with a "TardisTime" GPS over UDP transmitter functioning, allow 3 - 10 minutes to initialize
          and Serial output is started.
*/

#define DEMO true
#define DIAG false

#include <Streaming.h>                                          // \Documents\Arduino\libraries\Streaming (legacy) user-installed
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>

// forward declarations seem to be necessary in 1.6.7+ under Linux
void StartUp_OLED();
void displayOn(void);
void displayOff(void);
static void clear_display(void);
static void SendChar(unsigned char data);
// static void sendCharXY(unsigned char data, int X, int Y);
static void sendcommand(unsigned char com);
static void setXY(unsigned char row,unsigned char col);
static void sendStr(unsigned char *string);
static void sendStrXY( char *string, int X, int Y);
static void init_OLED(void);
void Draw_WIFI();
void Draw_WAVES();
void Listener();


uint8_t      hours, minutes, seconds;                           // hours, minure, seconds,
uint8_t      day, month, year;                                  // year, month, day;
unsigned int localPort = 8888;                                  // any unused port on LAN
char         packetBuffer[UDP_TX_PACKET_MAX_SIZE];              // buffer to hold incoming packet,
char         ReplyBuffer[] = "";                                // a null
IPAddress    apIP(10, 10, 10, 1);                               // Private network address: local & gateway
IPAddress    broadcastIP(255, 255, 255, 255);                   // https://en.wikipedia.org/wiki/Multicast

String Snumbers[] = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", 
                    "12", "13", "14", "14", "16", "17", "18", "19", "20", "21", "22", "23",
                    "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
                    "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
                    "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};

String Smonths[] = {"???", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

WiFiUDP           UDP;                                          // UDP protocol on STA interface, localPort


void setup()
{

  Serial.begin(115200);                                           // Initialise Serial for older PMB-648
  if(DIAG) {
   Serial << (F("2015 Ray Burnette")) << endl;
   Serial << (F("Tardis Time Listener Version 0.20151008")) << endl;
   Serial << (F("Visit my project web page on http://www.hackster.io/rayburne")) << endl << endl;
  }

  WiFi.mode(WIFI_STA);                                          // station mode
  WiFi.disconnect();
  WiFi.begin("TardisTime", "");                                 // The GPS server

  // Wire.pins(int sda, int scl), etc
  Wire.pins(0, 2);                                              // on ESP-01.
  Wire.begin();
  StartUp_OLED();                                               // Init Oled and fire up!
  Serial.println("OLED Init...");
  clear_display();                                              // done here to simplify also setXY(0,0);
  Draw_WAVES();                                                 // Graphic is optional
  sendStrXY(">>> Start-up <<<", 7, 0);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  clear_display(); 
  sendStrXY(">WiFi Connected<", 0, 0);

  while (! UDP.begin(localPort) )                               // UDP protocol connected to localPort variable
  {
    delay(250);
  }

  sendStrXY(">  UDP Started  <", 0, 0);
  char wifiIP = WiFi.localIP();
  char *P = &wifiIP;
  sendStrXY(P, 1, 0);
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Signal Strength: ");
  Serial.println(WiFi.RSSI());
  sendStrXY(">Listening: UDP<", 3, 0);

  delay(10);
  clear_display();
}


void loop()
{
  yield();                                                      // yield for RTOS
  Listener();                                                   // UPD (can you hear me now?)
}


void Listener() {
  int packetSize = UDP.parsePacket();                           // if there’s data available, read a packet
  char cTD[8];
  String Sdisplay;
  Sdisplay.reserve(8);

  if (packetSize)
  {
    IPAddress remote = UDP.remoteIP();                          // Server provided UDP listening port

    if (DIAG) {                                                 // For diags only
      Serial << endl;
      Serial << (F("Received packet of size: "));
      Serial << (packetSize);
      Serial << (F(" From remote: "));
    
      for (int k = 0; k < 4; k++)
      {
        Serial << (remote[k], DEC);
        if (k < 3)
        {
          Serial << (F("."));
        }
      }
    
      Serial << (F(", port: "));
      Serial << " UDPremotePort: " << (UDP.remotePort()) << endl;
    }

    UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);             // read the packet into packetBufffer
    UDP.beginPacket(apIP, UDP.remotePort());                    // send a reply, to the IP address and port that sent us the packet we received
    UDP.write(ReplyBuffer);                                     // should not be necessary but it seems that the ESP8266
    UDP.endPacket();                                            // is stablized by the inclusion of the send
    char *p = packetBuffer;                                     // grab a pointer to buffer

    p = strchr(p, ',') + 1;                                     // position after 1st comma
    yield();
    uint32_t  time  = atoi(p);

    hours    = time / 10000;
    minutes  = (time % 10000) / 100;
    seconds = (time % 100);

    Serial << "Signal Strength: " << WiFi.RSSI() << endl;

    Serial << (F("Time: ")) << ((hours<10)?"0":"") << hours << ":";
    Serial << ((minutes<10)?"0":"") << minutes << ":";
    Serial << ((seconds<10)?"0":"") << seconds << endl;

    Sdisplay += Snumbers[hours] + ":" + Snumbers[minutes] + ":" + Snumbers[seconds];
    Sdisplay.toCharArray(cTD, 9);
    sendStrXY(" Tardis - Time", 0, 0);
    sendStrXY("UTC Time/Date:", 2, 0);
    sendStrXY(cTD, 4, 0);

    yield();

    // Parse to integer date field like Ladyada
    p = strchr(p, ',') + 1; // A/V?
    p = strchr(p, ',') + 1; // lat
    p = strchr(p, ',') + 1; // N/S?
    p = strchr(p, ',') + 1; // lon
    p = strchr(p, ',') + 1; // E/W?
    p = strchr(p, ',') + 1; // speed
    p = strchr(p, ',') + 1; // angle
    p = strchr(p, ',') + 1; // move pass for date DDMMYY

    yield();
    
    // nmea date field looks like: 090914 (European)
    uint32_t fulldate = atoi(p);
    day               = (fulldate / 10000);
    month             = (fulldate % 10000) / 100;
    year              = (fulldate % 100);
    Serial << "Date: " << ((month<10)?"0":"") << month << "/"; 
    Serial << ((day<10)?"0":"") << day << "/" << "20"; 
    Serial << ((year<10)?"0":"") << year << endl;
    Sdisplay = "";
    Sdisplay += Smonths[month] + Snumbers[day];
    Sdisplay.toCharArray(cTD, 6);
    sendStrXY(cTD, 4, 9);

    yield();
  }                                                             // if(packetSize)
}                                                               // Listener()


Complete OLED receiver code attached but very old.
TardisTimeReceive.zip (2.2 KB)

code:

#include <WiFi.h>
#include <WiFiUdp.h>
#define BUTTON_PIN_BITMASK 0x200002000

const char* ssid = "*****";
const char* password = "*****";

/*const char* ssid = "*****";
const char* password = "*****";*/

WiFiUDP udp;
const IPAddress udpAddress = IPAddress (192, 168, 0, 255);
//const char * udpAddress = "192.168.0.37";
const int udpPort = 3333;

int MY_PIN = 13;
int MY_PIN2 = 33;
int MY_PIN3 = 15;

void setup(){
    Serial.begin(115200);

    pinMode(MY_PIN, INPUT_PULLUP);
    pinMode(MY_PIN2, INPUT_PULLUP);
    pinMode(MY_PIN3, OUTPUT);

    digitalWrite(MY_PIN3, HIGH);

    Serial.println("Booting");
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to wifi");

    Serial.println("Ready");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    udp.begin(udpPort);
    uint8_t message[50] = "hello world";
    Serial.println(udp.remoteIP());
    Serial.println(udp.remotePort());
    udp.beginPacket(udpAddress, udpPort);
    udp.write(message,11);
    int check = udp.endPacket();
    Serial.println(check);
    Serial.println("UDP send to: ");
    Serial.println(udp.remoteIP());
    Serial.println(udp.remotePort());
    Serial.println("          ");
    Serial.println(WiFi.localIP());

    int reading = digitalRead(MY_PIN);
    int reading2 = digitalRead(MY_PIN2);

    if(reading == 1 || reading2 == 1) {
        esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ALL_LOW);;
    }
    else {
        esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);;
    }

    Serial.println("Going to sleep now");
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH,   ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,         ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_MAX,          ESP_PD_OPTION_OFF);
    esp_deep_sleep_start();

    Serial.println("check_sleep");
}

void loop(){
  Serial.println("check_sleep2");
}

and the serial monitor:

Booting
........Connected to wifi
Ready
IP address: 192.168.0.42
0.0.0.0
0
1
UDP send to:
192.168.0.255
3333

192.168.0.42
Going to sleep now

But in wireshark and my receiver i can't see any packet.
Thanks for your time.

Try sending the UDP packet to the machine running Wireshark.

@lucasaenz, please read the following thread and then re-post your code properly using code tags. Actually, you should have read that BEFORE posting your first question on the forum. I wonder why you didn't????

Sorry. I've try to use code tags but they were not working as i was thinking it would be work so i decide to avoid them. Sorry for my mistake.

I try but continues not working. Thanks for your time trying to help.

the broadcast address for local network is 255.255.255.255., not 192.168.0.255

One difference I discovered between your code and the codes I use is

you are using

where I use just

#include <WiFi.h> // NO #include <WiFiUdp.h> !!!!!
IPAddress    remoteIP     (192, 168, 178, 160); // receiver-IP
unsigned int remotePort = 4210;                 // receiver port to listen on must match the portnumber the receiver is listening to

WiFiUDP Udp;

And here a demo-code which makes use of the SafeString-library which can be installed with the library-manager of the Arduino-IDE

// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// end of macros dbg and dbgi


// I wrote some basic documentation about receiving the UDP-messages with python at the end of the file
#include <WiFi.h>
#include <SafeString.h>

#define MaxMsgLength 1024
createSafeString(UDP_Msg_SS,MaxMsgLength); 
uint8_t UDP_Msg_uint8_Buffer[MaxMsgLength + 1]; // for some strange reasons on ESP32 the udp.write-function needs an uint8_t-array

#define MaxHeaderLength 32 
createSafeString(Header_SS,MaxHeaderLength);

#define MaxTimeStampLength 64 
createSafeString(TimeStamp_SS,MaxTimeStampLength);


char HeaderDelimiter = '$'; // must match the delimiter defined in the python-code 


const char *ssid     = ""; 

const char *password = "";

IPAddress    remoteIP     (192, 168, 178, 160); // receiver-IP
unsigned int remotePort = 4210;                 // receiver port to listen on must match the portnumber the receiver is listening to

WiFiUDP Udp;

const char* ntpServer = "fritz.box";
const long  gmtOffset_sec = 0;
const int   daylightOffset_sec = 7200;

#include <time.h>                   // time() ctime()
time_t now;                         // this is the epoch
tm myTimeInfo;                      // the structure tm holds time information in a more convient way


boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  }
  else return false;            // not expired
}

const byte OnBoard_LED = 2;
int BlinkTime = 500;

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) );
  }
}

unsigned long TestTimer;
unsigned long UDP_SendTimer;

int myCounter = 0;
int HeaderNr  = 0;


void PrintFileNameDateTime()
{
  Serial.print("Code running comes from file ");
  Serial.println(__FILE__);
  Serial.print(" compiled ");
  Serial.print(__DATE__);
  Serial.println(__TIME__);
}

void showTime() {
  time(&now);                       // read the current time
  localtime_r(&now, &myTimeInfo);           // update the structure tm with the current time
  Serial.print("year:");
  Serial.print(myTimeInfo.tm_year + 1900);  // years since 1900
  Serial.print("\tmonth:");
  Serial.print(myTimeInfo.tm_mon + 1);      // January = 0 (!)
  Serial.print("\tday:");
  Serial.print(myTimeInfo.tm_mday);         // day of month
  Serial.print("\thour:");
  Serial.print(myTimeInfo.tm_hour);         // hours since midnight  0-23
  Serial.print("\tmin:");
  Serial.print(myTimeInfo.tm_min);          // minutes after the hour  0-59
  Serial.print("\tsec:");
  Serial.print(myTimeInfo.tm_sec);          // seconds after the minute  0-61*
  Serial.print("\twday");
  Serial.print(myTimeInfo.tm_wday);         // days since Sunday 0-6
  if (myTimeInfo.tm_isdst == 1)             // Daylight Saving Time flag
    Serial.print("\tDST");
  else
    Serial.print("\tstandard");
    
  Serial.println();
}


void StoreTimeStampIntoSS(SafeString& p_RefToSS, tm p_myTimeInfo) {

  time(&now);                               // read the current time
  localtime_r(&now, &myTimeInfo);           // update the structure tm with the current time

  //p_RefToSS = " ";
  p_RefToSS  = myTimeInfo.tm_year + 1900;
  p_RefToSS += ".";

  // month
  if (p_myTimeInfo.tm_mon + 1 < 10) {
    p_RefToSS += "0";
  }  
  p_RefToSS += myTimeInfo.tm_mon + 1;

  p_RefToSS += ".";

  // day
  if (p_myTimeInfo.tm_mday + 1 < 10) {
    p_RefToSS += "0";
  }    
  p_RefToSS += myTimeInfo.tm_mday;

  p_RefToSS += "; ";

  // hour
  if (p_myTimeInfo.tm_hour < 10) {
    p_RefToSS += "0";
  }    
  p_RefToSS += myTimeInfo.tm_hour;
  p_RefToSS += ":";

  // minute
  if (p_myTimeInfo.tm_min < 10) {
    p_RefToSS += "0";
  }    
  p_RefToSS += myTimeInfo.tm_min;
  
  p_RefToSS += ":";

  // second
  if (p_myTimeInfo.tm_sec < 10) {
    p_RefToSS += "0";
  }    
  p_RefToSS += myTimeInfo.tm_sec;
  //p_RefToSS += ",";  
}


void connectToWifi() {
  Serial.print("Connecting to "); 
  Serial.println(ssid);

  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    BlinkHeartBeatLED(OnBoard_LED, 333);
    delay(332);
    Serial.print(".");
  }
  Serial.print("\n connected.");
  Serial.println(WiFi.localIP() );

}

void synchroniseWith_NTP_Time() {
  Serial.print("configTime uses ntpServer ");
  Serial.println(ntpServer);
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  Serial.print("synchronising time");
  
  while (myTimeInfo.tm_year + 1900 < 2000 ) {
    time(&now);                       // read the current time
    localtime_r(&now, &myTimeInfo);
    BlinkHeartBeatLED(OnBoard_LED, 100);
    delay(100);
    Serial.print(".");
  }
  Serial.print("\n time synchronsized \n");
  showTime();    
}

void setup() {
  Serial.begin(115200);
  Serial.println("\n Setup-Start \n");
  PrintFileNameDateTime();
  Serial.print("InitSensor() done \n");
  
  connectToWifi();
  synchroniseWith_NTP_Time();
  Header_SS = "Header"; 
}

void PrintMsg() {
  Serial.print("UDP_Msg_SS #");
  Serial.print(UDP_Msg_SS);
  Serial.println("#");
}

void loop() {
  BlinkHeartBeatLED(OnBoard_LED, BlinkTime);

  if (TimePeriodIsOver(UDP_SendTimer, 2000) ) {
    Serial.print("Send Message to #");
    Serial.print(remoteIP);
    Serial.print(":");
    Serial.println(remotePort);

    UDP_Msg_SS = "";

    UDP_Msg_SS = Header_SS;
    UDP_Msg_SS += HeaderDelimiter;

    StoreTimeStampIntoSS(TimeStamp_SS,myTimeInfo);
    UDP_Msg_SS += TimeStamp_SS;

    UDP_Msg_SS += ",my Testdata1,";
    UDP_Msg_SS += 123;
    UDP_Msg_SS += ",my Testdata2,";

    UDP_Msg_SS += 789;
    UDP_Msg_SS += ",";
    
    UDP_Msg_SS += myCounter++; 

    dbg("Send UDP_Msg #",UDP_Msg_SS);
    dbg("length:",UDP_Msg_SS.length());
    Udp.beginPacket(remoteIP, remotePort);
    Udp.write((const uint8_t*)UDP_Msg_SS.c_str(), UDP_Msg_SS.length() );  
    Udp.endPacket();
  }    
}

/*

This is a democode that demonstrates how to send TCP/UDP-messages with a timestamp 
The time is synchronized using a NTP-server. Most local routers like Fritz!Box can be used as the NTP-timer-server

The message has a userdefinable header which could be used for identifying the sender on the recieverside
There is a user-definable Header-delimiter that can be used to identify which characters of the
UDP-message belong to the header and which to the userdata

The code makes use of the SafeString-library. SafeStrings don't cause memory-problems like datatype "Strings"
and are easier to use than arrays of char. Example adding an integer to a SafeString-variable is as easy as
MySafeString = myInteger;

The userdata has commas between each data so you can import the textfile 
into table-calculation-software or databases as CSV-file comma separated values

the code has some additional useful functions as there are
- PrintFileNameDateTime() printing the path and filename of sourcecode file this program was generated with

- boolean TimePeriodIsOver  a non-blocking timing-function based on millis which is suitable for 
  timed execution in a regular manner (repeat every n milliseconds)

- BlinkHeartBeatLED() blinks the onboard-LED of ESP32 nodeMCU-boards. Gives visual feedback if the code is running  

The lines of code are grouped by functionality into several functions
The functions name says what the function does

I use this code for easy datalogging on a computer with a python-code that acts as the UDP-listener for 
incoming messages. Inside your ESP32-code you have to adjust the IP-adress to the receiver 
and the portnumber must be the same on sender (ESP32) and receiver-side 

In the python-code The header is used to create a file with the header as filename and extension ".txt" or if file 
is already existant to append the actual received UDP-message at the end of the file.

here is the python-code that does this. I tested it with python 3.9.2 for windows
#Start of python-code
# very simple and short upd-receiver based on python-code I found here
# https://www.studytonight.com/network-programming-in-python/working-with-udp-sockets#

import socket

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)      # For UDP

udp_host = socket.gethostname()            # Host IP
udp_port = 4210                     # specified port to connect

sock.bind((udp_host,udp_port))
print ("Waiting for client...")

HeaderDelimitChar = "$"

while True:
  data,addr = sock.recvfrom(1024)         #receive data from client
  print("data #",data,"#")
  Msg = data.decode('utf-8')
  print ("Received Message: #",Msg,"# from",addr)
  EndOfHeader = Msg.find(HeaderDelimitChar)
  HeaderBytes = Msg[0:EndOfHeader]
  FileName = HeaderBytes + ".txt"
  print("Filename #",FileName,"#")
  myFile = open(FileName, "a+")
  EndOfStr = data.find(0)
  MsgToWrite = Msg[EndOfHeader + 1 :1024] + '\r'
  myFile.write(MsgToWrite);
  myFile.close()
  print ("Data #",MsgToWrite,"#")

#End of python-code

For learning more about python just google with keyword "python" "your keyword of interest"  
 */

the pure sending is this part of the code

    Udp.beginPacket(remoteIP, remotePort);
    Udp.write((const uint8_t*)UDP_Msg_SS.c_str(), UDP_Msg_SS.length() );  
    Udp.endPacket();

test sending without going to sleep
then test sending with going to sleep after a delay of 2 seconds

best regards Stefan

Length of your message is 12 bytes, but you send only 11

Perhaps the ESP32 is going to sleep so fast the UDP packet is never sent. Try commenting out the sleep code. Or add a delay(1000) after the endPacket().

there are two things important to know about udp.write
udp.write expects unsigned byte hence the the typecasting
*(const uint8_t)**UDP_Msg_SS.c_str()
SafeString has a function length() which adapts the second parameter number of bytes to send always to the value needed.

I've try this, following your tip and StefanL38, using (192.168.0.255) and (255.255.255.255) as juraj recommend but continues doesn't appearing any message in my receiver or in wireshark. This is the code i've used for testing:

#include <WiFi.h>
#include <WiFiUdp.h>
#define BUTTON_PIN_BITMASK 0x200002000

const char* ssid = "***";
const char* password = "***";

/*const char* ssid = "***";
const char* password = "***";*/

WiFiUDP udp;
const IPAddress udpAddress = IPAddress (255, 255, 255, 255);
//const char * udpAddress = "192.168.0.255";
const int udpPort = 3333;
uint8_t message[50] = "hello world";

int MY_PIN = 13;
int MY_PIN2 = 33;
int MY_PIN3 = 15;

void setup(){
    Serial.begin(115200);

    pinMode(MY_PIN, INPUT_PULLUP);
    pinMode(MY_PIN2, INPUT_PULLUP);
    pinMode(MY_PIN3, OUTPUT);

    digitalWrite(MY_PIN3, HIGH);

    Serial.println("Booting");
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to wifi");

    Serial.println("Ready");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    udp.begin(udpPort);

}



void loop(){
    digitalWrite(MY_PIN3, HIGH);
    Serial.println(udp.remoteIP());
    Serial.println(udp.remotePort());
    udp.beginPacket(udpAddress, udpPort);
    udp.write(message,11);
    int check = udp.endPacket();
    Serial.println(check);
    Serial.println("UDP send to: ");
    Serial.println(udp.remoteIP());
    Serial.println(udp.remotePort());
    Serial.println("          ");
    Serial.println(WiFi.localIP());

    /*int reading = digitalRead(MY_PIN);
    int reading2 = digitalRead(MY_PIN2);

    if(reading == 1 || reading2 == 1) {
        esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ALL_LOW);;
    }
    else {
        esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);;
    }

    Serial.println("Going to sleep now");
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH,   ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,         ESP_PD_OPTION_OFF);
    esp_sleep_pd_config(ESP_PD_DOMAIN_MAX,          ESP_PD_OPTION_OFF);
    esp_deep_sleep_start();*/

    delay(2000);
    digitalWrite(MY_PIN3, LOW);
    Serial.println("check_sleep");
    delay(2000);
}

Now i'm going to test StefanL38 tip not using #include WiFIUdp.h but thanks for all the time all of you expend on trying to help