A few notes on working with the Raspberry Pi.
Run headless
- Write image onto the SD card.
- Place an empty file called
ssh
into the/boot
partition. - Place a file called
wpa_supplicant.conf
into the/boot
partition:
country=us
update_config=1
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="<Name of your WiFi>"
psk="<Password for your WiFi>"
}
Power it on, and ascertain its IP address from your router. SSH into it, using the default credentials (user: pi, password: raspberry).
Reduce power usage
Turn off HDMI (can be put in /etc/rc.local
):
sudo tvservice --off
Disable WiFi and/or Bluetooth (if using Ethernet connection). Put these lines at the bottom of /boot/config.txt
:
dtoverlay=disable-wifi
dtoverlay=disable-bt
… or for Raspberry Pi 3:
dtoverlay=pi3-disable-wifi
dtoverlay=pi3-disable-bt
Cut power to USB chip (if not using any peripherals) (can be put in /etc/rc.local
):
echo 0 | sudo tee /sys/devices/platform/soc/3f980000.usb/buspower >/dev/null
…or for Pi Zero:
echo 0 | sudo tee /sys/devices/platform/soc/3f980000.usb/buspower >/dev/null
Disable the PWR and ACT LEDs: Add these lines to /boot/config.txt
:
dtparam=act_led_trigger=none
dtparam=act_led_activelow=off
dtparam=pwr_led_trigger=none
dtparam=pwr_led_activelow=off
…or for Pi Zero:
dtparam=act_led_trigger=none
dtparam=act_led_activelow=on
Reduce SD card wear
An excellent Medium article on the subject with details. But to summarize:
- Remove extraneous logging packages:
sudo apt-get remove --purge triggerhappy logrotate dphys-swapfile
- Edit
/boot/cmdline.txt
and add the following to the end of the command line:fastboot noswap ro
- Install busybox in-memory logger instead of rsyslog:
sudo apt-get install busybox-syslogd
sudo apt-get remove --purge rsyslog
- Make filesystems on block devices read-only: add
,ro
flag (afterdefaults,...
to lines in/etc/fstab
) - Add
tmpfs
filesystems to the end ofetc/fstab
:
tmpfs /tmp tmpfs nosuid,nodev 0 0
tmpfs /var/log tmpfs nosuid,nodev 0 0
tmpfs /var/tmp tmpfs nosuid,nodev 0 0
- Move dhcp and spool related files to tmpfs:
sudo rm -rf /var/lib/dhcp /var/lib/dhcpcd5 /var/spool /etc/resolv.conf
sudo ln -s /tmp /var/lib/dhcp
sudo ln -s /tmp /var/lib/dhcpcd5
sudo ln -s /tmp /var/spool
sudo touch /tmp/dhcpcd.resolv.conf
sudo ln -s /tmp/dhcpcd.resolv.conf /etc/resolv.conf
- Update random seed generation to go to tmpfs:
sudo rm /var/lib/systemd/random-seed
sudo ln -s /tmp/random-seed /var/lib/systemd/random-seed
- Edit the file
/lib/systemd/system/systemd-random-seed.service
and add the lineExecStartPre=/bin/echo "" >/tmp/random-seed
under the[Service]
section, before theExecStart=
line.
Run script(s) on startup
- Add line(s) to
/etc/rc.local
- If it’s a long-running script, or continuously-running, then make sure to put an ampersand at the end of the line, so that the boot process can continue.
If you need to diagnose why a certain script failed during startup, you can redirect stdio for the commands that are executed in rc.local
by adding the following at the top:
exec 1>/tmp/rc.local.log 2>&1
set -x
See/kill background processes
- To see all processes:
ps aux
- To kill one of them:
sudo kill [pid]
Share folder via NFS
Install packages:
sudo apt-get install nfs-kernel-server portmap nfs-common
Add line(s) to /etc/exports:
/folder/path *(rw,all_squash,insecure,async,no_subtree_check,anonuid=1000,anongid=1000)
Then do the following:
sudo exportfs -ra
And for good measure:
sudo /etc/init.d/nfs-kernel-server restart
Set the network interface to a static IP
- Edit the file
/etc/dhcpcd.conf
- The file contains example lines for setting a static IP, gateway, DNS server, etc.
Automatically mount USB drive(s) on boot
When the Raspberry Pi is configured to boot into the desktop GUI, it will auto-mount USB drives, mounting them into the /media/pi
directory, with the mount points named after the volume label of the drive. However, if the Pi is configured to boot into the console only (not desktop), then it will not auto-mount USB drives, and they will need to be added to /etc/fstab
:
/dev/sda1 /media/path_to_mount vfat defaults,auto,users,rw,nofail,umask=000 0 0
(The umask=000 parameter enables write access to the entire disk.)
Basic GPIO in Python
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# input (pulled up):
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# output:
GPIO.setup(24, GPIO.OUT)
try:
if GPIO.input(23):
GPIO.output(24, True)
except KeyboardInterrupt:
print('interrupted.')
finally:
GPIO.cleanup()
Connect to a shared folder on a remote Windows PC
#!/bin/sh
sudo mount -t cifs //[pcname]/[sharename] /home/pi/Desktop/[localfolder] -o user=[username],pass=[password]
Play a video in omxplayer
Put the following command into a desktop shortcut:
xterm -fg black -bg black -maximize -fullscreen -e omxplayer -o hdmi -r %F
…or:
lxterminal -e omxplayer -o hdmi -r %F
…or configure VLC to use the RPi X11 splitter for video output.