Code Repository
Hello and welcome to our code repository! Here you can find the bulk of the code used in deploying the webpage, as well as hosting the flask server, as well as controlling the pi remotely. Please see below for samples of our code.
In the spirit of good attribution, here are the many Github links, tutorials that we borrowed from.
- https://github.com/adafruit/Adafruit_CircuitPython_PCA9685/blob/main/examples/pca9685_simpletest.py
- https://github.com/fatedier/frp
- https://github.com/jantman/python-amcrest-noauth-proxy/blob/master/amcrest_noauth_proxy.py
- https://github.com/jquery/jquery
- https://github.com/soundar24/roundSlider
- https://github.com/illuspas/Node-Media-Server
- https://github.com/sixrevisions/responsive-full-background-image/blob/master/responsive-full-background-image-demo.html
Code for App.py
this Python code deploys a Flask application, and remotely adjusts the pump power levels based on values inputted by the user from the webpage via jQurey/AJAX functionalities. (for more information, please refer to our software page!)
from bs4 import BeautifulSoup
from board import SCL, SDA
import busio
from flask import Flask, render_template, request, redirect, jsonify
# Import the PCA9685 module.
from adafruit_pca9685 import PCA9685
# Create the I2C bus interface.
i2c_bus = busio.I2C(SCL, SDA)
# Create a simple PCA9685 class instance.
pca = PCA9685(i2c_bus)
# Set the PWM frequency to 60hz.
pca.frequency = 60
app = Flask(__name__)
status = “blah blah blah”
acc_power = “lalaland”
@app.route(“/”)
def index():
return render_template(“webpage.html”)
@app.route(“/on”)
def on():
pca.channels[0].duty_cycle = 0
pca.channels[12].duty_cycle = 0
print(“Its on!”)
global status
status = “on”
return render_template(“on.html”)
@app.route(“/off”)
def off():
pca.channels[0].duty_cycle = 0
pca.channels[12].duty_cycle = 0
print(“Its off!”)
global status
status = “off”
return render_template(“off.html”)
@app.route(‘/_command’)
def command():
percentage = request.args.get(‘percent’)
print(percentage)
#return jsonify(description=”you sent something”, percentage=percentage)
if status == “on”:
pca.channels[0].duty_cycle = int(int(0xFFFF * int(percentage)/100) * int(acc_power)/100)
pca.channels[12].duty_cycle = int(int(0xFFFF * (1 – int(percentage)/100)) * int(acc_power)/100)
elif status == “off”:
pca.channels[0].duty_cycle = 0
pca.channels[12].duty_cycle = 0
print(request.args.get(‘percent’) + “% was pressed!”)
print(status)
print(“/_command acc_power is “, acc_power)
print(“left motor is “ + str(pca.channels[0].duty_cycle))
print(“right motor is “ + str(pca.channels[12].duty_cycle))
return jsonify(command=request.args.get(‘command’))
@app.route(‘/acc’)
def commando():
global acc_power
acc_power = request.args.get(‘power’)
#print(“power is” + str(power_percentage))
# global acc_power
# acc_power = int(power_percentage)/100
print(“acc_power is “ + str(acc_power))
print(“left motor is “ + str(pca.channels[0].duty_cycle))
print(“right motor is “ + str(pca.channels[12].duty_cycle))
return jsonify(power=request.args.get(‘power’))