Skip to content

Commit aa0efc0

Browse files
committed
Add the WiFiConnect test sketch
Sketch to verify connection to the WiFi network. Allow the user to enter the WiFi network name and password. A connection is then attempted to the specified WiFi network. When the connection is made the network information is displayed.
1 parent 81089c1 commit aa0efc0

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
Verify connection to the specified WiFi network
3+
*/
4+
5+
#include <SPI.h>
6+
#include <WiFi.h>
7+
8+
#define ASCII_LF 0x0a
9+
#define ASCII_CR 0x0d
10+
11+
int keyIndex = 0;
12+
char password[1024]; // WiFi network password
13+
char ssid[1024]; // WiFi network name
14+
int status = WL_IDLE_STATUS; // the WiFi radio's status
15+
int wifiBeginCalled;
16+
int wifiConnected;
17+
18+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
19+
void displayIpAddress(IPAddress ip) {
20+
21+
// Display the IP address
22+
Serial.println(ip);
23+
}
24+
25+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
26+
void displayWiFiGatewayIp() {
27+
28+
// Display the subnet mask
29+
Serial.print ("Gateway: ");
30+
IPAddress ip = WiFi.gatewayIP();
31+
displayIpAddress(ip);
32+
}
33+
34+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
35+
void displayWiFiIpAddress() {
36+
37+
// Display the IP address
38+
Serial.print ("IP Address: ");
39+
IPAddress ip = WiFi.localIP();
40+
displayIpAddress(ip);
41+
}
42+
43+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
44+
void displayWiFiMacAddress() {
45+
// Display the MAC address
46+
byte mac[6];
47+
WiFi.macAddress(mac);
48+
Serial.print("MAC address: ");
49+
Serial.print(mac[5], HEX);
50+
Serial.print(":");
51+
Serial.print(mac[4], HEX);
52+
Serial.print(":");
53+
Serial.print(mac[3], HEX);
54+
Serial.print(":");
55+
Serial.print(mac[2], HEX);
56+
Serial.print(":");
57+
Serial.print(mac[1], HEX);
58+
Serial.print(":");
59+
Serial.println(mac[0], HEX);
60+
}
61+
62+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
63+
void displayWiFiNetwork() {
64+
65+
// Display the SSID
66+
Serial.print("SSID: ");
67+
Serial.println(WiFi.SSID());
68+
69+
// Display the receive signal strength
70+
long rssi = WiFi.RSSI();
71+
Serial.print("Signal strength (RSSI):");
72+
Serial.println(rssi);
73+
}
74+
75+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
76+
void displayWiFiSubnetMask() {
77+
78+
// Display the subnet mask
79+
Serial.print ("Subnet Mask: ");
80+
IPAddress ip = WiFi.subnetMask();
81+
displayIpAddress(ip);
82+
}
83+
84+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
85+
void setup() {
86+
char data;
87+
int length;
88+
89+
// Initialize serial and wait for port to open:
90+
Serial.begin(115200);
91+
while (!Serial); // Wait for native USB serial port to connect
92+
93+
// Wait for WiFi connection
94+
wifiBeginCalled = false;
95+
wifiConnected = false;
96+
Serial.println();
97+
98+
// Read the WiFi network name (SSID)
99+
length = 0;
100+
do {
101+
Serial.println("Please enter the WiFi network name (SSID):");
102+
do {
103+
while (!Serial.available());
104+
data = Serial.read();
105+
if ((data == ASCII_LF) || (data == ASCII_CR))
106+
break;
107+
ssid[length++] = data;
108+
} while (1);
109+
ssid[length] = 0;
110+
} while (!length);
111+
Serial.printf("SSID: %s\n", ssid);
112+
Serial.println();
113+
114+
// Read the WiFi network password
115+
length = 0;
116+
do {
117+
Serial.println("Please enter the WiFi network password:");
118+
do {
119+
while (!Serial.available());
120+
data = Serial.read();
121+
if ((data == ASCII_LF) || (data == ASCII_CR))
122+
break;
123+
password[length++] = data;
124+
} while (1);
125+
password[length] = 0;
126+
} while (!length);
127+
Serial.printf("Password: %s\n", password);
128+
Serial.println();
129+
}
130+
131+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
132+
void loop() {
133+
134+
// Determine the WiFi status
135+
status = WiFi.status();
136+
switch (status) {
137+
default:
138+
Serial.printf("Unknown WiFi status: %d\n", status);
139+
delay (100);
140+
break;
141+
142+
case WL_DISCONNECTED:
143+
case WL_IDLE_STATUS:
144+
case WL_NO_SHIELD:
145+
case WL_SCAN_COMPLETED:
146+
break;
147+
148+
case WL_NO_SSID_AVAIL:
149+
Serial.println("Please set SSID and pass values!\n");
150+
while (1);
151+
152+
case WL_CONNECTED:
153+
if (!wifiConnected) {
154+
wifiConnected = true;
155+
Serial.println("WiFi Connected");
156+
157+
// Display the WiFi info
158+
displayWiFiNetwork();
159+
displayWiFiIpAddress();
160+
displayWiFiSubnetMask();
161+
displayWiFiGatewayIp();
162+
}
163+
break;
164+
165+
case WL_CONNECTION_LOST:
166+
Serial.println("WiFi connection lost");
167+
WiFi.disconnect();
168+
wifiBeginCalled = false;
169+
wifiConnected = false;
170+
break;
171+
172+
case WL_CONNECT_FAILED:
173+
wifiBeginCalled = false;
174+
wifiConnected = false;
175+
break;;
176+
}
177+
178+
// Attempt to connect to Wifi network
179+
if (!wifiBeginCalled) {
180+
wifiBeginCalled = true;
181+
WiFi.begin(ssid, password);
182+
Serial.println("Waiting for WiFi connection...");
183+
}
184+
}

0 commit comments

Comments
 (0)