Quantcast
Viewing all articles
Browse latest Browse all 1275

Python • Trouble with Flask

Good day everyone, I need another set of eyes or two.
I'm working on a Flask web server project for school and can't seem to figure an issue out. The project is to have a RPI4 serve a website that
has two buttons that control a LED, a LED status indicator 1= on 0=off, and a SoC temp read out. I have everything but the temp part working. Can someone please offer some guidance. The temp code will print out the correct temp, but I cant get the value to show on the webpage. See below for code and index file

Code______________________________________________________________________

Code:

[import RPi.GPIO as GPIOfrom flask import Flask, render_template, requestapp = Flask(__name__)GPIO.setmode(GPIO.BCM)GPIO.setwarnings(False)#define actuators GPIOsledRed = 17#initialize GPIO status variablesledRedSts = 0tempSts = float()# Define led pins as outputGPIO.setup(ledRed, GPIO.OUT)   # turn leds OFF GPIO.output(ledRed, GPIO.LOW)@app.route("/")def index():import io f = open("/sys/class/thermal/thermal_zone0/temp", "r")t = f.readline ()temperature = float(t)/1000print (temperature)# Read Sensors Status    ledRedSts = GPIO.input(ledRed)    tempSts = temperature    templateData = {              'title' : 'GPIO output Status!',              'ledRed'  : ledRedSts,              'temperature'  : tempSts,          }    return render_template('index2.html', **templateData)@app.route("/<deviceName>/<action>")def action(deviceName, action):if deviceName == 'ledRed':actuator = ledRed   if action == "on":GPIO.output(actuator, GPIO.HIGH)if action == "off":GPIO.output(actuator, GPIO.LOW)     ledRedSts = GPIO.input(ledRed)   templateData = {              'ledRed'  : ledRedSts,              'temperature'  : tempSts,}return render_template('index2.html', **templateData)if __name__ == "__main__":   app.run(host='0.0.0.0', port=80, debug=True)/code]Index:_______________________________________________________________________[code] <!DOCTYPE html>   <head>      <title>GPIO Control</title>      <link rel="stylesheet" href='../static/style2.css'/>   </head>   <body>         <h1>LED Control</h1><a  href="/ledRed/on" class="button">LED ON</a> <br><br><a href="/ledRed/off"class="button">LED OFF</a><br><h1> Status </h1><h4> 0=OFF  1=ON</h4><h3>LED is {{ ledRed }}</h3>            <a>Temperature is {{ temperature }}</a><br>        </form>        </body>        </html>

Statistics: Posted by beardenr — Sun Mar 03, 2024 3:16 am



Viewing all articles
Browse latest Browse all 1275

Trending Articles