30#include <ArduinoJson.h>
41constexpr char WIFI_PASS[] =
"mrbombasticcallmefantastic";
206 unsigned long now = millis();
308void callback(
char* topic,
byte* payload,
unsigned int length) {
311 if (length == 0)
return;
313 StaticJsonDocument<512> doc;
314 DeserializationError err = deserializeJson(doc, payload, length);
318 Serial.print(
"JSON parse failed: ");
319 Serial.println(err.c_str());
326 if (strcmp(topic,
net.makeTopic(
"access/keypad_response").c_str()) == 0) {
336 Serial.println(
"Access Denied");
343 Serial.println(
"Unlocking door");
359 else if (strcmp(topic,
net.makeTopic(
"access/response").c_str()) == 0) {
361 rfidAccess = (doc[
"response"][
"hasAccess"] |
false)
366 Serial.println(
"Access Denied");
376 else if (strcmp(topic,
net.makeTopic(
"keypad/beep").c_str()) == 0) {
387 else if (strcmp(topic,
net.makeTopic(
"admin/servo_control").c_str()) == 0) {
394 Serial.println(
"Admin servo control disabled");
399 Serial.println(
"Admin servo control enabled");
430 Serial.begin(115200);
443 Serial.println(
"WiFi & MQTT ready");
448 Serial.printf(
"access/response MQTT subscribe %s\n",
449 net.subscribe(
net.makeTopic(
"access/response").c_str()) ?
"OK" :
"FAILED");
451 Serial.printf(
"access/keypad_response MQTT subscribe %s\n",
452 net.subscribe(
net.makeTopic(
"access/keypad_response").c_str()) ?
"OK" :
"FAILED");
454 Serial.printf(
"keypad/beep MQTT subscribe %s\n",
455 net.subscribe(
net.makeTopic(
"keypad/beep").c_str()) ?
"OK" :
"FAILED");
457 Serial.printf(
"admin/servo_control MQTT subscribe %s\n",
458 net.subscribe(
net.makeTopic(
"admin/servo_control").c_str()) ?
"OK" :
"FAILED");
497 const uint32_t now = millis();
500 Serial.println(
"Locking door");
Lightweight WiFi + MQTT helper wrapper for ESP-based Arduino systems.
Combined WiFi and MQTT client abstraction.
constexpr char WIFI_PASS[]
WiFi password.
constexpr char DEVICE_ID[]
Unique device identifier used in MQTT topics.
WifiMqttClient net
WiFi + MQTT client wrapper.
constexpr char MQTT_USER[]
MQTT username.
constexpr char MQTT_PASS[]
MQTT password.
AccessResult accessGranted
Result of PIN verification.
constexpr char MQTT_HOST[]
MQTT broker hostname.
AccessResult
Access result enumeration.
constexpr uint32_t UNLOCK_TIME_MS
Door unlock display duration (ms).
constexpr uint16_t MQTT_PORT
MQTT broker port.
void callback(char *topic, byte *payload, unsigned int length)
MQTT message callback handler.
constexpr char WIFI_SSID[]
WiFi SSID.
AccessResult rfidAccess
Result of RFID authentication.
void playDeniedSound()
Plays denied-access sound pattern.
void playTapSound()
Plays keypad tap sound.
constexpr uint8_t RED_PIN
GPIO pin assignments.
const unsigned int deniedTimings[]
Denied access: single long beep. Extra long to scare.
void updateBuzzer()
Advances the buzzer state machine.
BuzzerPattern currentPattern
Currently active buzzer pattern.
void playLockSound()
Plays lock sound pattern.
void setup()
Arduino setup function.
bool unlocked
Indicates whether the door is currently unlocked.
uint32_t unlockUntil
Timestamp (ms) until which the door remains unlocked.
Servo lock_servo
Servo instance controlling the lock mechanism.
const unsigned int lockTimings[]
Lock sound: long-long-long pattern.
BuzzerState buzzerState
Current buzzer state.
void playPattern(const unsigned int *timings, uint8_t length)
Starts playing a buzzer pattern.
uint8_t servoAngle
Current servo angle (derived from potentiometer in admin mode).
void stopBuzzer()
Immediately stops any active buzzer sound.
void playUnlockSound()
Plays unlock sound pattern.
constexpr uint8_t SERVO_PIN
const unsigned int * timings
#define POT_PIN
Analog pin for potentiometer (admin servo control).
void callback(char *topic, byte *payload, unsigned int length)
MQTT message callback handler.
bool servoOpen
Indicates whether the servo is currently in the open position.
bool adminServoControl
Enables direct servo control via potentiometer (admin mode).
const unsigned int beepTimings[]
Keypad tap sound: short beep.
uint8_t stepIndex
Index into the timing array.
constexpr uint8_t GREEN_PIN
BuzzerState
Buzzer state enumeration.
unsigned long lastChange
Timestamp of last buzzer state change.
const unsigned int unlockTimings[]
Unlock sound: short-short pattern.
constexpr uint8_t BUZZER_PIN
void loop()
Arduino main loop.
static void forceLock()
Forces the system into a locked idle state.
Describes a buzzer sound pattern.