Připojte PicoW k Access Pointu. IP adresa bude přidělena pomocí DHCP. Jméno sítě (SSID) a heslo napíšu na tabuli.
Program
setupWifi.h
int setup(uint32_t country, const char *ssid, const char *pass,
uint32_t auth, const char *hostname, ip_addr_t *ip,
ip_addr_t *mask, ip_addr_t *gw)
{
if (cyw43_arch_init_with_country(country))
{
return 1;
}
cyw43_arch_enable_sta_mode();
if (hostname != NULL)
{
netif_set_hostname(netif_default, hostname);
}
if (cyw43_arch_wifi_connect_async(ssid, pass, auth))
{
return 2;
}
int flashrate = 1000;
int status = CYW43_LINK_UP + 1;
while (status >= 0 && status != CYW43_LINK_UP)
{
int new_status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
if (new_status != status)
{
status = new_status;
flashrate = flashrate / (status + 1);
printf("connect status: %d %d\n", status, flashrate);
}
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
sleep_ms(flashrate);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
sleep_ms(flashrate);
}
if (status < 0)
{
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
}
else
{
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
if (ip != NULL)
{
netif_set_ipaddr(netif_default, ip);
}
if (mask != NULL)
{
netif_set_netmask(netif_default, mask);
}
if (gw != NULL)
{
netif_set_gw(netif_default, gw);
}
printf("IP: %s\n", ip4addr_ntoa(netif_ip_addr4(netif_default)));
printf("Mask: %s\n", ip4addr_ntoa(netif_ip_netmask4(netif_default)));
printf("Gateway: %s\n", ip4addr_ntoa(netif_ip_gw4(netif_default)));
printf("Host Name: %s\n", netif_get_hostname(netif_default));
}
return status;
}
main.c
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "setupWifi.h"
int connect()
{
char ssid[] = "ssid";
char pass[] = "password";
uint32_t country = CYW43_COUNTRY_CZECH_REPUBLIC;
uint32_t auth = CYW43_AUTH_WPA2_MIXED_PSK;
return setup(country, ssid, pass, auth, "MyPicoW", NULL, NULL, NULL);
}
int main()
{
stdio_init_all();
sleep_ms(10000); // pro připojení minicomu
connect();
while( true ){
sleep_ms(500);
}
return 0;
}
lwipopts.h
#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H
#define _LWIPOPTS_EXAMPLE_COMMONH_H
// Common settings used in most of the pico_w examples
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details)
// allow override in some examples
#ifndef NO_SYS
#define NO_SYS 1
#endif
// allow override in some examples
#ifndef LWIP_SOCKET
#define LWIP_SOCKET 0
#endif
#if PICO_CYW43_ARCH_POLL
#define MEM_LIBC_MALLOC 1
#else
// MEM_LIBC_MALLOC is incompatible with non polling versions
#define MEM_LIBC_MALLOC 0
#endif
#define MEM_ALIGNMENT 4
#define MEM_SIZE 4000
#define MEMP_NUM_TCP_SEG 32
#define MEMP_NUM_ARP_QUEUE 10
#define PBUF_POOL_SIZE 24
#define LWIP_ARP 1
#define LWIP_ETHERNET 1
#define LWIP_ICMP 1
#define LWIP_RAW 1
#define TCP_WND (8 * TCP_MSS)
#define TCP_MSS 1460
#define TCP_SND_BUF (8 * TCP_MSS)
#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS))
#define LWIP_NETIF_STATUS_CALLBACK 1
#define LWIP_NETIF_LINK_CALLBACK 1
#define LWIP_NETIF_HOSTNAME 1
#define LWIP_NETCONN 0
#define MEM_STATS 0
#define SYS_STATS 0
#define MEMP_STATS 0
#define LINK_STATS 0
// #define ETH_PAD_SIZE 2
#define LWIP_CHKSUM_ALGORITHM 3
#define LWIP_DHCP 1
#define LWIP_IPV4 1
#define LWIP_TCP 1
#define LWIP_UDP 1
#define LWIP_DNS 1
#define LWIP_TCP_KEEPALIVE 1
#define LWIP_NETIF_TX_SINGLE_PBUF 1
#define DHCP_DOES_ARP_CHECK 0
#define LWIP_DHCP_DOES_ACD_CHECK 0
#ifndef NDEBUG
#define LWIP_DEBUG 1
#define LWIP_STATS 1
#define LWIP_STATS_DISPLAY 1
#endif
#define ETHARP_DEBUG LWIP_DBG_OFF
#define NETIF_DEBUG LWIP_DBG_OFF
#define PBUF_DEBUG LWIP_DBG_OFF
#define API_LIB_DEBUG LWIP_DBG_OFF
#define API_MSG_DEBUG LWIP_DBG_OFF
#define SOCKETS_DEBUG LWIP_DBG_OFF
#define ICMP_DEBUG LWIP_DBG_OFF
#define INET_DEBUG LWIP_DBG_OFF
#define IP_DEBUG LWIP_DBG_OFF
#define IP_REASS_DEBUG LWIP_DBG_OFF
#define RAW_DEBUG LWIP_DBG_OFF
#define MEM_DEBUG LWIP_DBG_OFF
#define MEMP_DEBUG LWIP_DBG_OFF
#define SYS_DEBUG LWIP_DBG_OFF
#define TCP_DEBUG LWIP_DBG_OFF
#define TCP_INPUT_DEBUG LWIP_DBG_OFF
#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF
#define TCP_RTO_DEBUG LWIP_DBG_OFF
#define TCP_CWND_DEBUG LWIP_DBG_OFF
#define TCP_WND_DEBUG LWIP_DBG_OFF
#define TCP_FR_DEBUG LWIP_DBG_OFF
#define TCP_QLEN_DEBUG LWIP_DBG_OFF
#define TCP_RST_DEBUG LWIP_DBG_OFF
#define UDP_DEBUG LWIP_DBG_OFF
#define TCPIP_DEBUG LWIP_DBG_OFF
#define PPP_DEBUG LWIP_DBG_OFF
#define SLIP_DEBUG LWIP_DBG_OFF
#define DHCP_DEBUG LWIP_DBG_OFF
#endif /* __LWIPOPTS_H__ */
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
set(PICO_BOARD pico_w)
set (CMAKE_C_STANDARD 11)
set (CMAKE_CXX_STANDARD 17)
include(pico_sdk_import.cmake)
project(PicoW C CXX ASM)
pico_sdk_init()
add_executable(main
main.c
)
target_include_directories(main PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries( main
pico_stdlib
pico_cyw43_arch_lwip_threadsafe_background
)
pico_enable_stdio_usb(main 1) (1)
pico_enable_stdio_uart(main 0) (2)
pico_add_extra_outputs(main)
| 1 | Zapíná sériovou linku přes USB, na kterou jsou potom vypisována hlášení printf(). Ve skutečném odladěném projektu není potřeba. |
| 2 | Zapíná sériovou linku přes UART, na kterou jsou potom vypisována hlášení printf(). Ve skutečném odladěném projektu není potřeba. |
Sestavení projektu
Do adresáře s projektem picow_cviceni3 nakopírujte main.c, setupWifi.h, lwipopts.h, CMakeLists.txt a pico_sdk_import.cmake (najdete ho v adresáři pico-sdk/external).
Sestavení
$ cd picow_cviceni3
$ mkdir build
$ cd build
$ cmake ..
$ make -j$(nproc)
Připojte se minicomem na výstup Pica, je na to 10 sekund od zapnutí.
Testování pomocí minicomu
$ minicom -b 115200 -c on -D /dev/ttyACM0
A na výstupu můžete vidět hlášení, minicom je možno ukončit stisknutím kláves Ctrl+a x.
Výsledek

ping na MyPicoW
jirka@jirka-Precision-T3610:~/vyuka_sspvc/pmp/wifi/picow_cviceni3/build$ ping 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.
64 bytes from 192.168.120.206: icmp_seq=1 ttl=255 time=88.4 ms
64 bytes from 192.168.120.206: icmp_seq=2 ttl=255 time=110 ms
64 bytes from 192.168.120.206: icmp_seq=3 ttl=255 time=132 ms
64 bytes from 192.168.120.206: icmp_seq=4 ttl=255 time=155 ms
64 bytes from 192.168.120.206: icmp_seq=5 ttl=255 time=178 ms
64 bytes from 192.168.120.206: icmp_seq=6 ttl=255 time=200 ms
64 bytes from 192.168.120.206: icmp_seq=7 ttl=255 time=2.91 ms
64 bytes from 192.168.120.206: icmp_seq=8 ttl=255 time=1.91 ms
64 bytes from 192.168.120.206: icmp_seq=9 ttl=255 time=2.21 ms
64 bytes from 192.168.120.206: icmp_seq=10 ttl=255 time=88.0 ms
64 bytes from 192.168.120.206: icmp_seq=11 ttl=255 time=111 ms
64 bytes from 192.168.120.206: icmp_seq=12 ttl=255 time=135 ms
64 bytes from 192.168.120.206: icmp_seq=13 ttl=255 time=157 ms
64 bytes from 192.168.120.206: icmp_seq=14 ttl=255 time=180 ms
64 bytes from 192.168.120.206: icmp_seq=15 ttl=255 time=203 ms
^C
--- 192.168.120.206 ping statistics ---
15 packets transmitted, 15 received, 0% packet loss, time 14015ms
rtt min/avg/max/mdev = 1.912/116.249/203.200/66.666 ms
jirka@jirka-Precision-T3610:~/vyuka_sspvc/pmp/wifi/picow_cviceni3/build$
Je vidět, že to funguje a MyPicoW odpovídá na ping. Rozdíly v časech odpovědí jsou způsobené poměrně slabým signálem AP chorche.