Raspberry Pi project – integration

Ideally, the logging / data storage script would run automatically, as would the graph image generation script.  There are multiple ways to make that happen, and some of them will vary depending on your operating system.

The simplest way is to just run the scripts a a scheduled process using cron.  If you do this, you should remove the endless loop and sleep commands from the scripts.  Since cron will execute the scripts on the schedule you choose, there is no need for the script to sleep and then wake up and run through a loop again.

If you choose to run them through cron, you’ll need to run the logging / data storage script every 5 minutes, so the minute parameter in your cron file will be “*/5”, meaning every 5 minutes.  The other timing parameters will be simply “*”.

For the graph image generation script, you would probably run it once each hour.  To run it at the top of the hour, set the minute value to “0”, and the other parameters to “*”. That means cron will run the script every time the minute is 0 (top of the hour).

Since I like the scripts to run as system services, my scripts have the loop and sleep commands in them.  On a linux operating system using systemd, you’ll create a system service for running the scripts. To do this you need root access – either switch to the root user or use sudo.  Go to the /etc/systemd/system directory, and create a file name temperature_logger.service.  Put the following code in it, adjusting the path to the file as required for your environment.




This file should be owned by root, with 644 permissions.

To activate this as a system service, type “systemctl enable temperature_logger.service”, followed by “systemctl start temperature_logger.service”. This will now start automatically when the computer is booted. To stop it from running, type “systemctl stop temperature_logger.service”. Note that it will restart if the computer is rebooted. If you don’t want it to start unless you start it, type “systemctl disable temperature_logger.service”.

Setting up the graph image generations as a system service is basically identical, except that the service file will have a different name, and the file name on the ExecStart line will be different.

It is your preference as to how you want to execute these scripts. Other OSes will offer different methods that give the same result.

To rotate the log file, we need to add a file into the /etc/logrotate.d/ directory. The name isn’t critical; I chose “mylogs” to differentiate it from the files added when other packages are installed. Presuming that you left the name of the log file as it was in the example code, here is what you’ll need to add.

/var/log/temp_humidity.log {
    su root adm
    rotate 7
    create 644 root adm
    maxage 7

This will keep 7 days worth of log files. The current log file and yesterday’s file are not compressed, the other log files are compressed to save space.

I hope you’re found this little project documentation useful. You could extend it by adding another sensor, perhaps locating this one outdoors. The documentation says that the sensor can be connected to the Raspberry Pi by wires as long as 50 yards, so you have some flexibility as to where you place it.

Leave a Reply