fbpixel
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.

Hardware

  • 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.

Code

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 0.0.0.0) binds the socket to all local interfaces.

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

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

#Parameters
localPort=8888
bufferSize=1024

#Objects
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)
	s.connect(("8.8.8.8",80))
	ip_address = s.getsockname()[0]
	s.close()
	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__':
	init()
	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.

Results

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

raspberrypi-udp-packetsender-result Setting up a UDP server on Raspberry Pi
raspberrypi-udp-server-result Setting up a UDP server on Raspberry Pi

Sources