当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Arduino WiFiNINA - client.available()用法及代码示例


说明

返回可供读取的字节数(即,它所连接的服务器已写入客户端的数据量)。

available() 继承自 Stream 实用程序类。

用法

client.available()

参数

  • None

返回

  • 可用的字节数。

示例

#include <SPI.h>
#include <WiFiNINA.h>

char ssid[] = "myNetwork";          //  your network SSID (name)
char pass[] = "myPassword";   // your network password

int status = WL_IDLE_STATUS;
char servername[]="google.com";  // Google

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");
    // don't do anything else:
    while(true);
  }
  else {
    Serial.println("Connected to wifi");
    Serial.println("\nStarting connection...");
    // if you get a connection, report back via serial:
    if (client.connect(servername, 80)) {
      Serial.println("connected");
      // Make a HTTP request:
      client.println("GET /search?q=arduino HTTP/1.0");
      client.println();
    }
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

相关用法


注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 WiFiNINA - client.available()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。