Delevelop alarm system based on amazing ESP-RFID Github project

I just saw this amazing GitHub project based on ESP8266 and RFID (PN532):


I am planing to use it as an alarm system. I just need to understand how to modify the code to add some leds to turn on/off when status change + use it in conjunction with Alarm control panel in Home Assistant.

Anyone has worked on this project or has an slight idea on how to acomplish this?

I’ve played with it annnnd.

It works via MQTT so you could use that unfortunately it’s a bit bare as right now it doesn’t support the ability to define your topics(dev is still trying to figure that out). It doesn’t even support IN vs OUT, its just a badge swipe to trigger a relay and fire off a message so you’d have to (re)write that. You’d probably be better off finding something else.

I know people have been asking for ESPEasy to include an RFID sensor and if it did that would be the way to go, but I haven’t seen support yet. Unless they’ve done it recently.

EDIT: ESPEasy does support 2 RFID devices (Here)[] so if you use those that would be my choice.

Be aware that rfid is not particularly secure.

So basically you are proposing to flash a NodeMCU or similar with ESEasy and edit a little bit of code to acomplish this, sounds good!

Quick question, my setup should be:

1 NodeMCU + 1 RFID + 3 leds (for status, alarm ON and alarom OFF). Then the HA will run the logic behind. Do you think this is fesable? (kind of worried with the leds part).

As long as nodemcu has enough io pins to run 3 leds as well as the rfid device, and you don’t mind programming the alarm/led stuff it should be fine.

What puzzles me is how the system knows how to arm or disarm the alarm.

1 Like

I’ve already written a sketch for this a few months ago based on a few things which I found online.
I haven’t got an alarm status led set up but I do have an led which flashes green if the card has been accepted.
If you’re interested in this, I’ll share the code tonight as I don’t have access to my computer right now.

That would be awesome! An I can try to add the other leds from there.
Thanks a lot.


You can always go with Nest:
Seems a good product, and I believe you can’t disable alarm through the API or any other means that is not “Nest native”. Only problem is he price.

Actually, this might be exactly what you are looking for:

I’m aware of this one, but it’s not working properly for me (getting stuck after some hours). So I was looking for something more developed and tested. And using something based on an existent project like ESPEasy souns good to me.

The code you said is the one you posted or is it another one? I rally would like to check this code to see if it feet my needs.

Thanks a lot!

So here is my code. As I have already said, it really is a mix of multiple sketches which I found online.

How it works:
When a card if scanned, it sends the card ID via MQTT on the home/rfid topic.
Then an automation checks if it is the correct card. If it is it disarms the alarm and sends accept to home/alarm/rfid. If the card it refused, it sends refuse to home/alarm/rfid.

The hardware:

Here is the code:


#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <MFRC522.h>

/* wiring the MFRC522 to ESP8266
Red     = D0
Green   = D1
Blue    = D2
RST     = D3
SCK     = D5
MISO    = D6
MOSI    = D7
SDA(SS) = D8
GND     = GND
3.3V    = 3.3V
#define RST_PIN  D3 // RST-PIN
#define SS_PIN  D8  // SDA-PIN 
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

//Define RGB LED pins
const int redPin = D0;
const int greenPin = D1;
const int bluePin = D2;

// Wifi Connection details
const char* ssid = "";
const char* password = "";

const char* mqtt_server = "";  // mqtt IP
const char* mqtt_user = "";  // mqtt user
const char* mqtt_password = "";  // mqtt password
const char* clientID = "RFID";  // rfid name
const char* rfid_topic = "home/rfid";  // rfid path

WiFiClient espClient;
PubSubClient client(espClient);

long lastMsg = 0;
char msg[50];

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  SPI.begin();           // Init SPI bus
  mfrc522.PCD_Init();    // Init MFRC522
  client.setServer(mqtt_server, 1883);

// Connect to Wifi
void setup_wifi() {
  //Turn off Access Point
  analogWrite(redPin, 238);
  analogWrite(greenPin, 130);
  analogWrite(bluePin, 238); 
  // We start by connecting to a WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  analogWrite(redPin, 0);
  analogWrite(greenPin, 0);
  analogWrite(bluePin, 200); 
// Check for incoming messages
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  for (int i = 0; i < length; i++) {

  char s[20];
  sprintf(s, "%s", payload);

    if ( (strcmp(topic,"home/alarm/rfid")==0))
    payload[length] = '\0';
    String sp = String((char*)payload);

  // Green LED if card accepted 
	if (sp == "accept")
    Serial.print("RFID Accepted");
    analogWrite(redPin, 0);
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 0);
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0); 
  // Red LED if card refused
    else if (sp == "refuse")
    Serial.print("RFID Refused");
    analogWrite(redPin, 255);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0); 
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0); 
/* interpret the ascii digits in[0] and in[1] as hex
* notation and convert to an integer 0..255.
int hex8(byte *in)
   char c, h;

   c = (char)in[0];

   if (c <= '9' && c >= '0') {  c -= '0'; }
   else if (c <= 'f' && c >= 'a') { c -= ('a' - 0x0a); }
   else if (c <= 'F' && c >= 'A') { c -= ('A' - 0x0a); }
   else return(-1);

   h = c;

   c = (char)in[1];

   if (c <= '9' && c >= '0') {  c -= '0'; }
   else if (c <= 'f' && c >= 'a') { c -= ('a' - 0x0a); }
   else if (c <= 'F' && c >= 'A') { c -= ('A' - 0x0a); }
   else return(-1);

   return ( h<<4 | c);
// Reconnect to wifi if connection lost
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {;
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 200); 
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(clientID, mqtt_user, mqtt_password)) {
      // Once connected, publish an announcement...
      client.publish("home/rfid", "connected");
      // ... and resubscribe
     analogWrite(redPin, 0);
     analogWrite(greenPin, 200);
     analogWrite(bluePin, 0);
     analogWrite(redPin, 0);
     analogWrite(greenPin, 0);
     analogWrite(bluePin, 0);
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

// Main functions
void loop() {
  if (!client.connected()) {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
  // Show some details of the PICC (that is: the tag/card)
  Serial.print(F("Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  //digitalWrite(idPin, HIGH);
  //digitalWrite(idPin, LOW);
  // Send data to MQTT
  String rfidUid = "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    rfidUid += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
    rfidUid += String(mfrc522.uid.uidByte[i], HEX);
  const char* id = rfidUid.c_str();
  client.publish("home/rfid", id);
// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
1 Like

And how do you turn the alarm back on?

I guess with either the same RFID or from HA alarm control panel.

Uou, didn’t knew these RGB leds even existed, very smart.

Could you also share the Home Assistant part of the code? I really would like to give it a try!


I’d love to share the Home Assistant part but I’m not at home this week so I don’t have access to the code of my HA instance. So I won’t be able to share it until next Monday.

In the meantime, I’ve tried to quickly write down my code from memory. However I’m not sure the syntax is correct.

    - platform: mqtt
      topic: home/rfid
    - condition: state
      state: 'armed_away'
    - service: mqtt.publish
        topic: "home/alarm/rfid"
        payload_template: "{% if trigger.payload == 'CARD ID' %}accept{% else %}refuse{% endif %}"
    - platform: alarm_control_panel.disarm
        entity_id: "{% if trigger.payload == 'CARD ID' %}{% endif %}"
        code: '0000'

I have a wall mounted tablet next to my RFID ready which I use to arm the alarm and to disarm it using a code.

However, it would be very easy to add an automation to arm the alarm using rfid using conditions.

Now my code is also causing this issue.
Did you find any other functional way to accomplish this ?

I believe this could be related to the device temporary losing wifi connectivity and not being able to reconnect after that.

I’ve improved my APs by upgrading to a newer model and changing the location, and seems it is kind of working now.

Anyway, I just bought this new Arduino model, with integrated ethernet. I will need to connect it through an ethernet cable, but should solve the issue:

Seems they are going to finally add status LED + Buzzer support for this amazing project!