-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_gcal2.py
103 lines (83 loc) · 2.84 KB
/
step_gcal2.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
#!/usr/bin/python -OO
"""Script to control an automated cat food dispenser. Stepper motor rotates thru 80 degrees then returns to resting position. This can be triggered by a button press or within 5 minutes of a calendar event."""
__author__ = "Rachael Stentiford"
import time
import datetime
import urllib.request
import re
import sys
import RPi.GPIO as GPIO
iCalUrl = 'https://calendar.google.com/calendar/ical/ohuvledfpis079ab01r97mqpqk%40group.calendar.google.com/private-b9e1cd2325b1b573fda61b57fc4704e4/basic.ics'
control_pins = [7,11,13,15]
ledPin = 12
buttonPin = 16
deg1 = int(90 * 11.3777777777 / 8)
deg2 = int(90 * 11.3777777777 /8)
halfstep_seq = [
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1]
]
#setup raspberryPi pins
GPIO.setmode(GPIO.BOARD)
for pin in control_pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
GPIO.setup(ledPin, GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#function to move stepper motor fowards by angle deg1 and backwards by angle deg2
def feedThatMoo(currenttime):
print('Feeding the Moo at ', currenttime)
GPIO.output(ledPin, GPIO.HIGH)
for step in range(0,deg1):
for halfstep in range(7,-1,-1):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_seq[halfstep][pin])
time.sleep(0.001)
time.sleep(0.5)
for step in range(deg2):
for halfstep in range(8):
for pin in range(4):
GPIO.output(control_pins[pin], halfstep_seq[halfstep][pin])
time.sleep(0.001)
GPIO.output(ledPin, GPIO.LOW)
for pin in control_pins:
GPIO.output(pin, GPIO.LOW)
def main():
# initialize current time
currenttime = datetime.datetime.utcnow()
nextcheck = datetime.timedelta(minutes=5)
nextchecktime = currenttime
# periodicaly check the calendar and find upcoming events
while True:
currenttime = datetime.datetime.utcnow()
try:
if currenttime>=nextchecktime:
nextchecktime = currenttime+nextcheck
print(currenttime, ' Checking the calendar. I will next check at: ', nextchecktime)
req = urllib.request.Request(iCalUrl)
response = urllib.request.urlopen(req)
for line in response.readlines():
string = line.rstrip().decode('utf-8')
if bool(re.match("^DTSTART:", string)):
splt = string.split(':')
starttime = datetime.datetime.strptime(splt[1], "%Y%m%dT%H%M%SZ")
if (starttime > currenttime) and (starttime <= currenttime+nextcheck):
feedThatMoo(currenttime)
except:
print("An exception occurred")
# check for button press
buttonState = GPIO.input(buttonPin)
if buttonState == False:
feedThatMoo(currenttime)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nExiting application\n')
sys.exit(0)