-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (51 loc) · 1.68 KB
/
main.py
File metadata and controls
60 lines (51 loc) · 1.68 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
54
55
56
57
58
59
60
import cv2
from flask import Flask, Response, request, jsonify
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
app = Flask(__name__)
pump_rate = 100 # litres per hour
pump_gpio = 18
GPIO.setup(pump_gpio, GPIO.OUT)
auger_rate = 10000 # grams per hour
auger_gpio = 22
GPIO.setup(auger_gpio, GPIO.OUT)
def generate_frames():
camera = cv2.VideoCapture(0)
while True:
success, frame = camera.read()
if not success:
break
else:
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
def push(gpio, sec_req):
GPIO.output(gpio, GPIO.HIGH)
time.sleep(sec_req)
GPIO.output(gpio, GPIO.LOW)
@app.route('/video')
def index():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/pump', methods=['POST'])
def pump():
data = request.get_json()
print(data)
if 'value' in data:
sec_req = int((float(data["value"])* 3600) // 100)
push(pump_gpio, sec_req)
return jsonify({'message': f'Pumped {data["value"]} litres in {sec_req} seconds'})
else:
return jsonify({'error': 'Invalid input'})
@app.route('/feed', methods=['POST'])
def feed():
data = request.get_json()
if "value" in data:
sec_req = int(float(data["value"]) * auger_rate)
push(auger_gpio, sec_req)
return jsonify({'message': f'Pushed {data["value"]} litres in {sec_req} seconds'})
else:
return jsonify({'error': 'Invalid input'})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)