Home Automation [Part 2] – Scheduling with CRON

# m h dom mon dow command
# * * * * *  command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)

Following on from my last post about the start of a Cheap, Easy Raspberry Pi Home Automation System this post shows how you can schedule outlets to turn off and on at particular times. This is achieved very easily on the Raspberry Pi using the cron program. I suggest reading this introduction to cron on the raspberrypi.org website.

As you can probably guess, we add a cron entry each time we want to turn on or off one of our wireless outlets. This means we can turn on the coffee machine at 7am each morning for example and then turn it off at 7:45am if it wasn’t already off.

We use the ‘send’ command to trigger the wireless outlets as described in the previous post. Firstly you need to know where the ‘send’ was created on the disk. In the previous post it would have been compiled to the ~/rcswitch-pi/ directory. If this sounds correct, try to run it using the full path: (replace USERNAME first!)

sudo /home/USERNAME/rcswitch-pi/send 0 0 0

If this works, copy the command (without the ‘sudo’ part) so we can add a cron entry. Edit the cron file as the root user with the command:

sudo crontab -e -u root

We need to use the ‘root’ user’s crontab here as the ‘send’ command and wiringpi library require root access. Now we can add a line to the cron file. For example, to turn on outlet 4 at 7am every day:

0 7 * * * /home/USERNAME/rcswitch-pi/send PIN 4 1

Obviously you should replace USERNAME and PIN with your setup. To turn off the same outlet at 7:45am:

45 7 * * * /home/USERNAME/rcswitch-pi/send PIN 4 0

Simple!

Advertisements
This entry was posted in Automation and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s