The WiFiClient class is used to connect, send and receive data to and from servers.
WiFiClientWiFiClient is the base class for all WiFi client based calls. It is not called directly, but invoked whenever you use a function that relies on it.
WiFiClientCreates a client that can connect to to a specified internet IP address and port as defined in client.connect().
WiFiClient client;
none
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "myNetwork"; // your network SSID (name)
char pass[] = "myPassword"; // your network password
int status = WL_IDLE_STATUS;
IPAddress server(74,125,115,105); // Google
// Initialize the client library
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(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
}
}
void loop() {
}
client.connected()Whether or not the client is connected. Note that a client is considered connected if the connection has been closed but there is still unread data.
client.connected()
none
Returns true if the client is connected, false if not.
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "myNetwork"; // your network SSID (name)
char pass[] = "myPassword"; // your network password
int status = WL_IDLE_STATUS;
IPAddress server(74,125,115,105); // Google
// Initialize the client library
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(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
}
}
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
client.connect()Connect to the IP address and port specified in the constructor. The return value indicates success or failure. connect() also supports DNS lookups when using a domain name (ex:google.com).
client.connect(ip, port)
client.connect(URL, port)
ip: the IP address that the client will connect to (array of 4 bytes)
URL: the domain name the client will connect to (string, ex.:"arduino.cc")
port: the port that the client will connect to (int)
Returns true if the connection succeeds, false if not.
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "myNetwork"; // your network SSID (name)
char pass[] = "myPassword"; // your network password
int status = WL_IDLE_STATUS;
char servername[]="google.com"; // remote server we will connect to
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() {
}
client.write()Write data to the server the client is connected to.
client.write(data)
data: the byte or char to write
byte: the number of characters written. it is not necessary to read this value.
client.print()Print data to the server that a client is connected to. Prints numbers as a sequence of digits, each an ASCII character (e.g. the number 123 is sent as the three characters '1', '2', '3').
client.print(data)
client.print(data, BASE)
data: the data to print (char, byte, int, long, or string)
BASE (optional): the base in which to print numbers:, DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).
byte : returns the number of bytes written, though reading that number is optional
client.println()Print data, followed by a carriage return and newline, to the server a client is connected to. Prints numbers as a sequence of digits, each an ASCII character (e.g. the number 123 is sent as the three characters '1', '2', '3').
client.println()
client.println(data)
client.print(data, BASE)
data (optional): the data to print (char, byte, int, long, or string)
BASE (optional): the base in which to print numbers: DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).
byte: return the number of bytes written, though reading that number is optional
client.available()Returns the number of bytes available for reading (that is, the amount of data that has been written to the client by the server it is connected to).
available() inherits from the Stream utility class.
client.available()
none
The number of bytes available.
#include <SPI.h>
#include <WiFi.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(;;)
;
}
}
client.read()Read the next byte received from the server the client is connected to (after the last call to read()).
read() inherits from the Stream utility class.
client.read()
none
The next byte (or character), or -1 if none is available.
client.flush()Discard any bytes that have been written to the client but not yet read.
flush() inherits from the Stream utility class.
client.flush()
none
none
client.stop()Disconnect from the server
client.stop()
none
none