In this short tutorial you can see how we schedule the running of Processing sketches (code) on a Raspberry Pi. In part 1 you can see how to set up a processing sketch to run on a schedule using CRON scheduling. In part 2 you can see how to set it up so the sketch runs when the Pi boots up, and make scheduled reboots using LXSessions and CRON.

What you need

A Raspberry PI with Raspberry Pi OS, or Rasbian.

Basic knowledge of Raspberry PI and Processing.

The Processing sketch

For this tutorial we are using a simple Processing sketch that writes three random numbers to a text file (.txt) in the sketch folder. Each time the code runs it overwrites the previous version of the file.

Copy the Processing code from this link. Paste it into an empty Processing sketch, and save it as Random_number_to_file

Since we only want the code to run once every time the PI starts up, we write our code inside the setup() function in Processing. We also uses the exit() function as the last thing in the code to make the program (sketch) exit when it is done. 

Move the Processing sketch with its enclosing folder called Random_numbers_to_file to the desktop of your Raspberry PI. The path to the .pde file with the code in should then be: /home/pi/Desktop/Random_numbers_to_file/Random_numbers_to_file.pde

Install Processing on the Raspberry PI

Open the Terminal application on your Raspberry PI and run the following command to install Processing:

curl https://processing.org/download/install-arm.sh | sudo sh

Processing should then be downloaded and installed. NB: You will need a internet connection for this to work…

Part 1

Scheduling with CRON

CRON is a built-in scheduling service that allows us to execute commands and scripts on a time schedule. You can set up these schedules from the terminal through a text editor.

Before we get started with CRON just a bit of helping info regarding timezone interpretation. We have experienced that CRON not necessarily follows the local timezone, or the universal one. Therefore we have here below posted all the location settings we used to make it work with the Copenhagen timezone. This is important since we experienced it can be difficult to guess which timezone CRON is using, or just confirm that it is using the one you expects it to do.

Location settings found at: Menu (raspberry) -> Preferences -> Raspberry Pi Configuration  (opens a dialogue) -> Localisation

If you are in the same timezone, hopefully you will not have any issues using these settings.

processing-java

Together with the Processing installation comes a feature (processing-java) for running the sketches with a terminal command. It is this feature we are using to run the sketches with. The command we are going to use comes here: DISPLAY=:0 /usr/local/bin/processing-java –sketch=/home/pi/Desktop/Random_numbers_to_file –run

Try to use it in a terminal window to see it working. You wont se much happening except a textfile being generated/updated in the sketch folder. Each time you run the sketch new numbers should be in the file.

NB: the DISPLAY=:0 part is not really needed when running sketches manually from the terminal, but is needed when running visual sketches with CRON, so we use it here just to be sure you have it for your own project.

CRON

CRON has a special notation for time scheduling that you can read more about here (link), and here is an editor (link) that makes it easier to generate the right timing sequence. The timing sequence consists of five slots (m h dom mon dow). Below are given three different examples on different timings.

Every minute: * * * * *

At 1 minute past every hour: 1 * * * *

Every day at one o’clock (13:00): 0 0 * * *

Start by opening a terminal window and typing: crontab -e

If it is the first time using cron on this Pi you will be prompted to choose a text editor. We use Nano since it is fairly easy to use. Now go to the bottom of the text document that just opened in the terminal and write the first one: * * * * * DISPLAY=:0 /usr/local/bin/processing-java –sketch=/home/pi/Desktop/Random_numbers_to_file –run

Now close CRON (press ctrl+x). Every time the clock counts up one minute the sketch runs, and the text document (randomNumbers.txt) in the sketch folder is updated.

Try a few different timings until you get the hang of it.

 

Thats it for part 1     🙂

 

Part 2

Run Processing sketch at boot

In order to run the Processing sketch each time the Raspberry Pi starts up do the following…

In Terminal type: sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

This should open a file in the Terminal text editor nano.

Go to the bottom of the file and write or paste the following on a new line: @processing-java --sketch=/home/Desktop/Random_numbers_to_file --run

NB: Note that the path leads to the folder with the .pde file in, not to the .pde file itself.

Now type the shortcut ctrl+x to exit the nano editor. When prompted to save the changes press Y.

Now try to reboot the Raspberry PI and see if the Processing sketch runs at boot, and changes the random numbers in the text file. You might be able to see a small squared sketch window open and close again. No matter if you see the window or not, you will be able to see the numbers change in the file, each time you boot up the Raspberry PI. After boot wait for a little while (about 30 sec), to make sure the autostart process han been running.

Setting up a Cron job to reboot the Raspberry PI

Now to the final step of automating the reboot.

In the following example we will set up a reboot of the PI once a day at the same time each day.

In the Terminal type crontab -e. This again opens a text file inside the Terminal. 

NB: If you are prompted to choose which editor to use for cron, type the number corresponding to the nano editor. If not disregard this info…

Go to the end of the file and type 15 09 * * * sudo reboot on a new line. This should make the PI reboot every day at 9:15 AM. For now all you need to know is this syntax mm hh * * * sudo reboot this will reboot the PI every day at the given minute (mm) and given hour (hh).

There are plenty of other options for timing tasks with cron you can read more about them here: raspberrypi.org/documentation/linux/usage/cron.md or by googeling cron scheduled tasks raspberry pi.

Warning: When you start to play around with this. Don’t put in * * * * * sudo reboot. This will make your PI reboot every minute. Since it takes some time to boot up, you risk loosing control of the PI, since you won’t be able to alter the CRON file again before the next reboot is starting. :-/

 

Now hopefully you are up and running with a Raspberry PI rebooting once a day and running a Processing sketch…