Skip to content

Commit

Permalink
update documentation example (#7697)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v authored Nov 10, 2020
1 parent 5d2563e commit c919f61
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions doc/esp8266wifi/server-examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ Then let's write a short function ``prepareHtmlPage()``, that will return a ``St
String prepareHtmlPage()
{
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec
"\r\n" +
"<!DOCTYPE HTML>" +
"<html>" +
"Analog input: " + String(analogRead(A0)) +
"</html>" +
"\r\n";
String htmlPage;
htmlPage.reserve(1024); // prevent ram fragmentation
htmlPage = F("HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n" // the connection will be closed after completion of the response
"Refresh: 5\r\n" // refresh the page automatically every 5 sec
"\r\n"
"<!DOCTYPE HTML>"
"<html>"
"Analog input: ");
htmlPage += analogRead(A0);
htmlPage += F("</html>"
"\r\n");
return htmlPage;
}
Expand Down Expand Up @@ -79,7 +81,7 @@ The content contains two basic `HTML <https://www.w3schools.com/html/>`__ tags,

.. code:: cpp
String(analogRead(A0))
analogRead(A0)
The Page is Served
~~~~~~~~~~~~~~~~~~
Expand All @@ -90,7 +92,7 @@ Serving of this web page will be done in the ``loop()`` where server is waiting
void loop()
{
WiFiClient client = server.available();
WiFiClient client = server.available();
if (client)
{
// we have a new client sending some request
Expand Down Expand Up @@ -126,6 +128,18 @@ The whole process is concluded by stopping the connection with client:
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
~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -163,24 +177,26 @@ Complete sketch is presented below.
// prepare a web page to be send to a client (web browser)
String prepareHtmlPage()
{
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec
"\r\n" +
"<!DOCTYPE HTML>" +
"<html>" +
"Analog input: " + String(analogRead(A0)) +
"</html>" +
"\r\n";
String htmlPage;
htmlPage.reserve(1024); // prevent ram fragmentation
htmlPage = F("HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n" // the connection will be closed after completion of the response
"Refresh: 5\r\n" // refresh the page automatically every 5 sec
"\r\n"
"<!DOCTYPE HTML>"
"<html>"
"Analog input: ");
htmlPage += analogRead(A0);
htmlPage += F("</html>"
"\r\n");
return htmlPage;
}
void loop()
{
WiFiClient client = server.available();
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
Expand All @@ -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:
client.stop();
Expand Down

0 comments on commit c919f61

Please sign in to comment.