0

I am new to Flask and am trying to think how I want to approach an idea.

I am building an Aquarium Controller on the Raspberry Pi using Python. I have already built one that works quite well on the Arduino but would like to move to Raspberry Pi and add a web interface.

My approach was to right the Python code along with Flask in the same application. But when I ran the code I had to have the browser open to the web server before the app would actually execute.

I need the base code to run irrespective of what is happening on the web server.

Because I am controlling things like the topoff pumps, main pumps, and heater, I need the application to run continuously regardless of the webserver.

My question is, can Flask be configured to run as a passive function to allow for the critical part of the code to work without the web server?

I also thought I could export the web server data to a file then, write a separate application for the webserver. The limitation here is I would like to be able to interact with the core application from the browser (i.e. turn pumps on/off from the browser).

from flask import Flask, render_template
import datetime
import RPi.GPIO as GPIO

#use Channel numbering for rpi pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

#import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

# Software SPI configuration:
CLK  = 18   # physical pin 12
MISO = 23   # physical pin 16
MOSI = 24   # physical pin 18
CS   = 25   # physical pin 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

# Setup Relays
Relay_Ch1 = 26   # physical pin 37
Relay_Ch2 = 20   # physical pin 38
Relay_Ch3 = 21   # physical pin 40

GPIO.setup(Relay_Ch1,GPIO.OUT)
GPIO.setup(Relay_Ch2,GPIO.OUT)
GPIO.setup(Relay_Ch3,GPIO.OUT)

#Initialize Relays to OPEN

GPIO.output(Relay_Ch1,GPIO.HIGH)
GPIO.output(Relay_Ch2,GPIO.HIGH)
GPIO.output(Relay_Ch3,GPIO.HIGH)

#Optical Switch Setup
OptSw1 = 17   # physical pin 11
OptSw2 = 27   # physical pin 13
GPIO.setup(OptSw1, GPIO.IN)
GPIO.setup(OptSw2, GPIO.IN)

def RSVR():
        values = float(mcp.read_adc(0))
    global levels
        levels = int(values/1023*100)

def TOPOFF():
    global Status1, Status2
    OptSwSts1=GPIO.input(OptSw1)
    if OptSwSts1 == True:
        Status1 = "Close"
    else:
        Status1 = "Open"

    OptSwSts2=GPIO.input(OptSw2)
        if OptSwSts2 == True:
                Status2 = "Close"
        else:
                Status2 = "Open"

    if now.hour % 4 == 0:
        if now.minute == 59:
            if now.second <= 15:
                if OptSwSts1 == False and OptSwSts2 == False:
                    GPIO.output(Relay_Ch1,GPIO.LOW)
                    print "Topoff Relay_Ch1 engaged", now
                else:
                    GPIO.output(Relay_Ch1,GPIO.HIGH)
            else:
                GPIO.output(Relay_Ch1,GPIO.HIGH)

def RELAYS():
    global Relay1, Relay2, Relay3
        Relay1_Sts=GPIO.input(Relay_Ch1)
        if Relay1_Sts == True:
                Relay1 = "Open"
        else:
                Relay1 = "Closed"

        Relay2_Sts=GPIO.input(Relay_Ch2)
        if Relay2_Sts == True:
                Relay2 = "Open"
        else:
                Relay2 = "Closed"

        Relay3_Sts=GPIO.input(Relay_Ch3)
        if Relay3_Sts == True:
                Relay3 = "Open"
        else:
                Relay3 = "Closed"

app = Flask(__name__)
@app.route("/")

def web():
    global now
    now = datetime.datetime.now()
    timeString = now.strftime("%Y-%m-%d %H:%M:%S")

    RSVR()

    TOPOFF()

    RELAYS()


    templateData = {
        'title' : 'AquaCR',
        'time' : timeString,
        'levels' : levels,
        'OptSw1' : Status1,
        'OptSw2' : Status2,
        'Relay1' : Relay1,
        'Relay2' : Relay2,
        'Relay3' : Relay3
        }
    return render_template('main.html', **templateData)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)
  • 1
    There is a very similar question which might be useful https://stackoverflow.com/questions/14384739/how-can-i-add-a-background-thread-to-flask – nutic May 12 '18 at 08:29
  • I found a really good step-by-step tutorial using a DHT22 sensor and storing the data in a database, then recalling the data and finally displaying in a browser and charting the data. – Paul Thomas May 14 '18 at 02:57
  • http://www.instructables.com/id/From-Data-to-Graph-a-Web-Jorney-With-Flask-and-SQL/ – Paul Thomas May 14 '18 at 02:57

0 Answers0