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______________________________________________________________________
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#define actuators GPIOs
ledRed = 17
#initialize GPIO status variables
ledRedSts = 0
tempSts = float()
# Define led pins as output
GPIO.setup(ledRed, GPIO.OUT)
# turn leds OFF
GPIO.output(ledRed, GPIO.LOW)
##import io
##f = open("/sys/class/thermal/thermal_zone0/temp", "r")
##t = f.readline ()
##temperature = float(t)/1000
##print (temperature)
##@app.route('/sensor/<sensor>')
##def get_sensor_data(sensor):
## sensor = sensor.lower()
##if sensor == 'temperature':
## sensor_data = cpu_sensor_data()
## elif sensor == 'ssd':
## sensor_data = ssd_sensor_data()
# etc.
## return render_template('index2.html', **templateData)
@app.route("/")
def index():
##import io
##f = open("/sys/class/thermal/thermal_zone0/temp", "r")
##t = f.readline ()
##temperature = float(t)/1000
##print (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)
Index:_______________________________________________________________________
<!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>
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______________________________________________________________________
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#define actuators GPIOs
ledRed = 17
#initialize GPIO status variables
ledRedSts = 0
tempSts = float()
# Define led pins as output
GPIO.setup(ledRed, GPIO.OUT)
# turn leds OFF
GPIO.output(ledRed, GPIO.LOW)
##import io
##f = open("/sys/class/thermal/thermal_zone0/temp", "r")
##t = f.readline ()
##temperature = float(t)/1000
##print (temperature)
##@app.route('/sensor/<sensor>')
##def get_sensor_data(sensor):
## sensor = sensor.lower()
##if sensor == 'temperature':
## sensor_data = cpu_sensor_data()
## elif sensor == 'ssd':
## sensor_data = ssd_sensor_data()
# etc.
## return render_template('index2.html', **templateData)
@app.route("/")
def index():
##import io
##f = open("/sys/class/thermal/thermal_zone0/temp", "r")
##t = f.readline ()
##temperature = float(t)/1000
##print (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)
Index:_______________________________________________________________________
<!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