1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-22 21:23:07 +03:00

update documentation example (#7697)

This commit is contained in:
david gauchard 2020-11-10 22:17:17 +01:00 committed by GitHub
parent 5d2563eee9
commit c919f61169
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,17 +39,19 @@ Then let's write a short function ``prepareHtmlPage()``, that will return a ``St
String prepareHtmlPage() String prepareHtmlPage()
{ {
String htmlPage = String htmlPage;
String("HTTP/1.1 200 OK\r\n") + htmlPage.reserve(1024); // prevent ram fragmentation
"Content-Type: text/html\r\n" + htmlPage = F("HTTP/1.1 200 OK\r\n"
"Connection: close\r\n" + // the connection will be closed after completion of the response "Content-Type: text/html\r\n"
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec "Connection: close\r\n" // the connection will be closed after completion of the response
"\r\n" + "Refresh: 5\r\n" // refresh the page automatically every 5 sec
"<!DOCTYPE HTML>" + "\r\n"
"<html>" + "<!DOCTYPE HTML>"
"Analog input: " + String(analogRead(A0)) + "<html>"
"</html>" + "Analog input: ");
"\r\n"; htmlPage += analogRead(A0);
htmlPage += F("</html>"
"\r\n");
return htmlPage; return htmlPage;
} }
@ -79,7 +81,7 @@ The content contains two basic `HTML <https://www.w3schools.com/html/>`__ tags,
.. code:: cpp .. code:: cpp
String(analogRead(A0)) analogRead(A0)
The Page is Served The Page is Served
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
@ -126,6 +128,18 @@ The whole process is concluded by stopping the connection with client:
client.stop(); client.stop();
But before that, we must not interrupt client's request:
.. code:: cpp
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();
}
Put it Together Put it Together
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
@ -163,17 +177,19 @@ Complete sketch is presented below.
// prepare a web page to be send to a client (web browser) // prepare a web page to be send to a client (web browser)
String prepareHtmlPage() String prepareHtmlPage()
{ {
String htmlPage = String htmlPage;
String("HTTP/1.1 200 OK\r\n") + htmlPage.reserve(1024); // prevent ram fragmentation
"Content-Type: text/html\r\n" + htmlPage = F("HTTP/1.1 200 OK\r\n"
"Connection: close\r\n" + // the connection will be closed after completion of the response "Content-Type: text/html\r\n"
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec "Connection: close\r\n" // the connection will be closed after completion of the response
"\r\n" + "Refresh: 5\r\n" // refresh the page automatically every 5 sec
"<!DOCTYPE HTML>" + "\r\n"
"<html>" + "<!DOCTYPE HTML>"
"Analog input: " + String(analogRead(A0)) + "<html>"
"</html>" + "Analog input: ");
"\r\n"; htmlPage += analogRead(A0);
htmlPage += F("</html>"
"\r\n");
return htmlPage; return htmlPage;
} }
@ -200,7 +216,14 @@ Complete sketch is presented below.
} }
} }
} }
delay(1); // give the web browser time to receive the data
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: // close the connection:
client.stop(); client.stop();