/
ConnectWithWPA.ino
159 lines (138 loc) · 4.8 KB
/
ConnectWithWPA.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
/*
This example connects to an unencrypted Wifi network.
Then it prints the MAC address,
the IP address obtained, and other network details.
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 08 May 2023
by Realtek SG
Example guide:
https://www.amebaiot.com/en/amebad-arduino-connect-wifi/
*/
#include <WiFi.h>
// Set if user wants to key in ssid/pwd manually during operation
//#define MANUAL_INPUT
#ifdef MANUAL_INPUT // Initialise ssid string, pwd string, and serial_in object
// Initialise strings
String str_ssid, str_pass;
#endif
// If you are connecting to an iPhone WiFi hotspot, the default SSID uses Unicode (U+2019) Right Single Quotation Mark instead of ASCII apostrophe
// Modify the "Your Name" section in the SSID below to connect to an iPhone using a default SSID style
// char ssid[] = "Your Name\xE2\x80\x99s iPhone";
// UTF-8 encoding can also be used for SSID with emoji characters
// Emoji characters can be converted into UTF-8 at https://mothereff.in/utf-8
// char ssid[] = "\xe2\x9c\x8c\xef\xb8\x8f Ameba \xe2\x9c\x8c\xef\xb8\x8f";
char ssid[] = "Network_SSID"; // your network SSID (name)
char pass[] = "Password"; // your network password
int status = WL_IDLE_STATUS; // Indicator of Wifi status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
#ifdef MANUAL_INPUT
Serial.println("Enter your ssid");
while (Serial.available() == 0) {}
str_ssid = Serial.readString();
str_ssid.trim();
Serial.print("SSID entered: ");
Serial.println(str_ssid);
Serial.println("Enter your password");
while (Serial.available() == 0) {}
str_pass = Serial.readString();
str_pass.trim();
if (str_pass.length() != 0) { // user has entered data
while (str_pass.length() <8 ) { // to catch pwd<8 exception
Serial.println("Password cannot be less than 8 characters! Try again");
while (Serial.available() == 0) {}
str_pass = Serial.readString();
str_pass.trim();
}
Serial.print("Password entered: ");
Serial.println(str_pass);
}
#endif
Serial.print("Attempting to connect to WPA SSID: ");
#ifndef MANUAL_INPUT
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
#else
char ssid_cust[str_ssid.length() + 1];
char pass_cust[str_pass.length() + 1];
strcpy(ssid_cust, str_ssid.c_str());
strcpy(pass_cust, str_pass.c_str());
Serial.println(str_ssid.c_str());
status = WiFi.begin(ssid_cust, pass_cust);
str_ssid = str_pass = "";
#endif
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.println();
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
void loop() {
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();
}
void printWifiData() {
// print your WiFi IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[0], HEX);
Serial.print(":");
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.println(mac[5], HEX);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5], HEX);
Serial.print(":");
Serial.print(bssid[4], HEX);
Serial.print(":");
Serial.print(bssid[3], HEX);
Serial.print(":");
Serial.print(bssid[2], HEX);
Serial.print(":");
Serial.print(bssid[1], HEX);
Serial.print(":");
Serial.println(bssid[0], HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}