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)

    /*|----------------------------------------------------------------------------|*/
    /*|SKETCH PRE UDP OVLADANIE ESP32 PROSTREDNICTVOM PROGRAMU PACKET SENDER       |*/
    /*|VYHOTOVIL: MARTIN CHLEBOVEC                                                 |*/
    /*|Web: https://arduino.php5.sk/udp-control-esp32.php                          |*/
    /*|Doska: ESP32 DevkitC v4 / ESP32 Devkit v1                                   |*/
    /*|----------------------------------------------------------------------------|*/
     
    #include "WiFi.h"
    #include "AsyncUDP.h"
    const char* ssid = "WIFI_MENO";
    const char* pass = "WIFI_HESLO";
    const int led = 2; //D pin (Build-in LED pre 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("Typ UDP datagramu: ");
          Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
          Serial.print(", Odosielatel: ");
          Serial.print(packet.remoteIP());
          Serial.print(":");
          Serial.print(packet.remotePort());
          Serial.print(", Prijimatel: ");
          Serial.print(packet.localIP());
          Serial.print(":");
          Serial.print(packet.localPort());
          Serial.print(", Dlzka spravy: ");
          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("ZAP")) {
            Serial.println("Akcia: Zapinam rele");
            digitalWrite(led, HIGH);
          } else if (myString.startsWith("VYP")) {
            Serial.println("Akcia: Vypinam rele");
            digitalWrite(led, LOW);
          }
          packet.printf("ESP32 prijalo %u bajty dat", packet.length()); //odpoved odosielatelovi
        });
      }
    }
     
    void loop()
    {
      delay(1000); //nutny delay, UDP sprava sa nestiha nacitat
    }
    

    Wiring diagram for ESP32 with UDP command control

    UDP ovládanie ESP32 - schéma zapojenia