-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart bin.py
More file actions
152 lines (115 loc) · 3.23 KB
/
smart bin.py
File metadata and controls
152 lines (115 loc) · 3.23 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import smtplib
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10,GPIO.IN)
GPIO.setup(8,GPIO.IN)
# -----------------------
# Define some functions
# -----------------------
flag1 = False
def mail():
global flag1
i=GPIO.input(10)
j=GPIO.input(8)
if i==0 and j==0:
print("both activated")
if flag1 == False:
print("mail sending")
smtpuser = "sagargupta@gmail.com"
smtppass = "************"
toadd = "sagargupta@gmail.com"
fromadd = smtpuser
subject = "Python Test"
header = "To: "+toadd+'\n'+"From: "+fromadd+'\n'+"Subject: "+subject+'\n'
body = "full"
message = header+'\n'+body
print(message)
s = smtplib.SMTP("smtp.gmail.com",587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(smtpuser, smtppass)
s.sendmail(fromadd, toadd, message)
s.quit()
flag1=True
elif i==0 and j==1:
flag1=False
elif i==1 and j==1:
flag1=False
def measure():
# This function measures a distance
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
elapsed = stop-start
distance = (elapsed * 34300)/2
return distance
def SetAngle(angle):
duty = angle / 18 + 2
GPIO.output(3,True)
pwm.ChangeDutyCycle(duty)
time.sleep(1)
GPIO.output(3,False)
pwm.ChangeDutyCycle(duty)
def measure_average():
# This function takes 3 measurements and
# returns the average.
distance1=measure()
time.sleep(0.1)
distance2=measure()
time.sleep(0.1)
distance3=measure()
distance = distance1 + distance2 + distance3
distance = distance / 3
return distance
# -----------------------
# Main Script
# -----------------------
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setup(3,GPIO.OUT)
pwm = GPIO.PWM(3,50)
# Define GPIO to use on Pi
GPIO_TRIGGER = 16
GPIO_ECHO = 18
flag = False
print ("Ultrasonic Measurement")
# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)
# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.
try:
while True:
distance = measure_average()
print ("Distance : %.1f" % distance)
if distance < 30 :
if flag == False :
pwm.start(0)
SetAngle(0)
print("open")
flag = True
elif flag == True:
SetAngle(90)
print("closed")
flag = False
else:
print("mailing")
mail()
time.sleep(1)
except KeyboardInterrupt:
# User pressed CTRL-C
# Reset GPIO settings
GPIO.cleanup()