-
Notifications
You must be signed in to change notification settings - Fork 1
/
WiFiTest.ino
275 lines (241 loc) · 6.52 KB
/
WiFiTest.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <WiFi.h>
#include <NetApiHelpers.h>
#include <MACAddress.h>
#include "arduino_secrets.h"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
void setup() {
Serial.begin(115200);
delay(500);
while (!Serial);
Serial.println("START");
Serial.println();
// Serial1.begin(115200);
// WiFi.init(Serial1);
// WiFi.setPersistent(false);
// WiFi.persistent(false);
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println();
Serial.println("ERROR: Communication with WiFi module failed!");
// don't continue
while (true) {
delay(1);
}
}
Serial.print("firmware version: ");
Serial.println(WiFi.firmwareVersion());
Serial.println();
Serial.print("Attempting to connect to SSID \"");
Serial.print(ssid);
Serial.println("\" with DHCP ..."); // <-------
WiFi.setHostname("arduino");
testWiFi();
IPAddress ip = WiFi.localIP();
IPAddress gw = WiFi.gatewayIP();
IPAddress mask = WiFi.subnetMask();
IPAddress dns = WiFi.dnsIP();
const char *serverName = "www.google.com";
Serial.print("Resolve IP for ");
Serial.println(serverName);
IPAddress resolvedIP;
if (WiFi.hostByName("www.google.com", resolvedIP) == 1) {
Serial.print("\t");
Serial.println(resolvedIP);
} else {
Serial.println("\t...ERROR");
}
Serial.println();
WiFi.disconnect();
// IPAddress ip(192, 168, 1, 177);
// IPAddress gw(192, 168, 1, 1);
// IPAddress dns(192, 168, 1, 1);
// IPAddress mask(255, 255, 255, 0);
Serial.print("Attempting to connect to SSID \"");
Serial.print(ssid);
Serial.print("\" with static IP "); // <-------
ip[3] = 177;
Serial.println(ip);
WiFi.config(ip, dns, gw, mask);
if (WiFi.dnsIP() == mask) {
Serial.println("ERROR: config() has wrong ordering of parameters");
while (true) {
delay(1);
}
}
testWiFi();
if (ip != WiFi.localIP()) {
Serial.println("ERROR: Static IP was not used.");
while (true) {
delay(1);
}
}
IPAddress dnsIP2(8, 8, 8, 8);
WiFi.setDNS(WiFi.dnsIP(), dnsIP2);
if (WiFi.dnsIP(1) != dnsIP2) {
Serial.print("ERROR: DNS IP setter or getter error. dnsIP(1) returned ");
Serial.println(WiFi.dnsIP(1));
Serial.println();
}
WiFi.disconnect();
Serial.println("Attempting to connect without resetting static IP configuration"); // <-------
testWiFi();
if (ip != WiFi.localIP()) {
Serial.println("ERROR: Static IP was cleared.");
Serial.println();
}
WiFi.disconnect();
Serial.print("Attempting to connect to SSID \"");
Serial.print(ssid);
Serial.print("\" with automatic gateway, DNS and mask with static IP "); // <-------
ip[3] = 178;
Serial.println(ip);
WiFi.config(ip);
testWiFi();
if (ip != WiFi.localIP()) {
Serial.println("ERROR: Static IP was not used.");
while (true) {
delay(1);
}
}
IPAddress autoIP(ip);
autoIP[3] = 1;
IPAddress defaultMask(255, 255, 255, 0);
if (WiFi.gatewayIP() == autoIP && WiFi.dnsIP() == autoIP && WiFi.subnetMask() == defaultMask) {
Serial.println("Automatic config values are OK");
} else {
Serial.println("ERROR: Automatic config values are wrong");
if (WiFi.gatewayIP() != autoIP) {
Serial.print("\tgateway IP Address: ");
Serial.println(WiFi.gatewayIP());
}
if (WiFi.subnetMask() != defaultMask) {
Serial.print("\tsubnet IP mask: ");
Serial.println(WiFi.subnetMask());
}
if (WiFi.dnsIP() != autoIP) {
Serial.print("\tDNS server: ");
Serial.println(WiFi.dnsIP());
}
}
Serial.println();
WiFi.disconnect();
Serial.print("Attempting to connect to SSID \"");
Serial.print(ssid);
Serial.println("\" with DHCP again..."); // <-------
WiFi.setHostname("arduino");
WiFi.config(INADDR_NONE);
testWiFi();
if (WiFi.localIP() == INADDR_NONE) {
Serial.println("ERROR: DHCP didn't run.");
} else if (WiFi.localIP() == ip) {
Serial.println("ERROR: The IP didn't change from static IP to DHCP assigned IP.");
} else {
Serial.println("Check on router the hostname and MAC address.");
}
Serial.println();
Serial.println("END");
}
void loop() {
}
void testWiFi() {
WiFi.begin(ssid, pass);
while (WiFi.status() == WL_DISCONNECTED || WiFi.status() == WL_IDLE_STATUS) {
Serial.print('.');
delay(1000);
}
Serial.println();
if (WiFi.status() != WL_CONNECTED) {
Serial.println("ERROR: STA didn't connect");
printWiFiStatus();
while (true) {
delay(1);
}
} else {
Serial.println("\t...success");
}
Serial.println();
printWiFiInfo();
Serial.println();
Serial.print("Attempt to connect to port 80 on ");
Serial.println(WiFi.gatewayIP());
WiFiClient client;
if (client.connect(WiFi.gatewayIP(), 80)) {
Serial.println("\t...success");
} else {
Serial.println("\t...ERROR");
}
client.stop();
Serial.println();
}
void printWiFiInfo() {
MACAddress mac;
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.println(mac);
if (mac[0] & 1) { // multicast bit is set
Serial.println("\t is the ordering of the MAC address bytes reversed?");
}
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
MACAddress bssid;
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.println(bssid);
if (bssid[0] & 1) { // multicast bit is set
Serial.println("\t is the ordering of the BSSID bytes reversed?");
}
Serial.print("RSSI:");
Serial.print(WiFi.RSSI());
Serial.println(" dBm");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("gateway IP Address: ");
Serial.println(WiFi.gatewayIP());
Serial.print("subnet IP mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("DNS server: ");
IPAddress dns = WiFi.dnsIP();
if (dns == INADDR_NONE) {
Serial.println("not set");
} else {
Serial.println(dns);
IPAddress dns2 = WiFi.dnsIP(1);
if (dns2 != INADDR_NONE) {
Serial.print("DNS server2: ");
Serial.println(dns2);
}
}
}
void printWiFiStatus() {
int status = WiFi.status();
const char* msg = nullptr;
switch (status) {
case WL_NO_SHIELD:
msg = "NO_SHIELD";
break;
case WL_IDLE_STATUS:
msg = "IDLE_STATUS";
break;
case WL_NO_SSID_AVAIL:
msg = "NO_SSID_AVAIL";
break;
case WL_CONNECTED:
msg = "CONNECTED";
break;
case WL_CONNECT_FAILED:
msg = "CONNECT_FAILED";
break;
case WL_CONNECTION_LOST:
msg = "CONNECTION_LOST";
break;
case WL_DISCONNECTED:
msg = "DISCONNECTED";
break;
}
Serial.print("status: ");
if (msg != nullptr) {
Serial.println(msg);
} else {
Serial.println(status);
}
}