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.
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.
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.
Disconnect from the server
这是官方版本。
void loop()
{
WiFiClient client = server.accept();
// wait for a client (web browser) to connect
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n')
{
client.println(prepareHtmlPage());
break;
}
}
}
while (client.available()) {
// but first, let client finish its request
// that's diplomatic compliance to protocols
// (and otherwise some clients may complain, like curl)
// (that is an example, prefer using a proper webserver library)
client.read();
}
// close the connection:
client.stop();
Serial.println("[Client disconnected]");
}
}
以下为我编写的控制继电器版本
#include
#define DEBUG
#define GPIO0 0
#define GPIO2 2
#define L false
#define H true
#define DELAYTRIGGER(GPIO, LEVEL) \
do { \
digitalWrite(GPIO, LEVEL); \
delay(200); \
digitalWrite(GPIO, !LEVEL); \
} while (0)
const char* ssid = "14-2001";
const char* password = "******";
IPAddress local_IP(192, 168, 3, 31);
IPAddress gateway(192, 168, 3, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiServer server(80);
void connectToWiFi() {
Serial.print("\n\nConnecting to ");
Serial.println(ssid);
WiFi.config(local_IP, gateway, subnet); //设置静态IP
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
pinMode(GPIO0, OUTPUT);
pinMode(GPIO2, OUTPUT);
digitalWrite(GPIO0, HIGH);
digitalWrite(GPIO2, HIGH);
connectToWiFi();
server.begin();
Serial.println("Server started : To control GPIO, use Android APP [esp8266 controller].");
}
void loop() {
WiFiClient client = server.accept();
if (!client) {
Serial.print("A");
delay(200);
return;
}
Serial.println("\n\n");
Serial.println("Client " + client.remoteIP().toString() + " connected.");
while (client.connected()) {
Serial.print("C");
delay(100);
if (client.available()) {
Serial.println("\nclient available...");
String req = client.readStringUntil('\r');
Serial.println("client request:");
Serial.println(req);
// Match the request
int val;
if (req.indexOf("ON1") != -1) { //IO0 ON 高电平触发
Serial.println("ON1");
DELAYTRIGGER(GPIO0, H);
} else if (req.indexOf("OFF1") != -1) { //IO0 OFF 低电平触发
Serial.println("OFF1");
DELAYTRIGGER(GPIO0, L);
} else if (req.indexOf("ON2") != -1) { //IO2 ON 高电平触发
Serial.println("ON2");
DELAYTRIGGER(GPIO2, H);
} else if (req.indexOf("OFF2") != -1) { //IO2 OFF 低电平触发
Serial.println("OFF2");
DELAYTRIGGER(GPIO2, L);
} else {
Serial.println("invalid request, reset...");
client.print("HTTP/1.1 404\r\n");
client.stop();
return;
}
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now ";
s += (val) ? "high" : "low";
s += "\n";
client.print(s);
client.flush();
client.stop();
}
}
Serial.println("\n[Client disonnected]");
}