Home weather station with Raspberry Pi Pico & Kitronik Air Quality Board.
Why? To monitor and record indoor readings that will facilitate modifications for greater comfort and allergen control.
In the original setup, the server is run from a Raspberry Pi, while development takes place on a laptop. They have slightly different needs for their environment setup and so are addressed separately here.
Overview:
- Environment setup on Mac/Linux computer
- Environment setup on Raspberry Pi
- SQLite Database
- Raspberry Pi Pico W + Kitronik Air Quality Board
- Development Server
- Deploying to Production
Once it is set up and running, graphs will begin to generate on the Visualise page:
Use pyenv
to change the python version for the project directory.*
pyenv install 3.11.10
pyenv local 3.11
* Python 3.11 in this case to match the Raspberry Pi that will host the server for continuous data logging
Ensure the following is in your ~/.zshrc
:
export PATH="/Users/username/.pyenv:$PATH"
eval "$(pyenv init -)"
To set up poetry
environment:
poetry shell
poetry install
After this point, continue to SQLite Database.
Raspberry Pi model 1B: Raspbian GNU/Linux 12 (bookworm)
Pillow
requires zlib
and libjpeg
. Install with:
sudo apt install libjpeg-dev zlib1g-dev
This project requires numpy
to be installed with apt. See the section Known issues with numpy for more detail.
sudo apt install python3-numpy
venv
is used instead of poetry
on the Raspberry Pi. It is lighter weight and works well to lock down the version of numpy
due to the problems listed below.
A requirements.txt
is generated with poetry
from a Mac/Linux computer and then used by venv
on the Raspberry Pi. See the Development section for more details on generating the requirements.txt.
ssh
into the Raspberry Pi, and from the weather-station
directory, run the following:
python -m venv --system-site-packages .venv
source .venv/bin/activate
pip install --prefer-binary -r requirements.txt
Note: --prefer-binary
directs pip
to use piwheels versions of packages if they are available. This saves time on compilation.
After this point, continue to SQLite Database.
Some issues may be encountered with numpy
throwing a ChefInstallError
when running poetry install
on a Raspberry Pi.
If numpy
appears to install correctly, it can be tested by opening Python and trying to import numpy
. If it mentions the following error and fails to import then a system installation of numpy
must be used:
libf77blas.so.3: cannot open shared object file: No such file or directory
The solution is to make numpy
available globally rather than installing it with pip
in the venv
.
pip3 uninstall numpy # remove previously installed version
sudo apt install python3-numpy
See https://numpy.org/devdocs/user/troubleshooting-importerror.html#raspberry-pi
From the src
directory, initialise the SQLite database with:
flask --app server init-db
The database will appear in the instance
directory as weather.sqlite
- Copy scripts from
rp2
onto Raspberry Pi Pico. - Change constants at the top of the copy of
main.py
on the Pico:
SERVER_URL = 'localhost'+'/data'
WIFI_NAME = 'Wifi_name'
WIFI_PASSWORD = 'Wifi_password'
From this point see Development Server or Deploying to Production as required.
- To run the server go to the
src
directory and execute:
flask --app server run --debug --host=0.0.0.0
If running the development server from the Raspberry Pi, remove the --debug
option
These instructions are written for a server that is run from a Raspberry Pi.
Start Gunicorn WSGI server from src
directory using:
gunicorn -c server/gunicorn_config.py server:gunicorn_app
Set up the config server/gunicorn_config.py
:
bind="127.0.0.1:8000"
workers=2
Install the nginx HTTP server:
sudo apt-get install nginx
Set up a symbolic link to the at /etc/nginx/sites-enabled/weather-station.config
:
cd /etc/nginx/sites-enabled/
sudo ln -s /home/{username}/Code/weather-station/src/server/nginx/weather-station.config .
Nginx
can be started with:
sudo systemctl start nginx
Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted. To reload configuration, execute:
sudo nginx -s reload
The last step is to automatically run the server on startup.
This is done with thesystemd
system and service manager, which is used by Raspberry Pi.
For Gunicorn we will need a new service that systemd
can run.
(Nginx has already created its own service on installation.)
Create a new file weather-station.service
for systemd
in the folder /usr/lib/systemd/system
:
[Unit]
Description=Weather Station
After=multi-user.target
[Service]
WorkingDirectory=/home/{username}/Code/weather-station/src
ExecStart=/home/{username}/Code/weather-station/.venv/bin/gunicorn -c server/gunicorn_config.py server:gunicorn_app &
[Install]
WantedBy=multi-user.target
*Gunicorn is started with &
so that it runs in the background.
Finally, both Gunicorn and nginx must be started and enabled so that they can run automatically at startup of the Raspberry Pi:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start weather-station
sudo systemctl enable weather-station
(If needed the services can be stopped with sudo systemctl stop {service}
.)
Setup is now complete!
- Update the
pyproject.toml
as required - Run
poetry install
to install new packages - Run
poetry lock
to update lock file - From within the
weather-station
dir, generate arequirements.txt
:
poetry export --without-hashes --format=requirements.txt > requirements.txt
- Copy over to the Raspberry Pi (via
scp
orgit
) and install using:
source .venv/bin/activate
pip install --prefer-binary -r requirements.txt
- The
rp2
directory mirrors the scripts that will go onto the Raspberry Pi Pico.