Using Cron with RaspBMC

I've had a bit of a change around this weekend, shuffling the various boxes in my media TV unit and generally fiddling purely for the sake of it. One of the more interesting changes I made was to put my RaspBMC media centre Pi into the Short Crust case that was sent to me by PiSupply.com, along with the PiLITEr board I had in my bits box.

The Short Crust case has a tinted top panel which I thought would be excellent in conjunction with the LEDs glowing through from my PiLITEr board. My aim was to have the LEDs showing when RaspBMC was on, so that I knew from a distance that the Pi was powered up, and also just to look cool alongside my other media boxes.

All I needed to do was get it to run the script every time I powered up, but Cron wasn't as straight forward as it usually is, due to this being a RaspBMC build rather than the usual vanilla Raspbian image.

Raspberry Pi Short Crust PiLITEr

My Short Crust with the PiLITEr lurking from inside

So what I'm doing here is enabling Cron on RaspBMC, creating a script to run the Pi-LITEr, and then adding that script to Cron to get it to run on boot.

Creating The Script

Back in April I wrote a blog on the PiLITEr board, and gave a handful of example scripts to use. I'm re-using my 'Knight Rider' style script so that my RaspBMC Pi will have a scanner light effect when it's running.

Here's the script I used:

 #!/usr/bin/python  
 #  
 # PiLITEr scanner script by the "Average Man"  
 #  
 # Check out http://AverageManVsRaspberryPi.com for more PiLITEr code examples  
 #  
   
 #import  
 import RPi.GPIO as GPIO  
 import time  
 import os  
   
 #Turn off annoying warnings  
 GPIO.setwarnings(False)  
   
 # Define PiLITEr to GPIO mapping  
 LED1 = 7  
 LED2 = 11  
 LED3 = 13  
 LED4 = 12  
 LED5 = 15  
 LED6 = 16  
 LED7 = 18  
 LED8 = 22  
   
 # Main program  
 def main():  
  GPIO.setmode(GPIO.BOARD)  
  GPIO.setup(LED1, GPIO.OUT) #Set GPIO pin to output (to 'give' power)  
  GPIO.setup(LED2, GPIO.OUT) #Set GPIO pin to output (to 'give' power)  
  GPIO.setup(LED3, GPIO.OUT) #Set GPIO pin to output (to 'give' power)  
  GPIO.setup(LED4, GPIO.OUT) #Set GPIO pin to output (to 'give' power)  
  GPIO.setup(LED5, GPIO.OUT) #Set GPIO pin to output (to 'give' power)  
  GPIO.setup(LED6, GPIO.OUT) #Set GPIO pin to output (to 'give' power)  
  GPIO.setup(LED7, GPIO.OUT) #Set GPIO pin to output (to 'give' power)  
  GPIO.setup(LED8, GPIO.OUT) #Set GPIO pin to output (to 'give' power)  
   
  #Set scanner speed  
  speed = 0.1  
   
  #Scanner Element  
  while 1:  
   #LED sequence down  
   GPIO.output(LED1,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED1,GPIO.LOW)  
   GPIO.output(LED2,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED2,GPIO.LOW)  
   GPIO.output(LED3,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED3,GPIO.LOW)  
   GPIO.output(LED4,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED4,GPIO.LOW)  
   GPIO.output(LED5,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED5,GPIO.LOW)  
   GPIO.output(LED6,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED6,GPIO.LOW)  
   GPIO.output(LED7,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED7,GPIO.LOW)  
   GPIO.output(LED8,GPIO.HIGH)  
   time.sleep(speed)  
   #LED sequence up  
   GPIO.output(LED8,GPIO.LOW)  
   GPIO.output(LED7,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED7,GPIO.LOW)  
   GPIO.output(LED6,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED6,GPIO.LOW)  
   GPIO.output(LED5,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED5,GPIO.LOW)  
   GPIO.output(LED4,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED4,GPIO.LOW)  
   GPIO.output(LED3,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED3,GPIO.LOW)  
   GPIO.output(LED2,GPIO.HIGH)  
   time.sleep(speed)  
   GPIO.output(LED2,GPIO.LOW)  
   
   
 if __name__ == '__main__':  
  main()  

I've changed how the time between each LED light works here as well.
Rather than updating the number in each instance of 'time.sleep(1)' I've used a global speed setting by defining the name 'speed' with the time, and just used 'time.sleep(speed)' across the script instead. This saves a lot of time when changing the speed of the scanner light.

Starting the Script at Reboot with Cron


First of all - what is Cron? Wikipedia has the full explanation: http://en.wikipedia.org/wiki/Cron.
In short, and in this context, it allows me to run scripts at set times, perfect for what we're doing here as I want this script to run at reboot.
Cron is usually straight forward, however additional steps are needed with RaspBMC due to the way the build is set up. By default, Cron is disabled, so let's sort that bit out first (you won't have to do this with Raspbian usually).
Let's enable Cron first. Go to the settings directory by using the following command:

 cd /home/pi/.xbmc/userdata/addon_data/script.raspbmc.settings/  

Now that you're in the right directory, open the settings file for editing (using nano):

 sudo nano settings.xml  

Navigate to the following line and change 'false' to 'true'

   


It should now look like this:

   


Press the usual 'Ctrl + X' and save the file under the same name.
That's Cron enabled, so now we just need to tell Cron which script to start up on reboot. To do this, use the following command to open up Cron for editing:

 sudo crontab -e  


You should now see this screen:

Cron, in all its glory


Ignore all the text in there already, just tab down to the bottom and add a new line like I have here. The line needs to point to your script - mine is in a directory called 'piliter' as you can see in the code below and in the screenshot:

 @reboot python /home/pi/piliter/test.py &  

You can see where I've added my reboot line here


Once again press 'Ctrl + X' and save/exit.
Try It Out
All you need to do now is reboot to see if it works. use the following command to reboot:

 sudo reboot  


RaspBMC takes a little longer to get to the Cron jobs on reboot (perhaps it's lower priority) but once the main interface loaded up, my PiLITEr scanner started running under the tinted arcylic cover of my Short Crust case. Now that's pretty...if you can ignore the dust on this close up!

Dusty...but still looking good


Give it a try yourself, and let me know via the comments section below if you get stuck.

Continue reading here: Raspberry Pi 3 - WiFi Comes To The Pi

Was this article helpful?

0 0