Let's break this answer in two parts: scheduling a task and turning a led on and off, than wrap together. You can go to the end and just get the script if you prefer!
Scheduling a task
So I would suggest you use the atd
for one time scheduling in Raspbian. If you need something repetitive, use cron
- I will cover here only how to use the at
command. More details here: https://debian-handbook.info/browse/stable/sect.task-scheduling-cron-atd.html
First, you need to install it!
sudo apt-get install at
Now for using it, see the date right now on your raspbian - I'm assuming you are connected to it using ssh or is accessing through the terminal.
date
Let's make a blank folder on your home for testing purpose
mkdir ~/test
cd ~/test
Let's schedule a job!!
echo "echo \"teste\" > ~/test/test.log " | at 10:52
Let's wait it to be executed!!!
watch ls
The watch command will execute the command after it repeatedly and will output it's output with a clock, so we can confirm it works! (an archive named test.log
be created at the scheduled time.
Turning a LED on and off
Now you need to make a script to turn on and off a LED - I don't know how you are doing this and schedule it with using the at command. We will use the gpio
utility from wiringpi
. Let's learn about it.
sudo apt-get install wiringpi
I will suppose you want to turn on and off led 5, but do whatever you want. Let's test if it works. Try:
gpio -g mode 5 out
gpio -g write 5 1
You can turn the led off using:
gpio -g write 5 0
Let's do both!
Ok, now we need to wrap all of this in a bash script for ease of use.
Let's create ledsched.sh:
#!/bin/bash
timeat="$1"
duration="$2"
ledpin="$3"
endtime=`date +%H:%M -d " $timeat today + $duration"`
gpio -g mode $ledpin out
echo "gpio -g write $ledpin 1" | at $timeat
echo "gpio -g write $ledpin 0" | at $endtime
And call it using (remember to use first chmod +x ledsched.sh
):
./ledsched.sh 12:00 2hours 5
I'm scheduling to turn pin 5 on at 12:00 and off after 2 hours. If your led is at pin 5 it should work!
Oh, if you do have access to the visual interface, you can use the alarm-clock-applet. Install it from the command line with sudo apt-get install alarm-clock-applet
.