WiFi Network

Last revision:2024/09/11

WiFiクラス

WiFiクラスは、ネットワーク設定の初期化やネットワークへの接続・切断といった、Wi-Fi独自の関数を含んでいる。

WiFi.begin()

説明

WiFiライブラリのネットワーク設定を初期化し、現在の状態を取得する。

書式

WiFi.begin(ssid);

WiFi.begin(ssid, pass);

WiFi.begin(ssid, keyIndex, key);

引数

ssidSSID(Service Set Identifier)は、接続したいWi-Fiネットワークの名称である。
keyIndexWEPで暗号化されたネットワークは4個までの異なるキーを持つことができる。これは、どのキーを利用するかを指定する。
keyWEPで暗号化されたネットワークのセキュリティコードの16進数の文字列。
passWPAで暗号化されたネットワークはセキュリティのために文字列で表されるパスワードを利用する。

戻り値

WL_CONNECTEDネットワークに接続されたとき。
WL_IDLE_STATUSネットワークに接続されたなかったが、電源がオンのとき。

コード例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <WiFi.h>

//SSID of your network
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";

void setup()
{
 WiFi.begin(ssid, pass);
}

void loop () {}
WiFi.disconnect()

説明

現在のネットワークからWiFiシールドを切断する。

書式

WiFi.disconnect();

引数

なし。

戻り値

なし。

WiFi.config()

説明

WiFi.config()を使い、WiFiシールドの静的IPアドレスの設定や、DNSやゲートウェイ、サブネットを変更する。

WiFi.begin()がDHCPを利用するよう自動的にWiFiシールドを設定するのとは異なり、WiFi.config()は、シールドのネットワークアドレスを手動設定する。

WiFi.begin()の実行前にWiFi.config()を呼び出せば、begin()で、WiFiシールドがconfig()で指定したネットワークアドレスを設定するよう強制する。

WiFi.begin()の後にWiFi.config()を呼び出せるが、その場合は、begin()でデフォルトのDHCPモードでシールドを初期化する。config()メソッドが呼ばれれば、ネットワークアドレスを指定した値に変更する。

書式

WiFi.config(ip);

WiFi.config(ip, dns);

WiFi.config(ip, dns, gateway);

WiFi.config(ip, dns, gateway, subnet);

引数

ipデバイスのIPアドレス(4バイトの配列)
dnsDNSサーバのアドレス
gatewayネットワークゲートウェイのIPアドエス(4バイトの配列)。オプション。省略時は、デバイスのIPアドレスの最後の1バイトを1にしたもの。
subnetネットワークのサブネットマスク(4バイトの配列)。デフォルトは255.255.255.0.

戻り値

なし。

コード例

 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
This example shows how to set the static IP address, 192.168.0.177, of the LAN network to the WiFi shield:

#include <SPI.h>
#include <WiFi.h>

// the IP address for the shield:
IPAddress ip(192, 168, 0, 177);    

char ssid[] = "yourNetwork";    // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

void setup()
{  
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while(true);  // don't continue
  }

  WiFi.config(ip);

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // print your WiFi shield's IP address:
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop () {}
WiFi.SSID()

説明

現在のネットワークのSSIDを取得する。

書式

WiFi.SSID();

WiFi.SSID(wifiAccessPoint);

引数

ipデバイスのIPアドレス(4バイトの配列)
wifiAccessPointどのネットワークから情報を取得するか指定する。

戻り値

現在接続しているWiFiシールドのSSIDを含む文字列。

文字列には要求したネットワークの名前を含む文字列が含まれる。

コード例

 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
#include <SPI.h>
#include <WiFi.h>

//SSID of your network
char ssid[] = "yourNetwork";
int status = WL_IDLE_STATUS;     // the Wifi radio's status

void setup()
{
  // initialize serial:
  Serial.begin(9600);

  // scan for existing networks:
  Serial.println("Scanning available networks...");
  scanNetworks();

  // attempt to connect using WEP encryption:
  Serial.println("Attempting to connect to open network...");
  status = WiFi.begin(ssid);

  Serial.print("SSID: ");
  Serial.println(ssid);

}

void loop () {}

void scanNetworks() {
  // scan for nearby networks:
  Serial.println("** Scan Networks **");
  byte numSsid = WiFi.scanNetworks();

  // print the list of networks seen:
  Serial.print("SSID List:");
  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(") Network: ");
    Serial.println(WiFi.SSID(thisNet));
  }
}
WiFi.BSSID()

説明

接続しているルータのMACアドレスを取得する。

書式

WiFi.BSSID(bssid);

引数

bssid6バイトの配列

戻り値

WiFiシールドが現在接続しているルータのマックアドレスを含むバイト配列。

コード例

 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
#include <WiFi.h>

//SSID of your network
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";

void setup()
{
 WiFi.begin(ssid, pass);

  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while(true);
  }
  // if you are connected, print out info about the connection:
  else {
  // 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);
  }
}

void loop () {}
WiFi.RSSI()

説明

ルータへの接続の信号強度を取得する。

書式

WiFi.RSSI();

WiFi.RSSI(wifiAccessPoint);

引数

wifiAccessPointどのネットワークから情報を取得するかを指定する

戻り値

現在のRSSI/受信信号強度のdBm。(long)

コード例

 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
#include <SPI.h>
#include <WiFi.h>

//SSID of your network
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";

void setup()
{
 WiFi.begin(ssid, pass);

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while(true);
  }
  // if you are connected, print out info about the connection:
  else {
   // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("RSSI:");
  Serial.println(rssi);
  }
}

void loop () {}
WiFi.encryptionType()

説明

現在のネットワークの暗号化方式を取得する。

書式

WiFi.encryptionType();

WiFi.encryptionType(wifiAccessPoint);

引数

wifiAccessPointどのネットワークから情報を取得するかを指定する

戻り値

暗号化方式を表す値。(byte)

TKIP (WPA) = 2 WEP = 5 CCMP (WPA) = 4 NONE = 7 AUTO = 8

コード例

 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
#include <SPI.h>
#include <WiFi.h>

//SSID of your network
char ssid[] = "yourNetwork";
//password of your WPA Network
char pass[] = "secretPassword";

void setup()
{
 WiFi.begin(ssid, pass);

  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while(true);
  }
  // if you are connected, print out info about the connection:
  else {
   // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption,HEX);
  }
}

void loop () {}
WiFi.scanNetworks()

説明

利用可能なWiFiネットワークをスキャンし発見した数を返す。

書式

WiFi.scanNetworks();

引数

なし。

戻り値

発見したネットワーク数。(byte)

コード例

 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
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "yourNetwork";  // the name of your network

void setup() {
  Serial.begin(9600);

  int status = WiFi.begin(ssid);

  if (status != WL_CONNECTED) {
    Serial.println("Couldn't get a WiFi connection");
    while (true);
  }
  else {
    // if you are connected, scan for available WiFi networks and print the number discovered:
    Serial.println("** Scan Networks **");
    byte numSsid = WiFi.scanNetworks();

    Serial.print("Number of available WiFi networks discovered:");
    Serial.println(numSsid);
  }
}

void loop() {}
WiFi.status()

説明

接続状態を返す。

書式

WiFi.status();

引数

なし。

戻り値

WL_CONNECTEDWiFiネットワークに接続されている
WL_NO_SHIELDWiFiシールドが存在しない
WL_IDLE_STATUSWiFi.begin()が呼ばれて、試行回数が完了(WL_CONNECT_FAILEDとなる)するか、コネクションが確立(WL_CONNECTED)するまで。
WL_NO_SSID_AVAILSSIDが利用不可
WL_SCAN_COMPLETEDネットワークスキャンが完了した
WL_CONNECT_FAILEDすべての試行で接続に失敗した
WL_CONNECTION_LOSTコネクションが喪失した
WL_DISCONNECTEDネットワークから切断した

コード例

 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
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "yourNetwork";                     // your network SSID (name)
char key[] = "D0D0DEADF00DABBADEAFBEADED";       // your network key
int keyIndex = 0;                                // your network key Index number
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 Leonardo 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);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WEP network, SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, keyIndex, key);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // once you are connected :
  Serial.print("You're connected to the network");
}

void loop() {
  // check the network status connection once every 10 seconds:
  delay(10000);
 Serial.println(WiFi.status());
}
WiFi.getSocket()

説明

利用可能な最初のソケットを返す。

書式

WiFi.getSocket();

引数

なし。

戻り値

利用可能な最初のソケット。(int)

WiFi.macAddress()

説明

WiFiシールドのMACアドレスを返す。

書式

WiFi.macAddress();

引数

なし。

戻り値

シールドのMACアドレスを表す6バイト。(バイト配列)

コード例

 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
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "yourNetwork";     // the name of your network
int status = WL_IDLE_STATUS;     // the Wifi radio's status

byte mac[6];                     // the MAC address of your Wifi shield


void setup()
{
 Serial.begin(9600);

 status = WiFi.begin(ssid);

 if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while(true);
  }
  // if you are connected, print your MAC address:
  else {
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[5],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.println(mac[0],HEX);
  }
}

void loop () {}
inserted by FC2 system