Processing / Raspberry Pi: Auto Run a Sketch on Boot

I made this tutorial as I have just recently learned how to do this myself, I have been working on a Processing Sketch to do generative and audio reactive visuals for my musical performance. As I already carry around a 16kg synthesiser I don’t want anymore setup complexity for gigs, and don’t really want to be lugging a screen, mouse and keyboard around effectively just to press play! So after a little research I found a nice and simple method that adds executes a script on launching the x-session.

If you don’t know Processing, then it is a popular and powerful IDE for doing graphics, animation and interactive visual work in general – and it is free. There is a distribution for the Raspberry Pi with useful extra libraries both bundled and available through 3rd parties that make good use of the Pi hardware, such as GPIO access. So if you want to build an interactive installation that perhaps plays video, and reacts to sound or other environmental stimuli – this might be a useful route for you! Also, you would only need to make basic modifications to the steps shown below to run other programs such as Python scripts.

Disclaimer: Tested and working on the Jessie-Pixel Raspbian distribution with Processing 3.2.3 – Presumes that you already have the x-session setup to autorun as it does by default. I cannot guarantee the method will work with previous versions of either as I believe the java executable wasn’t included until a more recent version of Processing, and on different Raspbian distributions, you might find that config files are located or named differently – I will try my best to keep this page up to date as things change. Please put a message in the comments below if you encounter any problems.

 

What you need:

  • A Raspberry Pi with Processing installed. You can find instructions for installing Processing 3 here:
  • A Processing sketch file (make something simple that you know will run under normal circumstances as a test)
  • A means of editing text files on the Pi, I would recommend that you use nano either in a Terminal session on the Pi, or by SSH from another machine – but if you have a preferred method that is fine

How to do it:

This is so easy that I am not going to include pictures in this tutorial. I presume that you already have a Processing sketch on your Pi that you want to be able to execute – if you don’t and you just want to learn, make something simple like a sketch that renders a box on the screen.

1. Save your sketch somewhere straight forward in your home folder – the sketchbook folder for Processing is an obvious choice. Make a note of the file path. e.g /home/pi/sketchbook/mySketch

2. Open terminal and use nano to create a new text file, type the following lines in terminal, name the file as you wish, this will be saved in your current directory so I would suggest making the directory your home folder:

cd ~

sudo nano myScript.sh

3. In this new text file, we are going to include a script, albeit a basic one that is just a single line. This will instruct the java executable that is bundled with Processing to run your sketch, write the following line in the text file:

processing-java –sketch=/home/pi/sketchbook/mySketch –run

Hopefully that is pretty self explanatory, the script says “with processing java, find this sketch, run”. Once you have done this, press ctrl+x to save the file.

4. Before we go on, we can test our script by entering the following line in Terminal:

bash myScript.sh

This will probably take a couple of seconds, but if successful then you should see your sketch run in a new window. If you are successful, then press esc to close the sketch and move on to the next step.

5. Next we need to edit a config file that determines what the x-session does on boot, we want to tell it to run our script when it is ready. First use nano to open the file:

sudo nano ~/.config/lxsession/LXDE-pi/autostart

The file that opens will probably have a couple of basic lines in it already.

6. On a new line at the bottom of the file, enter the following text:

@bash /home/pi/myScript.sh

It is important to include the @ symbol at the start of the line.

7. Press ctrl+x to save your changes, do not rename this file!

8. Now, hopefully you are done! Test that your script works by rebooting your Pi, you can do this in Terminal with:

sudo reboot

Now the rest is up to you! As far as I have found, all features that are functional in Processing 3 on the Pi will be functional in the java executable. Have fun and feel free to leave comments in the section below.

Advertisements

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