@@ -39,17 +39,19 @@ Then let's write a short function ``prepareHtmlPage()``, that will return a ``St
39
39
40
40
String prepareHtmlPage()
41
41
{
42
- String htmlPage =
43
- String("HTTP/1.1 200 OK\r\n") +
44
- "Content-Type: text/html\r\n" +
45
- "Connection: close\r\n" + // the connection will be closed after completion of the response
46
- "Refresh: 5\r\n" + // refresh the page automatically every 5 sec
47
- "\r\n" +
48
- "<!DOCTYPE HTML>" +
49
- "<html>" +
50
- "Analog input: " + String(analogRead(A0)) +
51
- "</html>" +
52
- "\r\n";
42
+ String htmlPage;
43
+ htmlPage.reserve(1024); // prevent ram fragmentation
44
+ htmlPage = F("HTTP/1.1 200 OK\r\n"
45
+ "Content-Type: text/html\r\n"
46
+ "Connection: close\r\n" // the connection will be closed after completion of the response
47
+ "Refresh: 5\r\n" // refresh the page automatically every 5 sec
48
+ "\r\n"
49
+ "<!DOCTYPE HTML>"
50
+ "<html>"
51
+ "Analog input: ");
52
+ htmlPage += analogRead(A0);
53
+ htmlPage += F("</html>"
54
+ "\r\n");
53
55
return htmlPage;
54
56
}
55
57
@@ -79,7 +81,7 @@ The content contains two basic `HTML <https://www.w3schools.com/html/>`__ tags,
79
81
80
82
.. code :: cpp
81
83
82
- String( analogRead(A0) )
84
+ analogRead(A0)
83
85
84
86
The Page is Served
85
87
~~~~~~~~~~~~~~~~~~
@@ -90,7 +92,7 @@ Serving of this web page will be done in the ``loop()`` where server is waiting
90
92
91
93
void loop()
92
94
{
93
- WiFiClient client = server.available();
95
+ WiFiClient client = server.available();
94
96
if (client)
95
97
{
96
98
// we have a new client sending some request
@@ -126,6 +128,18 @@ The whole process is concluded by stopping the connection with client:
126
128
127
129
client.stop();
128
130
131
+ But before that, we must not interrupt client's request:
132
+
133
+ .. code :: cpp
134
+
135
+ while (client.available()) {
136
+ // but first, let client finish its request
137
+ // that's diplomatic compliance to protocols
138
+ // (and otherwise some clients may complain, like curl)
139
+ // (that is an example, prefer using a proper webserver library)
140
+ client.read();
141
+ }
142
+
129
143
Put it Together
130
144
~~~~~~~~~~~~~~~
131
145
@@ -163,24 +177,26 @@ Complete sketch is presented below.
163
177
// prepare a web page to be send to a client (web browser)
164
178
String prepareHtmlPage()
165
179
{
166
- String htmlPage =
167
- String("HTTP/1.1 200 OK\r\n") +
168
- "Content-Type: text/html\r\n" +
169
- "Connection: close\r\n" + // the connection will be closed after completion of the response
170
- "Refresh: 5\r\n" + // refresh the page automatically every 5 sec
171
- "\r\n" +
172
- "<!DOCTYPE HTML>" +
173
- "<html>" +
174
- "Analog input: " + String(analogRead(A0)) +
175
- "</html>" +
176
- "\r\n";
180
+ String htmlPage;
181
+ htmlPage.reserve(1024); // prevent ram fragmentation
182
+ htmlPage = F("HTTP/1.1 200 OK\r\n"
183
+ "Content-Type: text/html\r\n"
184
+ "Connection: close\r\n" // the connection will be closed after completion of the response
185
+ "Refresh: 5\r\n" // refresh the page automatically every 5 sec
186
+ "\r\n"
187
+ "<!DOCTYPE HTML>"
188
+ "<html>"
189
+ "Analog input: ");
190
+ htmlPage += analogRead(A0);
191
+ htmlPage += F("</html>"
192
+ "\r\n");
177
193
return htmlPage;
178
194
}
179
195
180
196
181
197
void loop()
182
198
{
183
- WiFiClient client = server.available();
199
+ WiFiClient client = server.available();
184
200
// wait for a client (web browser) to connect
185
201
if (client)
186
202
{
@@ -200,7 +216,14 @@ Complete sketch is presented below.
200
216
}
201
217
}
202
218
}
203
- delay(1); // give the web browser time to receive the data
219
+
220
+ while (client.available()) {
221
+ // but first, let client finish its request
222
+ // that's diplomatic compliance to protocols
223
+ // (and otherwise some clients may complain, like curl)
224
+ // (that is an example, prefer using a proper webserver library)
225
+ client.read();
226
+ }
204
227
205
228
// close the connection:
206
229
client.stop();
0 commit comments