How To: Raspberry Pi

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.

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.