Skip to main content

Overview

Production-ready door access control system where guests WhatsApp your agent to open doors. Perfect for hotels, apartments, coworking spaces, or any building requiring secure access control. What it does:
  • Guests WhatsApp to unlock doors
  • Verify guest access permissions
  • Time-window access control
  • Audit logging of all unlocks
  • Staff tools for guest registration
Hardware: Raspberry Pi 4/5, relay module, 12V electric strike or maglock APIs used: Lua WhatsApp channel + Edge API (Flask) + Lua Data API (guest management)

Architecture

Security-First Design: Always verify guest access before unlocking. This demo includes time-window validation, rate limiting, and audit logging.

Hardware Setup

Components

Raspberry Pi 4/5

Main controller running Edge API

Relay Module

3.3V-compatible, optocoupled (active-low)

Electric Strike

12V fail-secure strike or maglock

Power Supply

Separate 12V PSU for the lock

Wiring

Safety Critical:
  • Use optocoupled relay module for isolation
  • NEVER power lock from Pi (use separate 12V PSU)
  • Add flyback diode across lock coil
  • Use fail-secure locks (locked when de-energized)
  • Keep unlock pulses short (2-5 seconds)

Complete Implementation

1. Raspberry Pi Edge API

Setup

Edge API Code

Create edge_api.py:

Run Edge API

Test Edge API


2. Lua Agent Implementation

Environment Variables

Or set via CLI:

src/tools/CheckAccessTool.ts

src/tools/UnlockDoorTool.ts

src/tools/RegisterGuestTool.ts

src/index.ts

Uses LuaAgent with preprocessors for rate limiting and security validation.

2. WhatsApp Channel Setup

1

Deploy Your Agent

2

Connect WhatsApp

Select WhatsApp and provide:
  • Phone Number ID (from Meta Business Suite)
  • WhatsApp Business Account ID
  • Access Token
3

Configure Webhook

Copy the webhook URL provided by Lua CLIIn Meta Business Suite → WhatsApp → Configuration:
  • Add webhook URL
  • Subscribe to messages events
  • Verify webhook
4

Test

Send a WhatsApp message to your business number:
  • “Open front door”

WhatsApp Setup Guide

Complete WhatsApp channel setup instructions

Guest Management

Register a Guest (Staff Operation)

Use lua test to register guests:
Example inputs:
  • Phone: +14155551234
  • Name: John Doe
  • Building ID: hotel-abc
  • Doors: [“front”, “garage”]
  • Start: 1735660800000 (epoch ms for check-in)
  • End: 1735833600000 (epoch ms for check-out)
  • Status: ACTIVE
Or create via Data API directly:

Security Best Practices

Never unlock without checking permissions
Check current time is within access windowGuests table includes startAt and endAt epoch timestamps. Query ensures current time is within window.
Prevent abuse with rate limits
  • Server-side: 2-second minimum between unlocks
  • PreProcessor: Max 3 unlock requests per minute
  • Audit log: Track all attempts
Log every unlock attempt
Electrical safety is critical
  • Use separate 12V PSU for lock (never from Pi)
  • Optocoupled relay for isolation
  • Flyback diode across lock coil
  • Keep unlock pulses short (2-5s)
  • Fail-secure locks (locked when unpowered)

Alternative: Twilio WhatsApp Webhook

If using Twilio instead of Meta’s WhatsApp Business API:

src/webhooks/twilio-whatsapp.ts

Add to your agent:

Conversation Flows

Guest with Valid Access

Guest without Access

Staff Registering Guest


Production Deployment

Run Edge API on Boot

Create /etc/systemd/system/door-edge.service:
Enable:

Set Static IP for Pi

Update PI_BASE_URL to use the static IP.

Monitoring & Audit

View Unlock Logs

View Active Guests


Key Features

WhatsApp Control

Guests unlock doors via WhatsApp messages

Time-Window Access

Check-in/check-out time validation

Multi-Door Support

Control multiple doors with different pins

Audit Trail

Complete logs of all unlock attempts

Rate Limiting

Prevent abuse with preprocessor filtering

Staff Tools

Register and manage guest access

WhatsApp Best Practices

WhatsApp Rules:
  • Users must message you first (opt-in required)
  • 24-hour conversation window applies
  • Use message templates for notifications outside window
  • Respect Meta’s Business messaging policies
See complete WhatsApp guidelines: WhatsApp Channel Guide

Use Cases

Guest Room Access
  • Check-in: Register guest with room access
  • Guest WhatsApps: “Open room 305”
  • Check-out: Access automatically expires
Multi-property: Use different buildingId per location

Troubleshooting

Check:
  • Edge API is running: curl http://raspberrypi.local:5001/health
  • GPIO permissions: User in gpio group, logged back in
  • Relay wiring: Correct pins, proper power supply
  • Active-low setting: Try toggling ACTIVE_LOW
Check:
  • Guest is in database: Data.get('guests')
  • Time window is current: startAt < now < endAt
  • Status is ACTIVE
  • Building ID matches
  • Phone number format matches (E.164)
Check:
  • Agent deployed: lua deploy
  • WhatsApp channel connected: lua channels
  • Webhook verified in Meta Business Suite
  • Check logs: lua logs
Adjust:
  • Increase time window in PreProcessor (60000ms → 120000ms)
  • Increase max attempts (3 → 5)
  • Adjust server-side throttle in edge_api.py (2s → 5s)

Next Steps

View All IoT Demos

See all Raspberry Pi examples

WhatsApp Setup

Complete WhatsApp channel guide

Data API

Learn about guest data management

PreProcessor API

Learn about rate limiting and filtering