-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfid_reader.py
49 lines (38 loc) · 1.27 KB
/
rfid_reader.py
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
###############################################################################
#
# File: rfid_reader.py
#
# Author: Isaac Ingram
#
# Purpose: Run the NFC reader. This should be run as a standalone script as it
# runs in its own loop and publishes user data to MQTT
#
###############################################################################
import platform
import paho.mqtt.client as mqtt
import database
USE_RFID = False
MQTT_BROKER_ADDRESS = "localhost"
MQTT_BROKER_PORT = 1883
# Check if the NFC reader can be used at all
if platform.system() == "Linux" and "rpi" in platform.uname().release:
USE_RFID = True
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
def main():
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqtt_client.connect(MQTT_BROKER_ADDRESS, port=MQTT_BROKER_PORT)
# Check if RFID can be used
if USE_RFID:
reader = SimpleMFRC522()
try:
while True:
tag_uid, _ = reader.read()
user = database.get_user(user_token=tag_uid)
mqtt_client.publish("rfid/user", str(user))
except KeyboardInterrupt:
print("Control+C pressed, exiting program")
finally:
GPIO.cleanup()
if __name__ == '__main__':
main()