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/
  • ESP32 program - UDP CONTROL (via Packet Sender) Async

    /*|-----------------------------------------------------------|*/
    /*|SKETCH FOR ESP32 UDP DATAGRAMS CONTROL USING PACKET SENDER |*/
    /*|Author: MARTIN CHLEBOVEC                                   |*/
    /*|Web: http://arduino.clanweb.eu/udp-control-esp32.php       |*/
    /*|Board: ESP32 DevkitC v4 / ESP32 Devkit v1                  |*/
    /*|Buy me coffee: paypal.me/chlebovec                         |*/
    /*|Send ON or OFF string from Packet Sender for change of pin |*/
    /*|-----------------------------------------------------------|*/
     
    #include "WiFi.h"
    #include "AsyncUDP.h"
    const char* ssid = "WIFI_NAME";
    const char* pass = "WIFI_PASSWORD";
    const int led = 2; //D pin (Build-in LED for Devkit V1)
    AsyncUDP udp;
     
    void setup()
    {
      Serial.begin(115200);
      pinMode(led, OUTPUT);
      WiFi.disconnect(true);
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, pass);
     
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
     
      if (udp.listen(4444)) {
        Serial.print("UDP server na IP: ");
        Serial.print(WiFi.localIP());
        Serial.println(" port: 4444");
        udp.onPacket([](AsyncUDPPacket packet) {
          Serial.print("Type of UDP datagram: ");
          Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
          Serial.print(", Sender: ");
          Serial.print(packet.remoteIP());
          Serial.print(":");
          Serial.print(packet.remotePort());
          Serial.print(", Receiver: ");
          Serial.print(packet.localIP());
          Serial.print(":");
          Serial.print(packet.localPort());
          Serial.print(", Message length: ");
          Serial.print(packet.length()); //dlzka spravy
          Serial.print(", Payload: ");
          Serial.write(packet.data(), packet.length());
          Serial.println();
          String myString = (const char*)packet.data();
          if (myString.startsWith("ON")) {
            Serial.println("Action: Turning ON relay");
            digitalWrite(led, HIGH);
          } else if (myString.startsWith("OFF")) {
            Serial.println("Action: Turning OFF relay");
            digitalWrite(led, LOW);
          }
          packet.printf("ESP32 received %u bytes of data", packet.length()); //odpoved odosielatelovi
        });
      }
    }
     
    void loop()
    {
      delay(1000);
    }
    

    Wiring diagram for ESP32 with UDP command control

    UDP ovládanie ESP32 - schéma zapojenia