Arduino RFID door lock with UART RFID antenna
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RFID-Door.ino 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Very simple RFID Door Opener
  3. Input: RFID Antenna, outputs TLL UART level, 9600 8N1, 5bytes ID in binary
  4. Input: pushbutton to activate output
  5. Output: Relais connected to Solenoid controlling door lock
  6. */
  7. #include <SoftwareSerial.h>
  8. #define DOOR_OPEN LOW /* when correct id transmitted */
  9. #define DOOR_CLOSED HIGH /* idle or after timeout */
  10. #define DOOR_OPEN_TIME (2 * 1000) /* how long to keep door open with rfid tag */
  11. #define DOOR_OPEN_TIME_MANUAL (1 * 1000) /* how long to keep door open with button */
  12. #define BUFFER_CLEAR_TIME 250 /* bigger than time between serial input characters */
  13. #define ID_LENGTH 5 /* length of ID to read and compare */
  14. #define BUFFER_LEN (2 * ID_LENGTH) /* slightly longer than ID, just in case */
  15. // pin assignments
  16. static const int rfid_rx = 5;
  17. static const int door_out = 6;
  18. static const int button_in = 7;
  19. // known RFID ID codes
  20. static const unsigned char known_codes[][ID_LENGTH] = {
  21. { 0x00, 0x00, 0x00, 0x00, 0x00 },
  22. { 0x00, 0x00, 0x00, 0x00, 0x00 }
  23. };
  24. unsigned char read_buffer[BUFFER_LEN];
  25. int buffer_pos = 0;
  26. unsigned long door_time = 0;
  27. unsigned long manual_time = 0;
  28. unsigned long buffer_time = 0;
  29. SoftwareSerial rfid_serial(rfid_rx, LED_BUILTIN);
  30. void setup() {
  31. pinMode(rfid_rx, INPUT);
  32. pinMode(button_in, INPUT);
  33. pinMode(door_out, OUTPUT);
  34. digitalWrite(door_out, DOOR_CLOSED);
  35. digitalWrite(button_in, HIGH); // enable pull-up
  36. rfid_serial.begin(9600);
  37. rfid_serial.println("RFID Door initialized");
  38. Serial.begin(115200);
  39. Serial.println("RFID Door initialized");
  40. }
  41. void loop() {
  42. // read from RFID antenna
  43. if (rfid_serial.available()) {
  44. unsigned char one = rfid_serial.read();
  45. read_buffer[buffer_pos] = one;
  46. if (buffer_pos < (BUFFER_LEN - 1)) {
  47. buffer_pos++;
  48. } else {
  49. buffer_pos = 0; // reset on overflow
  50. return;
  51. }
  52. // dump to USB so we can add new codes later if we need to
  53. Serial.print("Got byte: ");
  54. Serial.println(one, HEX);
  55. // store time of last received byte
  56. buffer_time = millis();
  57. // check for matching ID
  58. if (buffer_pos >= ID_LENGTH) {
  59. for (int o = 0; o < (buffer_pos - ID_LENGTH); o++) {
  60. for (int c = 0; c < (sizeof(known_codes) / sizeof(known_codes[0])); c++) {
  61. int match = 1;
  62. for (int i = 0; i < ID_LENGTH; i++) {
  63. if (read_buffer[o + i] != known_codes[c][i]) {
  64. match = 0;
  65. break;
  66. }
  67. }
  68. if (match) {
  69. digitalWrite(door_out, DOOR_OPEN);
  70. door_time = millis();
  71. buffer_pos = 0;
  72. buffer_time = 0;
  73. return;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. if (digitalRead(button_in) == LOW) {
  80. digitalWrite(door_out, DOOR_OPEN);
  81. manual_time = millis();
  82. door_time = 0;
  83. }
  84. // if enough time without data, clear buffer
  85. if ((buffer_time != 0) && ((millis() - buffer_time) >= BUFFER_CLEAR_TIME)) {
  86. buffer_pos = 0;
  87. buffer_time = 0;
  88. }
  89. // if enough time since door opened, close it
  90. if (((door_time != 0) && ((millis() - door_time) >= DOOR_OPEN_TIME))
  91. || ((manual_time != 0) && ((millis() - manual_time) >= DOOR_OPEN_TIME_MANUAL))) {
  92. digitalWrite(door_out, DOOR_CLOSED);
  93. door_time = 0;
  94. manual_time = 0;
  95. }
  96. }