21#include "network/Network.h"
22#include <pico/cyw43_arch.h>
33#include "pico/version.h"
43 if (cyw43_arch_init())
45 printf(
"[Network] Failed to initialise Wi-Fi\n");
49 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
50 cyw43_arch_enable_sta_mode();
52 printf(
"[Network] Wi-Fi initialized successfully.\n");
53 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
91 printf(
"[Network] Connection lost. Attempting reconnect...\n");
93 cyw43_arch_enable_sta_mode();
99 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
102 cyw43_wifi_get_pm(&cyw43_state, &pm);
103 cyw43_wifi_pm(&cyw43_state, CYW43_DEFAULT_PM & ~0xf);
105 for (
int i = 0; i < attempts; ++i)
107 printf(
"\n\n[Network] Connecting to WiFi SSID: %s (attempt %d)\n", WIFI_SSID, i + 1);
109 if (cyw43_arch_wifi_connect_async(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_MIXED_PSK) != 0)
111 printf(
"[Network] Failed to initiate connection.\n");
121 vTaskDelay(pdMS_TO_TICKS(1000));
125 if (status == CYW43_LINK_UP)
128 printf(
"[Network] Connected to Wi-Fi network at %s\n", ip4addr_ntoa(netif_ip4_addr(netif_list)));
129 cyw43_wifi_pm(&cyw43_state, pm);
133 printf(
"[Network] Attempt %d failed.\n", i + 1);
137 cyw43_wifi_pm(&cyw43_state, pm);
151 printf(
"[Network] Forcing Wi-Fi restart...\n");
176 int status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
180 printf(
"\n[Network] Link is up\n");
183 case CYW43_LINK_NOIP:
184 if (lastStatus == CYW43_LINK_NOIP)
190 printf(
"\n[Network] Acquiring IP address ");
194 case CYW43_LINK_JOIN:
195 if (lastStatus == CYW43_LINK_JOIN)
201 printf(
"\n[Network] Joining network ");
205 case CYW43_LINK_DOWN:
206 printf(
"\n[Network] Link is down\n");
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
Static class providing Wi-Fi and network control on the Pico W.
static bool initialize()
Start Wi-Fi with resilience, retrying connection if it fails.
static bool wifiConnected
Make the Wi-Fi connection in station mode Requires WIFI_SSID and WIFI_PASSWORD to be set in the envir...
static bool checkAndReconnect()
Attempt to connect to Wi-Fi with retries.
static bool wifiInitialized
static bool startWifiWithResilience()
Start Wi-Fi with resilience, retrying connection if it fails.
static bool tryConnect(int attempts)
Try to connect to Wi-Fi network.
static bool isConnected()
Check whether the device is connected to Wi-Fi.
static void wifi_deinit()
Deinitialize the CYW43 Wi-Fi stack.
static bool restart_wifi()
Restart the Wi-Fi interface.
static int getLinkStatus(int lastStatus)
Get the current link status from the Wi-Fi interface.
Delegates to user or system configuration.
#define WIFI_RETRY_TIMEOUT_MS
This setting defines the retry timeout for WiFi connection The default is 15000 ms (15 seconds)
#define WIFI_MAX_RETRIES
This setting defines the maximum number of retries for WiFi connection.