Skip to content

Commit 07a1484

Browse files
Add WiFi.channel() reporting (#937)
Also return error conditions when WiFi status is inquired while not connected.
1 parent 9503620 commit 07a1484

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

docs/wifi.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ Please note that WiFi on the Pico W is a work-in-progress and there are some imp
4040

4141
* Combined STA/AP mode is not supported
4242

43-
* Certain WiFi status values (RSSI, BSSID, etc.) are not available.
44-
4543
* Multicore is supported, but only one core may run ``WiFi`` code.
4644

4745
* FreeRTOS is not yet supported due to the requirement for a very different LWIP implementation. PRs always appreciated!

libraries/WiFi/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ gatewayIP KEYWORD2
4242
SSID KEYWORD2
4343
BSSID KEYWORD2
4444
RSSI KEYWORD2
45+
channel KEYWORD2
4546
encryptionType KEYWORD2
4647
beginPacket KEYWORD2
4748
endPacket KEYWORD2

libraries/WiFi/src/WiFiClass.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,30 @@ uint8_t* WiFiClass::BSSID(uint8_t* bssid) {
314314
#ifndef CYW43_IOCTL_GET_BSSID
315315
#define CYW43_IOCTL_GET_BSSID ( (uint32_t)23 * 2 )
316316
#endif
317-
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_BSSID, WL_MAC_ADDR_LENGTH, bssid, CYW43_ITF_STA);
317+
318+
if (_wifi.connected()) {
319+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_BSSID, WL_MAC_ADDR_LENGTH, bssid, CYW43_ITF_STA);
320+
} else {
321+
memset(bssid, 0, WL_MAC_ADDR_LENGTH);
322+
}
318323
return bssid;
319324
}
320325

326+
int WiFiClass::channel() {
327+
#ifndef CYW43_IOCTL_GET_CHANNEL
328+
#define CYW43_IOCTL_GET_CHANNEL 0x3a
329+
#endif
330+
331+
int32_t channel;
332+
if (_wifi.connected()) {
333+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_CHANNEL, sizeof channel, (uint8_t *)&channel, CYW43_ITF_STA);
334+
} else {
335+
channel = -1;
336+
}
337+
return channel;
338+
}
339+
340+
321341
/*
322342
Return the current RSSI /Received Signal Strength in dBm)
323343
associated with the network
@@ -330,7 +350,11 @@ int32_t WiFiClass::RSSI() {
330350
#endif
331351

332352
int32_t rssi;
333-
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_RSSI, sizeof rssi, (uint8_t *)&rssi, CYW43_ITF_STA);
353+
if (_wifi.connected()) {
354+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_RSSI, sizeof rssi, (uint8_t *)&rssi, CYW43_ITF_STA);
355+
} else {
356+
rssi = -255;
357+
}
334358
return rssi;
335359
}
336360

libraries/WiFi/src/WiFiClass.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ class WiFiClass {
267267
*/
268268
int32_t RSSI();
269269

270+
271+
/* Return the current network channel */
272+
int channel();
273+
270274
/*
271275
Return the Encryption Type associated with the network
272276

0 commit comments

Comments
 (0)