Turning the Raspberry Pi into a device

To turn the Raspberry Pi into an embedded device which can run headless and be switched on and off by plugging it requires some preparations:

Secure the SD card from being corrupted

out of the box the Pi wants to be treated as a computer which get proper shut-down via commandline or gui on xorg. In an embedded context this can't be done because it usually runs then without monitor and keyboard. If in that case the wallwart gets unplugged, the filesystem on the sd card could get corrupted.

To prevent this the configuration can be changed so the card gets mounted readonly which is in many cases sufficient. If your application needs to store some information of course not, then different considerations has to be made, like mounting another partition or USBstick for writing.

To configure readonly, the fstab needs to be changed with your favorite editor:

$ sudo vi /etc/fstab

and make follwing changes:

PARTUUID=xxxxxxx / ext4 defaults,noatime,ro 0 1 
tmpfs /tmp tmpfs defaults,size=30M 0 0

the ro is key here, standing for read only mounting of root /

Upon a reboot the system is readonly and much less likely corrupted. We run this on exhibitions several weeks long with daily plugging on and off without problems.

But wait you might think, now that its readonly how can I ever make changes to it?

The whole root filesystem can be remounted in read and write mode on the fly with

$ sudo mount / -o remount,rw

without rebooting you can make changes on the card. But its temporarly, the next reboot makes it read only again unless you change back the fstab, removing the ro from the / entry.

Optional some background programs which are eager to write information, can be disabled, on Raspbien Jessie its

$ sudo systemctl disable rsyslog 
$ sudo systemctl disable dphys-swapfile

autostart programs

there were different methods for autostarting programs, varying which Raspbien version you use. With the current Jessie it first needs to make booting directly in which can be set with raspi-config.

The program to autostart can be set in /etc/profile which can be edited as sudo, add as the last line the command you want to schedule. Alternative is to write the command as last line in .bashrc. In both cases your command will be also executed each time you open a terminal, which is not problem for a headless system.

For Pure Data for instance it can be

pd -audiooutdev 3 -r 48000 -nogui -outchannels 4 tms.pd

or Csound with realtime low latency audio:

sudo csound -d --sched -+rtaudio=alsa -iadc:plughw:1,0 -odac:plughw:1,0 --fftlib=1 csoundFXTest.csd

of course you need to adapt the parameters to your environment.