This content originally appeared on DEV Community and was authored by John Ajera
Configuring WiFi on ESP32-C3 DevKitM-1 / Rust-1
The Problem: Hardcoded Credentials in IoT Projects
When deploying ESP32 devices in the field, one of the biggest challenges is managing WiFi credentials. Hardcoding passwords directly in your code creates several problems:
- Security Risk: Credentials are visible in your source code and version control
- Deployment Complexity: Different devices need different WiFi networks
- Maintenance Nightmare: Updating credentials requires recompiling and reflashing
A Practical Solution: Externalized Credential Management
Here’s a simple approach that improves upon hardcoded credentials while keeping the code clean and maintainable:
Externalized Credential Management
- Credentials stored in a separate
wifi_config.h
file - Template file (
wifi_config_template.h
) for easy setup - Git-ignored credentials to prevent accidental commits
- No hardcoded passwords in source code
Visual Signal Strength Indicator
- Built-in LED shows WiFi signal strength through blink patterns
- Fast blink = Strong signal (-30 to -50 dBm)
- Medium blink = Medium signal (-50 to -70 dBm)
- Slow blink = Weak signal (below -70 dBm)
Auto-Reconnection
- Automatically reconnects if WiFi drops
- Continuous monitoring every 2 seconds
- LED feedback for connection status
Arduino IDE Ready
- Works seamlessly with Arduino IDE UI
- No complex build systems or CLI tools
- Simple copy-paste setup for field deployment
Code Architecture
Main Sketch Structure
#include "WiFi.h"
#include "Preferences.h"
#include "wifi_config.h" // WiFi credentials (gitignored)
#define LED_PIN 7 // ESP32C3 built-in LED
// Signal strength thresholds
#define STRONG_SIGNAL -50
#define MEDIUM_SIGNAL -70
void setup() {
// Initialize serial, LED, and connect to WiFi
}
void loop() {
// Monitor connection and show signal strength
if (WiFi.status() == WL_CONNECTED) {
showSignalStrength(WiFi.RSSI());
} else {
// Attempt reconnection
}
}
Credential Management
// wifi_config.h (gitignored)
const char* WIFI_SSID = "YourWiFiNetwork";
const char* WIFI_PASSWORD = "YourWiFiPassword";
// wifi_config_template.h (committed to git)
const char* WIFI_SSID = "YourWiFiNetwork";
const char* WIFI_PASSWORD = "YourWiFiPassword";
Key Features Explained
1. Signal Strength Visualization
The LED provides instant visual feedback about WiFi quality:
void showSignalStrength(int rssi) {
if (rssi >= STRONG_SIGNAL) {
// Fast blink for strong signal
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
} else if (rssi >= MEDIUM_SIGNAL) {
// Medium blink for medium signal
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
} else {
// Slow blink for weak signal
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
}
2. Robust Connection Management
The system handles various WiFi states gracefully:
String getWiFiStatus() {
switch (WiFi.status()) {
case WL_CONNECTED: return "Connected";
case WL_NO_SSID_AVAIL: return "No SSID Available";
case WL_CONNECT_FAILED: return "Connection Failed";
case WL_CONNECTION_LOST: return "Connection Lost";
case WL_DISCONNECTED: return "Disconnected";
default: return "Unknown";
}
}
3. Auto-Reconnection Logic
Continuous monitoring ensures the device stays connected:
void loop() {
if (WiFi.status() == WL_CONNECTED) {
// Show signal strength
showSignalStrength(WiFi.RSSI());
} else {
// Attempt to reconnect
connectToWiFi();
}
delay(2000); // Check every 2 seconds
}
Setup Instructions
1. Hardware Requirements
- ESP32 C3 DevKitM1 board
- USB cable for programming and power
- Windows 10/11 with Arduino IDE
2. Software Setup
- Install Arduino IDE
- Add ESP32 board package URL:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
- Install “ESP32 by Espressif Systems” from Boards Manager
3. Project Configuration
- Copy
wifi_config_template.h
towifi_config.h
- Edit
wifi_config.h
with your WiFi credentials - Select board: “ESP32C3 Dev Module”
- Set port to your ESP32’s COM port
- Upload and monitor at 115200 baud
Security Considerations
Improvements Over Hardcoded Credentials
- Credentials are externalized from source code
- Template file provides clear setup instructions
- Git ignores actual credentials
- No secrets in version control
Security Limitations
- Credentials are still stored in plain text
- No encryption at rest
- No authentication or access control
- Consider this a starting point, not a complete security solution
For Production Use, Consider:
- WiFiManager for first-time setup
- Encrypted storage for sensitive data
- OTA updates for credential changes
- Device authentication and secure boot
- Network-level security measures
Troubleshooting Common Issues
Board Not Found
- Check USB connection and drivers
- Verify COM port selection
- Try different USB cable
Upload Fails
- Press and hold BOOT button during upload
- Check board settings (CPU frequency: 80MHz, Flash mode: DIO)
- Verify upload speed (115200)
WiFi Connection Fails
- Verify credentials in
wifi_config.h
- Check WiFi network availability
- Ensure 2.4GHz network (ESP32 doesn’t support 5GHz)
Serial Monitor Issues
- Set USB CDC On Boot to “Enabled”
- Check baud rate (115200)
- Try different COM port
Project Structure
esp32-wifi-config-cpp/
├── .gitignore # Git ignore rules
├── .vscode/c_cpp_properties.json # IDE configuration
├── esp32-wifi-config/
│ ├── esp32-wifi-config.ino # Main Arduino sketch
│ ├── wifi_config.h # WiFi credentials (gitignored)
│ └── wifi_config_template.h # Credentials template
├── LICENSE # MIT License
└── README.md # Documentation
Why This Approach Works
For Development
- Clean separation of concerns
- Easy to test with different credentials
- No accidental credential commits
- Clear project structure
For Field Deployment
- Simple setup process
- Visual feedback for troubleshooting
- Robust connection handling
- Easy credential updates
For Maintenance
- Clear documentation and examples
- Standard Arduino IDE workflow
- Easy credential updates
- Clean, readable code
Next Steps
This foundation can be extended with:
- WiFiManager Integration: Web-based credential setup
- OTA Updates: Remote firmware updates
- Encrypted Storage: Secure credential storage
- Device Authentication: Production-ready security
- Multiple Network Support: Failover between networks
Conclusion
Building better IoT devices doesn’t have to be complicated. This ESP32 WiFi configuration system demonstrates how to:
- Keep credentials external and manageable
- Provide visual feedback for troubleshooting
- Handle connection issues gracefully
- Maintain clean, readable code
- Deploy easily in the field
This approach improves upon hardcoded credentials while keeping things simple. It’s a good starting point for projects that need basic credential management without complex security requirements.
Ready to improve your IoT project? Start with externalizing your credentials and adding visual feedback. It’s a small change that makes a big difference in maintainability!
Full Project Details
For the complete source code, setup instructions, and examples, check out the full project:
ESP32 WiFi Configuration Project
The repository includes:
- Complete Arduino sketch with all features
- Template files for easy setup
- Detailed README with troubleshooting
- Project structure optimized for field deployment
What’s your experience with IoT credential management? Have you found better approaches for field deployment? Let me know in the comments!
This content originally appeared on DEV Community and was authored by John Ajera