-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alfred.py
138 lines (104 loc) · 3.18 KB
/
Alfred.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
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
from gpiozero import LightSensor, Buzzer, Button, CamJamKitRobot
from picamera import PiCamera
from time import sleep
from math import fabs
import random
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import base64
import xml.etree.ElementTree as ET
def decode(enc , key):
dec = []
enc = base64.urlsafe_b64decode(enc).decode()
for i in range(len(enc)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
def moveRobot():
global robot
motorspeed = random.randint(-100, 100)/100.0
motorforward = (motorspeed, motorspeed)
motorspeed = random.randint(-100, 100)/100.0
motorbackward = (-motorspeed, -motorspeed)
motorspeed = random.randint(-100, 100)/100.0
motorleft = (motorspeed, 0)
motorright = (0, motorspeed)
robot.value = motorforward
sleep(1)
robot.value = motorbackward
sleep(1)
robot.value = motorleft
sleep(1)
robot.value = motorright
sleep(1)
robot.stop()
def sendPic():
tree = ET.parse('config.xml')
root = tree.getroot()
print(root.find('email').text)
password = root.find('password').text
email_user = root.find('email').text
password = decode(password,"malincoh")
subject = 'Test email'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_user
msg['Subject'] = subject
body = "Hi, there"
msg.attach(MIMEText(body,'plain'))
filename = 'catpic.jpg'
attachment = open(filename, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP( 'smtp.gmail.com', 587 )
server.starttls()
server.login( email_user, password )
server.sendmail( email_user, email_user, text )
server.quit()
def takePic():
print("ZA")
isNotTakingPic = False
print("ZA WARUDO")
global environment
global camera
global ldr
# checking if the light is sufficient to take a picture
if ldr.value > 0.6:
camera.start_preview()
sleep(2)
camera.capture('catpic.jpg')
camera.stop_preview()
sendPic()
#reser all conditions including the previous and current value of the ldr, because 2 seconds have passed
isNotTakingPic = True
global currentValue
currentValue = ldr.value
global previousValue
previousValue = ldr.value
#button = Button(18)
camera = PiCamera()
camera.rotation = 180
ldr = LightSensor(17)
robot = CamJamKitRobot()
print("Hello")
previousValue = ldr.value
print(ldr.value)
isNotTakingPic = True
#connecting a pushbutton to the taking of picture
#button.when_pressed = takePic
while True:
currentValue = ldr.value
if fabs( currentValue - previousValue ) > 0.0165 and isNotTakingPic:
print("isNotTakingPic", isNotTakingPic)
moveRobot()
takePic()
previousValue = currentValue
sleep(0.1)