Tags: ,


When the Raspberry Pi is connected to the Wifi network, it can communicate with other devices on the network using the UDP protocol. This makes it possible to control the Raspberry Pi from another device or to manage other microcontrollers using UDP requests. It can be used to send commands and receive data from Arduino, ESP32 or ESP8266 devices connected to the same Wifi network.


  • Raspberry Pi
  • Wifi or Ethernet connection

Operating principle

The User Datagram Protocol (UDP) enables datagrams to be transmitted quickly and easily between two devices. Each device is defined by an IP address and a port number. No prior communication is required to establish a connection (as opposed to handshaking).

Wifi communication generally uses a client-server architecture. In this architecture, there is a server and one or more clients. The server manages client requests and may send them a response.


To communicate using the UDP protocol, we “ll simply create the udp server, which will be able to read and write on the localPort port. The socket library can be used to open a connection between two devices.

The socket constructor expects two parameters: the address family (in this case, Internet addresses) and the socket type (in this case, UDP).

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) ## Internet,UDP

The socket must be bound to a machine port sock.bind((hostname, localPort)). Putting a blank character in place of hostname (equivalent to binds the socket to all local interfaces.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket	#https://wiki.python.org/moin/UdpCommunication


sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  ## Internet,UDP

# function get_ip_address 
def get_ip_address():
	"""get host ip address"""
	ip_address = '';
	s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	ip_address = s.getsockname()[0]
	return ip_address

# function init 
def init():
	sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
	sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) #enable broadcasting mode
	sock.bind(('', localPort))
	print("UDP server : {}:{}".format(get_ip_address(),localPort))

# function main 
def main():
	while True:
		data, addr = sock.recvfrom(bufferSize) # get data
		print("received message: {} form {}".format(data,addr))
		sock.sendto("received OK",addr)  # write data

if __name__ == '__main__':

N.B.: The get_ip_address() function implements a method for obtaining the Raspberry Pi’s IP address by opening a socket temporarily. It is also possible to use the os library with the ifconfig command.


To test the result, we use PacketSender as a client. We configure the interface to send a UDP packet to address on port 8888. We can see the UDP server’s “received OK” response on the interface.


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?