Python Forum
display multiple sensors on webpage python flask jinja
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
display multiple sensors on webpage python flask jinja
#1
Hi,
I am a bit stuck,There are plenty of tutorials and forum replies for one DS18B20 sensors but I cannot find any for multiple sensors that dont use esp826 or something else. My code (below) displays the temperature of 11 senrors in the console, but I am stuck with display more than one on a web page. i have seperated my code into different modules to keep things clean and so I can import them as I need, The first is nested dictionaries with the 11 sensors
sensorsID = {
    1 : {'name':'top Cylinder','ID': '28-020f92456b1b'},
    2 : {'name':'Middle Cylinder','ID': '28-000398430420'},
    3 : {'name':'Bottom Cylinder','ID': '28-03170017b5ff'},
    4 : {'name':'Boiler output','ID': '28-0212924519b0'},
    5 : {'name':'Boiler Input','ID': '28-000798430de0'},
    6 : {'name':'Kitchen Output','ID': '28-0202924581e6'},
    7 : {'name':'kitchen Return','ID': '28-02169245238b'},
    8 : {'name':'Back Area Output','ID': '28-0317000e43ff'},
    9 : {'name':'Back Area Return','ID': '28-0317003f1cff'},
    10 : {'name':'House Output','ID': '28-0416c4a3a0ff'},
    11 : {'name':'House Return','ID': '28-021692451c79'}
}
the next is the code to read these sensors,
def read_sensor(sensorID):  
    tempfile = open("/sys/bus/w1/devices/"+ sensorID +"/w1_slave")
    thetext = tempfile.read()
    tempfile.close()
    tempdata = thetext.split("\n") [1].split(" ")[9]
    temperature = float(tempdata[2:])
    temp_sensor = temperature / 1000
    return (temp_sensor)
the next is the code that works in terminal
from mySensorModule import read_sensor
from mySensors import sensorsID
import time

for sensor in sensorsID:
    print("The temperatue of ",sensorsID[sensor]['name'],
            " is ",read_sensor(sensorsID[sensor]['ID']))
But the problem arises when i try to display this in a webpage, I can display one sensor but not all, Here is the code I have tried but I get an error; sensors.py
from flask import (
    Blueprint, flash, g, redirect, render_template, request, url_for
)
from werkzeug.exceptions import abort
from mySensorModule import read_sensor
from homeHeating.auth import login_required
from homeHeating.db import get_db
from mySensorModule import read_sensor
from mySensors import sensorsID
import time


@bp.route('/sensors')
@login_required
def sensors():
    for sensor in sensorsID:
        name=sensorsID[sensor]['name'],
        temp=read_sensor(sensorsID[sensor]['ID']
        
    templateData = {
       'name' : name,
       'ID' : ID
       }
        return render_template('sensors/sensors.html', **templateData)
Error:
File "/home/pi/heating/homeHeating/sensors.py", line 20 templateData = { ^ SyntaxError: invalid syntax
this code is an adaptation of matt richardsons tutorial the html script looks like this;
{% extends 'base.html' %}

{% block header %}
  <h1>{% block title %}Sensors{% endblock %}</h1>
    <a class="action" href="{{ url_for('sensors.addsensor') }}">New Sensor</a>
{% endblock %}{% block devises %}
  {% for sensor in sensorsID %}
    <p>The {{ name }}
    the temperature of {{ name }} is {{ temp }}
    </p>
   {% endfor %}
{% endblock %}
if I use the origional code with this template no sensors are displayed, I hope you are able to follow this! Any help is needed.
Paul
Reply


Messages In This Thread
display multiple sensors on webpage python flask jinja - by pascale - Jan-27-2019, 09:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Browser won't parse text from yaml loaded into Jinja SpongeB0B 1 1,030 Jul-07-2022, 09:37 PM
Last Post: SpongeB0B
Question Accessing a value of a dictionary with jinja.... SpongeB0B 2 10,689 Aug-06-2021, 09:05 AM
Last Post: ndc85430
  Help using Flask for a countdown on webpage ZenBuddhism 1 2,282 Feb-05-2021, 03:35 PM
Last Post: snippsat
  Using range slider in flask webpage to use in python KimPet 2 7,752 Jan-23-2021, 11:58 PM
Last Post: snippsat
  Flask, Display a picture outisde the static SpongeB0B 6 15,828 Aug-29-2020, 05:15 PM
Last Post: nilamo
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 4,043 Aug-12-2020, 07:24 PM
Last Post: Alpy
  How to display XML tree structure with Python? sonicblind 13 42,363 Aug-12-2020, 02:05 PM
Last Post: mreshko
  Jinja sort values from request.form SpongeB0B 2 2,285 Jul-26-2020, 07:41 AM
Last Post: SpongeB0B
  how to create dynamic arguments to be passed in jinja template in url_for function? experimental 1 2,821 May-01-2020, 05:50 PM
Last Post: Larz60+
  Python values on a WebPage Petrouil 1 1,951 Apr-01-2020, 05:08 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020