Skip to content

Instantly share code, notes, and snippets.

@leecade
Forked from revolunet/0-raspi-cheatsheet-intro.md
Created May 17, 2017 19:53
Show Gist options
  • Save leecade/9fde9daad676748b83b41c8f5df55739 to your computer and use it in GitHub Desktop.
Save leecade/9fde9daad676748b83b41c8f5df55739 to your computer and use it in GitHub Desktop.
Raspberry PI 3 cheat sheet

Raspian

JavaScript

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.

Pinout

CLI

Useful commands for OSX

SD cards

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment