forked from crserran/home-alarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc522_sensor.h
74 lines (64 loc) · 2.62 KB
/
rc522_sensor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "esphome.h"
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 22 // Configurable, see typical pin layout above
#define SS_1_PIN 21 // Configurable, take a unused pin, only HIGH/LOW required
#define LED_PIN 12 // Configurable
MFRC522 mfrc522; // Create MFRC522 instance.
unsigned long tempo2 = 0;
class RFIDRC522Sensor : public Component, public CustomMQTTDevice {
public:
TextSensor *rfid = new TextSensor();
TextSensor *rfidlast = new TextSensor();
std:: string idnumberstd = "";
std:: string last_cardstd= "";
void setup() override {
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(SS_1_PIN, RST_PIN); // Init each MFRC522 card
pinMode(LED_PIN, OUTPUT);
subscribe("esphome/safe_mode", &RFIDRC522Sensor::on_message);
}
void loop() override {
// Look for new cards
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size, mfrc522.PICC_GetTypeName(piccType)); //llama a la funcion que trabaja con nuestra tarjeta
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}
if (millis() > tempo2 && tempo2 != 0){
//Borramos tarjeta y publicamos (vacio)
idnumberstd.clear();
rfid->publish_state(idnumberstd);
tempo2 = 0;
}
}
void on_message(const std::string &payload) {
if (payload == "ON") {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
void dump_byte_array(byte *ID, byte IDSize, String picc) {
//le damos formato al numero de de la tarjeta leido
String idnumber = "";
for(byte i=0; i < IDSize; i++) {
if(ID[i] < 0x10) { //Añadimos el 0 de la izquierda en cada byte (si lo tuviera).
idnumber += "0";
}
idnumber += String (ID[i], HEX);
}
idnumberstd = idnumber.c_str(); //pasamos a variable standard para que funcione en ESPHome y podamos mandarla
//creamos los datos de la ultima tarjeta leida
String last_card = (" ID card: " + idnumber + " PICC type: " + picc);
last_cardstd = last_card.c_str(); //pasamos a variable standard para que funcione en ESPHome y podamos mandarla
//Publicamos la tarjeta leida
rfid->publish_state(idnumberstd);
rfidlast->publish_state(last_cardstd);
//Activamos temporizador de borrado
tempo2 = millis() + 1000;
}
};