In this short tutorial you can see how we schedule the running of Processing sketches (code) on a Raspberry PI. The method used here works by setting up the PI to run the Processing code at startup, and then making it reboot once a day, at a specific time.
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…
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…