I have this problem and i new in python (alone and with flask).
I need to use the value of testRun on a loop in the function record_loop, and this variable change on the flask event testConfig when i have a Get or Post. But the variable always read '0' in the function record_loop. I know the variable change inside the flask event, when i do a Post.
What i do wrong
import RPi.GPIO as GPIO
import time
from flask import Flask, render_template, request
from multiprocessing import Process, Value
app = Flask(__name__)
TurrG = 21
TurrY = 20
TurrR = 6
Buzz = 0
Siren = 5
Rele1 = 16
Rele2 = 17
Rele3 = 22
Rele4 = 23
Rele5 = 24
Rele6 = 25
Rele7 = 26
Rele8 = 27
_485_SIM = 7
GPIO.setmode(GPIO.BCM)
GPIO.setup(TurrG, GPIO.OUT)
GPIO.setup(TurrY, GPIO.OUT)
GPIO.setup(TurrR, GPIO.OUT)
GPIO.setup(Buzz, GPIO.OUT)
GPIO.setup(Siren, GPIO.OUT)
GPIO.setup(Rele1, GPIO.OUT)
GPIO.setup(Rele2, GPIO.OUT)
GPIO.setup(Rele3, GPIO.OUT)
GPIO.setup(Rele4, GPIO.OUT)
GPIO.setup(Rele5, GPIO.OUT)
GPIO.setup(Rele6, GPIO.OUT)
GPIO.setup(Rele7, GPIO.OUT)
GPIO.setup(Rele8, GPIO.OUT)
GPIO.setup(_485_SIM, GPIO.OUT)
task = [
{
'id': 1,
'title': u'Task01',
'description': u'First Task',
'done': False
},
{
'id': 2,
'title': u'Task02',
'description': u'Second Task',
'done': False
}
]
global testRun
disString = 'disabled=""'
firstRun = 0
@app.route('/')
def index():
global firstRun
if firstRun == 0:
firstRun = 1
return render_template('index.html', var1 = disString, var2 = disString)
else:
if testRun == 0:
return render_template('index.html', var1 = disString, var2 = disString)
else:
return render_template('index.html', var1 = disString, var2 = None)
def get_tasks():
return jsonify({'tasks': tasks})
def record_loop(loop_on):
try:
GPIO.output(_485_SIM, False)
GPIO.output(TurrR, True)
GPIO.output(TurrY, True)
GPIO.output(TurrG, True)
GPIO.output(Buzz, True)
GPIO.output(Siren, True)
time.sleep(1)
GPIO.output(TurrR, False)
GPIO.output(TurrY, False)
GPIO.output(TurrG, False)
GPIO.output(Buzz, False)
GPIO.output(Siren, False)
GPIO.output(Rele1, True)
time.sleep(0.5)
GPIO.output(Rele2, True)
time.sleep(0.5)
GPIO.output(Rele3, True)
time.sleep(0.5)
GPIO.output(Rele4, True)
time.sleep(0.5)
GPIO.output(Rele1, False)
GPIO.output(Rele5, True)
time.sleep(0.5)
GPIO.output(Rele2, False)
GPIO.output(Rele6, True)
time.sleep(0.5)
GPIO.output(Rele3, False)
GPIO.output(Rele7, True)
time.sleep(0.5)
GPIO.output(Rele4, False)
GPIO.output(Rele8, True)
time.sleep(0.5)
GPIO.output(Rele5, False)
time.sleep(0.5)
GPIO.output(Rele6, False)
time.sleep(0.5)
GPIO.output(Rele7, False)
time.sleep(0.5)
GPIO.output(Rele8, False)
outState = False
while True:
if testRun == 0:
GPIO.output(TurrG, True)
else:
outState = not outState
GPIO.output(TurrG, outState)
time.sleep(0.5)
except KeyboardInterrupt:
print "\n"
except:
print "Other error or exception occurred!"
finally:
GPIO.cleanup()
@app.route('/testConfig',methods=['GET', 'POST'])
def testConfig():
global testRun
print(request.method)
if request.method == 'POST':
testType = request.args.get('Test_Type')
if testType == 0:
return render_template('index.html', var1 = disString, var2 = disString)
else:
print(testRun)
if request.form.get('start') == 'INICIAR':
print(" Butt: Start")
testRun = 1
return render_template('index.html', var1 = disString, var2 = None)
elif request.form.get('stop') == 'PARAR':
print(" Butt: Stop")
testRun = 0
return render_template('index.html', var1 = disString, var2 = disString)
else:
print("I don't know why")
if testRun == 1:
return render_template('index.html', var1 = disString, var2 = None)
elif testRun == 0:
return render_template('index.html', var1 = None, var2 = disString)
else:
testRun = 0
return render_template('index.html', var1 = disString, var2 = disString)
elif request.method == 'GET':
print("Get method")
if __name__ == '__main__':
global testRun
testRun = 0
recording_on = Value('b', True)
p = Process(target=record_loop, args=(recording_on,))
p.start()
app.run(debug = True, host = '0.0.0.0', use_reloader=False)
p.join()