Sub topic 7
Moving an Arduino HTTP Request to HTTPS
Assume you already know how to connect a Wi-Fi-capable Arduino to a network, create a WiFiClient, and send an HTTP request. You also need the WiFiNINA library, an arduino_secrets.h file containing SECRET_SSID and SECRET_PASS, and a board such as the MKR WiFi 1010 or Nano 33 IoT.
HTTP sends data without encryption. HTTPS is HTTP carried inside TLS, a protocol that encrypts the connection and checks the server's identity. Use HTTPS when a request contains passwords, tokens, sensor data, or any other information that should not be read or changed in transit.
What changes when you use HTTPS?
An ordinary HTTP client connects to port 80. An HTTPS client normally connects to port 443. The client and server then perform a TLS handshake:
- The Arduino asks for a secure connection.
- The server sends a certificate containing its identity and public key.
- The Arduino checks that the certificate is trusted and matches the hostname.
- Both sides agree on encryption keys.
- The Arduino sends the HTTP request through the encrypted connection.
The WiFiSSLClient class performs these TLS operations. You still write the HTTP request in nearly the same way as before.
Use `WiFiSSLClient`
Replace WiFiClient with WiFiSSLClient and change the port from 80 to 443.
#include <WiFiNINA.h>
#include "arduino_secrets.h"
WiFiSSLClient client;
const char host[] = "example.com";
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
while (WiFi.begin(SECRET_SSID, SECRET_PASS) != WL_CONNECTED) {
delay(5000);
}
if (client.connect(host, 443)) {
client.println("GET / HTTP/1.1");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println();
} else {
Serial.println("TLS connection failed");
}
}
void loop() {
while (client.available()) {
Serial.write(client.read());
}
if (!client.connected()) {
client.stop();
while (true) {}
}
}The Host header is still required by HTTP/1.1. Use the same hostname in client.connect() and the Host header. The hostname helps the server choose the correct certificate and website. Do not replace it with a raw IP address unless you understand the certificate consequences.
Certificate handling
The Wi-Fi module must have a trusted root certificate for the server's certificate chain. A root certificate is a public certificate used as a trust anchor. It does not contain your password or private key.
For WiFiNINA boards, manage certificates with the Arduino IDE's firmware updater tool. Select the board's Wi-Fi module, open the certificate storage section, and add the root certificate required by the service. A certificate can expire or be replaced, so certificate storage is part of maintaining the device.
If the certificate is missing, expired, or not trusted, client.connect() can fail even though Wi-Fi works. This is useful protection. Do not solve the problem by disabling certificate verification or accepting every certificate.
Common HTTPS failures
- Port 443 is blocked or the server does not provide TLS.
- The hostname does not match the certificate.
- The module has no matching root certificate.
- The module's firmware or certificate store is outdated.
- The board's clock is wrong, so a valid certificate appears expired or not yet valid.
- The server requires a TLS feature that the module firmware does not support.
Test the same hostname from a computer first. Then check the Serial Monitor output, the Wi-Fi status, the selected board, the WiFiNINA firmware version, and the certificate store.
Exercise
What two code changes usually convert a WiFiNINA HTTP client into an HTTPS client, and what additional setup may HTTPS require?
Show answer
Use WiFiSSLClient instead of WiFiClient and connect on port 443. HTTPS may also require the server's trusted root certificate in the Wi-Fi module's certificate store.