Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Network Class Reference

Static class providing Wi-Fi and network control on the Pico W. More...

#include <Network.h>

+ Collaboration diagram for Network:

Static Public Member Functions

static bool initialize ()
 Start Wi-Fi with resilience, retrying connection if it fails.
 
static bool startWifiWithResilience ()
 Start Wi-Fi with resilience, retrying connection if it fails.
 
static bool checkAndReconnect ()
 Attempt to connect to Wi-Fi with retries.
 
static bool tryConnect (int attempts)
 Try to connect to Wi-Fi network.
 
static bool restart_wifi ()
 Restart the Wi-Fi interface.
 
static void wifi_deinit ()
 Deinitialize the CYW43 Wi-Fi stack.
 
static int getLinkStatus (int lastStatus)
 Get the current link status from the Wi-Fi interface.
 
static bool isConnected ()
 Check whether the device is connected to Wi-Fi.
 
static char * getIpAddress ()
 Get the IP address.
 

Static Private Attributes

static bool wifiConnected = false
 Make the Wi-Fi connection in station mode Requires WIFI_SSID and WIFI_PASSWORD to be set in the environment.
 
static bool wifiInitialized = false
 

Detailed Description

Definition at line 26 of file Network.h.

Member Function Documentation

◆ checkAndReconnect()

bool Network::checkAndReconnect ( )
static

This method will try to connect to the Wi-Fi network defined by WIFI_SSID and WIFI_PASSWORD. It will retry up to a maximum number of attempts defined by WIFI_MAX_RETRIES.

Parameters
attemptsNumber of connection attempts to make.
Returns
true if connected, false otherwise.

Definition at line 84 of file Network.cpp.

85{
86 if (isConnected() && getLinkStatus(CYW43_LINK_UP) == CYW43_LINK_UP)
87 {
88 return true; // Already connected
89 }
90
91 printf("[Network] Connection lost. Attempting reconnect...\n");
92
93 cyw43_arch_enable_sta_mode(); // do not re-init or deinit
94 return tryConnect(1); // One-shot reconnect attempt
95}
static bool tryConnect(int attempts)
Try to connect to Wi-Fi network.
Definition Network.cpp:97
static bool isConnected()
Check whether the device is connected to Wi-Fi.
Definition Network.cpp:144
static int getLinkStatus(int lastStatus)
Get the current link status from the Wi-Fi interface.
Definition Network.cpp:173

References getLinkStatus(), isConnected(), and tryConnect().

Referenced by FrameworkManager::poll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getIpAddress()

static char * Network::getIpAddress ( )
inlinestatic
Returns
true if connected and has IP

Definition at line 98 of file Network.h.

99 {
100 return ip4addr_ntoa(netif_ip4_addr(netif_list));
101 }

◆ getLinkStatus()

int Network::getLinkStatus ( int  lastStatus)
static

Get the current link status from the Wi-Fi interface.

Returns
int CYW43_LINK_UP, CYW43_LINK_DOWN, etc.
Returns
int CYW43_LINK_UP, CYW43_LINK_DOWN, etc.

Definition at line 173 of file Network.cpp.

174{
175
176 int status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
177 switch (status)
178 {
179 case CYW43_LINK_UP:
180 printf("\n[Network] Link is up\n");
181 break;
182
183 case CYW43_LINK_NOIP:
184 if (lastStatus == CYW43_LINK_NOIP)
185 {
186 printf(".");
187 }
188 else
189 {
190 printf("\n[Network] Acquiring IP address ");
191 }
192 break;
193
194 case CYW43_LINK_JOIN:
195 if (lastStatus == CYW43_LINK_JOIN)
196 {
197 printf(".");
198 }
199 else
200 {
201 printf("\n[Network] Joining network ");
202 }
203 break;
204
205 case CYW43_LINK_DOWN:
206 printf("\n[Network] Link is down\n");
207 break;
208 }
209 return status;
210}

Referenced by checkAndReconnect(), and tryConnect().

+ Here is the caller graph for this function:

◆ initialize()

bool Network::initialize ( )
static

Uses a static method to handle retries and connection status.

Returns
true if Wi-Fi started successfully, false otherwise.

Definition at line 41 of file Network.cpp.

42{
43 if (cyw43_arch_init())
44 {
45 printf("[Network] Failed to initialise Wi-Fi\n");
46 return false;
47 }
48
49 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); // LED off during startup
50 cyw43_arch_enable_sta_mode();
51 wifiInitialized = true;
52 printf("[Network] Wi-Fi initialized successfully.\n");
53 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); // LED on after startup
54 return true;
55}
static bool wifiInitialized
Definition Network.h:105

References wifiInitialized.

Referenced by FrameworkManager::onStart(), and startWifiWithResilience().

+ Here is the caller graph for this function:

◆ isConnected()

bool Network::isConnected ( )
static

Check whether the device is connected to Wi-Fi.

Returns
true if connected
Returns
true if connected

Definition at line 144 of file Network.cpp.

145{
146 return wifiConnected;
147}
static bool wifiConnected
Make the Wi-Fi connection in station mode Requires WIFI_SSID and WIFI_PASSWORD to be set in the envir...
Definition Network.h:104

References wifiConnected.

Referenced by checkAndReconnect(), and PicoModel::restoreState().

+ Here is the caller graph for this function:

◆ restart_wifi()

bool Network::restart_wifi ( )
static

This method will deinitialize and reinitialize the Wi-Fi stack.

Returns
true if Wi-Fi restarted successfully, false otherwise.

Definition at line 149 of file Network.cpp.

150{
151 printf("[Network] Forcing Wi-Fi restart...\n");
152
153 cyw43_arch_deinit();
154 wifiConnected = false;
155
157}
static bool startWifiWithResilience()
Start Wi-Fi with resilience, retrying connection if it fails.
Definition Network.cpp:63

References startWifiWithResilience(), and wifiConnected.

Referenced by FrameworkManager::poll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ startWifiWithResilience()

bool Network::startWifiWithResilience ( )
static

Uses a static method to handle retries and connection status.

Returns
true if Wi-Fi started successfully, false otherwise.

Definition at line 63 of file Network.cpp.

64{
65 if (!wifiInitialized)
66 {
67 if (!initialize())
68 {
69 return false;
70 }
71 wifiInitialized = true;
72 }
73
74 bool success = tryConnect(WIFI_MAX_RETRIES);
75
76 if (!success)
77 {
78 cyw43_arch_deinit(); // Only on total failure
79 }
80
81 return success;
82}
static bool initialize()
Start Wi-Fi with resilience, retrying connection if it fails.
Definition Network.cpp:41
#define WIFI_MAX_RETRIES
This setting defines the maximum number of retries for WiFi connection.

References initialize(), tryConnect(), WIFI_MAX_RETRIES, and wifiInitialized.

Referenced by FrameworkManager::onStart(), and restart_wifi().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tryConnect()

bool Network::tryConnect ( int  attempts)
static

This method will attempt to connect to the Wi-Fi network defined by WIFI_SSID and WIFI_PASSWORD. It will retry up to a maximum number of attempts defined by WIFI_MAX_RETRIES.

Parameters
attemptsNumber of connection attempts to make.
Returns
true if connected, false otherwise.

Definition at line 97 of file Network.cpp.

98{
99 cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); // LED during all connect attempts
100
101 uint32_t pm;
102 cyw43_wifi_get_pm(&cyw43_state, &pm);
103 cyw43_wifi_pm(&cyw43_state, CYW43_DEFAULT_PM & ~0xf); // disable power mgmt during connect
104
105 for (int i = 0; i < attempts; ++i)
106 {
107 printf("\n\n[Network] Connecting to WiFi SSID: %s (attempt %d)\n", WIFI_SSID, i + 1);
108
109 if (cyw43_arch_wifi_connect_async(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_MIXED_PSK) != 0)
110 {
111 printf("[Network] Failed to initiate connection.\n");
112 vTaskDelay(pdMS_TO_TICKS(WIFI_RETRY_TIMEOUT_MS));
113 continue;
114 }
115
116 int status = 0;
117 int waitMs = 0;
118 do
119 {
120 status = Network::getLinkStatus(status);
121 vTaskDelay(pdMS_TO_TICKS(1000));
122 waitMs += 1000;
123 } while (status != CYW43_LINK_UP && waitMs < WIFI_RETRY_TIMEOUT_MS);
124
125 if (status == CYW43_LINK_UP)
126 {
127 wifiConnected = true;
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);
130 return true;
131 }
132
133 printf("[Network] Attempt %d failed.\n", i + 1);
134 }
135
136 wifiConnected = false;
137 cyw43_wifi_pm(&cyw43_state, pm);
138 return false;
139}
#define WIFI_RETRY_TIMEOUT_MS
This setting defines the retry timeout for WiFi connection The default is 15000 ms (15 seconds)

References getLinkStatus(), WIFI_RETRY_TIMEOUT_MS, and wifiConnected.

Referenced by checkAndReconnect(), and startWifiWithResilience().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ wifi_deinit()

void Network::wifi_deinit ( )
static

Deinitialize the CYW43 Wi-Fi stack.

Definition at line 160 of file Network.cpp.

161{
162 cyw43_arch_deinit();
163}

Member Data Documentation

◆ wifiConnected

bool Network::wifiConnected = false
staticprivate

Make the Wi-Fi connection in station mode Requires WIFI_SSID and WIFI_PASSWORD to be set in the environment.

Definition at line 104 of file Network.h.

Referenced by isConnected(), restart_wifi(), and tryConnect().

◆ wifiInitialized

bool Network::wifiInitialized = false
staticprivate

Definition at line 105 of file Network.h.

Referenced by initialize(), and startWifiWithResilience().


The documentation for this class was generated from the following files: