Pi Headless Cheat Sheet

Fresh Installation

Sometimes a fresh install of Raspbian is needed to ensure your OS is as clean and up-to-date as can be. It's also important for any code testing to ensure that everything works with the latest packages and configuration

Most of my Pi Zero configurations are headless, meaning that the X server desktop is not installed. For embedded Pi devices this makes the most sense as they will never connect to a screen or use a keyboard/mouse directly.

New Image

Use Win32 Disk Imager or similar tool to load the IMG file to a new SD card

Old cards will need formatting. SD Formatter does the job well. Ensure the option to adjust the format size is selected.

Download the latest Raspbian Lite version from the downloads section on the raspberrypi.org website.
Extract the image file from the downloaded ZIP and write to the card.

First Boot

Insert the card with the power off. A monitor and keyboard will be needed for the first boot.

Login at the command prompt
Default username is pi and the password is raspberry for all new images.

Type the following
sudo raspi-config

The raspi-config screens do change with updates so options may be under different menus for later releases.

Select option 2 and follow prompts for your WiFi access (if WiFi used for connection). Enter your network details.
Raspi-Config Network Option

Next go to the Interfacing Options and enable SSH. Whilst here I also enable SPI, I2C and 1-Wire as most add on boards use one of these protocols.
Raspi-Config Interfacing Option

Finally go to the Advanced Option and select the Expand Filesystem option. This will ensure that all the SD card is in use.

A restart is required to expand the filesystem. If not prompted to reboot you can type the following
sudo shutdown -h now

This should also shutdown the computer and give you time to disconnect the HDMI and keyboard.

Headless Login

Without the HDMI and keyboard the Pi can be accessed over SSH. This assumes you have a WiFi connection, or a wired connection setup for a Pi 2/3.

On Windows you can download PuTTY. This software is small and reliable for SSH connections.
Bonjour drivers are needed on Windows if you do not know the assigned IP address. Download from Apple and install.

Open PuTTY and type into the Host name box the address (see image below)
raspberrypi.local

A Pi Zero may be called zeropi.local so instead enter
zeropi.local



Press on the Open button and you should see a login prompt. As before use the default username and password (unless you changed in raspi-config)

Package Installation

Before you do anything update 
sudo apt-get update

Listed below are a set of packages which are useful, but not essential to have installed for development on the Pi. Some of these are required for hardware displays or other devices so installation up-front can save time later.

Install Git

Essential for cloning a repository
sudo apt-get install git

Install PIP (Pillow)

An image library for Python
sudo apt-get install python-pip
sudo apt-get install python3-pip

Install Smbus

Essential if you are coding for I2C in Python
sudo apt-get install python-smbus
sudo apt-get install python3-smbus
sudo apt-get install i2c-tools

Install FreeFonts

TrueType fonts for Linux
sudo apt-get install fonts-freefont-ttf 

Install Numpy

Extend Python with fast array facility. Sometimes a requirement of other tools and drivers
sudo apt-get install python-numpy
sudo apt-get install python3-numpy

Install PIP

Python install packager. This can be a requirement of other python install packages
sudo apt-get install pip

Install SPIDEV

Required for SPI development and other Python drivers using SPI (Python 2)
sudo apt-get install python-spidev

Install Emacs (no X)

Love it or hate it I do like emacs on the console. Stick to Nano if you are new, but if you like emacs and don't have X windows installed then type the following
sudo apt-get install emacs-nox


Advanced Connectivity

UART login access

Instead of WiFi the UART can be used. See this post.


Access over USB

Until Microsoft totally destroyed network sharing in Windows 10 I did like powering a Pi Zero from the USB of a PC. Sharing the power and connection at the same time is very neat. To enable this a couple of changes are needed to the boot files. This enables the Ethernet Gadget on the Pi Zero USB.
Note that this doesn't work on Pi 2/3 versions.

Edit /boot/config.txt. At the end of the file add
dtoverlay=dwc2

Save config.txt and now open /boot/cmdline.txt
After rootwait add the following
modules-load=dwc2,g_ether

Check your spaces as this config line is very sensitive, it should look something like this (all on one line)
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=d07618e7-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether

No newline or extra spaces should be added!

Restart your Pi Zero and you can now connect your PC to the USB data (not the power). 
The Zero will boot and have an address allocated over RNDIS drivers (Windows). Connection can still be made as shown above using PuTTY and Bonjour drivers. 

No comments:

Post a Comment