IoT Security and Notifications
Now we move to one of the most important ESP32 topics: securing your IoT device and notifying the user when something important happens.
An insecure IoT device can be controlled by unauthorized people, expose Wi-Fi credentials, leak sensor data, or receive malicious firmware.
A secure system looks like:
SENSOR ↓ ESP32 ↓ Security Check ↓ Event Found? ↙ ↘ NO YES ↓ ↓ Continue Notification ↓ 📱 Phone
1. What is IoT Security?
IoT Security means protecting:
- ESP32
- Sensors
- Wi-Fi connection
- APIs
- Cloud services
- MQTT broker
- Firmware
- Stored credentials
- User data
For example, imagine your smart-home ESP32 controls:
💡 Light 🌀 Fan 🚪 Door 🔒 Security 📷 Camera
If someone gains unauthorized access, they might control your devices.
So we need:
Authentication + Encryption + Authorization + Secure Firmware + Secure Communication
2. Three Main Security Goals
A simple way to remember IoT security is:
🔒 Confidentiality
Only authorized people can see the data.
Example:
Temperature = 28°C
shouldn't be freely exposed to everyone.
🛡️ Integrity
Data should not be secretly modified.
Example:
Original: FAN = OFF Attacker: FAN = ON
Integrity protection helps prevent unauthorized modification.
✅ Availability
The device should remain usable.
For example:
ESP32 ↓ Wi-Fi ↓ Server
should remain available to authorized users.
These three are commonly called the:
CIA Triad
Confidentiality /\ / \ / \ / \ Integrity ───── Availability
3. Never Hard-Code Sensitive Credentials Carelessly
A beginner might write:
const char* ssid = "MyWiFi"; const char* password = "mypassword123";
This works, but if you upload the code to a public GitHub repository:
GitHub ↓ Anyone can see ↓ Wi-Fi password exposed ❌
Instead, during development, keep secrets outside publicly shared source code where practical.
For example:
secrets.h
and add it to:
.gitignore
Example:
secrets.h
4. Never Publish API Keys
Suppose you use a cloud API:
String apiKey = "ABC123SECRET";
Don't publish that code publicly.
Instead:
ESP32 ↓ Secret ↓ Private configuration
Not:
ESP32 ↓ GitHub ↓ Everyone
5. Wi-Fi Security
Your ESP32 should connect to a properly secured Wi-Fi network.
Prefer:
WPA2 or WPA3
Avoid relying on open Wi-Fi for sensitive IoT communication.
Example:
WiFi.begin(ssid, password);
The ESP32 then connects to your network.
You can check:
if (WiFi.status() == WL_CONNECTED) { Serial.println("WiFi connected"); }
6. Don't Expose ESP32 Directly to the Internet
Suppose your ESP32 has:
192.168.1.50
That's normally a private LAN address.
Your safer architecture is:
Internet │ ↓ Secure Cloud │ HTTPS/MQTT │ ↓ Router │ ↓ ESP32
Rather than:
Internet ↓ Public port ↓ ESP32
Directly exposing an embedded web server to the public Internet can create significant security risks.
7. Authentication
Authentication answers:
Who are you?
For example, your ESP32 web dashboard could require:
Username: admin Password: ********
Instead of:
http://192.168.1.50
being accessible to everyone on the network.
8. Authorization
Authentication and authorization are different.
Authentication
Who are you?
Authorization
What are you allowed to do?
Example:
User ↓ Login ↓ Authenticated ↓ Can view temperature ↓ Can control light
But perhaps:
Normal User ❌ ↓ Cannot change security settings
while:
Admin ✅ ↓ Can change security settings
9. HTTP vs HTTPS
HTTP:
http://example.com
HTTPS:
https://example.com
HTTPS encrypts communication using TLS.
Conceptually:
ESP32 ───── encrypted ───── Server
instead of:
ESP32 ───── readable ───── Server
For production IoT systems, encrypted communication is strongly preferred.
10. MQTT Security
Earlier we learned MQTT:
ESP32 ↓ MQTT Broker ↓ Phone / Dashboard
Security can be added using:
Username + Password + TLS + Access control
Example:
ESP32 │ │ TLS ↓ MQTT Broker │ ├── Smart Home ├── Mobile App └── Dashboard
11. MQTT Topics and Authorization
Suppose your ESP32 publishes:
home/temperature
and subscribes to:
home/light
A properly configured broker can restrict what each client can access.
For example:
ESP32-Camera ↓ home/camera/status ESP32-SmartHome ↓ home/light home/fan home/door
This is called access control.
12. Notifications
Now let's connect security events to notifications.
Suppose:
PIR detects movement
We can create:
PIR ↓ ESP32 ↓ Security Event ↓ Notification ↓ 📱 User
Example message:
🚨 SECURITY ALERT Motion detected! Time: 10:42 PM Device: Main Door
13. Different Notification Methods
ESP32 projects can notify users through different systems.
| Method | Example |
|---|---|
| Telegram | Security message |
| Alert email | |
| Firebase | Mobile notification |
| Blynk | App notification |
| MQTT | Dashboard alert |
| WebSocket | Real-time browser alert |
| Webhook | External service |
| Local buzzer | Immediate alarm |
| LED | Visual warning |
14. Local Alert
The simplest notification doesn't require the Internet.
Example:
PIR ↓ ESP32 ├── Buzzer 🔔 └── LED 🔴
Code:
#define PIR_PIN 13 #define BUZZER_PIN 23 #define LED_PIN 25 void setup() { pinMode(PIR_PIN, INPUT); pinMode(BUZZER_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT); } void loop() { if (digitalRead(PIR_PIN)) { digitalWrite(BUZZER_PIN, HIGH); digitalWrite(LED_PIN, HIGH); } else { digitalWrite(BUZZER_PIN, LOW); digitalWrite(LED_PIN, LOW); } }
This is a local security system.
15. Smart Security Logic
Instead of immediately triggering an alarm for every sensor change, create logic.
For example:
Security Mode ↓ ARMED? ↙ ↘ NO YES ↓ ↓ Ignore Check PIR ↓ Motion? ↙ ↘ NO YES ↓ ↓ Continue Alarm ↓ Notification
This avoids unnecessary alerts.
16. Example: Security Mode
bool securityArmed = false; void loop() { if (securityArmed && digitalRead(PIR_PIN)) { Serial.println("🚨 Motion detected!"); digitalWrite(BUZZER_PIN, HIGH); digitalWrite(LED_PIN, HIGH); } }
You could control securityArmed through your web dashboard:
SECURITY [ ARM ]
17. Avoid Notification Spam
Suppose the PIR remains HIGH for 30 seconds.
Without protection:
Motion Motion Motion Motion Motion Motion ...
You might receive hundreds of notifications.
Instead use a cooldown:
Motion detected ↓ Send notification ↓ Wait 30 seconds ↓ Allow next notification
Example:
unsigned long lastAlert = 0; const unsigned long ALERT_COOLDOWN = 30000; void checkMotion() { if (digitalRead(PIR_PIN)) { unsigned long now = millis(); if (now - lastAlert >= ALERT_COOLDOWN) { Serial.println("Motion Alert!"); lastAlert = now; } } }
This is much better than using long delay() calls.
18. ESP32 + Camera + Security
Now combine the previous topic with this one.
PIR │ ↓ Motion Event │ ↓ ESP32-CAM / \ ↓ ↓ Capture Alarm │ │ ↓ ↓ Image Buzzer │ ↓ microSD │ ↓ Wi-Fi │ ┌─────┴─────┐ ↓ ↓ Cloud Phone
This creates a practical security camera.
19. Security Event Example
Imagine someone enters your room.
10:42:10 PM ↓ PIR detects motion ↓ ESP32 confirms security = ARMED ↓ ESP32-CAM captures image ↓ Image stored on SD ↓ Buzzer ON ↓ Notification sent
Phone receives:
🚨 Security Alert Motion detected at Main Door. 📷 Image captured. 🕒 10:42 PM
20. Firmware Security
Earlier we discussed OTA.
OTA itself must be protected.
Bad:
Anyone ↓ Upload firmware ↓ ESP32 ❌
Better:
Authorized Update ↓ Authentication ↓ Firmware verification ↓ ESP32 ↓ Install
Important security mechanisms can include:
- Secure Boot
- Flash Encryption
- Signed firmware
- Authenticated OTA
- TLS
- Rollback protection
21. Secure Boot
Secure Boot helps ensure the ESP32 boots only trusted firmware.
Conceptually:
Power ON ↓ Verify firmware ↓ Valid? ↙ ↘ YES NO ↓ ↓ Boot Reject
This protects against unauthorized firmware replacement.
22. Flash Encryption
Flash memory may contain:
Wi-Fi credentials Configuration Application data Certificates
Flash encryption helps protect sensitive data stored in flash if someone obtains physical access to the device.
23. Protect Physical Hardware
IoT security isn't only software.
Someone can physically access the device.
For example:
ESP32 ↓ USB ↓ Computer
A secure production device should consider:
- Debug interfaces
- Flash contents
- Firmware extraction
- Physical reset
- Unauthorized reflashing
- Enclosure protection
For your classroom/demo projects, this is less critical. For real products, it becomes very important.
24. API Security
Suppose your ESP32 has:
GET /light/on
Don't assume that knowing the URL means everyone should be able to control it.
Better:
Request ↓ Authentication ↓ Authorization ↓ Validate request ↓ Turn light ON
Also validate inputs.
Never blindly trust:
HTTP request MQTT message Cloud data User input
25. Example Secure Architecture
A professional architecture might look like:
📱 Mobile App │ HTTPS │ ↓ ┌──────────────┐ │ Cloud / API │ └──────┬───────┘ │ MQTT/TLS │ ↓ ┌──────────────┐ │ ESP32 │ └──────┬───────┘ │ ┌─────────┼─────────┐ ↓ ↓ ↓ PIR DHT22 Relay │ ↓ Security Event │ ↓ Notification
26. Practical Smart Home Security System
For your IoT ZONE project, you can build:
IoT ZONE │ ┌──────────────┼──────────────┐ ↓ ↓ ↓ PIR RFID LDR │ │ │ └──────────────┼──────────────┘ ↓ ESP32 │ ┌───────────────┼────────────────┐ ↓ ↓ ↓ Relay Servo ESP32-CAM │ │ │ Light/Fan Door Camera │ ↓ Image │ ↓ Notification
This is a very good IoT + cybersecurity demonstration project.
27. Security Levels
You can think of IoT security in levels.
Level 1 — Beginner
Wi-Fi password + Local authentication
Level 2 — Intermediate
HTTPS + MQTT authentication + Access control
Level 3 — Advanced
TLS + Secure Boot + Flash Encryption + Signed Firmware + Secure OTA + Certificate management
28. Common IoT Security Mistakes
❌ 1. Hard-coded passwords
password = "12345678";
and then uploading it publicly.
❌ 2. No authentication
Anyone on the LAN can control:
/light/on
❌ 3. Publicly exposing ESP32
Avoid directly forwarding ESP32 ports to the Internet unless you have a carefully designed security architecture.
❌ 4. Using HTTP for sensitive data
Prefer encrypted communication for sensitive production systems.
❌ 5. Unlimited notifications
One sensor event can generate:
1000 notifications ❌
Use cooldown/debouncing/state logic.
❌ 6. Trusting sensor input blindly
Sensor:
HIGH
doesn't automatically mean:
REAL INTRUSION
Combine logic and, where appropriate, multiple sensors.
❌ 7. Ignoring OTA security
An unsecured OTA mechanism can become an attack path.
🧠 Quick Summary
Remember:
IoT Security = Authentication + Authorization + Encryption + Secure Firmware + Secure Updates
Notifications:
Sensor ↓ ESP32 ↓ Event Detection ↓ Alert ↓ 📱 User
Security camera:
PIR ↓ ESP32-CAM ↓ Capture ↓ SD / Cloud ↓ Notification
For a production system:
HTTPS/TLS MQTT/TLS Secure OTA Secure Boot Flash Encryption Strong Credentials Access Control