-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatFeeder.py
101 lines (76 loc) · 3.07 KB
/
CatFeeder.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
# Writen by Darian Johnson
# This python code runs on the raspberry pi and takes photos and controls the servo that dispenses food
# This project is part of the Automated Cat Feeder project
import paho.mqtt.client as paho
import os
import socket
import ssl
import uuid
import json
import time
import os
import tinys3
#update this topic with the code you received in the alexa app
topic = 'UPDATE WITH CODE PROVIDED IN ALEXA APP'
#initialize servo & pwm commands
os.system("gpio -g mode 18 pwm")
os.system("gpio pwm-ms")
os.system("gpio pwmc 192")
os.system("gpio pwmr 2000")
def on_connect(client, userdata, flags, rc):
print("Connection returned result: " + str(rc) )
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
#client.subscribe("#" , 1 )
client.subscribe(topic + "/#",1)
def on_message(client, userdata, msg):
print("payload: " + msg.payload)
parsed_json = json.loads(msg.payload)
#Logic to take the photo
if msg.topic ==topic + "/feed":
try:
runtime = parsed_json["amount"]
t_end = time.time() + runtime
while time.time() < t_end:
os_string = "gpio -g pwm 18 200"
os.system(os_string)
print("done spinning")
os_string = "gpio -g pwm 18 150"
os.system(os_string)
except:
print('did not publish')
elif msg.topic == topic + "/photo": #logic to take the photo
try:
photo_name = parsed_json["photo"]
#delete old photos
os.system("rm Photos/*.jpg")
#take a photo using the name passed to us from mqtt message
photo = "Photos/" + photo_name + ".jpg"
os_string = "fswebcam --no-banner " + photo
os.system(os_string)
#use tinyS3 to upload the photo to AWS S3
#Update the secret and access key with the keys you downloaded in the instructions zip
S3_SECRET_KEY = 'UPDATE WITH KEY ID IN KEYS.TEXT FILE'
S3_ACCESS_KEY = 'UPDATE WITH ACCES KEY IN KEYS.TEXT FILE'
conn = tinys3.Connection(S3_ACCESS_KEY,S3_SECRET_KEY,tls=True)
f = open(photo,'rb')
conn.upload(photo,f,'catfeeder')
except:
print('did not publish')
mqttc = paho.Client()
mqttc.on_connect = on_connect
mqttc.on_message = on_message
#mqttc.on_log = on_log
#variables to connect to AWS IoT
#Note these certs allow access to send IoT messages
awshost = "data.iot.us-east-1.amazonaws.com"
awsport = 8883
clientId = "MyCatFeeder-" + str(uuid.uuid4())
thingName = "MyCatFeeder"
caPath = "/home/pi/certs/verisign-cert.pem"
certPath = "/home/pi/certs/CatFeeder.cert.pem"
keyPath = "/home/pi/certs/CatFeeder.private.key"
mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
mqttc.connect(awshost, awsport, keepalive=60)
mqttc.loop_forever()