-
Notifications
You must be signed in to change notification settings - Fork 0
/
feeder.py
174 lines (137 loc) · 4.3 KB
/
feeder.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Import libraries
import os
import RPi.GPIO as GPIO
import time
from gpiozero import Button, LED
from picamera import PiCamera
from signal import pause
import subprocess
import time
import threading
import calendar
import json
import requests
import schedule
servoPIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 50) # GPIO 17 for PWM with 50Hz
p.start(0) # Initialization
# GET TOKEN FROM ENV VARIABLE
pushToken = os.getenv("PUSH_BULLET")
camera = PiCamera()
scheduler = schedule.Scheduler()
url_request = "https://api.pushbullet.com/v2/upload-request"
url_push = "https://api.pushbullet.com/v2/pushes"
data_request = {"file_type": "image/jpeg"}
headers_request = {"Content-Type": "application/json",
"Access-Token": pushToken}
print("File loaded")
led1 = LED(22)
flash = LED(23)
led2 = LED(27)
button_release = Button(20)
button_onoff = Button(21)
isOpen = False
running = True
hours = ["09:00", "12:00", "16:00", "20:00", "00:00"]
try:
def sendMessage(title, msg):
data_push = {"type": "note", "title": str(title), "body": str(
msg), "email": "[email protected]"}
requests.post(url_push, json=data_push, headers=headers_request)
def shot():
flash.on()
time.sleep(0.5)
timestamp = calendar.timegm(time.gmtime())
filename = ('%s.jpg' % timestamp)
camera.capture(filename)
time.sleep(0.5)
imageRequest = requests.post(
url_request, json=data_request, headers=headers_request)
imageReqJson = json.loads(json.dumps(imageRequest.json()))
files = {"file": open(filename, "rb")}
time.sleep(0.5)
flash.off()
upload = requests.post(imageReqJson["upload_url"], files=files)
time.sleep(0.3)
print(upload.status_code)
data_push = {"type": "file", "file_name": filename,
"file_type": imageReqJson["file_type"], "file_url": imageReqJson["file_url"], "email": "[email protected]"}
push = requests.post(url_push, json=data_push, headers=headers_request)
time.sleep(0.3)
print(push.status_code)
print(push.content)
def release():
global p
print("Release")
cicleTime = 0.37
try:
p.ChangeDutyCycle(2+(70/18))
time.sleep(cicleTime)
p.ChangeDutyCycle(0)
time.sleep(0.1)
p.ChangeDutyCycle(2+(17.05/18))
time.sleep(cicleTime)
p.ChangeDutyCycle(0)
time.sleep(0.1)
p.ChangeDutyCycle(2+(17.05/18))
time.sleep(cicleTime)
p.ChangeDutyCycle(0)
except Exception as e:
sendMessage("Erro no feeder", "Erro no release " + str(e))
time.sleep(1)
shot()
time.sleep(1)
p.ChangeDutyCycle(0)
finally:
print("Release finalizado")
def setInterval(func, time):
e = threading.Event()
while not e.wait(time):
func()
def looping():
global running
if running:
led2.on()
scheduler.run_pending()
else:
led2.off()
def onOff():
print("onOff")
global running
running = not running
print("Running", running)
def job():
release()
shot()
for hour in hours:
scheduler.every().day.at(str(hour)).do(job)
button_release.when_pressed = release
button_onoff.when_pressed = onOff
# scheduler.every(5).seconds.do(release)
led1.on()
sendMessage("Inicializado", hours)
shot()
time.sleep(0.5)
setInterval(looping, 1)
print("After interval")
pause()
except Exception as e:
print("Oops!", e, "occurred.")
data_push = {"type": "note", "title": "Erro no feeder",
"body": "Erro no feeder, reiniciando" + str(e), "email": "[email protected]"}
push = requests.post(url_push, json=data_push, headers=headers_request)
time.sleep(1)
shot()
time.sleep(1)
command = "/usr/bin/sudo /sbin/shutdown -r now"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print(output)
finally:
# Clean things up at the end
led1.off()
p.stop()
GPIO.cleanup()
print("Goodbye!")