Skip to content

Raspberry Pi Zero W

Jürg Luthiger edited this page Mar 29, 2022 · 24 revisions

Buy

Setup

Create a bootable SD card

Download Raspberry Pi Imager

Download and Flash IMG

Use the Raspberry Pi Imager to download and create a bootable SD card

  • Insert an SD card to your computer
  • Launch the Raspberry Pi Imager tool
  • Choose OS Raspberry Pi OS (other) > Raspberry Pi OS Lite
  • Choose SD Card
  • Flash the (zipped) Raspbian Lite IMG to the SD card
  • Remove and re-insert the SD card to your computer
  • There should now be a disk drive named boot

Enable SSH

Create an ssh marker file on the SD card

  • The following file will enable sshd, the SSH daemon, on the Raspberry Pi
  • Navigate to the disk drive named boot and create an empty file named ssh with
    $ cd /Volumes/boot
    $ touch ssh
    or on Windows, assuming the boot drive is E:
    C:> E:
    E:> type nul > ssh
  • Note: once sshd is enabled, the Raspberry Pi will remove the ssh file

Enable RNDIS

Edit configuration files on the SD card

  • The following edits convert the Raspberry into a RNDIS USB device
  • Open config.txt and append dtoverlay=dwc2
    $ cd /Volumes/boot
    $ nano config.txt
    $ cat config.txt
    …
    dtoverlay=dwc2
  • Open cmdline.txt and add modules-load=dwc2,g_ether after rootwait
    $ nano cmdline.txt
    $ cat cmdline.txt
    … rootwait modules-load=dwc2,g_ether …

Configure Wi-Fi (optional)

  • On the disk drive named boot, create a file named wpa_supplicant.conf
    $ cd /Volumes/boot
    $ nano wpa_supplicant.conf
  • For file content and options, see Configure Wi-Fi

Connect the USB

  • Insert the SD card into the Raspberry Pi (it is the Pi's "hard disk")
  • Connect the USB cable, use the left USB connector on the Raspberry Pi

Get USB access from Windows

Install RNDIS driver on Windows

Install Bonjour 3.x on Windows

  • Download iTunes installer https://www.apple.com/itunes/download/win64
  • Unpack the installer EXE/ZIP, find the Bonjour64.msi installer inside
  • Run Bonjour64.msi, select Bonjour uninstall old version
  • Run Bonjour64.msi, select Bonjour install 3.0

Share Internet to RNDIS

  • Wi-Fi > Properties > Sharing > [✓] Allow

Get USB access from Mac

Share Internet to RNDIS

  • System Preferences > Sharing > [✓] Internet
  • Sharing > Share your connection from: Wi-Fi to computers using RNDIS Ethernet Gadget

Get USB access from Linux

Share Internet to RNDIS

Find your Pi

To find your Pi Zero via USB (if Internet Sharing is enabled), type

$ dns-sd -G v4 raspberrypi.local

Or, use ifconfig (or ipconfig on Windows) you find the local IP prefix, e.g. 192.168.0.

$ ifconfig
en0: flags…
    ether…
    inet6…
    inet 192.168.0.23
    …

Then use nmap to scan the subnet for open ssh ports (port=22), e.g.

Note: make sure port scanning is fine with the local network admin, e.g. at FHNW it's not allowed.

$ nmap -A -p22 192.168.0.23/24
Nmap scan report for 192.168.0.42
…
ORT    STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.4p1 Raspbian 5+b1 (protocol 2.0)

Access your Pi

With ssh (or PuTTY on Windows), default password raspberry

Or, via USB, if there's only one Pi

Or via FTDI Serial (up to Pi 2, or with this fix for Pi 3).

3V3 (!), Pin 6 = GND, 8 = TX, 10 = RX

Remotely access your Pi

Use a relay service like Ngrok, Pagekite or Yaler.net.

Configure Wi-Fi

https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md

Open the Wi-Fi config file wpa_supplicant.conf (or create it on the SD card)

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

The file should contain the following content (using your credentials)

country=UK
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="MY_SSID"
    psk="MY_PASSWORD"
    key_mgmt=WPA-PSK
}

Consider using the hashed version of psk, see output of

$ sudo wpa_passphrase 'MY_SSID' 'MY_PASSWORD'

To activate the new config, type

$ sudo ifconfig wlan0 up

Configure interface priorities

To display the priority metric, use

$ route -n

To change the priority metric (here wlan0 before usb0), type

$ sudo nano /etc/dhcpcd.conf

interface wlan0
    metric 200

interface usb0
    metric 300

Get the MAC Address

$ cat /sys/class/net/wlan0/address

Install git

Install the git client

$ sudo apt-get update
$ sudo apt-get install git
$ git

Configure git user

$ git config --global user.email "USER_EMAIL"
$ git config --global user.name "USER_NAME"

Create an SSH key

$ ssh-keygen -t rsa -b 4096 -C "USER_EMAIL"
$ eval "$(ssh-agent -s)"
$ cat ~/.ssh/id_rsa.pub

Register the key

Store the key

$ sudo apt-get install keychain
$ keychain ~/.ssh/id_rsa
$ . ~/.keychain/$HOSTNAME-sh

(Until next reboot)

Tools

SCP

Copy a file from the computer to the Pi

$ scp -P 22 LOCAL_FILE pi@RASPI_IP:RASPI_PATH

Copy a file from the Pi to the computer

$ scp -P 22 pi@RASPI_IP:RASPI_FILE LOCAL_PATH

Code

Java

Install

Libraries & Examples

Javascript with Node.js

Install Node.js

Install Noble (BLE Central)

  • Update apt-get with
    $ sudo apt-get update
  • Install prerequisites with
    $ sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev
  • Install the abandonware fork of noble with
    $ npm install @abandonware/noble

Libraries & Examples

Python

C

Pinout

Resources