Christopher Mendez Martinez
Published © GPL3+

RFID Smart Door Lock With ESP8266

In this video I am going to show you how to build your own access control system with RFID and Arduino + Cloud logging of attendance.

IntermediateFull instructions provided5 hours3,466
RFID Smart Door Lock With ESP8266

Things used in this project

Hardware components

DigitSpace LCD 16x02 I2C
×1
DigitSpace RFID Reader
×1
DigitSpace nodeMCU v3
×1
DigitSpace RGB LED
×1
DigitSpace LM7805
×1

Software apps and online services

Arduino IDE
Arduino IDE
Ubidots
Ubidots

Story

Read more

Custom parts and enclosures

Enclosures

Schematics

Schematics:

Code

RFID_AccessUbidotsI2C.ino

Processing
/*
    Creado por MCMCHRIS Enero 2020
*/

#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include "Ubidots.h"

#define SS_PIN D4  //D4
#define RST_PIN D3 //D3
#define lock 1 //tx
#define G D1 //D1
#define R D0 //D0
#define bl D8 //D8


const char* UBIDOTS_TOKEN = "..................";  // Ubidots Token
const char* WIFI_SSID = "............";      // SSID de tu WiFi
const char* WIFI_PASS = "............";      // Clave de tu WiFi
const char* DEVICE_LABEL = "rfid";      // Nombre del dispositivo

Ubidots ubidots(UBIDOTS_TOKEN, UBI_UDP);

// Setear el Tipo de LCD, 16x2 en este caso
LiquidCrystal_I2C lcd(0x27, 16, 2);

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Crear instancia para sensor RFID MFRC522

const int cantidad = 4; // Elegir la cantidad de llaves con Acceso.

/////////Tarjetas con Acceso/////////////

String pases[cantidad] = {"49 91 99 99","A9 E4 D6 A3", "39 3B 55 B8", "47 4E 87 62"}; //Codigos de tus tarjetas RFID que tendran acceso a entrar.
String nombres[cantidad] = {"  CHRISTOPHER","     OLIVER", "     BRYAN", "    CARIDAD"}; //Modificar los nombres a tu gusto.

/////////////////////////////////////////

int acceso = 0;
int out = 0;
String llave;
int x= -1 ;



/****************************************
   Funciones principales
 ****************************************/

void setup()
{
  ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);
  Wire.begin(D2, 10); // Iniciar I2C
  SPI.begin();      // Iniciar SPI
  mfrc522.PCD_Init();   // Iniciar MFRC522
  pinMode(R, OUTPUT);
  digitalWrite(R, HIGH);
  pinMode(G, OUTPUT);
  digitalWrite(G, HIGH);
  pinMode(lock, OUTPUT);
  digitalWrite(lock, HIGH);
  pinMode(bl, OUTPUT);
  lcd.begin();
  lcd.setCursor(0, 0);
  lcd.print(" MCMCHRIS ROOM");
  //Serial.begin(115200);
}
void loop()
{ 
  lcd.setCursor(0, 0);
  lcd.print(" MCMCHRIS ROOM");
  lcd.setCursor(0, 1);
  lcd.print(" MUESTRE LLAVE");
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  //Show UID on serial monitor
  //Serial.println();
  //Serial.print(" UID tag :");
  String content = "";
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    //Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    //Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  //Serial.println();

  llave = content.substring(1);
  
  verifico();

  if (acceso==1) 
  {
    entra();
    registro();
    acceso=0;
  }

  else   {
    noentra();
    acceso=0;
  }
}

void entra() {
  //Serial.println("Aceptado");
  lcd.clear();
  digitalWrite(G, LOW);
  digitalWrite(lock, LOW);
  digitalWrite(bl, HIGH);
  lcd.setCursor(0, 0);
  lcd.print("  BIENVENIDO:");
  lcd.setCursor(0, 1);
  lcd.print(nombres[x]);
  delay(2000); 
  digitalWrite(G, HIGH);
  digitalWrite(lock, HIGH);
  digitalWrite(bl, LOW);
  lcd.clear();
}

void noentra() {
  //Serial.println("No va a entrar");
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print("  DESCONOCIDO");
  digitalWrite(bl, HIGH);
  digitalWrite(R, LOW);
  delay(2000);
  digitalWrite(R, HIGH);
  digitalWrite(bl, LOW);
  lcd.clear();

}

void verifico() {
  for (int y = 0; y <cantidad; y++) {
    //Serial.println(y);
    if (llave == pases[y]) {
      //Serial.println(llave);
      //Serial.println(pases[y]);
      acceso = 1;
      x = y;
    }
  }
}

void registro() {
  
  //Serial.println("Registrando");
  
  char str[11];
  
  nombres[x].toCharArray(str, sizeof(nombres[x]));

  
  ubidots.addContext("Entr", str);

  char* context = (char*)malloc(sizeof(char) * 60);

  /* Estructura el contexto para ser enviado */
  ubidots.getContext(context);

  /* Envia la variable con su contexto */
  ubidots.add("asistencia", 1, context);  // Cambiar con el nombre de tu variable

  bool bufferSent = false;
  bufferSent = ubidots.send(DEVICE_LABEL);  // Enviar el dato al DEVICE_LABEL declarado.
  free(context);
}

Credits

Christopher Mendez Martinez

Christopher Mendez Martinez

35 projects • 68 followers
Electronic Engineer and Tech YouTuber who loves automation, programming and sharing his knowledge with everyone.

Comments