-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogTags.py
executable file
·70 lines (59 loc) · 2.11 KB
/
LogTags.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import requests
import base64
import time
import json
import sys
from uuid import getnode as get_mac
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
url = 'http://raffy-admin/iot/log-tag'
while True:
try:
while True:
rc522_reader = SimpleMFRC522()
print(rc522_reader.READER)
if not rc522_reader.READER:
raise ValueError
print('Ready. Listening...')
id, text = rc522_reader.read()
print("CARD", id, text)
encoded = str(id)
encoded_bytes = encoded.encode('ascii')
base64_bytes = base64.b64encode(encoded_bytes)
reader = str(get_mac())
print("READER", reader)
reader_bytes = reader.encode('ascii')
reader_base64 = base64.b64encode(reader_bytes)
postdata = {'id': base64_bytes, 'text': text, 'reader': reader_base64}
x = requests.post(url, data = postdata, verify = False)
print(x.text)
result = json.loads(x.text)
wait = 2
pin = 17
print("STATUS", result['status'])
if result['status'] == 'success':
wait = int(result['action']['time'])
if result['action']['is_exit'] > 0:
pin = 27
print("OPENING PIN", pin)
GPIO.output(pin, GPIO.HIGH)
time.sleep(wait)
GPIO.output(pin, GPIO.LOW)
print("CLOSED PIN", pin)
else:
time.sleep(2)
except KeyboardInterrupt:
print("ABORTED.")
except Exception as e:
print("ERROR!")
print(str(e))
finally:
try:
GPIO.cleanup()
except Exception:
print("IO CLEANUP ERROR!")
sys.exit()