forked from mxgxw/MFRC522-python
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDoorSystem_HowTo.txt
More file actions
242 lines (174 loc) · 9.96 KB
/
DoorSystem_HowTo.txt
File metadata and controls
242 lines (174 loc) · 9.96 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
1 - Enable SPI
sudo raspi-config
2- Download MFCR522-python
git clone https://github.com/danjperron/MFRC522-python
3- Connect the reader according to the README.md of the github
4- Check if the reader is able to read a card
cd MFRC522-python
python Read.py
now approach a card to the reader
5- install mariadb mysql server
sudo apt-get update
sudo apt-get install mysql-server python-mysqldb
6- create your data base access
sudo mysql -u root -p (set root password)
create dabase rfidcardsdb and user
MariaDB [(none)]> create database rfidcardsdb;
MariaDB [(none)]> create user 'rfidreader'@localhost identified by 'password';
MariaDB [(none)]> grant all on rfidcardsdb.* to 'rfidreader' identified by 'password';
MariaDB [(none)]> exit
P.S. change password to a real password
// now set the password for rfidreader
7- create the database structure with specific fields
pi@Pi2:~/MFRC522-python $ mysql -u rfidreader -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 26
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use rfidcardsdb
Database changed
MariaDB [rfidcardsdb]> CREATE TABLE `cards` (
-> `card_id` int(11) NOT NULL AUTO_INCREMENT,
-> `serial_no` varchar(20) NOT NULL,
-> `user_id` int(11) NOT NULL,
-> `valid` tinyint(1) NOT NULL DEFAULT '0',
-> `expire` datetime NOT NULL,
-> `zones_access` int(11) NOT NULL,
-> PRIMARY KEY (`card_id`));
Query OK, 0 rows affected (3.37 sec)
8- Create user_tbl and log_tbl table
MariaDB [rfidcardsdb]> CREATE TABLE `user_tbl` (
-> `user_id` int(11) NOT NULL AUTO_INCREMENT,
-> `firstName` varchar(256) NOT NULL,
-> `lastName` varchar(256) NOT NULL,
-> `zones_access` smallint(11) NOT NULL,
-> `expired_date` datetime NOT NULL,
-> PRIMARY KEY (`user_id`));
MariaDB [rfidcardsdb]> CREATE TABLE `reader_tbl` (
-> `reader_id` int(11) NOT NULL AUTO_INCREMENT,
-> `reader_name` varchar(256) NOT NULL,
-> `enable` ttinyint(1) NOT NULL,
-> `zones_access` int(11) NOT NULL);
MariaDB [rfidcardsdb]> CREATE TABLE `log_tbl` (
-> `serial_no` varchar(20) NOT NULL,
-> `card_id` int(11) NOT NULL,
-> `date_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
-> `reader` int(11) NOT NULL,
-> `action` tinyint(4) NOT NULL);
MariaDB [rfidcardsdb]> describe cards;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| card_id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| serial_no | varchar(20) | NO | | NULL | |
| valid | tinyint(1) | NO | | 0 | |
| expire | datetime | NO | | NULL | |
| zones_access | int(11) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
MariaDB [rfidcardsdb]> describe user_tbl;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| user_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| firstName | varchar(256) | NO | | NULL | |
| lastName | varchar(256) | NO | | NULL | |
| zones_access | smallint(11) | NO | | NULL | |
| expired_date | datetime | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
MariaDB [rfidcardsdb]> describe log_tbl;
+------------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+-------------------+-----------------------------+
| serial_no | varchar(20) | NO | | NULL | |
| card_id | int(11) | NO | | NULL | |
| date_stamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| reader | int(11) | NO | | NULL | |
| action | tinyint(4) | NO | | NULL | |
+------------+-------------+------+-----+-------------------+-----------------------------+
5 rows in set (0.00 sec)
9- Read the rfid card Id of each cards and store it somewhere.
python Read.py
10- Create user
mysql -u rfidreader -p
MariaDB [(none)]> use rfidcardsdb
Database changed
MariaDB [rfidcardsdb]> describe user_tbl;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| user_id | int(11) | NO | PRI | NULL | auto_increment |
| firstName | varchar(256) | NO | | NULL | |
| lastName | varchar(256) | NO | | NULL | |
| zones_access | smallint(11) | NO | | NULL | |
| expired_date | datetime | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
create user with different user_id
dMariaDB [rfidcardsdb]> INSERT INTO user_tbl (user_id,firstName,lastName,zones_access) values("1","Daniel","Perron","1");
Query OK, 1 row affected, 1 warning (0.01 sec)
MariaDB [rfidcardsdb]> INSERT INTO user_tbl (user_id,firstName,lastName,zones_access) values("2","Claude","Tessier","1");
Query OK, 1 row affected, 1 warning (0.01 sec)
do this for all your user.
11- fill the data base
mysql -u rfidreader -p
MariaDB [(none)]> use rfidcardsdb
Database changed
MariaDB [rfidcardsdb]> describe reader_tbl;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| reader_id | int(11) | NO | PRI | NULL | auto_increment |
| reader_name | varchar(256) | NO | | NULL | |
| enable | tinyint(1) | NO | | NULL | |
| zones_access | int(11) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
MariaDB [rfidcardsdb]> INSERT INTO reader_tbl (reader_id,reader_name,enable,zones_access) values("1","reader 1","1","1");
Query OK, 1 row affected (0.01 sec)
MariaDB [rfidcardsdb]> describe cards;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| card_id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| serial_no | varchar(20) | NO | | NULL | |
| valid | tinyint(1) | NO | | 0 | |
| expire | datetime | NO | | NULL | |
| zones_access | int(11) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
MariaDB [rfidcardsdb]> INSERT INTO cards (card_id,serial_no,user_id,valid) values("1","803697F280BA04","1","1");
Query OK, 1 row affected (0.02 sec)
MariaDB [rfidcardsdb]> INSERT INTO cards (card_id,serial_no,user_id,valid) values("2","0658D9A5","2","1");
Query OK, 1 row affected (0.02 sec)
MariaDB [rfidcardsdb]> INSERT INTO cards (card_id,serial_no,user_id,valid) values("3","0658D555","3","0");
Query OK, 1 row affected (1.29 sec)
MariaDB [rfidcardsdb]> INSERT INTO cards (card_id,serial_no,user_id,valid) values("4","458E812A","4","1");
Query OK, 1 row affected (0.02 sec)
MariaDB [rfidcardsdb]> select * from cards;
+---------+---------+-----------+-------+---------------------+--------------+
| card_id | user_id | serial_no | valid | expire | zones_access |
+---------+---------+-----------+-------+---------------------+--------------+
| 1 | 1 | 2BDEFC25 | 1 | 0000-00-00 00:00:00 | 1 |
| 2 | 3 | 2BFFEC95 | 1 | 0000-00-00 00:00:00 | 3 |
| 3 | 2 | 0658D555 | 0 | 0000-00-00 00:00:00 | 2 |
| 4 | 4 | 458E812A | 1 | 0000-00-00 00:00:00 | 1 |
+---------+---------+-----------+-------+---------------------+--------------+
4 rows in set (0.01 sec)
12- Create reader table
13- Let's install pymsql for python3
sudo pip3 install PyMySQL
14 - And run DoorSystem
pi@Pi2:~/MFRC522-python $ python3 DoorSystem.py
Door System using Raspberry Pi
Press Ctrl-C to stop.
Card 1:803697F280BA04 from user Daniel Accepted!
Card ?:024CD43C from unknown Refused!
Card 3:0658D555 from user Gilbert Invalid
Card 4:458E812A from user Chantal Accepted!
Card 2:0658D9A5 from user Claude Accepted!