Web view for BME688 air quality sensor.
Reference: electro-dan.co.uk
Any modern (RAM >= 512 MB) Raspberry Pi should work fine. Below is a picture of my setup with a Raspberry Pi Zero 2 W enclosed in a Pibow Zero 2 W case with a Pimoroni BME688 sensor with a female right angle header soldered on sticking out.
These steps are for MariaDB.
- Create user
db_userwith write privileges for logger and add password tologger/password.txt. - Create user
db_readerwith read-only privileges for view and add password toview/password.txt. - Create database
bme688_telemetry. - Create a table inside
bme688_telemetrycalledtime_series:
CREATE TABLE time_series (
timestamp DATETIME NOT NULL,
iaq DECIMAL(5, 2),
iaq_accuracy TINYINT(1) UNSIGNED,
static_iaq DECIMAL(5, 2),
static_iaq_accuracy TINYINT(1) UNSIGNED,
co2_equivalent DECIMAL(6, 2),
co2_accuracy TINYINT(1) UNSIGNED,
breath_voc_equivalent DECIMAL(5, 1),
breath_voc_accuracy TINYINT(1) UNSIGNED,
raw_temperature DECIMAL(4, 2),
raw_pressure DECIMAL(8, 2),
raw_humidity DECIMAL(5, 2),
raw_gas DECIMAL(8, 2),
stabilization_status TINYINT,
run_in_status TINYINT,
temperature DECIMAL(4, 2),
humidity DECIMAL(5, 2),
gas_percentage DECIMAL(4, 1),
gas_percentage_accuracy TINYINT(1) UNSIGNED
);- Enable scheduler:
SET @@GLOBAL.event_scheduler = ON;- Create event that hourly cleans up measurements that are one day old. If you want to see measurements on a longer time scale, just change the
INTERVALvalue.
CREATE EVENT AutoDeleteExpiredRows
ON SCHEDULE
EVERY 1 HOUR
DO
BEGIN
DELETE LOW_PRIORITY FROM bme688_telemetry.time_series WHERE timestamp < DATE_SUB(NOW(), INTERVAL 1 DAY);
END- Create virtual environment in folder
logger:
python -m venv env- Activate virtual environment:
source env/bin/activate- Install MySQLdb and BME68x Python packages. You might also want to run burn_in.py and provide your own
state_bme688.txtfile for more accurate measurements. - Deactivate virtual environment:
deactivate- Copy service file:
sudo cp bme688_logger.service /etc/systemd/system/- Edit service file to match your directory structure:
sudo nano /etc/systemd/system/bme688_logger.service- Reload services and enable and start logger:
sudo systemctl daemon-reload
sudo systemctl enable bme688_logger
sudo systemctl start bme688_loggerThe logger service will now write measurements to the database at XX:00, XX:15, XX:30 and XX:45.
- Create virtual environment in folder
view:
python -m venv env- Activate virtual environment:
source env/bin/activate- Install packages:
pip install -r requirements.txt- Deactivate virtual environment:
deactivate- Copy service file:
sudo cp bme688_view.service /etc/systemd/system/- Edit service file to match your directory structure:
sudo nano /etc/systemd/system/bme688_view.service- Reload services and enable and start web view:
sudo systemctl daemon-reload
sudo systemctl enable bme688_view
sudo systemctl start bme688_viewYou should now be able to see your BME688 at your Raspberry Pi's port 8050.
You can see more data in the dropdown menu by uncommenting lines from col_to_label declaration in bme688_view.py.
Dash, by default, gets some resources from external CDNs. We have our own server, so relying on other servers is not necessary. To fix this, I added the required .css files to the assets folder and added these two lines of code:
app.css.config.serve_locally = True
app.scripts.config.serve_locally = TrueSpeaking of .css files, if you don't like the current theme (SLATE), you can download other themes here. Then update this part of bme688_view.py:
SLATE = "assets/slate/bootstrap.min.css"
app = Dash(external_stylesheets=[SLATE])You can change the look of the graph, by changing the template parameter here:
fig = px.line(data_frame=df, x="timestamp", y=val, labels=col_to_label, template="plotly_dark")It also seems like the database connection will eventually timeout and reloading the page will at first show a broken graph. Adding pool_pre_ping=True to the create_engine call fixed this.


