Skip to content

Instantly share code, notes, and snippets.

@maartenpaauw
Created January 28, 2016 20:15
Show Gist options
  • Save maartenpaauw/94eebd7163f5f38d2733 to your computer and use it in GitHub Desktop.
Save maartenpaauw/94eebd7163f5f38d2733 to your computer and use it in GitHub Desktop.
# Group: YouMedtBro (#25)
# Students: Don, Maarten, Youri, Murtaza, Idriss
from flask import Flask, render_template, request, json # Flask Webserver (http://flask.pocoo.org/docs/0.10/quickstart/)
from Hwctrl import Hwctrl # Hardware control class
import RPi.GPIO as GPIO
hwctrl = Hwctrl() # New instance of hardware control class
# Create a new Flask App
app = Flask(__name__)
@app.route("/")
@app.route("/index.htm")
def index():
if hwctrl.animationActive:
icon = 'fa-stop'
id = 'auto_off'
animation = True
else:
icon = 'fa-play'
id = 'auto_on'
animation = False
return render_template('index.html', icon=icon, id=id, animation=animation)
# Reboot
@app.route('/reboot.htm')
def hw_reboot():
if hwctrl.reboot():
return json.dumps({'success': True, 'message': 'De Raspberry Pi gaat rebooten.'})
else:
return json.dumps({'success': False, 'message': 'Oeps! Er is iets fout gegaan...'})
# Shutdown
@app.route('/shutdown.htm')
def hw_shutdown():
if hwctrl.shutdown():
return json.dumps({'success': True, 'message': 'De Rasberry Pi gaat uit.'})
else:
return json.dumps({'success': False, 'message': 'Oeps! Er is iets fout gegaan...'})
# Return X-position
@app.route('/ypos.htm')
def returnYpos():
return str(hwctrl.getYpos())
# Return Y-position
@app.route('/xpos.htm')
def returnXpos():
return str(hwctrl.getXpos())
# Move UP
@app.route('/up.htm')
def moveUp():
try:
s = int(request.args.get('steps'))
except:
s = 1
if s == 1:
word = 'stap'
else:
word = 'stappen'
if hwctrl.move(True,s,False):
return json.dumps({'success': True, 'message': 'Het balletje is ' + str(s) + ' ' + word + ' omhoog gegaan.'})
else:
return json.dumps({'success': False, 'message': 'De animatie is bezig, het up commando kan niet worden uitgevoerd.'})
# Move DOWN
@app.route('/down.htm')
def moveDown():
try:
s = int(request.args.get('steps'))
except:
s = 1
if s == 1:
word = 'stap'
else:
word = 'stappen'
if hwctrl.move(False,s,False):
return json.dumps({'success': True, 'message': 'Het balletje is ' + str(s) + ' ' + word + ' omlaag gegaan.'})
else:
return json.dumps({'success': False, 'message': 'De animatie is bezig, het down commando kan niet worden uitgevoerd.'})
# Return to the Home position
@app.route('/home.htm')
def returnHome():
if hwctrl.moveHome():
return json.dumps({'success': True, 'message': 'Het balletje is helemaal omhoog gegaan.'})
else:
return json.dumps({'success': False, 'message': 'De animatie is bezig, het home commando kan niet worden uitgevoerd.'})
# Start the animation
@app.route('/auto_on.htm')
def animateRun():
if hwctrl.animateStart():
return json.dumps({'success': True, 'message': 'De animatie is succesvol gestart.'})
else:
return json.dumps({'success': False, 'message': 'De animatie kan niet gestart worden omdat de animatie al gestart is.'})
# Stop the animation
@app.route('/auto_off.htm')
def animatePause():
if hwctrl.animateStop():
return json.dumps({'success': True, 'message': 'De animatie is succesvol gestopt.'})
else:
return json.dumps({'success': False, 'message': 'De animatie kan niet gestopt worden omdat er geen animatie gestart is.'})
# Run the Flask app
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5000)
hwctrl.cleanShutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment