Class WiFi¶
WiFiClass Class
Members
Public Constructors |
|
---|---|
WiFiClass::WiFiClass |
Constructs a WiFiClass object and initializes the WiFi libraries and network settings |
Public Methods |
|
WiFiClass::firmwareVersion |
Get firmware version |
WiFiClass:: begin |
Start Wifi connection for OPEN networks |
WiFiClass:: config |
Configure network IP settings |
WiFiClass:: setDNS |
Set the DNS server IP address to use |
WiFiClass:: disconnect |
Disconnect from the network |
WiFiClass:: macAddress |
Get the interface MAC address |
WiFiClass:: localIP |
Get the interface IP address |
WiFiClass:: subnetMask |
Get the interface subnet mask address |
WiFiClass:: gatewayIP |
Get the gateway IP address |
WiFiClass:: SSID |
Return the current SSID associated with the network |
WiFiClass:: BSSID |
Return the current BSSID associated with the network |
WiFiClass:: RSSI |
Return the current RSSI (Received Signal Strength in dBm) associated with the network |
WiFiClass:: encryptionType |
Return the Encryption Type associated with the network |
WiFiClass:: scanNetworks |
Start scan WiFi networks available |
WiFiClass:: SSID |
Return the SSID discovered during the network scan |
WiFiClass:: encryptionType |
Return the encryption type of the networks discovered during the scanNetworks |
WiFiClass:: encryptionTypeEx |
Return the security type and encryption type of the networks discovered during the scanNetworks |
WiFiClass:: RSSI |
Return the RSSI of the networks discovered during the scanNetworks |
WiFiClass:: status |
Return Connection status |
WiFiClass:: hostByName |
Resolve the given hostname to an IP address |
WiFiClass:: apbegin |
Start AP mode |
WiFiClass:: disablePowerSave |
Disable power-saving mode |
WiFiClass::WiFiClass
WiFiClass::firmwareVersion
#include <WiFi.h>
// char ssid[] = 「yourNetwork」; // your network SSID (name)
// char pass[] = 「secretPassword」; // your network password
char ssid[] = 「SINGTEL-D45F」; // your network SSID (name)
char pass[] = 「mooxuteeth」; // your network key
int status = WL_IDLE_STATUS; // the Wifi radio’s status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(「WiFi shield not present」);
// don’t continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != 「1.1.0」) {
Serial.println(「Please upgrade the firmware」);
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print(「Attempting to connect to WPA SSID: 「);
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you’re connected now, so print out the data:
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 shield’s 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();
}
WiFiClass::begin
WiFiClass::config
WiFiClass::setDNS
WiFiClass::disconnect
WiFiClass::macAddress
WiFiClass::localIP
WiFiClass::subnetMask
#include <WiFi.h>
char ssid[] = 「SINGTEL-D45F_5G」; // the name of your network
int status = WL_IDLE_STATUS; // the Wifi radio’s status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(「WiFi shield not present」);
// don’t continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != 「1.1.0」) {
Serial.println(「Please upgrade the firmware」);
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print(「Attempting to connect to open SSID: 「);
Serial.println(ssid);
status = WiFi.begin(ssid);
// wait 10 seconds for connection:
delay(10000);
}
// you’re connected now, so print out the data:
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 shield’s 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);
// print your subnet mask:
IPAddress subnet = WiFi.subnetMask();
Serial.print(「NetMask: 「);
Serial.println(subnet);
// print your gateway address:
IPAddress gateway = WiFi.gatewayIP();
Serial.print(「Gateway: 「);
Serial.println(gateway);
}
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);
}
WiFiClass::gatewayIP
WiFiClass::SSID
WiFiClass::BSSID
WiFiClass::RSSI
WiFiClass::encryptionType
WiFiClass::scanNetworks
#include <WiFi.h>
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(「WiFi shield not present」);
// don’t continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != 「1.1.0」) {
Serial.println(「Please upgrade the firmware」);
}
// Print WiFi MAC address:
printMacAddress();
}
void loop() {
// scan for existing networks:
Serial.println(「Scanning available networks…」);
listNetworks();
delay(10000);
}
void printMacAddress() {
// the MAC address of your Wifi shield
byte mac[6];
// print your MAC address:
WiFi.macAddress(mac);
Serial.print(「MAC: 「);
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 listNetworks() {
// scan for nearby networks:
Serial.println(「* Scan Networks *」);
int numSsid = WiFi.scanNetworks();
if (numSsid == -1) {
Serial.println(「Couldn’t get a wifi connection」);
while (true);
}
// print the list of networks seen:
Serial.print(「number of available networks:」);
Serial.println(numSsid);
// print the network number and name for each network found:
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
Serial.print(thisNet);
Serial.print(「) 「);
Serial.print(WiFi.SSID(thisNet));
Serial.print(「tSignal: 「);
Serial.print(WiFi.RSSI(thisNet));
Serial.print(」 dBm」);
Serial.print(「tEncryptionRaw: 「);
printEncryptionTypeEx(WiFi.encryptionTypeEx(thisNet));
Serial.print(「tEncryption: 「);
printEncryptionType(WiFi.encryptionType(thisNet));
}
}
void printEncryptionTypeEx(uint32_t thisType) {
/* Arduino wifi api use encryption type to mapping to security type.
* This function demonstrate how to get more richful information of security type.
*/
switch (thisType) {
case SECURITY_OPEN:
Serial.print(「Open」);
break;
case SECURITY_WEP_PSK:
Serial.print(「WEP」);
break;
case SECURITY_WPA_TKIP_PSK:
Serial.print(「WPA TKIP」);
break;
case SECURITY_WPA_AES_PSK:
Serial.print(「WPA AES」);
break;
case SECURITY_WPA2_AES_PSK:
Serial.print(「WPA2 AES」);
break;
case SECURITY_WPA2_TKIP_PSK:
Serial.print(「WPA2 TKIP」);
break;
case SECURITY_WPA2_MIXED_PSK:
Serial.print(「WPA2 Mixed」);
break;
case SECURITY_WPA_WPA2_MIXED:
Serial.print(「WPA/WPA2 AES」);
break;
}
}
void printEncryptionType(int thisType) {
// read the encryption type and print out the name:
switch (thisType) {
case ENC_TYPE_WEP:
Serial.println(「WEP」);
break;
case ENC_TYPE_TKIP:
Serial.println(「WPA」);
break;
case ENC_TYPE_CCMP:
Serial.println(「WPA2」);
break;
case ENC_TYPE_NONE:
Serial.println(「None」);
break;
case ENC_TYPE_AUTO:
Serial.println(「Auto」);
break;
}
}
WiFiClass::SSID
WiFiClass::encryptionType
WiFiClass::encryptionTypeEx
WiFiClass::RSSI
WiFiClass::status
WiFiClass::hostByName
WiFiClass::apbegin
#include
char ssid[] = 「yourNetwork」; //Set the AP’s SSID
char pass[] = 「Password」; //Set the AP’s password
char channel[] = 「1」; //Set the AP’s channel
int status = WL_IDLE_STATUS; // the Wifi radio’s status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(「WiFi shield not present」);
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != 「1.1.0」) {
Serial.println(「Please upgrade the firmware」);
}
// attempt to start AP:
while (status != WL_CONNECTED) {
Serial.print(「Attempting to start AP with SSID: 「);
Serial.println(ssid);
status = WiFi.apbegin(ssid, pass, channel);
delay(10000);
}
//AP MODE already started:
Serial.println(「AP mode already started」);
Serial.println();
printWifiData();
printCurrentNet();
}
void loop() {
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();
}
void printWifiData() {
// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print(「IP Address: 「);
Serial.println(ip);
// print your subnet mask:
IPAddress subnet = WiFi.subnetMask();
Serial.print(「NetMask: 「);
Serial.println(subnet);
// print your gateway address:
IPAddress gateway = WiFi.gatewayIP();
Serial.print(「Gateway: 「);
Serial.println(gateway);
Serial.println();
}
void printCurrentNet() {
// print the SSID of the AP:
Serial.print(「SSID: 「);
Serial.println(WiFi.SSID());
// print the MAC address of AP:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print(「BSSID: 「);
Serial.print(bssid[0], HEX);
Serial.print(「:」);
Serial.print(bssid[1], HEX);
Serial.print(「:」);
Serial.print(bssid[2], HEX);
Serial.print(「:」);
Serial.print(bssid[3], HEX);
Serial.print(「:」);
Serial.print(bssid[4], HEX);
Serial.print(「:」);
Serial.println(bssid[5], HEX);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print(「Encryption Type:」);
Serial.println(encryption, HEX);
Serial.println();
}
WiFiClass::disablePowerSave