Back to projects
Jeff G. Njoroge
Jeff G. Njoroge

ESP32 Presence Detector + Telegram Alerts

Year: 2024
ESP32Embedded C++ (Arduino)Wi‑Fi IoTTelegram Bot APIGPIO interruptsNTP time sync

This project is an embedded presence/security notifier built for small spaces (home office, lab room, storage area) where you want simple, low-latency awareness without running a dedicated server. It runs on an ESP32 and monitors common sensors (a door contact switch and a PIR motion sensor), then sends push-style alerts to a Telegram chat over Wi‑Fi. Control commands are restricted to a single pre-authorized Telegram chat/user, and messages from other senders are ignored.

The core user workflow is straightforward: power the device, connect it to Wi‑Fi, and receive alerts when motion is detected or when a door opens/closes. For day-to-day control, the system supports a lightweight “monitoring on/off” mode that can be toggled remotely via Telegram commands, allowing you to suppress notifications when you’re present.

Under the hood, the firmware uses a secure TLS client to call the Telegram Bot API, and (for door events) syncs time via NTP so notifications can include human-readable timestamps. The design is intentionally minimal—no cloud backend, no database—just sensors → microcontroller logic → secure outbound message to a chat destination.

Key features:

  • Real-time door open/close alerts (state-change based)
  • Real-time motion alerts using an interrupt-driven PIR signal
  • Remote monitoring enable/disable and status commands via Telegram, restricted to a single authorized chat/user
  • Timestamped notifications via NTP time synchronization
  • Secure outbound communication using TLS

How it works (high-level):

  • ESP32 boots, connects to Wi‑Fi, and initializes a secure client for Telegram API calls
  • For door monitoring, the firmware continuously samples the door contact state and triggers on state transitions (open ↔ close)
  • For motion monitoring, the PIR sensor triggers an interrupt that sets a flag, which the main loop turns into a Telegram message
  • A simple command loop polls Telegram for messages and updates the system mode (monitoring enabled/disabled) and status responses
  • For timestamping, the device syncs its clock using NTP and formats the current time when generating door-event notifications

Notes/assumptions:

  • Assumes reliable Wi‑Fi connectivity; if the network drops, alerts may be delayed or missed until reconnect
  • Assumes Telegram is the only notification channel; there’s no secondary local alarm or offline queue by default
  • Assumes a single authorized chat/user is configured (commands from other chat IDs are ignored); multi-user roles and audit logs are out of scope
  • Sensor noise (e.g., contact bounce / PIR false positives) may require additional debouncing/cooldown logic depending on the physical install