|
| 1 | +/* |
| 2 | +
|
| 3 | + This example connects to a encrypted Wifi network (WPA/WPA2). |
| 4 | + Then it prints the MAC address of the Wifi shield, |
| 5 | + the IP address obtained, and other network details. |
| 6 | + Then it continuously pings given IP Address. |
| 7 | +
|
| 8 | + Circuit: |
| 9 | + * WiFi shield attached / MKR1000 |
| 10 | +
|
| 11 | + created 13 July 2010 |
| 12 | + by dlf (Metodo2 srl) |
| 13 | + modified 13 May 2016 |
| 14 | + by Petar Georgiev |
| 15 | + */ |
| 16 | +#include <SPI.h> |
| 17 | +#include <WiFi101.h> |
| 18 | + |
| 19 | +char ssid[] = "yourNetwork"; // your network SSID (name) |
| 20 | +char pass[] = "secretPassword"; // your network password |
| 21 | +int status = WL_IDLE_STATUS; // the Wifi radio's status |
| 22 | + |
| 23 | +String hostName = "www.google.com"; |
| 24 | +byte pingResult; |
| 25 | + |
| 26 | +void setup() { |
| 27 | + //Initialize serial and wait for port to open: |
| 28 | + Serial.begin(9600); |
| 29 | + while (!Serial) { |
| 30 | + ; // wait for serial port to connect. Needed for native USB port only |
| 31 | + } |
| 32 | + |
| 33 | + // check for the presence of the shield: |
| 34 | + if (WiFi.status() == WL_NO_SHIELD) { |
| 35 | + Serial.println("WiFi shield not present"); |
| 36 | + // don't continue: |
| 37 | + while (true); |
| 38 | + } |
| 39 | + |
| 40 | + // attempt to connect to Wifi network: |
| 41 | + while ( status != WL_CONNECTED) { |
| 42 | + Serial.print("Attempting to connect to WPA SSID: "); |
| 43 | + Serial.println(ssid); |
| 44 | + // Connect to WPA/WPA2 network: |
| 45 | + status = WiFi.begin(ssid, pass); |
| 46 | + |
| 47 | + // wait 5 seconds for connection: |
| 48 | + delay(5000); |
| 49 | + } |
| 50 | + |
| 51 | + // you're connected now, so print out the data: |
| 52 | + Serial.print("You're connected to the network"); |
| 53 | + printCurrentNet(); |
| 54 | + printWifiData(); |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +void loop() { |
| 59 | + |
| 60 | + Serial.print("Pinging "); |
| 61 | + Serial.print(hostName); |
| 62 | + Serial.print(": "); |
| 63 | + |
| 64 | + pingResult = WiFi.ping(hostName); |
| 65 | + |
| 66 | + if (pingResult == WL_PING_SUCCESS){ |
| 67 | + Serial.println("SUCCESS"); |
| 68 | + } else { |
| 69 | + switch (pingResult){ |
| 70 | + case WL_PING_DEST_UNREACHABLE: { Serial.println("Destination host unreachable"); }; break; |
| 71 | + case WL_PING_TIMEOUT: { Serial.println("Request Timed Out"); }; break; |
| 72 | + case WL_PING_UNKNOWN_HOST: { Serial.println("Unable to resolve hostname to IP Address"); }; break; |
| 73 | + case WL_PING_ERROR: { Serial.println("Internal error"); }; break; |
| 74 | + }; |
| 75 | + }; |
| 76 | + |
| 77 | + delay(3000); |
| 78 | +} |
| 79 | + |
| 80 | +void printWifiData() { |
| 81 | + // print your WiFi shield's IP address: |
| 82 | + IPAddress ip = WiFi.localIP(); |
| 83 | + Serial.print("IP Address: "); |
| 84 | + Serial.println(ip); |
| 85 | + Serial.println(ip); |
| 86 | + |
| 87 | + // print your MAC address: |
| 88 | + byte mac[6]; |
| 89 | + WiFi.macAddress(mac); |
| 90 | + Serial.print("MAC address: "); |
| 91 | + Serial.print(mac[5], HEX); |
| 92 | + Serial.print(":"); |
| 93 | + Serial.print(mac[4], HEX); |
| 94 | + Serial.print(":"); |
| 95 | + Serial.print(mac[3], HEX); |
| 96 | + Serial.print(":"); |
| 97 | + Serial.print(mac[2], HEX); |
| 98 | + Serial.print(":"); |
| 99 | + Serial.print(mac[1], HEX); |
| 100 | + Serial.print(":"); |
| 101 | + Serial.println(mac[0], HEX); |
| 102 | + Serial.println(); |
| 103 | +} |
| 104 | + |
| 105 | +void printCurrentNet() { |
| 106 | + // print the SSID of the network you're attached to: |
| 107 | + Serial.print("SSID: "); |
| 108 | + Serial.println(WiFi.SSID()); |
| 109 | + |
| 110 | + // print the MAC address of the router you're attached to: |
| 111 | + byte bssid[6]; |
| 112 | + WiFi.BSSID(bssid); |
| 113 | + Serial.print("BSSID: "); |
| 114 | + Serial.print(bssid[5], HEX); |
| 115 | + Serial.print(":"); |
| 116 | + Serial.print(bssid[4], HEX); |
| 117 | + Serial.print(":"); |
| 118 | + Serial.print(bssid[3], HEX); |
| 119 | + Serial.print(":"); |
| 120 | + Serial.print(bssid[2], HEX); |
| 121 | + Serial.print(":"); |
| 122 | + Serial.print(bssid[1], HEX); |
| 123 | + Serial.print(":"); |
| 124 | + Serial.println(bssid[0], HEX); |
| 125 | + |
| 126 | + // print the received signal strength: |
| 127 | + long rssi = WiFi.RSSI(); |
| 128 | + Serial.print("signal strength (RSSI):"); |
| 129 | + Serial.println(rssi); |
| 130 | + |
| 131 | + // print the encryption type: |
| 132 | + byte encryption = WiFi.encryptionType(); |
| 133 | + Serial.print("Encryption Type:"); |
| 134 | + Serial.println(encryption, HEX); |
| 135 | + Serial.println(); |
| 136 | +} |
0 commit comments