|
| 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 | +} |
0 commit comments