0

Description

My ultimate goal with this program is to have a Flask web app be projected, someone connects to it, and they can control stepper motors with it using the Adafruit Stepper Motor HAT. At the moment, the visual code is a bit of a mess due to me wanting to have a login system, and realizing that with my limited time to get this done, I need to focus on making it work...

What's Wrong?

At the moment, the stepper motors work fine. The user pushes the (what's called for now) login button, and the motors spin, and pushes the stop motors button to stop. That's where the problem comes in... The motors don't stop, they just slow down to a painful pace, and the web page gets stuck trying to load an infinite loop. How do I stop this?

Try to understand that I have been at this for a week, and have pulled every trick I know. The code is a bit messy with the global variables and all now due to my desperation to just make it work.

Code is included here:

Python

from flask import Flask, flash, redirect, render_template, request, session, abort
import os
import time
from adafruit_motorkit import MotorKit


kit = MotorKit()

app = Flask(__name__)

def set_z_to_zero():
    global z    # Needed to modify global copy of globvar
    z = 0

def set_z_to_one():   # Needed to modify global copy of globvar
    z = 1

def rel():
    kit.stepper1.release()
    kit.stepper2.release()
    set_z_to_one()

@app.route('/')
def home():
    rel()
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        return "Hello Boss!  <a href=/logout>Logout</a>"

@app.route('/stop', methods=['GET'])
def stop_motors():
    rel()
    return render_template('login.html')

def run():
    for i in range(100):
        kit.stepper1.onestep()
        kit.stepper2.onestep()
        if z == 1:
            break

@app.route('/login', methods=['GET'])
def start_motors():
    while z == 0:
        run()
        if z == 1:
            break
        set_z_to_zero()
    rel()
    return render_template('login.html')


if __name__ == "__main__":
    app.secret_key = os.urandom(12)
    set_z_to_zero()
    app.run(debug=True,host='0.0.0.0', port=3860)

HTML


<form action="/login" method="GET">

<div class="login">

<div class="login-screen">

<div class="app-title">
<h1>Login</h1>
</div>


<div class="control-group">
<input type="text" class="login-field" value="" placeholder="username" name="username">
<label class="login-field-icon fui-user" for="login-name">
</label>
</div>


<input type="submit" value="Log in" class="btn btn-primary btn-large btn-block">

</form>
<form action="/stop" method="GET">
<input type="submit" value="Stop Motors" class="btn btn-primary btn-large btn-block">
</form>

</div>
</div>

CSS

box-sizing: border-box;
}

*:focus {
outline: none;
}
body {
font-family: Arial;
background-color: #3498DB;
padding: 50px;
}
.login {
margin: 20px auto;
width: 300px;
}
.login-screen {
background-color: #FFF;
padding: 20px;
border-radius: 5px
}

.app-title {
text-align: center;
color: #777;
}

.login-form {
text-align: center;
}
.control-group {
margin-bottom: 10px;
}

input {
text-align: center;
background-color: #ECF0F1;
border: 2px solid transparent;
border-radius: 3px;
font-size: 16px;
font-weight: 200;
padding: 10px 0;
width: 250px;
transition: border .5s;
}

input:focus {
border: 2px solid #3498DB;
box-shadow: none;
}

.btn {
border: 2px solid transparent;
background: #3498DB;
color: #F0FFFF;
font-size: 16px;
line-height: 25px;
padding: 10px 0;
text-decoration: none;
text-shadow: none;
border-radius: 3px;
box-shadow: none;
transition: 0.25s;
display: block;
width: 250px;
margin: 0 auto;
}

.btn:hover {
background-color: #2980B9;
}

.login-link {
font-size: 12px;
color: #444;
display: block;
margin-top: 12px;
}

And here's a screenshot of the web page enter image description here

Thank you for any help, and if I need to clarify something please ask.

Daniil Rose
  • 123
  • 1
  • 9

2 Answers2

0

According to Python documentation

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

You need to add global z to methods where you modify the variable. In your set_z_to_one(), you're initiating a new variable z within the method's scope, which is not the same as your global z.

def set_z_to_one():   # Needed to modify global copy of globvar
    z = 1


changing to

def set_z_to_one():
    global z
    z = 1

fixes the problem

Kasem Alsharaa
  • 892
  • 1
  • 6
  • 15
  • Did exactly this, and the same thing occurs. The steppers spin, and then when stop is issued, the motors slow down to a crawl but don't stop. – Daniil Rose Feb 08 '20 at 16:48
  • Depending on your flask version, you might have to run it in threaded mode explicitly `app.run(debug=True,host='0.0.0.0', port=3860, threaded=True)`. Otherwise your code could be blocking next calls. Try that? – Kasem Alsharaa Feb 08 '20 at 17:01
  • Ay, tried that now too. Same thing. Could there be an error elsewhere causing it to never stop? – Daniil Rose Feb 08 '20 at 17:19
  • This didn't solve my problem specifically, but is the correct answer to what I presented! – Daniil Rose Feb 13 '20 at 01:41
0

I solved this for future help...

I had a login system that had issued a token, and I deleted all but that... so every time somebody issued a command it stopped it. Everything was fixed and works.

Daniil Rose
  • 123
  • 1
  • 9