Projects / Raspberry Pi sensor dashboard with Flask

Raspberry Pi sensor dashboard with Flask

Intermediate1.5 hGuided buildReviewed

Build it step by step

0 of 5 done
  1. Pi 4

    Enable I2C

    Run sudo raspi-config, Interface Options, I2C, Enable. Reboot.

  2. Pi 4

    Wire the sensor

    VIN to pin 1 (3.3 V), GND to pin 9, SDA to pin 3, SCL to pin 5.

  3. Pi 4

    Check the address

    Run i2cdetect -y 1. You should see 76 or 77.

  4. Pi 4BME28026.0°C

    Install and run

    Create the virtual environment, install flask and RPi.bme280, then run the script.

  5. Pi 4

    Open it

    Find the Pi's address with hostname -I and open http://that-address:5000 on a phone on the same Wi-Fi.

In short

How do you build a raspberry Pi sensor dashboard with Flask?

A BME280 on the Pi's I2C bus feeds a small Flask web app that shows temperature, humidity and pressure on any phone on your Wi-Fi. You need Raspberry Pi 4 Model B (4 GB), Raspberry Pi 15 W USB-C power supply, SanDisk microSD card 32 GB (A1, Class 10), BME280 Environmental Sensor and Jumper wires female to female (40 pcs). It takes 5 steps and about 90 minutes.

Level
Intermediate
Time
About 90 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
BME280 VINPi pin 1 (3.3 V)Sensor power
BME280 GNDPi pin 9 (GND)Ground
BME280 SDAPi GPIO2 / SDA (pin 3)I2C data
BME280 SCLPi GPIO3 / SCL (pin 5)I2C clock

Code

Raspberry Pi OS Bookworm. Enable I2C in raspi-config and install Flask and RPi.bme280 in a virtual environment, as in the first lines of the code.

# Setup once:  sudo raspi-config  (Interface Options > I2C > Enable)
#              python3 -m venv --system-site-packages ~/dash && ~/dash/bin/pip install flask RPi.bme280
# Run:         ~/dash/bin/python dashboard.py   then open http://<pi-address>:5000
import smbus2, bme280
from flask import Flask, jsonify

ADDR = 0x76                       # 0x77 on some boards
bus = smbus2.SMBus(1)
cal = bme280.load_calibration_params(bus, ADDR)
app = Flask(__name__)

def read():
    d = bme280.sample(bus, ADDR, cal)
    return {"temperature": round(d.temperature, 1), "humidity": round(d.humidity), "pressure": round(d.pressure, 1)}

@app.route("/api")
def api():
    return jsonify(read())

@app.route("/")
def page():
    return """<!doctype html><meta name=viewport content="width=device-width"><title>Room</title>
<style>body{font-family:sans-serif;padding:24px}b{font-size:2.4em;display:block}</style>
<p>Temperature<b id=t>-</b></p><p>Humidity<b id=h>-</b></p><p>Pressure<b id=p>-</b></p>
<script>async function u(){const r=await (await fetch('/api')).json();
t.textContent=r.temperature+' °C';h.textContent=r.humidity+' %';p.textContent=r.pressure+' hPa'}
u();setInterval(u,5000)</script>"""

app.run(host="0.0.0.0", port=5000)

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
i2cdetect -y 176 (or 77) in the grid
http://pi:5000/apiJSON with temperature, humidity and pressure
Web pageValues refresh every 5 s
Pressure at sea levelAbout 1000 to 1015 hPa; lower in hill towns

If it does not work

Nothing in i2cdetect

I2C not enabled, or SDA/SCL swapped. Check pins 3 and 5.

"externally-managed-environment" error

Bookworm blocks system-wide pip. Use the virtual environment as shown.

Humidity missing or 0

Your board is a BMP280 (no humidity). Check the chip marking.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the raspberry Pi sensor dashboard with Flask?

Raspberry Pi 4 Model B (4 GB), Raspberry Pi 15 W USB-C power supply, SanDisk microSD card 32 GB (A1, Class 10), BME280 Environmental Sensor and Jumper wires female to female (40 pcs).

How long does the raspberry Pi sensor dashboard with Flask take to build?

About 90 minutes for a intermediate maker, in 5 steps.

How do I know the raspberry Pi sensor dashboard with Flask works?

i2cdetect -y 1: 76 (or 77) in the grid. http://pi:5000/api: JSON with temperature, humidity and pressure. Web page: Values refresh every 5 s.

Nothing in i2cdetect: what should I check?

I2C not enabled, or SDA/SCL swapped. Check pins 3 and 5.

externally-managed-environment" error: what should I check?

Bookworm blocks system-wide pip. Use the virtual environment as shown.

Humidity missing or 0: what should I check?

Your board is a BMP280 (no humidity). Check the chip marking.