Skip to content

Instantly share code, notes, and snippets.

@revolunet
Last active March 14, 2020 10:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save revolunet/f85a6fbe8b2688632c288f26010c9542 to your computer and use it in GitHub Desktop.
Save revolunet/f85a6fbe8b2688632c288f26010c9542 to your computer and use it in GitHub Desktop.
Raspberry PI 3 cheat sheet

Raspian

⚠️ add a ssh empty file to the /boot partition to enable headless boot and SSH server

⚠️ add a wpa_supplicant.conf file to the /boot partition to enable WiFi

country=fr
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
 scan_ssid=1
 ssid="Livebox-taiti"
 psk="PouetPouet"
}

JavaScript

NVM setup

curl -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
nvm install stable
sudo ln -s /home/pi/.nvm/versions/node/v8.4.0/bin/node /usr/bin/node
sudo ln -s /home/pi/.nvm/versions/node/v8.4.0/bin/npm /usr/bin/npm

Audio

apt-get install alsa-utils
modprobe snd_bcm2835
amixer cset numid=3 1
amixer sset PCM,0 90%

add dtparam=audio=on to /boot/config.txt add snd_bcm2835 to /etc/modules numid: 0=auto, 1=headphones, 2=hdmi.

in /boot/config.txt set audio_pwm_mode=2. source

Pinout

Model A+/B+/Raspberry Pi 2/Raspberry Pi 3/Raspberry Pi Zero

pinout

CLI

Useful commands for OSX

Inspect SD Card content

https://www.jeffgeerling.com/blog/2017/mount-raspberry-pi-sd-card-on-mac-read-only-osxfuse-and-ext4fuse

Kiosk mode

https://pimylifeup.com/raspberry-pi-kiosk/

SD cards

⚠️ add a ssh empty file to the /boot partition to enable headless boot and SSH server

write image

see disks : diskutil list

⚠️ careful with the DISK parameter, it wipes the disk

IMAGE=/path/to/image.img
DISK=/dev/rdisk3
sudo dd if=$IMAGE of=$DISK bs=8m

also : unzip -p sdcard.img.zip | sudo dd bs=1m of=<device>

or use ./prepare-disk.sh /path/to/image.img

  • expand filsystem : sudo resize2fs /dev/mmcblk0p2

backup image

use `./backup-disk.sh

network

detect your rpi on the network : sudo nmap -sS 192.168.0.1/24

GPIO

apt-get install wiringpi
gpio mode 7 out
gpio write 7 1
gpio write 7 0

Skrink an image

Using PiShrink and this docker-compose.yml :

version: '2'

services:
  pishrink:
    build: .
    volumes:
      - .:/pishrink
      - /dev:/dev
    privileged: true

Shrink an image is as easy as :

docker-compose run pishrink /pishrink/pishrink.sh /pishrink/backup.img /pishrink/backup-shrinked.img

create a WiFi access point

https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/

#!/bin/sh
# set -x
#
# Backup given disk to backup.img
# USAGE : ./backup-disk /dev/rdisk3
#
function accept {
echo
read -p "▶ $1 (yN) " -n 1 -r
echo
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
return 0
fi
return 1
}
function log {
echo " $1"
}
function backup {
DISK=$1
log "☕ Backup $DISK to backup.img - press Ctrl+T to see progress"
CMD="sudo dd if=$DISK of=./backup.img bs=8m"
echo `eval $CMD`
}
function unmount {
DISK=$1
log "🔨 Unmount $DISK"
diskutil unmountDisk $DISK
}
function launch {
DISK=$1
if accept " ⚠ backup $DISK to ./backup.img ?"
then
log "🚀 Launch operations on $DISK"
unmount $DISK
backup $DISK
log "👌 Done !"
fi
}
function listDevices {
diskutil list | grep /dev/disk -A 2 | grep "0:"
}
function getDiskBySize {
echo `eval "diskutil list | grep '*$1' | grep -oE \"[^ ]+\$\""`
}
function guessDevice {
DEVICE=$(getDiskBySize "4.0 GB")
if [[ ! $DEVICE ]]
then
DEVICE=$(getDiskBySize "8.0 GB")
fi
if [[ ! $DEVICE ]]
then
DEVICE=$(getDiskBySize "2.0 GB")
fi
if [[ $DEVICE ]]
then
# add /dev/r for raw access
DEVICE="/dev/r$DEVICE"
fi
echo $DEVICE
}
DEVICE=$1
if [[ ! $DEVICE ]]
then
listDevices
log "Guess device..."
DEVICE=$(guessDevice)
fi
if [[ ! $DEVICE ]]
then
echo '😡 No disk detected, use backup-disk.sh /dev/XXX'
echo '😡 Use diskutil list to check the device'
exit 1
else
launch $DEVICE
fi
#!/bin/sh
# set -x
#
# Write image to disk.
# USAGE : ./prepare-disk /path/to/image.img
#
function accept {
echo
read -p "▶ $1 (yN) " -n 1 -r
echo
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
return 0
fi
return 1
}
function log {
echo " $1"
}
function write {
DISK=$1
log "☕ Writing $IMAGE to disk - press Ctrl+T to see progress"
CMD="sudo dd if=$IMAGE of=$DISK bs=8m"
echo `eval $CMD`
}
function unmount {
DISK=$1
log "🔨 Unmount $DISK"
diskutil unmountDisk $DISK
}
function launch {
DISK=$1
if accept " ⚠ Reformat $DISK with $IMAGE ?"
then
log "🚀 Launch operations on $DISK"
unmount $DISK
write $DISK
unmount $DISK
log "👌 Done !"
fi
}
function listDevices {
diskutil list | grep /dev/disk -A 2 | grep "0:"
}
function getDiskBySize {
echo `eval "diskutil list | grep '*$1' | grep -oE \"[^ ]+\$\""`
}
function guessDevice {
DEVICE=$(getDiskBySize "4.0 GB")
if [[ ! $DEVICE ]]
then
DEVICE=$(getDiskBySize "8.0 GB")
fi
if [[ ! $DEVICE ]]
then
DEVICE=$(getDiskBySize "7.9 GB")
fi
if [[ ! $DEVICE ]]
then
DEVICE=$(getDiskBySize "7.7 GB")
fi
if [[ ! $DEVICE ]]
then
DEVICE=$(getDiskBySize "2.0 GB")
fi
if [[ $DEVICE ]]
then
# add /dev/r for raw access
DEVICE="/dev/r$DEVICE"
fi
echo $DEVICE
}
IMAGE=$1
if [[ ! $IMAGE ]]
then
echo 'USAGE: prepare-disk /path/to/image.img'
exit 1
fi
listDevices
log "Guess device..."
DEVICE=$(guessDevice)
if [[ ! $DEVICE ]]
then
echo '😡 No disk detected, use prepare-disk.sh /dev/XXX'
echo '😡 Use diskutil list to check the device'
exit 1
else
launch $DEVICE
fi
@jedisct1
Copy link

@revolunet
Copy link
Author

thanks @jedisct1 didnt know it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment