aziz.s.lok
Posts: 2
Joined: Mon Aug 17, 2015 9:14 am

Running a while loop along with a flask app.

Mon Aug 17, 2015 9:17 am

I am working on writing a code in raspberry pi using python where i want the user to input the set temperature and fan mode via web page i'm using flask for that and the values are returned successfully but i also want to run a infinite while loop along with the flask app which will compare the set temperature with the current temperature from a sensor.. how do i achieve this without interrupting the flask app?

Code: Select all

from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
import time
temp = ""
t = ""
fan_High = 0
fan_Med = 0
fan_Low =0
fanspeed = ""


app = Flask(__name__)


@app.route('/form', methods=['POST'])
def aziz():
    global temp ,fanspeed
    fanspeedlocal = ''
    if request.form['settemp'] != "":
        temp = request.form['settemp']
        templocal = temp
    else:
        templocal = temp


    if request.form['speed'] == "null":
        fanspeedlocal = fanspeed
    else:
        if request.form['speed'] == "High":
            fan_Med = False
            fan_Low = False
            fan_High = True
            fanspeed = "High"
            fanspeedlocal = fanspeed
        elif request.form['speed'] == "Med":
            fan_High = False
            fan_Low = False
            fan_Med = True
            fanspeed = "Medium"
            fanspeedlocal = fanspeed
        elif request.form['speed'] == "Low":
            fan_High = False
            fan_Med = False
            fan_Low = True
            fanspeed = "Low"
            fanspeedlocal = fanspeed
    print 'Settemp = %s' %temp
    print 'fanspeed = %s' %fanspeed
    return render_template('Output.html',temp=templocal,currtemp=strct,time=t,fanspeed=fanspeedlocal




@app.route('/')
def start():
    global t , fanspeed
    t = time.strftime("%H:%M:%S")    
    return render_template('Start.html',temp=temp,currtemp=strct,time=t,fanspeed=fanspeed)




if __name__ == '__main__':
    app.debug = False
    app.run(host = '192.168.1.101')


var = 1
while var == 1:
    inttemp = int(temp)
    if currtemp >= inttemp:
            #set GPIO to high
    else:
            #set GPIO to low

    if fanspeed == 'High':
            #set GPIO to high
    elif fanspeed == 'Med':
            #set GPIO to high
    elif fanspeed == 'LOW':
            #set GPIO to high
    time.sleep(10)

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Running a while loop along with a flask app.

Mon Aug 17, 2015 10:20 am

The common answer seems to be to use flask-celery to run a background task

http://stackoverflow.com/questions/1125 ... 8#11257228
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

gkreidl
Posts: 6326
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Running a while loop along with a flask app.

Mon Aug 17, 2015 10:23 am

run it in a thread.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

aziz.s.lok
Posts: 2
Joined: Mon Aug 17, 2015 9:14 am

Re: Running a while loop along with a flask app.

Mon Aug 17, 2015 12:10 pm

Can explain a little for running in thread .. i just started using python.. so dont know much about threading... i tried to look into it but its way too complicted ...

thanx guys

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Running a while loop along with a flask app.

Mon Aug 17, 2015 7:20 pm

It may have been updated since I used it but I found that Flask needed to be in the main thread. As my app needed to be in the main thread as well I had to put Flask into a subprocess with associated piping of info back and forth see here https://github.com/davidedc/devart-temp ... /AutoVJ.py

However I found lots of really practical info on using Flask on Matt Richardson's site http://mattrichardson.com/Raspberry-Pi-Flask/

good luck if you're new to programming, this involves quite tricky concepts.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

mesand
Posts: 65
Joined: Tue Oct 22, 2019 9:21 pm

Re: Running a while loop along with a flask app.

Mon Mar 23, 2020 2:03 am

Did you end up solving this? I have a while loop keeping a sensor value within a range and am experiencing some trouble

pcmanbob
Posts: 9467
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running a while loop along with a flask app.

Mon Mar 23, 2020 10:01 am

mesand wrote:
Mon Mar 23, 2020 2:03 am
Did you end up solving this? I have a while loop keeping a sensor value within a range and am experiencing some trouble

You are unlikely to get an answer from the OP they only ever made 2 posts back in September 2015.

You would be better starting a new thread asking you own question, don't for get to include your code so far.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

mesand
Posts: 65
Joined: Tue Oct 22, 2019 9:21 pm

Re: Running a while loop along with a flask app.

Mon Mar 23, 2020 3:05 pm

great I did. Here is the link to the new post. viewtopic.php?f=63&t=268709&p=1630684#p1630684

Return to “Python”