-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaspberryController.py
More file actions
95 lines (77 loc) · 2.32 KB
/
RaspberryController.py
File metadata and controls
95 lines (77 loc) · 2.32 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
# The Project Code ↓↓
import time
from multiprocessing import Value
import MLX90614
import RPi.GPIO as GPIO
from mfrc522 import MyMFRC522
class RaspberryController():
def __init__(self, interrupt):
GPIO.setmode(GPIO.BCM)
self.nfc_reader = MyMFRC522(interrupt)
self.interrupt = interrupt
def __del__(self):
GPIO.cleanup()
def getNFCId(self):
try:
id = self.nfc_reader.read_id()
finally:
pass
return id
def getTemp(self):
temp_sensor = MLX90614.MLX90614()
total = 0
n = 4
while(self.getDistance() >= 8.0):
time.sleep(0.2)
for i in range(n):
total += float(temp_sensor.get_obj_temp())
if(self.interrupt.value):
return 'INTERRUPTED'
time.sleep(0.5)
return round(total / n, 2)
def getDistance(self):
trig = 5
echo = 6
GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)
time.sleep(0.2)
GPIO.output(trig, 1)
time.sleep(0.00001)
GPIO.output(trig, 0)
while GPIO.input(echo) == 0:
pulse_start = time.time()
while GPIO.input(echo) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17000
distance = round(distance, 2)
return distance
# For Test Code ↓↓
# import time
# import random
# from multiprocessing import Value
# class RaspberryController():
# def __init__(self, interrupt):
# self.interrupt = interrupt
# def getNFCId(self):
# nfc_numbers = [584187407785, 584186083456, 584184898698, 584183666907]
# return random.choice(nfc_numbers)
# def getTemp(self):
# total = 0
# n = 4
# while(self.getDistance() >= 8.0):
# time.sleep(0.2)
# for i in range(n):
# total += float(random.uniform(36.0, 38.5))
# if(self.interrupt.value):
# return 'INTERRUPTED'
# time.sleep(0.5)
# return round(total / n, 2)
# def getDistance(self):
# return 2.0
if __name__ == '__main__':
v = Value('b', False)
con = RaspberryController(v)
print(con.getNFCId())
print(con.getTemp())
print(con.getDistance())