Week 9

Internet of Things (IoT)

Connecting physical objects to the digital world: ESP32-CAM streaming, sensor triggers, and SMS gateways.

The Objective

IoT is simply "stuff connected to other stuff." For this assignment, I wanted to build a bedroom security system to monitor my room while I'm away (a necessary countermeasure against siblings).

Part I: ESP32-CAM

Video Streaming Server

The ESP32-CAM is a powerful module, but it lacks a USB port. To program it, I had to use an FTDI programmer (USB-to-TTL serial converter).

After following the Random Nerd Tutorials guide, I successfully flashed the board to act as a local web server, streaming video over my home WiFi.

Setup 1 Setup 2 Pinout FTDI

Part II: The Trigger

Ultrasonic Door Sensor

I attempted to use Pyro (Motion) sensors, but they were buggy. I pivoted to an HC-SR04 Ultrasonic Sensor mounted facing the door.

Logic: IF (Distance < Closed_Door_Distance) THEN (Door is Open -> Trigger Alarm)

Sensor Front Sensor Back

Simulating door opening with hand

Part III: The Cloud Alert

SMTP to SMS Gateway

Email-to-SMS Gateway

Since setting up a secure global web server was complex, I found a workaround. ESP32 sends an email via SMTP to a carrier gateway (e.g., number@vtext.com for Verizon), which automatically forwards the email as a text message to my phone.

This creates a real-time alert system without needing a dedicated mobile app or complex port forwarding.

ESP32 SMTP Logic

#include <ESP_Mail_Client.h>
#include <WiFi.h>

#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define RECIPIENT_EMAIL "xxxxxxxxxx@vtext.com" // SMS Gateway

SMTPSession smtp;

void loop() {
  // Ultrasonic Logic
  digitalWrite(trigPin, LOW); delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distanceCm = duration * 0.034 / 2;

  // If door opens (distance drops)
  if (distanceCm < 10) {
    digitalWrite(LED, HIGH);
    
    // Configure Email Session
    ESP_Mail_Session session;
    session.server.host_name = SMTP_HOST;
    session.server.port = SMTP_PORT;
    session.login.email = AUTHOR_EMAIL;
    session.login.password = AUTHOR_PASSWORD;

    // Construct Message
    SMTP_Message message;
    message.sender.name = "Bedroom Security";
    message.subject = "Intruder Alert!";
    message.addRecipient("Mustafa", RECIPIENT_EMAIL);

    String textMsg = "Alert! Sensor triggered. View Cam: " + CAM_URL;
    message.text.content = textMsg.c_str();

    // Send
    if (!smtp.connect(&session)) return;
    MailClient.sendMail(&smtp, &message);
  }
}
                

Final System Test

*Dramatic reenactment of security breach.