-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_sensor.py
77 lines (59 loc) · 1.88 KB
/
thread_sensor.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
71
72
73
74
75
76
77
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import urllib2
from threading import Thread
import RPi.GPIO as GPIO
import datetime, time
import csv
GPIO.setmode(GPIO.BCM)
N1 = "sensor1"
T1 = 23
D1 = 24
N2 = "sensor2"
T2 = 7
D2 = 8
class counter(Thread):
def __init__(self, sensor, triger, name):
Thread.__init__(self)
self.sensor = sensor
self.triger = triger
self.name = name
GPIO.setup(self.sensor, GPIO.IN)
GPIO.setup(self.triger, GPIO.OUT)
def run(self):
print "Run - ", self.sensor, self.triger, self.name
while True:
GPIO.output(self.triger, False)
time.sleep(0.025)
GPIO.output(self.triger, True)
time.sleep(0.00001)
GPIO.output(self.triger, False)
while (GPIO.input(self.sensor)==0):
pulse_start1 = time.time()
while GPIO.input(self.sensor)==1:
pulse_end1 = time.time()
pulse_duration1 = pulse_end1 - pulse_start1
distance1 = pulse_duration1 * 17150
distance1 = round(distance1, 2)
status = 1
if distance1 < 15:
print str(datetime.datetime.now()), self.name, distance1,"parked"
status = 1
time.sleep(1)
else
print str(datetime.datetime.now()), self.name, distance1,"unparked"
status = 0
time.sleep(1)
row = [self.sensor, self.triger,self.name,status,datetime.datetime.now() ]
with open('data.csv', 'a') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()
def main():
thread1 = counter(D1,T1,N1)
thread1.start()
thread2 = counter(D2,T2,N2)
thread2.start()
if __name__ == "__main__":
main()