Cornell University ECE4760
UDP from PicoW to PIcoW

Pi Pico RP2040


UDP on RP2040 PicoW
The Picow has a WIFI module that can be used to connect to the internet, using the LWIP package running on the rp2040. This project uses the UDP protocol to send/receive data between two picoW's on the same subnet. A serial connection from one pico to a laptop is used for control and debugging. Since IP addresses are assigned via DHCP, a simple discovery scheme allows the two pico's to find each other. A transaction consists of one pico (the sender) sending an integer representing the blink time in mSec to set the on-board LED rate to the other pico (the echo). The receiving picow then echos the value back to the sender.

UDP send/recv code is slightly modified to run in Protothreads and is from :
-- Pico examples
-- lwip contrib apps:

A very simple packet logger was running on the laptop. Code from Microsoft :

The structure of the program is built around Protothreads, with a thread to send a packet, a thread to process a received packet, but note that the actual low level packet receive is performed by an ISR required by LWIP. The ISR is a short as possible and just copies the receive buffer, then signals a semaphore to run the receive thread. The receive thread decodes one integer or an IP commend sent from the from the other pico, then signals a semaphore to run the send thread, to echo the value or return an IP address. There is also a thread to blink the on-board LED based on the integer-time sent from the laptop.

Initially, both pico's boot up in passive echo state and wait for a packet. If either of them gets data, they send an echo packet to the UDP boradcast address of It is therefore received by a packet logger on the laptop, as well as the other pico. At this point only the laptop can start a UDP transaction. The serial connection can be used to switch one of the pico's to send mode. It can then send a data packet or self-identify with its IP address. Both types of data are sent to the broadcast address. A data packet will modify the blink rate of the other pico. An IP packet will cause the echo pico to record a new transmit address and then send it's address back to the initiating pico. At that point each pico knows the address of the other and both stop using the broadcast address.

Serial commands are:

In the following screen capture of the picow serial channel there are several status messages sent by LWIP, then the DHCP assigned IP address of the pico attached to the serial terminal, then commands to the pico. Refer to both the images to interpret the following list of events. Console refers to the serial terminal.

-- console: Messages from LWIP
-- console: the IP address for the serial attached pico
-- laptop logger: shows a empty broadcast packet from each pico (52,54)
-- laptop logger: green shows a 500 sent from the logger to 54 then an echo from 54
-- console: commands "set send" and "send 40"
-- logger: shows a 40 sent from 52 and the out of order echo from 54.
-- console: command "pair"
-- logger: shows the IP address from 52 sent on broadcast address.
-- logger: then stops receiving because no more broadcast packets are sent
-- console: command "send 40" is sent only to 54, which modifies its blink rate
and sends back "Blink time =40"

The Microsoft packet logger running on the laptop. Most recent packets at top.


Code, Project ZIP
Note that the code has defines for WIFI SSID and password that must be specified for your system.


Copyright Cornell University July 2, 2023