1

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?

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)
Aziz L
  • 19
  • 1
  • 3

2 Answers2

1

I would just use cron. You could use a simple script that looks something like this:

#!/usr/bin/python3 
# or python, if that's your thing

import requests

def get_sensor_data():
    '''Presumably you know what actually goes here.'''
    return {'temperature': 5, 'fan speed': 'no, three, sir'}

requests.post('http://example.com/update?api_key=12345', data=get_sensor_data())

Then just setup cron to run it every 60s or less. Now your Flask app just gets requests from your script that updates the data, while your page updates.

Alternatively, you could just setup a Flask @app.before_request decorator that will just request the necessary data and attach it to the special g object.

If it's quick (<250ms) to read the data, I'd probably do the latter. If it takes more than 250ms, then I'd probably make cron do it.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
-1

I think, you just input the while command inside the:

@app.route('/')
def start():

or in some place which you like

S. Andy
  • 1
  • 5