IoT WiFi Credential Manager With ESP32
by Tech_Demons in Circuits > Arduino
57 Views, 1 Favorites, 0 Comments
IoT WiFi Credential Manager With ESP32
![wifi.png](/proxy/?url=https://content.instructables.com/FD2/C0H9/M6P2A4OF/FD2C0H9M6P2A4OF.png&filename=wifi.png)
This project creates a self-configuring WiFi manager that stores credentials in non-volatile storage (NVS) and provides a web interface for management. Perfect for IoT devices needing flexible network configuration.
Supplies
The IoT WiFi Credential Manager project utilizing the ESP32 serves as a versatile tool for managing WiFi connections in Internet of Things (IoT) applications. Its primary function is to allow users to easily configure and store WiFi credentials through a user-friendly web interface. By leveraging the ESP32's capabilities, the project enables devices to connect to different WiFi networks without requiring hard-coded credentials in the firmware, which enhances flexibility and usability.When powered on, the ESP32 attempts to connect to previously stored WiFi credentials. If it fails, it automatically switches to Access Point (AP) mode, allowing users to connect directly to the device via a dedicated network. Through a simple web page hosted on the ESP32, users can input new SSID and password information, which is then securely stored in non-volatile memory. This ensures that the credentials persist even after power cycles. Additionally, users can remove stored credentials if needed.Overall, this project is particularly useful for developers and hobbyists working on IoT devices that require dynamic network configurations, making it easier to deploy devices in various environments without needing to reprogram them for different networks.
Required Components
- ESP32 development board
- Micro-USB cable
- Computer with Arduino IDE
- WiFi network (2.4GHz)
- Source Code: Link
Library Imports:
#include <WiFi.h>
#include <WebServer.h>
#include <FS.h>
#include <SPIFFS.h>
#include <Preferences.h>
HTML Web Interface
![wifi.png](/proxy/?url=https://content.instructables.com/F1L/XCOD/M6P2A83A/F1LXCODM6P2A83A.png&filename=wifi.png)
const char* html_page = R"=====(
<!DOCTYPE html>
<html>
<head>
<title>WiFi Manager</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h1 {
color: #333;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
input[type="text"], input[type="password"] {
width: calc(100% - 22px);
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.container {
max-width: 400px;
margin: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Manage WiFi Connections</h1>
<form action="/save">
<label for="ssid">SSID:</label><br>
<input type="text" id="ssid" name="ssid" required><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" required><br>
<input type="submit" value="Save">
</form>
<form action="/remove">
<input type="submit" value="Remove Credentials">
</form>
</div>
</body>
</html>
)=====";
Core Functions: WiFi Connection Handler
void connectToWiFi() {
// Attempts connection using stored credentials
// Falls back to AP mode after 10 seconds
}
Credential Management
void handleSave() {
// Saves new credentials to NVS
preferences.putString("ssid", newSSID);
preferences.putString("password", newPassword);
}
void handleRemove() {
// Clears stored credentials
preferences.clear();
}
Step-by-Step Working
- Initial Boot SequenceChecks SPIFFS initialization
- Loads credentials from NVS
- Attempts WiFi connection
- Network ModesStation Mode: Connected to existing WiFi
- AP Mode: Creates config hotspot (192.168.4.1)
- Web Server WorkflowAccess http://[IP]/ for interface
- /save endpoint stores new credentials
- /remove deletes existing credentials
- Credential StorageEncrypted storage in NVS
- Automatic reboot after changes
ESP32 Board
│
├── USB Cable ↔ Computer (Power/Programming)
└── WiFi ↔ Router/AP (Wireless Connection)
Testing & Usage
- Upload code via Arduino IDE
- Initial boot connects to stored network
- If unavailable:Connect to "ESP32-Setup" AP
- Visit 192.168.4.1
- Enter new credentials