Demo 19: How to use UDP/IP with Arduino ESP32

1. Introduction
- I made a TCP/IP demo in Demo 8: How to use TCP/IP with Arduino ESP32 so in this tutorial I will make a demo with UDP and apply tutorial How to make IoT testing/debugging application (TCP, UDP, HTTP, MQTT) using Python for testing.
- Create a UDP server using Python and Arduino ESP32 UDP client. Client will send the data to server, server convert to upper case and respond to client.
2. Hardware
You do not need any extra hardware.
3. Software
- In order to make a UDp client in Arduino ESP32, we will use class WiFiUDP. It has some interfaces:
+ begin(port): initialize UDP protocol and transfer buffer.
+ beginPacket(udpAddress, udpPort): prepare sending data to server at IP and port.
+ write(buffer, size of buffer): send data buffer with size of it.
+ endPacket(): end of sending.
+ parsePacket(): processing incoming packet, must be called before reading the buffer.
+ read(buffer, size of buffer): read incoming data into buffer.
3.1 Python server
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import socket

# bind all IP
HOST = '0.0.0.0' 
# Listen on Port 
PORT = 44444 
#Size of receive buffer   
BUFFER_SIZE = 1024    
# Create a TCP/IP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the host and port
s.bind((HOST, PORT))
while True:
    # Receive BUFFER_SIZE bytes data
    # data is a list with 2 elements
    # first is data
    #second is client address
    data = s.recvfrom(BUFFER_SIZE)
    if data:
        #print received data
        print('Client to Server: ' , data)
        # Convert to upper case and send back to Client
        s.sendto(data[0].upper(), data[1])
# Close connection
s.close()
3.2 Arduino ESP32 UDP client
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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
#include <WiFi.h>
#include <WiFiUdp.h>

/* WiFi network name and password */
const char * ssid = "dd-wrt";
const char * pwd = "0000000000";

// IP address to send UDP data to.
// it can be ip address of the server or 
// a network broadcast address
// here is broadcast address
const char * udpAddress = "192.168.1.100";
const int udpPort = 44444;

//create UDP instance
WiFiUDP udp;

void setup(){
  Serial.begin(115200);
  
  //Connect to the WiFi network
   WiFi.begin(ssid, pwd);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop(){
  //data will be sent to server
  uint8_t buffer[50] = "hello world";
  //This initializes udp and transfer buffer
  udp.beginPacket(udpAddress, udpPort);
  udp.write(buffer, 11);
  udp.endPacket();
  memset(buffer, 0, 50);
  //processing incoming packet, must be called before reading the buffer
  udp.parsePacket();
  //receive response from server, it will be HELLO WORLD
  if(udp.read(buffer, 50) > 0){
    Serial.print("Server to client: ");
    Serial.println((char *)buffer);
  }
  //Wait for 1 second
  delay(1000);
}
4. Result
 Figure: UDP/IP with Arduino ESP32

Post a Comment

5 Comments

Anonymous said…
Incredibly helpful! Thank you!
Khoi Ly said…
Hi, could you give me a quick explanation the difference between "Serial.println(WiFi.localIP())" and the IPaddress that you assigned "const char * udpAddress = "192.168.1.100"

If the ESP32 only needs the udpAddress "udp.beginPacket(udpAddress, udpPort)", when do we need the localIP 192.168.1.108?

Thanks
NR0X said…
Serial.println(WiFi.localIP()) is a command to send the esp32's IP address through the serial terminal, just to let the user know the device's IP when it connects to WiFi. It's just for the convenience of the user is all.
Anonymous said…
setupp is missing: udp.begin(udpPort); after addiong that it works nice.