Sub topic 10
Serving a Page from the Board
This chapter assumes that you can upload Arduino sketches, use setup() and loop(), and connect a Wi-Fi-capable board to your 2.4 GHz network. It also assumes you know basic terms such as IP address, port, and router.
A web server waits for an HTTP request and sends an HTTP response. On an Arduino board, WiFiServer listens for TCP connections on a port. The usual HTTP port is 80. A browser on the same network can connect to the board's local IP address.
A minimal Arduino web server
The following sketch connects to Wi-Fi, starts a server, and returns a small HTML page for every request. Put your network name and password in the separate arduino_secrets.h file described earlier.
#include <WiFiNINA.h>
#include "arduino_secrets.h"
WiFiServer server(80);
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Wi-Fi module not found");
while (true) {}
}
while (WiFi.begin(SECRET_SSID, SECRET_PASS) != WL_CONNECTED) {
Serial.println("Connecting to Wi-Fi...");
delay(5000);
}
server.begin();
Serial.print("Open http://");
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
unsigned long start = millis();
// Read the request headers until the blank line that ends them.
while (client.connected() && millis() - start < 2000) {
if (!client.available()) {
continue;
}
String line = client.readStringUntil('\n');
if (line == "\r" || line.length() == 0) {
break;
}
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html; charset=utf-8");
client.println("Connection: close");
client.println();
client.println("<!doctype html>");
client.println("<html><head><title>Arduino</title></head>");
client.println("<body><h1>Hello from Arduino</h1>");
client.print("<p>Uptime: ");
client.print(millis() / 1000);
client.println(" seconds</p></body></html>");
client.stop();
}After uploading, open the Serial Monitor at 115200 baud. Enter the printed address, such as http://192.168.1.42, in a browser connected to the same Wi-Fi network.
The request contains a request line such as GET / HTTP/1.1, followed by headers. A blank line marks the end of the headers. The sketch does not need to understand every header, so it discards them and sends the same page for every path.
The response has three important parts:
HTTP/1.1 200 OKtells the browser that the request succeeded.Content-Typetells the browser to interpret the body as HTML.- The blank line separates the headers from the HTML body.
Common errors
The board's IP address usually comes from DHCP, which means the router assigns it automatically. That address can change after a reboot. Print it during every startup instead of assuming it will remain fixed.
A browser may cache an earlier page. Refresh the page, or open it in a private window while testing.
A server on port 80 is available to other devices on the same local network. It is not automatically available from the public internet. Do not configure router port forwarding for this simple sketch. It has no authentication, encryption, or input validation.
The WiFiClient object represents one browser connection. Calling client.stop() closes that connection after the response. A more advanced server can inspect the request path and return different pages, but it should still limit page size and avoid blocking for a long time.
Exercise
Why does the browser need a blank line between the HTTP headers and the HTML page?
Show answer
The blank line marks the end of the headers, so the browser knows that the following bytes are the response body.