Sub topic 12
Securing a Connected Device
This chapter assumes you know how to upload an Arduino sketch, connect a Wi-Fi board, and use basic IP, port, HTTP, and MQTT terms. A connected board can expose sensor data, accept commands, or reveal network credentials. Security must be part of the design, not an addition after the device works.
Protecting credentials
Keep Wi-Fi passwords, MQTT passwords, and API keys outside the main sketch. Store them in a separate arduino_secrets.h file:
#define SECRET_SSID "your-network-name"
#define SECRET_PASS "your-password"
#define MQTT_USER "your-mqtt-user"
#define MQTT_PASS "your-mqtt-password"Add this file to .gitignore so Git does not upload it:
arduino_secrets.hThis protects the credentials from accidental commits. It does not make them invisible on the board. The values become part of the compiled firmware. Anyone who can read the firmware, access the device, or see serial debug output may still recover them.
Use a separate account for each device when the service supports it. Give that account only the permissions it needs. If a password or key appears in a public repository, log, or shared message, replace it immediately.
Use HTTPS and secure MQTT
Plain HTTP sends requests and responses without encryption. Someone on the same network may read or modify them. HTTPS adds TLS, which encrypts the connection and checks that the board is talking to the intended server.
For WiFiNINA boards, WiFiSSLClient provides a TLS connection:
#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)) {
Serial.println("TLS connection failed");
return;
}
client.println("GET / HTTP/1.1");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println();
}
void loop() {
while (client.available()) {
Serial.write(client.read());
}
if (!client.connected()) {
client.stop();
while (true) {}
}
}TLS is useful only when certificate validation is configured correctly. The Wi-Fi module needs the root certificate that can validate the server's certificate. Manage the certificate store with the firmware updater tool for your board and module. Do not disable certificate checking just to make a connection succeed. That allows an attacker to impersonate the server.
For MQTT, use a TLS-capable client and normally port 8883, rather than unencrypted port 1883. Configure a broker certificate, username, and password according to the broker and Arduino library documentation. Use topic permissions so a device can publish or subscribe only to its assigned topics.
Limit the board's exposure
A board running WiFiServer accepts connections from other devices on the local network. Do not assume that a local network is automatically trusted. Avoid placing control endpoints on the public internet. If the board must accept commands:
- Require authentication for every command.
- Validate values and reject unexpected input.
- Do not place passwords in URLs, because URLs can appear in logs.
- Return only the data the client needs.
- Close idle connections and limit request sizes.
A small board may not support a complete web security stack. Put a properly secured gateway in front of it when stronger authentication, logging, or internet access is required.
Security checks before deployment
| Area | Safer choice | Common mistake |
|---|---|---|
| Transport | HTTPS or MQTT over TLS | Sending secrets over HTTP |
| Certificates | Validate the server certificate | Disabling validation |
| Credentials | Separate, limited, replaceable accounts | Reusing an administrator password |
| Web server | Authentication and input validation | Trusting every local client |
| Debugging | Remove sensitive serial output | Printing passwords or tokens |
Exercise
Why does using WiFiSSLClient alone not guarantee a secure connection?
Show answer
TLS encryption protects the traffic, but the board must also validate the server certificate. Without validation, an attacker could impersonate the server.