Parcourir la source

Initial commit

Thomas Buck il y a 5 ans
révision
53f9393700
1 fichiers modifiés avec 113 ajouts et 0 suppressions
  1. 113
    0
      RFID-Door.ino

+ 113
- 0
RFID-Door.ino Voir le fichier

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

Chargement…
Annuler
Enregistrer