ESP8266 Door Access Control System
Loading...
Searching...
No Matches
ESP1 - RFID & Motion

Enumerations

enum class  AccessResult : uint8_t { AccessResult::Denied , AccessResult::Granted , AccessResult::Denied , AccessResult::Granted }
 Access result enumeration. More...

Functions

MFRC522 mfrc522 (RFID_SS_PIN, RFID_RST_PIN)
 MFRC522 RFID reader instance.
LiquidCrystal_I2C lcd (LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS)
 I2C LCD instance.
void lcdPrintLine (const __FlashStringHelper *msg, uint8_t line)
 Clears an LCD line and prints a flash-resident string.
void lcdPrintLine (const char *msg, uint8_t line)
 Clears an LCD line and prints a RAM-resident string.
static void forceLock ()
 Forces the system back into locked idle state.
void makeEnteredPins (uint8_t pinLength)
 Builds a masked PIN string for LCD display.
void callback (char *topic, byte *payload, unsigned int length)
 MQTT message callback handler.
bool isDisplayActive (uint32_t now)
 Checks whether the LCD backlight should remain active.
void onMotionDetected (uint32_t now)
 Handles motion-detected events.
void onMotionIdle (uint32_t now)
 Handles motion-idle state.
void updateMotionState (uint32_t now)
 Updates motion state based on PIR sensor input.
void uidToHexString (const MFRC522::Uid &uid, char *output, size_t outputSize)
 Converts an RFID UID to a hexadecimal string.
void handleRFID ()
 Handles RFID card detection and request publishing.
void setup ()
 Arduino setup function.
void loop ()
 Arduino main loop.

Variables

WifiMqttClient net
 WiFi + MQTT client wrapper.
constexpr char WIFI_SSID [] = "Mathias2.4"
 WiFi SSID.
constexpr char WIFI_PASS [] = "mrbombasticcallmefantastic"
 WiFi password.
constexpr char MQTT_HOST [] = "maqiatto.com"
 MQTT broker hostname.
constexpr uint16_t MQTT_PORT = 1883
 MQTT broker port.
constexpr char MQTT_USER [] = "hectorfoss@gmail.com"
 MQTT username.
constexpr char MQTT_PASS [] = "potter"
 MQTT password.
constexpr char DEVICE_ID [] = "door1"
 Unique device identifier used in MQTT topics.
constexpr uint8_t RFID_SS_PIN = 15
 RC522 SPI Slave Select pin (D8 / GPIO15).
constexpr uint8_t RFID_RST_PIN = 16
 RC522 Reset pin (D0 / GPIO16).
constexpr uint8_t MOTION_PIN = 0
 Motion sensor pin (D3 / GPIO0).
constexpr uint8_t I2C_SDA_PIN = 4
 I2C SDA pin for LCD (D2 / GPIO4).
constexpr uint8_t I2C_SCL_PIN = 5
 I2C SCL pin for LCD (D1 / GPIO5).
constexpr uint8_t LCD_COLUMNS = 16
 Number of LCD columns.
constexpr uint8_t LCD_ROWS = 2
 Number of LCD rows.
constexpr uint8_t LCD_ADDRESS = 0x27
 I2C address of the LCD module.
constexpr uint32_t DISPLAY_MS = 3000
 Duration (ms) to display text before resetting.
constexpr uint32_t DISPLAY_BACKLIGHT_MS = 5000
 Duration (ms) to keep LCD backlight on after motion.
constexpr uint32_t PIN_TIME_MS = 15000
 Time window (ms) for entering PIN after RFID success.
constexpr uint32_t UNLOCK_TIME_MS = 5000
 Door unlock display duration (ms).
constexpr uint32_t POLL_MS = 30
 Main loop polling delay (ms).
AccessResult rfidAccess
 Result of RFID authentication.
AccessResult accessGranted
 Result of PIN verification.
bool textshown = false
 Indicates whether a status message is currently displayed.
uint32_t showTextUntil = 0
 Timestamp (ms) when the displayed text should expire.
uint32_t showDisplayUntil = 0
 Timestamp (ms) when LCD backlight should turn off.
bool motionActive = false
 Indicates whether motion is currently considered active.
static char enteredPins [5] = " "
 Masked PIN buffer shown on LCD (max 4 digits + null terminator).

Detailed Description

This firmware runs on an ESP-based Arduino-compatible board and implements the first stage of a distributed door access control system.

Hardware components:

  • MFRC522 RFID reader (RC522)
  • I2C LCD display
  • PIR motion sensor

Functional responsibilities:

  • Detects motion to manage LCD backlight and user interaction
  • Reads RFID tags and initiates authentication
  • Displays system and authentication status on the LCD
  • Communicates authentication requests and results via MQTT

RFID authentication must succeed before the keypad stage is enabled.

Enumeration Type Documentation

◆ AccessResult

enum class AccessResult : uint8_t
strong

Access result enumeration.

Enumerator
Denied 

Access denied

Granted 

Access granted

Denied 

Access denied

Granted 

Access granted

Definition at line 130 of file esp1_rfid_motion.ino.

Function Documentation

◆ callback()

void callback ( char * topic,
byte * payload,
unsigned int length )

MQTT message callback handler.

Parses incoming JSON messages and dispatches logic based on topic:

  • RFID access decision
  • Keypad PIN validation result
  • Keypad keypress feedback
Parameters
topicMQTT topic string.
payloadRaw payload bytes.
lengthPayload length.

Definition at line 240 of file esp1_rfid_motion.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ forceLock()

void forceLock ( )
static

Forces the system back into locked idle state.

Resets access state, clears temporary flags, and restores the default idle LCD message.

Definition at line 196 of file esp1_rfid_motion.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleRFID()

void handleRFID ( )

Handles RFID card detection and request publishing.

Detects new cards, reads UID, updates LCD, and sends an MQTT access request.

Definition at line 440 of file esp1_rfid_motion.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isDisplayActive()

bool isDisplayActive ( uint32_t now)

Checks whether the LCD backlight should remain active.

Uses signed time comparison to safely handle millis() wraparound.

Parameters
nowCurrent time in milliseconds.
Returns
true if display is active, false otherwise.

Definition at line 353 of file esp1_rfid_motion.ino.

Here is the caller graph for this function:

◆ lcd()

LiquidCrystal_I2C lcd ( LCD_ADDRESS ,
LCD_COLUMNS ,
LCD_ROWS  )

I2C LCD instance.

Here is the caller graph for this function:

◆ lcdPrintLine() [1/2]

void lcdPrintLine ( const __FlashStringHelper * msg,
uint8_t line )

Clears an LCD line and prints a flash-resident string.

Parameters
msgFlash string (use F("...")).
lineLCD row index.

Definition at line 170 of file esp1_rfid_motion.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lcdPrintLine() [2/2]

void lcdPrintLine ( const char * msg,
uint8_t line )

Clears an LCD line and prints a RAM-resident string.

Parameters
msgC-string to print.
lineLCD row index.

Definition at line 183 of file esp1_rfid_motion.ino.

Here is the call graph for this function:

◆ loop()

void loop ( )

Arduino main loop.

Executes non-blocking system logic:

  • MQTT client processing
  • Display timeout handling
  • Motion-based backlight control
  • RFID polling when active

Definition at line 536 of file esp1_rfid_motion.ino.

Here is the call graph for this function:

◆ makeEnteredPins()

void makeEnteredPins ( uint8_t pinLength)

Builds a masked PIN string for LCD display.

Converts a numeric PIN length into a visual representation using '*' characters and trailing spaces.

Parameters
pinLengthNumber of digits entered so far.

Definition at line 211 of file esp1_rfid_motion.ino.

Here is the caller graph for this function:

◆ mfrc522()

MFRC522 mfrc522 ( RFID_SS_PIN ,
RFID_RST_PIN  )

MFRC522 RFID reader instance.

Here is the caller graph for this function:

◆ onMotionDetected()

void onMotionDetected ( uint32_t now)

Handles motion-detected events.

Activates LCD backlight and refreshes display timeout.

Parameters
nowCurrent time in milliseconds.

Definition at line 364 of file esp1_rfid_motion.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onMotionIdle()

void onMotionIdle ( uint32_t now)

Handles motion-idle state.

Turns off LCD backlight when motion has ceased and the timeout has expired.

Parameters
nowCurrent time in milliseconds.

Definition at line 383 of file esp1_rfid_motion.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup()

void setup ( )

Arduino setup function.

Initializes hardware peripherals, LCD, WiFi, MQTT client, and topic subscriptions.

Definition at line 481 of file esp1_rfid_motion.ino.

Here is the call graph for this function:

◆ uidToHexString()

void uidToHexString ( const MFRC522::Uid & uid,
char * output,
size_t outputSize )

Converts an RFID UID to a hexadecimal string.

Each UID byte is encoded as two uppercase hexadecimal characters.

Parameters
uidMFRC522 UID structure.
outputDestination buffer.
outputSizeSize of destination buffer.

Definition at line 416 of file esp1_rfid_motion.ino.

Here is the caller graph for this function:

◆ updateMotionState()

void updateMotionState ( uint32_t now)

Updates motion state based on PIR sensor input.

Dispatches to motion-detected or motion-idle handlers.

Parameters
nowCurrent time in milliseconds.

Definition at line 397 of file esp1_rfid_motion.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ accessGranted

AccessResult accessGranted

Result of PIN verification.

Definition at line 139 of file esp1_rfid_motion.ino.

◆ DEVICE_ID

char DEVICE_ID[] = "door1"
constexpr

Unique device identifier used in MQTT topics.

Definition at line 60 of file esp1_rfid_motion.ino.

◆ DISPLAY_BACKLIGHT_MS

uint32_t DISPLAY_BACKLIGHT_MS = 5000
constexpr

Duration (ms) to keep LCD backlight on after motion.

Definition at line 102 of file esp1_rfid_motion.ino.

◆ DISPLAY_MS

uint32_t DISPLAY_MS = 3000
constexpr

Duration (ms) to display text before resetting.

Definition at line 99 of file esp1_rfid_motion.ino.

◆ enteredPins

char enteredPins[5] = " "
static

Masked PIN buffer shown on LCD (max 4 digits + null terminator).

Filled with '*' for entered digits and spaces for remaining slots.

Definition at line 158 of file esp1_rfid_motion.ino.

◆ I2C_SCL_PIN

uint8_t I2C_SCL_PIN = 5
constexpr

I2C SCL pin for LCD (D1 / GPIO5).

Definition at line 79 of file esp1_rfid_motion.ino.

◆ I2C_SDA_PIN

uint8_t I2C_SDA_PIN = 4
constexpr

I2C SDA pin for LCD (D2 / GPIO4).

Definition at line 76 of file esp1_rfid_motion.ino.

◆ LCD_ADDRESS

uint8_t LCD_ADDRESS = 0x27
constexpr

I2C address of the LCD module.

Definition at line 92 of file esp1_rfid_motion.ino.

◆ LCD_COLUMNS

uint8_t LCD_COLUMNS = 16
constexpr

Number of LCD columns.

Definition at line 86 of file esp1_rfid_motion.ino.

◆ LCD_ROWS

uint8_t LCD_ROWS = 2
constexpr

Number of LCD rows.

Definition at line 89 of file esp1_rfid_motion.ino.

◆ MOTION_PIN

uint8_t MOTION_PIN = 0
constexpr

Motion sensor pin (D3 / GPIO0).

Definition at line 73 of file esp1_rfid_motion.ino.

◆ motionActive

bool motionActive = false

Indicates whether motion is currently considered active.

Definition at line 151 of file esp1_rfid_motion.ino.

◆ MQTT_HOST

char MQTT_HOST[] = "maqiatto.com"
constexpr

MQTT broker hostname.

Definition at line 51 of file esp1_rfid_motion.ino.

◆ MQTT_PASS

char MQTT_PASS[] = "potter"
constexpr

MQTT password.

Definition at line 57 of file esp1_rfid_motion.ino.

◆ MQTT_PORT

uint16_t MQTT_PORT = 1883
constexpr

MQTT broker port.

Definition at line 53 of file esp1_rfid_motion.ino.

◆ MQTT_USER

char MQTT_USER[] = "hectorfoss@gmail.com"
constexpr

MQTT username.

Definition at line 55 of file esp1_rfid_motion.ino.

◆ net

WiFi + MQTT client wrapper.

Definition at line 43 of file esp1_rfid_motion.ino.

◆ PIN_TIME_MS

uint32_t PIN_TIME_MS = 15000
constexpr

Time window (ms) for entering PIN after RFID success.

Definition at line 105 of file esp1_rfid_motion.ino.

◆ POLL_MS

uint32_t POLL_MS = 30
constexpr

Main loop polling delay (ms).

Definition at line 111 of file esp1_rfid_motion.ino.

◆ RFID_RST_PIN

uint8_t RFID_RST_PIN = 16
constexpr

RC522 Reset pin (D0 / GPIO16).

Definition at line 70 of file esp1_rfid_motion.ino.

◆ RFID_SS_PIN

uint8_t RFID_SS_PIN = 15
constexpr

RC522 SPI Slave Select pin (D8 / GPIO15).

Definition at line 67 of file esp1_rfid_motion.ino.

◆ rfidAccess

AccessResult rfidAccess

Result of RFID authentication.

Definition at line 136 of file esp1_rfid_motion.ino.

◆ showDisplayUntil

uint32_t showDisplayUntil = 0

Timestamp (ms) when LCD backlight should turn off.

Definition at line 148 of file esp1_rfid_motion.ino.

◆ showTextUntil

uint32_t showTextUntil = 0

Timestamp (ms) when the displayed text should expire.

Definition at line 145 of file esp1_rfid_motion.ino.

◆ textshown

bool textshown = false

Indicates whether a status message is currently displayed.

Definition at line 142 of file esp1_rfid_motion.ino.

◆ UNLOCK_TIME_MS

uint32_t UNLOCK_TIME_MS = 5000
constexpr

Door unlock display duration (ms).

Definition at line 108 of file esp1_rfid_motion.ino.

◆ WIFI_PASS

char WIFI_PASS[] = "mrbombasticcallmefantastic"
constexpr

WiFi password.

Definition at line 48 of file esp1_rfid_motion.ino.

◆ WIFI_SSID

char WIFI_SSID[] = "Mathias2.4"
constexpr

WiFi SSID.

Definition at line 46 of file esp1_rfid_motion.ino.