IoT/ESP32 B.Tech/Diploma Beginner

ESP32: Save Wi-Fi Credentials in a Separate File (and Other Sensitive Information)

Securely store Wi-Fi credentials and other sensitive information in a separate file for your ESP32 projects (works with other Arduino based projects). This approach eliminates the need to type your credentials in every sketch.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

Learn how to securely store Wi-Fi credentials and other sensitive information in a separate file for your ESP32 projects (also works for other Arduino based projects). This approach eliminates the need to type your credentials in every sketch, while preventing accidental disclosure of personal account details when sharing your code. It also applies to other sensitive data, such as API keys, tokens, and more.
This tutorial was written by one of our readers, Ron Brinkman, and edited by Sara Santos.
Why Save Your Credentials in a Separate File?
Many projects you create require account names, passwords, tokens, or other sensitive information in order for them to work.
These may be secure if they never leave your desktop, but it can be all too easy to forget and publish or distribute a script file with your personal credentials in it rather than the intended placeholder
“REPLACE_WITH_YOUR_PASSWORD”
You can address this by placing your credentials in a separate file in your
libraries
folder and including a reference to that file in your sketch initiation. That way, your private credentials will not accidentally escape your desktop and will automatically be included when you compile your sketch.
Additionally, this method removes the need to hardcode credentials or API tokens every time you compile a new sketch that requires them.
ESP32 Useful Wi-Fi Library Functions (Arduino IDE)
Project Overview
Here is a quick overview of how our approach works:
Your private credentials, including account names, passwords, and tokens, are saved in a library file
.h
, not in your sketch.
The library file is
#included
at the beginning of your sketch.
The sketch is modified to reference the
#defined
name of the private information contained in the library file, keeping the private information out of your sketch.
If you subsequently send or distribute your sketch to someone else, you don’t have to take any action to “clean” the file, and you will never have to worry about inadvertently disclosing your personal information because you forgot to edit the file before sending.
Creating/Installing the MyLogin library
We’ll create a library called
MyLogin
that will hold the credentials. You can give it any other name. Follow the next steps to install a skeleton
MyLogin
file in your library:
1. Go to the Arduino
libraries
folder. You can find the path for your Arduino
libraries
folder, by going to
File
>
Preferences
. The path in the
Sketchbook location
field is where your
libraries
folder is located.
2. Create a folder named
MyLogin
in your Arduino IDE
libraries
folder.
3. Finally, copy the following skeleton code to a file named
MyLogin.h
in the
MyLogin
folder and save it.
/*********
Created by Ron Brinkman
Complete instructions at https://ajvikalabs.com/esp32-save-credentials-separate-file/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/

// Keep account names, passwords, tokens, etc. current in this file
// When visiting another location put their credentials in the "currently in effect" section

// Credentials currently in effect
#define STASSID "REPLACE_WITH_YOUR_SSID" // WiFi
#define STAPSK "REPLACE_WITH_YOUR_PASSWORD"

#define emailSenderAccount "
[email protected]
" // Email
#define emailSenderPassword "YOUR_EMAIL_APP_PASSWORD"
#define emailRecipient "
[email protected]
"

#define DuckDNStoken "YOUR_DUCKDNS_TOKEN" // DuckDNS

/*********Saved credentials for substitution above when "on the road"
//Credentials used at home
#define STASSID "my_wifi_ssid"
#define STAPSK "my_wifi_password"
#define emailSenderAccount "
[email protected]
"
#define emailSenderPassword "my_email_app_password"
#define emailRecipient "
[email protected]
"
#define DuckDNStoken "my_duckdns_token"

// Credentials used at Mom and Dad's house
#define STASSID "parents_wifi_ssid"
#define STAPSK "parents_wifi_password"

// Credentials used at wife's Mom and Dad's house
#define STASSID "wifes_parents_wifi_ssid"
#define STAPSK "wifes_parents_wifi_password"

// Credentials used at our daughter's house
#define STASSID "daughter_wifi_ssid"
#define STAPSK "daughter_wifi_password"

// Credentials used at our son's house
#define STASSID "son_wifi_ssid"
#define STAPSK "son_wifi_password"

// Credentials used at our friends Joe and Sally's house
#define STASSID "friend_wifi_ssid"
#define STAPSK "friend_wifi_password"
*********/
Customize the
MyLogin.h
file with your credentials
Open the skeleton
MyLogin.h
file in your Arduino IDE installation
libraries
folder. Follow the next steps to isolate personal account, password, and token information in this private file:

Edit the
Credentials currently in effect
section. Write your network credentials in the following definition statements, so that applications can connect to your local network.
// Credentials currently in effect
#define STASSID "REPLACE_WITH_YOUR_SSID" // WiFi
#define STAPSK "REPLACE_WITH_YOUR_PASSWORD"

Continue editing or adding any other credentials your applications use. This may include SMTP email, SMS messaging, Telegram messaging, WhatsApp instant messaging, Duck DNS, NoIP, DynDNS, or other dynamic DNS support, etc.
For this tutorial, we’ll only be testing the network credentials, but this gives you an idea if you want to add more sensitive information.
#define emailSenderAccount "
[email protected]
" // Email
#define emailSenderPassword "YOUR_EMAIL_APP_PASSWORD"
#define emailRecipient "
[email protected]
"

#define DuckDNStoken "YOUR_DUCKDNS_TOKEN" // DuckDNS

Optionally place credentials in the commented-out section of the file so that you don’t need to look them up each time you travel to another location and want to work on your application.
Start with a copy of your home credentials for the case where the
Current credentials
are temporarily overwritten while “on the road”.
// Saved credentials for substitution above when "on the road"
// Credentials used at home
#define STASSID "my_wifi_ssid"
#define STAPSK "my_wifi_password"
#define emailSenderAccount "
[email protected]
"
#define emailSenderPassword "my_email_app_password"
#define emailRecipient "
[email protected]
"
#define DuckDNStoken "my_duckdns_token

Continue customizing with any other family, friend, or business locations’ credentials as needed.
// Credentials used at Mom and Dad's house
#define STASSID "wifes_parents_wifi_ssid"
#define STAPSK "wifes_parents_wifi_password"
Save the file, now containing your private information. It’s ready to be used by your scripts to keep private information out of the scripts.
Tailoring Your Script Files
Each script file you want to protect from inadvertent disclosure of your credentials will require two simple edits. Follow the next steps:

Place the following statement in your list of
#include
files to include the “library” we created previously:
#include <MyLogin.h>

Find the place in your script file where your account name, password, token, or other credentials would normally be entered. For your WiFi account, it will look something like:
// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Replace the text with the following:
const char

Technologies used

ESP32ArduinoWiFiArduino IDE

Hardware components

  • ESP32
  • Arduino

Software & tools

  • Arduino IDE

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.