On this page
During operation, the weather station reads measurements from the sensors, transmits the gathered data to the remote server and executes maintenance procedures in regular intervals. To do so, we employ cron and systemd. For monitoring and debugging purposes, it is quite important to maintain logs of the programs you’re running. For this purpose, create a directory for log files, e.g.,
mkdir /home/pi/logs
Log files do not have to be manually created, as they are usually created by the programs run. Of course, you can use another path for your log file directory, just make sure that you adapt the corresponding paths in the guide below.
Cron jobs
In our setup, cron is used to
restart the battery voltage ready every 5 minutes in case its execution was stopped
check the connectivity to the internet and apply simple problem solving steps if the internet connection was lost every 15 minutes
check if all devices connected via USB (e.g., an external hard drive) are writable and apply some simple problem solving steps if they are not writable every 15 minutes
try to transmit the collected unsynchronized data to the remote server every hour
reboot the Raspi for maintenance every day
Per default, cron should already be installed on your Raspi. Verify this by executing
ps aux | grep cron
If there is no output, cron is not installed. In that case, execute
sudo apt update && sudo apt install cron
to install cron and enable it through
sudo systemctl enable cron
Now that cron is running, we can paste our cron jobs into the cron table. To do so, execute
crontab -e
and choose an editor of your choice, e.g., nano by entering 1 in the selection menu. Then paste the list of tasks
# Check connectivity
*/15 * * * * ~/.pyenv/versions/windbearer/bin/python -m wind_bearer_software.cellular_init_V2 > /home/pi/logs/cellular_error.log 2>&1
# Reboot
3 1 */2 * * ~/.pyenv/versions/windbearer/bin/python -m wind_bearer_software.rebooter > /home/pi/logs/rebooter_log.log 2>&1
# Transfer file to remote server
1 * * * * ~/.pyenv/versions/windbearer/bin/python -m wind_bearer_software.remote_transfer_V1 > /home/pi/logs/remote_transfer_log.log 2>&1
# Battery voltage log
*/5 * * * * ~/.pyenv/versions/windbearer/bin/python -m wind_bearer_software.voltage_reader > /home/pi/logs/voltage_reader_log.log 2>&1
# USB checker
*/15 * * * * ~/.pyenv/versions/windbearer/bin/python -m wind_bearer_software.usb_checker > /home/pi/logs/usb_checker_log.log 2>&1
into the end of the table of tasks that opened after choosing an editor. You can verify that these scripts are executed by checking for log files in your logging directory. It might take a while before log files are created, as some of the scripts are executed after larger time intervals. As you have probably not set the parameters in the /home/pi/wind-bearer-software/wind_bearer_software/parameter folder, you may encounter some Python errors in the log file. Nonetheless, this means your program is running. You can also change the time points at which each of the tasks is executed in the above list of tasks. Refer to the WindBearer Software Documentation to see which script belongs to which task and to the cron documentation on how to adapt the scheduling scheme.
Scheduling with systemd
The main runner, which reads the data from all the sensors except for the battery voltage, is scheduled as a service using systemd to ensure a fast restart after a crash. For data continuity, it is run continuously, though, data is only saved every 60 seconds. To set up the main runner service, head to the directory where service files are stored
cd /etc/systemd/system
and create a service file
sudo nano main_runner.service
in which you paste the following configuration
[Unit]
Description=Weather Station Main Runner
After=network.target
After=multi-user.target
[Service]
Type=simple
User=pi
Group=pi
WorkingDirectory=/home/pi
# Source .bashrc-like env manually
Environment=PATH=/home/pi/.pyenv/versions/windbearer/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Environment=PYTHONUNBUFFERED=1
Environment=XDG_RUNTIME_DIR=/run/user/1000
Environment=HOME=/home/pi
ExecStart=/home/pi/.pyenv/versions/windbearer/bin/python -m wind_bearer_software.mr_V6
StandardOutput=append:/home/pi/logs/mr_V6.log
StandardError=append:/home/pi/logs/mr_V6_error.log
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Make sure that the path in the line containing ExecStart= matches your virtual environmnet’s python executable and the paths in the lines containing StandardOutput and StandardError match with your logging directory.
Afterwards, reload all services by executing
sudo systemctl daemon-reload
and enable the main_runner service through
sudo systemctl enable main_runner.service
Now, the service will always be started at boot time and will be reexecuted 10 seconds after a crash. As it was not started yet, you can verify that the service is working by executing
sudo systemctl start main_runner.service
If the service is set up correctly, every 10 seconds the console output of the main runner or an error message should be pasted to
/home/pi/logs/mr_V6_error.log
For further configuration, we recommend stopping the service
sudo systemctl stop main_runner.service
and to delete the log file due to the continuous execution of the service. Do not forget to either reboot your raspi or starting the service again before deploying the weather station.
Initiating the Mobile Network Connection During Startup as a Oneshot Service
We schedule a script to try to reconnect to the mobile network if the connection was lost every 15 minutes as a cron job. Though, as cron jobs are scheduled at specific time points and not at startup, we also run this script as a oneshot service in order to improve the continuity of the mobile network connection.
Note
It is not strictly necessary that the Raspi is connected to the mobile network at all times. Nonetheless, it is recommendable that this is the case, so that you minimize the risk of data loss and keep the system clock synchronized at all times.
To set up this service, create and open a new service file
sudo nano /etc/systemd/system/cellular_init.service
and paste the following configuration into it:
[Unit]
Description=Restart cellular service
[Service]
Type=oneshot
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/pi/.Xauthority"
ExecStart=/home/pi/.pyenv/versions/windbearer/bin/python -m wind_bearer_software.cellular_init_V2
User=pi
StandardOutput=file:/home/pi/logs/restart_cellular_init_log.log
StandardError=file:/home/pi/logs/restart_cellular_init_error_log.log
[Install]
WantedBy=multi-user.target
If no logs directory exists yet, create it
mkdir /home/pi/logs
Finally, reload all services
sudo systemctl daemon-reload
and enable the cellular_init service
sudo systemctl enable cellular_init.service
