Skip to content

UDP Documentation

Introduction

User Datagram Protocol (UDP) is a simple, connectionless transport layer protocol used for sending messages (datagrams) between devices over a network. Unlike TCP, UDP does not guarantee delivery, ordering, or duplicate protection, but it is fast and lightweight, making it ideal for real-time applications.

UDP is commonly used in robotics, drones, and IoT for telemetry, control commands, and sensor data streaming.

How It Works

  • Connectionless: No handshake; sender transmits packets without establishing a connection.
  • Datagram-based: Each message is sent as an independent packet.
  • Ports: Messages are sent to a specific IP address and port number.
  • No guarantee: Packets may be lost, duplicated, or arrive out of order.

Diagram – Basic UDP Communication

[Sender Board] ---UDP---> [Receiver Board]

Use Cases

  • Telemetry from UAVs to ground stations
  • Sending sensor data in real time
  • Lightweight communication between microcontrollers
  • Real-time video or audio streaming (low latency)

Common Pitfalls / Gotchas

  • Packet loss: Applications must handle lost or out-of-order messages.
  • No congestion control: Sending too fast can flood the network.
  • Firewall/NAT issues: Ensure ports are open on all devices.
  • Message size: Large packets may be fragmented or dropped.

Tools to Debug

  • Wireshark for capturing and inspecting UDP packets
  • netcat (nc) or socat for testing UDP communication
  • Logging received messages on microcontroller serial output

References

Onboarding Tutorial (ESP32 or STM32)

Goal: Send a simple message from one microcontroller to another using UDP over Wi-Fi.

Materials

  • 2× ESP32 boards with Wi-Fi
  • Wi-Fi network (or one ESP32 as SoftAP)
  • Arduino IDE or PlatformIO

Steps

  1. Connect both boards to the same Wi-Fi network.
  2. Flash one board with the UDP transmitter code, sending a simple counter or message.
  3. Flash the second board with the UDP receiver code to print incoming messages to the serial monitor.

// Example ESP32 UDP Transmitter
#include <WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "YourSSID";
const char* password = "YourPassword";
const char* remoteIP = "192.168.1.100";
const int remotePort = 4210;

WiFiUDP udp;
int counter = 0;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { delay(500); }
}

void loop() {
  char message[32];
  sprintf(message, "Counter: %d", counter++);
  udp.beginPacket(remoteIP, remotePort);
  udp.write(message);
  udp.endPacket();
  delay(1000);
}
// Example ESP32 UDP Receiver
#include <WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "YourSSID";
const char* password = "YourPassword";
const int localPort = 4210;

WiFiUDP udp;
char incomingPacket[255];

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { delay(500); }
  udp.begin(localPort);
}

void loop() {
  int packetSize = udp.parsePacket();
  if (packetSize) {
    int len = udp.read(incomingPacket, 255);
    if (len > 0) incomingPacket[len] = 0;
    Serial.println(incomingPacket);
  }
}

Mini Challenges

  • Send multiple values (e.g., sensor readings) in a single UDP packet.
  • Handle dropped packets by adding sequence numbers.
  • Implement a simple “heartbeat” message to monitor connectivity.