Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template_string, request
- from time import sleep
- import pigpio
- from OpenSSL import SSL
- # This HTML page + JS will be served when the root path is requested with a GET
- TPL = '''
- <html>
- <body>
- <button id="connectbtn" onclick="requestPermission()">Connect</button>
- <div id="outputdiv"></div>
- </body>
- <script>
- var output = document.getElementById("outputdiv");
- var btn = document.getElementById("connectbtn");
- var socket = false;
- var vertical = 0;
- var horizontal = 0;
- function sendToFlask()
- {
- const xmlhttpreq = new XMLHttpRequest();
- const data = new FormData();
- data.append("updown", vertical);
- data.append("leftright", horizontal);
- xmlhttpreq.open("POST", "campan");
- xmlhttpreq.send(data);
- }
- function handleRotation(event)
- {
- vertical = Math.round(event.gamma);
- horizontal = Math.round(event.beta);
- }
- function requestPermission()
- {
- if (typeof(DeviceMotionEvent) !== 'undefined' && typeof(DeviceMotionEvent.requestPermission) ===
- 'function')
- {
- DeviceMotionEvent.requestPermission().
- then(response => {
- if (response === 'granted') {
- window.addEventListener('deviceorientation', handleRotation);
- }
- finishRequest();
- }).catch(console.error);
- }
- else
- {
- window.addEventListener('deviceorientation', handleRotation);
- finishRequest();
- }
- }
- function finishRequest()
- {
- setInterval(Flask, 200);
- }
- // schedule the first one.
- setTimeout(processVideo, 0);
- </script>
- </html>
- '''
- # The servos are connected on these GPIO pins (BCM numbering)
- HORIZ_SERVO_PORT = 13
- VERT_SERVO_PORT= 11
- HORIZ_SERVO_CENTER = 1750
- VERT_SERVO_CENTER = 1500
- # Create Flask App
- app = Flask(__name__)
- # GPIO Info
- gpio = pigpio.pi()
- def setupGPIO():
- gpio.set_servo_pulsewidth(HORIZ_SERVO_PORT, 0)
- gpio.set_servo_pulsewidth(VERT_SERVO_PORT, 0)
- def setServoDuty(servo, duty):
- gpio.set_servo_pulsewidth(servo, duty)
- def clamp(num, minimum, maximum):
- return max(min(num, maximum), minimum)
- # Serve the HTML file when the root path is requested
- @app.route("/")
- def serveRoot():
- return render_template_string(TPL)
- # Expose an endpoint for sending the servo coordinates
- # from the JS to the Flask Backend
- @app.route("/moveservos", methods=["POST"])
- def moveServos():
- # Get the values from the request
- horizontal = 25 * int(request.form["updown"])
- vertical = 25 * int(request.form["leftright"])
- print(str(horizontal) + ", " + str(vertical))
- # Move the Servos
- setServoDuty(HORIZ_SERVO_PORT, clamp(HORIZ_SERVO_CENTER - horizontal, 500, 2500))
- setServoDuty(VERT_SERVO_PORT, clamp(VERT_SERVO_CENTER - vertical, 500, 2500))
- # Wait for 0.2s so that the servos have time to move
- sleep(0.2)
- # Stop the servo motors to save energy and reduce noise
- gpio.set_servo_pulsewidth(HORIZ_SERVO_PORT, 0)
- gpio.set_servo_pulsewidth(VERT_SERVO_PORT, 0)
- # Return empty request (Should return a 200 OK with an empty body)
- return ""
- # Run the app on the local development server
- # Accept any IP address
- # Create ad-hoc SSL encryption (needed for iOS 13 support)
- if __name__ == "__main__":
- app.run(host="192.168.0.157", port=5001, ssl_context='')
Advertisement
Add Comment
Please, Sign In to add comment