The WiFiServer is used for server based calls, such as creating server that listens to a specific port, or writing data to connected clients.
WiFiServerWiFiServer is the base class for all WiFi server based calls. It is not called directly, but invoked whenever you use a function that relies on it.
WiFiServer()Creates a server that listens for incoming connections on the specified port.
WiFiServer server(port);
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;
WiFiServer server(80);
void setup() {
// initialize serial:
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 {
server.begin();
Serial.print("Connected to wifi. My address:");
IPAddress myAddress = WiFi.localIP();
Serial.println(myAddress);
}
}
void loop() {
}
server.begin()Tells the server to begin listening for incoming connections.
server.begin()
None
None
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "lamaison"; // your network SSID (name)
char pass[] = "tenantaccess247"; // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
// initialize serial:
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 {
server.begin();
Serial.print("Connected to wifi. My address:");
IPAddress myAddress = WiFi.localIP();
Serial.println(myAddress);
}
}
void loop() {
}
server.available()Gets a client that is connected to the server and has data available for reading. The connection persists when the returned client object goes out of scope; you can close it by calling client.stop().
available() inherits from the Stream utility class.
server.available()
None
a Client object; if no Client has data available for reading, this object will evaluate to false in an if-statement
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "Network"; // your network SSID (name)
char pass[] = "myPassword"; // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
// initialize serial:
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 {
server.begin();
Serial.print("Connected to wifi. My address:");
IPAddress myAddress = WiFi.localIP();
Serial.println(myAddress);
}
}
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
Serial.println("Connected to client");
}
// close the connection:
client.stop();
}
}
server.write()Write data to all the clients connected to a server.
server.write(data)
data: the value to write (byte or char)
byte : the number of bytes written. It is not necessary to read this.
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork";
char pass[] = "yourPassword";
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
// initialize serial:
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 {
server.begin();
}
}
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client == true) {
// read bytes from the incoming client and write them back
// to any clients connected to the server:
server.write(client.read());
}
}
server.print()Print data to all the clients connected to a server. 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').
server.print(data)
server.print(data, BASE)
data: the data to print (char, byte, int, long, or string)
BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).
byte print() will return the number of bytes written, though reading that number is optional
server.println()Prints data, followed by a newline, to all the clients connected to a server. 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').
server.println()
server.println(data)
server.println(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 println() will return the number of bytes written, though reading that number is optional