-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARFID_Arduino.py
More file actions
125 lines (107 loc) · 4.2 KB
/
ARFID_Arduino.py
File metadata and controls
125 lines (107 loc) · 4.2 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
import serial
import serial.tools.list_ports
import time
from datetime import datetime
tapLogFile = open('ARFIDTapLog.txt','a')
def tapLog(rfid):
tapLogFile.write(str(datetime.now()) + " | " + rfid + "\n")
# Checks if the Arduino rfid scanner and sim module is active; resets arduino if not
def initArduino():
try:
arduino_ports = [
p.device
for p in serial.tools.list_ports.comports()
if 'Arduino' in p.description
]
ser = serial.Serial(arduino_ports[0], 9600)
print("Connected to arduino through port '{}'.".format(arduino_ports[0]))
ser.write(b'0\n')
while (True):
ser.write(b'1\n')
fromArduino = readSerial(ser)
if("Ready" in fromArduino):
print("Rfid scanner ready.")
break
# while (True):
# ser.write(b'2\n') #I dont have sim yet
# fromArduino = readSerial(ser)
# if("Ready" in fromArduino):
# print("GSM ready.")
# break
print("Connected to rfid and sim module through port '{}'.".format(arduino_ports[0]))
return ser
except Exception as e:
print(e)
print("Retrying in 5 seconds...")
time.sleep(5)
return initArduino()
def readSerial(ser):
line = str(ser.readline())[2:-5]
return line
# Wait for an rfid card and return its UID
def scan(ser):
ser.write(b'3\n')
print("\nScanning...\n")
line = ""
while("UID" not in line):
line = readSerial(ser)
print(line)
ser.write(b'x\n')
tapLog(line[5:])
return line[5:] ##Check if scan returns something
# Instruct Arduino to send an SMS message to guardian
def sendSMS(ser, student):
try:
if(student[8]!="nan"):
print("Sending message...")
# ser.write(b'4\n')
# time.sleep(0.2)
# ser.write(bytes(student[8]+'\n', 'utf-8'))
# time.sleep(0.2)
# ser.write(bytes("{}, THIS IS SENT BY ARFID\n".format(student[7]), 'utf-8'))
# while("COMPLETE" not in readSerial(ser)): pass
print("[{}]----{}----{} of {} has arrived today at {}. //This message is sent by ARFID. " \
"Please do not reply. You may contact us through upisarfid@gmail.com.".format(datetime.now().strftime('%a, %b %d, %Y'),student[7],student[3]+" "+student[2],student[6],datetime.now().strftime('%I:%M:%S %p')))
print("Message sent to {} through {}!\n".format(student[7], student[8]))
tapLog("Message sent to " + student[7])
return True
else:
print("No guardian number found\n")
tapLog("Guardian number not found.")
return False
except Exception as e:
print("Error in sendSMS():", e)
tapLog("Error in sendSMS(): " + str(e))
return False
def sendAbsentSMS(ser, student):
try:
if(student[8]!="nan"):
print("Sending message...")
# ser.write(b'4\n')
# time.sleep(0.2)
# ser.write(bytes(student[8]+'\n', 'utf-8'))
# time.sleep(0.2)
# ser.write(bytes("{}, THIS IS SENT BY ARFID\n".format(student[7]), 'utf-8'))
# while("COMPLETE" not in readSerial(ser)): pass
print("Message sent to {} through {}!\n".format(student[7], student[8]))
# tapLog("Message sent to " + student[7])
return True
else:
print("No guardian number found\n")
tapLog("Guardian number not found.")
return False
except Exception as e:
print("Error in sendAbsentSMS():", e)
tapLog("Error in sendAbsentSMS(): " + str(e))
return False
# Test if these functions work
if __name__ == "__main__":
ser = initArduino()
if (ser):
print("Arduino found!")
rfid = scan(ser)
print(rfid,"tapped!")
sampleStudent = ("92", "42", "Nicolas", "Marew", "Cruz", "F", "Banzon", "Jorbeagle", "09276311408", "DE AD BE EF")
sendSMS(ser, sampleStudent)
else:
print("Arduino not found")