-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainloop.py
More file actions
53 lines (44 loc) · 1.4 KB
/
mainloop.py
File metadata and controls
53 lines (44 loc) · 1.4 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
# Detect Motion, Take Picture, Send Tweet
# add to crontab, run each hour, finish after 55 minutes
from GetCamera import CamCapt
from DetectBird import BirdDetector
from SendTweets import TwitterSender
import time
import datetime
print(__name__)
print('Initializing classes')
# Initialize functionalities
watchdog = BirdDetector()
paperazzi = CamCapt()
shouter = TwitterSender()
sleeptime = 10 # seconds
# Notify Followers of starting loop
try:
print('Sending Ping')
shouter.send_msg("I'm Alive! @ {}".format(datetime.datetime.utcnow().time()))
except Exception:
print('Could not notify followers, starting anyway...')
previously_triggered = False
i=0
print('Starting wachtdogloop')
while i < (55*60)/sleeptime:
try:
# Check if there is movement
triggered = watchdog.detect()
if triggered and not previously_triggered:
print('Triggered!')
# Take image
print('Taking image')
imgloc = './photo.jpeg'
paperazzi.take_image(imgloc)
# Post Tweet
print('Sending image')
shouter.send_img(imgloc,'Look at this picture!')
except Exception as e:
print('Whoops, something went wrong. Trying again.')
print(e)
# Remember previous state
previously_triggered = triggered
print('Sleeping', 'triggered' if triggered else '')
time.sleep(sleeptime)
print('Finished!')