Skip to content

Commit f4fea69

Browse files
authored
Merge pull request #815 from JAndrassy/wifi_scan_getters_fix
WiFi - add missing scan result getter implementations (BSSID, channel) and the ScanNetworksAdvanced example
2 parents e70c085 + b922e6b commit f4fea69

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
This example prints the board's MAC address, and
3+
scans for available WiFi networks using the NINA module.
4+
Every ten seconds, it scans again. It doesn't actually
5+
connect to any network, so no encryption scheme is specified.
6+
BSSID and WiFi channel are printed
7+
8+
This example is based on ScanNetworks
9+
10+
created 1 Mar 2017
11+
by Arturo Guadalupi
12+
*/
13+
14+
15+
16+
#include <WiFi.h>
17+
18+
void setup() {
19+
//Initialize serial and wait for port to open:
20+
Serial.begin(9600);
21+
while (!Serial) {
22+
; // wait for serial port to connect. Needed for native USB port only
23+
}
24+
25+
// check for the WiFi module:
26+
if (WiFi.status() == WL_NO_MODULE) {
27+
Serial.println("Communication with WiFi module failed!");
28+
// don't continue
29+
while (true);
30+
}
31+
32+
// scan for existing networks:
33+
Serial.println();
34+
Serial.println("Scanning available networks...");
35+
listNetworks();
36+
37+
// print your MAC address:
38+
byte mac[6];
39+
WiFi.macAddress(mac);
40+
Serial.print("MAC: ");
41+
printMacAddress(mac);
42+
}
43+
44+
void loop() {
45+
delay(10000);
46+
// scan for existing networks:
47+
Serial.println("Scanning available networks...");
48+
listNetworks();
49+
}
50+
51+
void listNetworks() {
52+
// scan for nearby networks:
53+
Serial.println("** Scan Networks **");
54+
int numSsid = WiFi.scanNetworks();
55+
if (numSsid == -1)
56+
{
57+
Serial.println("Couldn't get a WiFi connection");
58+
while (true);
59+
}
60+
61+
// print the list of networks seen:
62+
Serial.print("number of available networks: ");
63+
Serial.println(numSsid);
64+
65+
// print the network number and name for each network found:
66+
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
67+
Serial.print(thisNet + 1);
68+
Serial.print(") ");
69+
Serial.print("Signal: ");
70+
Serial.print(WiFi.RSSI(thisNet));
71+
Serial.print(" dBm");
72+
Serial.print("\tChannel: ");
73+
Serial.print(WiFi.channel(thisNet));
74+
byte bssid[6];
75+
Serial.print("\t\tBSSID: ");
76+
printMacAddress(WiFi.BSSID(thisNet, bssid));
77+
Serial.print("\tEncryption: ");
78+
printEncryptionType(WiFi.encryptionType(thisNet));
79+
Serial.print("\t\tSSID: ");
80+
Serial.println(WiFi.SSID(thisNet));
81+
Serial.flush();
82+
}
83+
Serial.println();
84+
}
85+
86+
void printEncryptionType(int thisType) {
87+
// read the encryption type and print out the name:
88+
switch (thisType) {
89+
case ENC_TYPE_WEP:
90+
Serial.print("WEP");
91+
break;
92+
case ENC_TYPE_WPA:
93+
Serial.print("WPA");
94+
break;
95+
case ENC_TYPE_WPA2:
96+
Serial.print("WPA2");
97+
break;
98+
case ENC_TYPE_NONE:
99+
Serial.print("None");
100+
break;
101+
case ENC_TYPE_AUTO:
102+
Serial.print("Auto");
103+
break;
104+
case ENC_TYPE_WPA3:
105+
Serial.print("WPA3");
106+
break;
107+
case ENC_TYPE_UNKNOWN:
108+
default:
109+
Serial.print("Unknown");
110+
break;
111+
}
112+
}
113+
114+
void print2Digits(byte thisByte) {
115+
if (thisByte < 0xF) {
116+
Serial.print("0");
117+
}
118+
Serial.print(thisByte, HEX);
119+
}
120+
121+
void printMacAddress(byte mac[]) {
122+
for (int i = 0; i < 6; i++) {
123+
if (i > 0) {
124+
Serial.print(":");
125+
}
126+
if (mac[i] < 16) {
127+
Serial.print("0");
128+
}
129+
Serial.print(mac[i], HEX);
130+
}
131+
Serial.println();
132+
}

libraries/WiFi/src/WiFi.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ uint8_t arduino::WiFiClass::encryptionType(uint8_t networkItem) {
196196
return sec2enum(ap_list[networkItem].get_security());
197197
}
198198

199+
uint8_t* arduino::WiFiClass::BSSID(uint8_t networkItem, uint8_t* bssid) {
200+
memcpy(bssid, ap_list[networkItem].get_bssid(), 6);
201+
return bssid;
202+
}
203+
204+
uint8_t arduino::WiFiClass::channel(uint8_t networkItem) {
205+
return ap_list[networkItem].get_channel();
206+
}
207+
199208
int32_t arduino::WiFiClass::RSSI() {
200209
return wifi_if->get_rssi();
201210
}

0 commit comments

Comments
 (0)