WiFiClient()

描述

創建一個用戶端,用來與指定的 IP 位址與埠編號連線。(使用 client.connect() 指定伺服端的 IP 位址與埠編號)

語法


WiFiClient client;

參數

無參數

範例

#include <WiFi.h>

char ssid[] = "myNetwork";          // 無線網路的 SSID
char pass[] = "myPassword";   // WPA 的密碼

int status = WL_IDLE_STATUS;
IPAddress server(74,125,115,105);  // Google

// 初始化 WiFiClient 類別
WiFiClient client;

void setup() {
  Serial.begin(9600);
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
  Serial.println(ssid);

  status = WiFi.begin(ssid, pass);
  if ( status != WL_CONNECTED) { 
    Serial.println("Couldn't get a wifi connection");
    while(true);
  } 
  else {
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    // 如果成功連線,透過序列埠印出訊息
    if (client.connect(server, 80)) {
      Serial.println("connected");
      // 傳送 HTTP 封包
      client.println("GET /search?q=arduino HTTP/1.0");
      client.println();
    }
  }
}

void loop() {

}

函式庫參考主頁面

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.