Project is available for free download!
ESP32 UDP Command Code Packet Sender Scheme

ESP32 control via Windows application by sending datagrams

  • Software designed for easy output control at the users request
  • The interface for sending commands is any UDP client that can send a UDP message - a datagram, such as Packet Sender on Windows.
  • Packet Sender requires configuration modification for valid sending of UDP datagrams. Select the option to add a 500ms pause (slow mode).
  • Packet Sender Configuration - Add a pause for slow devices
  • Setting up the Packet Sender is simple, it requires entering the IP address to which the data is sent, including the port. (ESP32 IP address - available in Serial Line List - UART)
  • Serial Monitor List - Contains the ESP32 IP address in the LAN Packet Sender - user interface, table of received and sent datagrams
  • UDP does not guarantee delivery of messages. For this reason, ESP32 will send a message to the sender on the success of the transmission upon receiving the message (ZAP / VYP). (However, the sender may discard or not receive the message)
  • As a result, it is possible to control the output on request, whether in the form of a diode or a relay with the switching of power consumers.
  • An interesting feature is the use of the asynchronous library for ESP32, where the functionality runs in the background without the need to program the transmission elements of the system.
  • Packet Sender can be downloaded at: https://packetsender.com/
  • Program ESP32 (Arduino Core)

    /*|----------------------------------------------------------------------------|*/
    /*|SKETCH PRE UDP OVLADANIE ESP32 PROSTREDNICTVOM PROGRAMU PACKET SENDER       |*/
    /*|VYHOTOVIL: MARTIN CHLEBOVEC                                                 |*/
    /*|PODPOR PROJEKT PRE VIAC VARIANT, SUPPORT: https://paypal.me/chlebovec       |*/
    /*|Doska: ESP32 DevkitC v4 / ESP32 Devkit v1                                   |*/
    /*|----------------------------------------------------------------------------|*/
    
    #include "WiFi.h"
    #include "AsyncUDP.h"
    const char* ssid = "WiFi_meno";
    const char* pass = "heslo_WiFi";
    const int rele = 23;
    AsyncUDP udp;
    
    void setup()
    {
      Serial.begin(115200);
      pinMode(rele, OUTPUT);
      WiFi.disconnect(true);
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, pass);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      if (udp.listen(1234)) {
        Serial.print("UDP Listening on IP: ");
        Serial.println(WiFi.localIP());
        udp.onPacket([](AsyncUDPPacket packet) {
          Serial.print("UDP Packet Type: ");
          Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
          Serial.print(", From: ");
          Serial.print(packet.remoteIP());
          Serial.print(":");
          Serial.print(packet.remotePort());
          Serial.print(", To: ");
          Serial.print(packet.localIP());
          Serial.print(":");
          Serial.print(packet.localPort());
          Serial.print(", Length: ");
          Serial.print(packet.length()); //dlzka packetu
          Serial.print(", Data: ");
          Serial.write(packet.data(), packet.length());
          Serial.println();
          String myString = (const char*)packet.data();
          if (myString == "ZAP") {
            Serial.println("Zapinam rele");
            digitalWrite(rele, LOW);
          } else if (myString == "VYP") {
            Serial.println("Vypinam rele");
            digitalWrite(rele, HIGH);
          }
          packet.printf("Got %u bytes of data", packet.length());
        });
      }
    }
    
    void loop()
    {
      delay(1000);
      udp.broadcast("Anyone here?");
    }
    
    

    Wiring diagram for ESP32 with UDP command control

    UDP ovládanie ESP32 - schéma zapojenia