Sub topic 3
Keeping Wi-Fi Credentials in a Separate File
This chapter assumes you can create, upload, and monitor an Arduino sketch. You should also know basic C++ preprocessor directives, such as #include and #define, and understand that an SSID identifies a Wi-Fi network.
Putting the network name and password directly in a sketch is convenient during a first test. It becomes a problem when you share the sketch or commit it to a version-control repository. Anyone who can read the file can use the credentials. Keep the credentials in a separate header file instead.
Create the secrets header
A header file is a text file that contains declarations or definitions included by another source file. Create a file named arduino_secrets.h in the same folder as the .ino sketch:
#define SECRET_SSID "your-network-name"
#define SECRET_PASS "your-password"Replace the placeholder values with the credentials for the network you are allowed to use. Keep the quotation marks. The #define directive gives each value a name that the preprocessor substitutes before compilation.
Your main sketch can then include the header:
#include <WiFiNINA.h>
#include "arduino_secrets.h"
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) {}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Wi-Fi module not found");
while (true) {}
}
int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(SECRET_SSID);
status = WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(10000);
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {}The double quotes in #include "arduino_secrets.h" tell the compiler to look for the file in the project, including the sketch folder. Angle brackets, as in #include <WiFiNINA.h>, normally refer to an installed library.
The file is separate from the main sketch, but it is still compiled into the firmware. This arrangement protects the source from accidental sharing. It does not make the credentials impossible to extract from a compiled program.
Exclude the file from version control
If you use Git, add the secrets file to .gitignore:
arduino_secrets.hA .gitignore file lists paths that Git should not track. Add this rule before making the first commit containing the file. If the file was already committed, adding it to .gitignore does not remove it from Git history. You must remove the tracked copy and change the exposed Wi-Fi password.
| File | Commit to the repository? | Purpose |
|---|---|---|
| `internet_demo.ino` | Yes | Main sketch without secret values |
| `arduino_secrets.h` | No | Local Wi-Fi credentials |
| `.gitignore` | Yes | Rule that excludes the secrets file |
You can provide a safe template for other developers:
#define SECRET_SSID "replace-with-your-network-name"
#define SECRET_PASS "replace-with-your-password"Save that template under a different name, such as arduino_secrets.example.h. It documents the required names without exposing a real password.
Understand the project layout
A typical project has this structure:
The board still needs the header when you compile and upload. If another computer reports arduino_secrets.h: No such file or directory, create the file there or copy it through a secure channel. Do not solve the error by placing real credentials in the public sketch.
Exercise
Why does adding arduino_secrets.h to .gitignore protect a new project but not erase an old password from Git history?
Show answer
.gitignore prevents future tracking, but Git keeps files that were already committed. An already exposed password must be removed from history when practical and changed on the network.