ClareGarrard / weather-station

Home weather station with Raspberry Pi Pico & Kitronik Air Quality Board
MIT License
0 stars 1 forks source link

weather-station

Home weather station with Raspberry Pi Pico & Kitronik Air Quality Board

Temperature Pressure Humidity IAQ eCO2

Weather station home

Setup

In the orginal 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:

Once it is set up and running, graphs will begin to generate on the Visualise page:

Visualise page

Environment setup on Mac/Linux computer

Pyenv

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 -)"

Poetry

To set up poetry environment:

poetry shell
poetry install

After this point, continue to SQLite Database.

Environment setup on Raspberry Pi

Raspberry Pi model 1B: Raspbian GNU/Linux 12 (bookworm)

Dependencies

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

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.

Known issues with numpy

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

SQLite Database

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

Raspberry Pi Pico W + Kitronik Air Quality Board

From this point see Development Server or Deploying to Production as required.

Development Server

Deploying to Production

These instructions are written for a server that is run from a Raspberry Pi.

WSGI Server

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

HTTP Server

Install the nginx HTTP server:

sudo apt-get install nginx

Set up a config at /etc/nginx/sites-enabled/default:

server {
    listen 80;
    listen [::]:80;
    server_name raspberrypi.local;
    access_log  /var/log/nginx/weather-station.log;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }

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

Systemd: set up server to automatically run on startup

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!

Development

To update the environment:

  1. Update the pyproject.toml as required
  2. Run poetry install to install new packages
  3. Run poetry lock to update lock file
  4. From within the weather-station dir, generate a requirements.txt:
    poetry export --without-hashes --format=requirements.txt > requirements.txt
  5. Copy over to the Raspberry Pi (via scp or git) and install using:
    source .venv/bin/activate
    pip install --prefer-binary -r requirements.txt 

Development of Pico scripts

Appendix