Skip to content
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

samples: net: sockets: echo: Add WiFi support #82442

Closed
Closed
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
16 changes: 16 additions & 0 deletions samples/net/sockets/echo/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2024 Steve Boylan <[email protected]>
# SPDX-License-Identifier: Apache-2.0

mainmenu "Sample app Configuration"

if WIFI
config NET_SAMPLE_WIFI_SSID
string "WIFI SSID - Network name"
default "my_network"

config NET_SAMPLE_WIFI_PSK
string "WIFI PSK - Network password key"
default "secret_passwd"
endif

source "Kconfig.zephyr"
13 changes: 13 additions & 0 deletions samples/net/sockets/echo/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,16 @@ the IP address will be printed to the console upon running this echo
application.

Proceed to test as above.

Running on Raspberry Pi Pico W
==============================

Edit boards/rpi_pico_rp2040_w.conf to add the Access Point SSID and password
at the end:

.. code-block:: cfg

CONFIG_NET_SAMPLE_WIFI_SSID="MyWifi"
CONFIG_NET_SAMPLE_WIFI_PSK="SuperSecret"

Build and test as above.
49 changes: 49 additions & 0 deletions samples/net/sockets/echo/boards/rpi_pico_rp2040_w.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
CONFIG_EARLY_CONSOLE=y

CONFIG_MAIN_STACK_SIZE=5200
CONFIG_SHELL_STACK_SIZE=5200
CONFIG_NET_TX_STACK_SIZE=2048
CONFIG_NET_RX_STACK_SIZE=2048
CONFIG_HEAP_MEM_POOL_SIZE=16384
CONFIG_SHELL_STACK_SIZE=2560

CONFIG_WIFI=y
CONFIG_WIFI_LOG_LEVEL_ERR=y
CONFIG_NET_L2_WIFI_SHELL=y
# printing of scan results puts pressure on queues in new locking
# design in net_mgmt. So, use a higher timeout for a crowded
# environment.
CONFIG_NET_MGMT_EVENT_QUEUE_TIMEOUT=5000
CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=16

CONFIG_NETWORKING=y
CONFIG_NET_L2_ETHERNET=y

CONFIG_NET_IPV6=n
CONFIG_NET_IPV4=y
CONFIG_NET_DHCPV4=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR=""
CONFIG_NET_CONFIG_PEER_IPV4_ADDR=""

CONFIG_NET_LOG=y
CONFIG_INIT_STACKS=y

CONFIG_NET_SHELL=y

CONFIG_NET_STATISTICS=y
CONFIG_NET_STATISTICS_PERIODIC_OUTPUT=n

CONFIG_COMPILER_SAVE_TEMPS=y

CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y
CONFIG_LOG_BACKEND_SHOW_COLOR=n

CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=y
CONFIG_NET_CONN_LOG_LEVEL_DBG=y
CONFIG_NET_CONTEXT_LOG_LEVEL_DBG=y

CONFIG_NET_TCP_LOG_LEVEL_DBG=y

CONFIG_NET_SAMPLE_WIFI_SSID="<YOUR SSID HERE>"
CONFIG_NET_SAMPLE_WIFI_PSK="<YOUR WIFI PASSWORD HERE>"
36 changes: 36 additions & 0 deletions samples/net/sockets/echo/src/socket_echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@

#include <zephyr/net/socket.h>
#include <zephyr/kernel.h>
#include <zephyr/net/net_mgmt.h>

#ifdef CONFIG_NET_L2_WIFI_MGMT
#include <zephyr/net/wifi_mgmt.h>
#endif /* CONFIG_NET_L2_WIFI_MGMT */

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(socket_echo);

#endif

Expand All @@ -36,6 +44,34 @@ int main(void)
};
static int counter;

#if defined(CONFIG_WIFI)
int nr_tries = 10;
struct net_if *iface = net_if_get_default();
static struct wifi_connect_req_params cnx_params = {
.ssid = CONFIG_NET_SAMPLE_WIFI_SSID,
.ssid_length = 0,
.psk = CONFIG_NET_SAMPLE_WIFI_PSK,
.psk_length = 0,
.channel = 0,
.security = WIFI_SECURITY_TYPE_PSK,
};

cnx_params.ssid_length = strlen(CONFIG_NET_SAMPLE_WIFI_SSID);
cnx_params.psk_length = strlen(CONFIG_NET_SAMPLE_WIFI_PSK);

/* Let's wait few seconds to allow wifi device be on-line */
while (nr_tries-- > 0) {
ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &cnx_params,
sizeof(struct wifi_connect_req_params));
if (ret == 0) {
break;
}

LOG_INF("Connect request failed %d. Waiting iface be up...", ret);
k_msleep(500);
}
#endif

serv = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (serv < 0) {
printf("error: socket: %d\n", errno);
Expand Down
Loading