Skip to content

Feature: Implementing UDP server and corresponding Python test file #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ App|Description
[picow_ntp_client](pico_w/wifi/ntp_client) | Connects to an NTP server to fetch and display the current time.
[picow_tcp_client](pico_w/wifi/tcp_client) | A simple TCP client. You can run [python_test_tcp_server.py](pico_w/wifi/python_test_tcp/python_test_tcp_server.py) for it to connect to.
[picow_tcp_server](pico_w/wifi/tcp_server) | A simple TCP server. You can use [python_test_tcp_client.py](pico_w//wifi/python_test_tcp/python_test_tcp_client.py) to connect to it.
[picow_udp_server](pico_w/wifi/udp_server) | A simple UDP server. You can use [python_test_udp_client.py](pico_w//wifi/python_test_udp/python_test_udp_client.py) to connect to it.
[picow_tls_client](pico_w/wifi/tls_client) | Demonstrates how to make a HTTPS request using TLS.
[picow_tls_verify](pico_w/wifi/tls_client) | Demonstrates how to make a HTTPS request using TLS with certificate verification.
[picow_wifi_scan](pico_w/wifi/wifi_scan) | Scans for WiFi networks and prints the results.
Expand Down
1 change: 1 addition & 0 deletions pico_w/wifi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ else()
add_subdirectory_exclude_platforms(tcp_client)
add_subdirectory_exclude_platforms(tcp_server)
add_subdirectory_exclude_platforms(udp_beacon)
add_subdirectory_exclude_platforms(udp_server)
add_subdirectory_exclude_platforms(http_client)
add_subdirectory_exclude_platforms(mqtt)

Expand Down
31 changes: 31 additions & 0 deletions pico_w/wifi/python_test_udp/python_test_udp_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import socket
import time

# === Configuration ===
PICO_IP = "PI_IP_HERE!" # Replace with your Pico's IP
PICO_PORT = 12345 # Port Pico is listening on

# === Data to send ===
data = {
"1": " Message 1 received!",
"2": " Message 2 received!",
"3": " Message 3 received!",
"4": " Message 4 received!",
"5": " Message 5 received!"
}

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

try:
for key, value in data.items():
message = f"{key}:{value}"
print(f"Sending to {PICO_IP}:{PICO_PORT} -> {message}")
sock.sendto(message.encode(), (PICO_IP, PICO_PORT))
time.sleep(0.1) # slight delay to prevent message loss

except Exception as e:
print(f"Error: {e}")

finally:
sock.close()
43 changes: 43 additions & 0 deletions pico_w/wifi/udp_server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Threadsafe

add_executable(picow_udp_server_background
picow_udp_server.c
)
target_compile_definitions(picow_udp_server_background PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
)
target_include_directories(picow_udp_server_background PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for the common lwipopts.h
)
target_link_libraries(picow_udp_server_background
pico_cyw43_arch_lwip_threadsafe_background
pico_stdlib
)

pico_add_extra_outputs(picow_udp_server_background)

pico_enable_stdio_usb(picow_udp_server_background 1)

# Manual polling

add_executable(picow_udp_server_poll
picow_udp_server.c
)
target_compile_definitions(picow_udp_server_poll PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
)
target_include_directories(picow_udp_server_poll PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts
)
target_link_libraries(picow_udp_server_poll
pico_cyw43_arch_lwip_poll
pico_stdlib
)

pico_add_extra_outputs(picow_udp_server_poll)

pico_enable_stdio_usb(picow_udp_server_poll 1)
10 changes: 10 additions & 0 deletions pico_w/wifi/udp_server/lwipopts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _LWIPOPTS_H
#define _LWIPOPTS_H

// Generally you would define your own explicit list of lwIP options
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html)
//
// This example uses a common include to avoid repetition
#include "lwipopts_examples_common.h"

#endif
Loading