A simple system for monitoring temperature and humidity data from my flat.
The basic steps are as follows:
- Use systemd to run a couple of python scripts every minute to collect indoor and outdoor temp/humidity data.
- Log the results to
/var/log/climate
which is set up with atmpfs
mount. - On my mac, run a cron job to
rsync
the log files on a regular basis (currently every 5 minutes). - Use the ELK stack to consume, parse and index the log files and display pretty graphs.
Create a bash script for syncing with RPI.
#!/bin/sh
rsync -avz --remove-source-files matt@raspberrypi.local:~/logs/* ~/Dev/climate/logs/
Edit crontab using crontab -e
and add the following line in order to run the script on an hourly basis:
0 * * * * cd ~/Dev/climate && ./backup.sh
We now need to pick up the synced log files and ingest them into elasticsearch. To do that we need logstash:
brew install logstash-full kibana-full elasticsearch-full
Using brew serivces to run the ELK stack:
brew services list
DHT20 Pins to RPI pins
- 5v DC (Pi pin 2)
- SDA0 (Pi pin 3)
- GND (Pi pin 25, or any other ground pin)
- SCL0 (Pi pin 5)
To run the main.py
script on a scheduled basis I've used a systemd timer and service. These can be found at /etc/systemd/system
:
- climate.service
[Unit]
Description=Sends temp and humidity data to ELK stack
Wants=climate.timer
[Service]
Type=oneshot
ExecStart=/home/matt/climate.sh
[Install]
WantedBy=multi-user.target
- climate.timer
[Unit]
Description=Logs climate metrics
Requires=climate.service
[Timer]
Unit=climate.service
OnCalendar=*-*-* *:*:00
[Install]
WantedBy=timers.target
Following the approach mentioned here we create a directory within /var/log that is owned by a non-root user. This allows us to write logs without using sudo.
I set up tmpfs for /var/log/climate. This creates an in-memory drive and should help to reduce the number of writes made to the SD card. Hopefully this will increase its lifespan.
tmpfs /var/log/climate tmpfs defaults,noatime,size=16m 0 0
Then run sudo mount -a
to load the new mount. Running df -h
should show the new mount in place.