-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrfid_read.py
executable file
·42 lines (39 loc) · 1.3 KB
/
rfid_read.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
'''
This module will detect the RFID, decode and send
RFID data to controller.
This only runs on Raspberry pi.
Hence, checking pylint on import module is disabled.
'''
from time import sleep
from RPi import GPIO # pylint: disable-msg=E0401
from mfrc522 import SimpleMFRC522 # pylint: disable-msg=E0401
from paho.mqtt import publish
import paho.mqtt.client as mqtt
from pyaml_env import parse_config, BaseConfig
config = BaseConfig(parse_config('./config/config.yml'))
reader = SimpleMFRC522()
# Try to read the RFID card
try:
while True:
print("Hold a tag near the reader")
rfid_id, rfid_body = reader.read()
print(rfid_id)
print(rfid_body)
# Publish RFID body message to controller
publish.single(
config.client_camera.publish.controller,
payload=rfid_body,
hostname=config.mqtt.host,
port=int(config.mqtt.port),
protocol=mqtt.MQTTv311,
qos=config.mqtt.qos,
auth={
'username': config.client_camera.user,
'password': config.client_camera.password})
print("published to: " + config.client_camera.publish.controller)
print("payload: " + rfid_body)
sleep(5)
except KeyboardInterrupt:
# Reset the program port
GPIO.cleanup()
raise