Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update documentation example #7697

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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