Pi Zero W

From Wildsong
Jump to navigationJump to search

I have a PI 2 Model B (without the wireless features) and wanted to play with a camera so it made sense to invest $10 in a Pi Zero W. I can't imagine using a webcam that's wired up (I have no wired network in this rental house.)

Since it's the first Pi Zero for me I got the cute C4Labs Zebra Zero+ protoboard setup from Adafruit at the same time.

If it all works out and I will get another Pi Zero to use with the camera without the protoboard.

First boot

  1. Plug in a cable to the Windows machine and see if it recognizes the Pi Zero as a smoke test.
  2. Download Raspian ZIP image from https://www.raspberrypi.org/downloads/raspian
  3. Download Etcher from https://www.balena.io/etcher/
  4. Run Etcher to copy image to 32GB card (32GB is overkill but it's the first one I grabbed.)
  5. While waiting, dig up the HDMI cable and USB keyboard.
    1. Discover there are many connectors in the world and they used an HDMI I don't have.
    2. Order a Type A (std HDMI) to Type C (Mini) cable. Note the BeagleBoneBlack uses Type D (Micro).
    3. Wait a few days. 11/26/18 cable arrived and works)
    4. OR use headless mode https://learn.adafruit.com/raspberry-pi-zero-creation/text-file-editing
    5. Configure the WiFI by editing wpa_supplicant.conf (see next section)
    6. OR BLUETOOTH headless mode!! I like this idea, I will try it tonight!
  6. Transfer card to Pi
  7. Connect HDMI monitor and USB keyboard if you go that route.
  8. Power on via a cellphone charger and a generic micro USB cable, everyone has those!
  9. Log in as "pi" with password "raspberry"
  10. Change the password!

Headless mode

  1. Enable WiFI so card comes up on network.
  2. Enable ssh so I can log in.

Put the SD card in the Mac. Create these files.

cd /Volumes/boot
touch ssh
vi wpa_supplicant.conf

wpa_supplicant.conf should contain

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

network={
   ssid="YOURSSID"
   psk="YOURPASSWORD"
   scan_ssid=1
}

The wpa_supplicant.conf file gets moved on boot so don't freak if it "disappears".

Remember to update the dnsmasq settings on Bellman to issue it a proper IP address.

ssh pi@zebra
pi@zebra's password:
Linux raspberrypi 4.14.79+ #1159 Sun Nov 4 17:28:08 GMT 2018 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

pi@raspberrypi:~$

and there was much rejoicing... seconds later, I realized that I cannot show it off because there are no LEDs to blink, no beeper, just this cold dead looking thing... anticlimax hits.

Serial console: I did not add enable_uart=1 to config.txt because I don't need it right now.

Bluetooth console headless mode

Mostly from https://hacks.mozilla.org/2017/02/headless-raspberry-pi-configuration-over-bluetooth/

Edit the disk image on a Mac using Vagrant. (We can't directly mount and modify an IMG file in EXT4FS)

  1. Unzip the downloaded Raspian image, the .img will be modified before transferring to SD.
  2. Start up a vagrant engine.
    1. mkdir debian && cd debian
    2. vagrant init debian/stretch64
    3. Edit VagrantFile to share Downloads folder
    4. vagrant up
    5. vagrant ssh
  3. Mount the IMG file
    1. List the partitions using fdisk -l *.img
    2. Calculate the offset using bc 512*start
    3. Mount the EXT4 partition. mount -v -o offset=XXXXXXX *.img /mnt
  4. cd /mnt
  5. Edit away.
    1. sudo vi home/pi/btserial.sh
  6. Unmount the image; cd / && umount /mnt/root
  7. Shutdown the vagrant machine
  8. Copy the image to a card using Etcher.

btserial.sh:

#!/bin/bash -e

echo PRETTY_HOSTNAME=raspberrypi > /etc/machine-info

# Edit /lib/systemd/system/bluetooth.service to enable BT services
sudo sed -i: 's|^Exec.*toothd$| \
ExecStart=/usr/lib/bluetooth/bluetoothd -C \
ExecStartPost=/usr/bin/sdptool add SP \
ExecStartPost=/bin/hciconfig hci0 piscan \
|g' /lib/systemd/system/bluetooth.service

# create /etc/systemd/system/rfcomm.service to enable 
# the Bluetooth serial port from systemctl
sudo cat <<EOF | sudo tee /etc/systemd/system/rfcomm.service > /dev/null
[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/rfcomm watch hci0 1 getty rfcomm0 115200 vt100 -a pi

[Install]
WantedBy=multi-user.target
EOF

# enable the new rfcomm service
sudo systemctl enable rfcomm

# start the rfcomm service
sudo systemctl restart rfcomm

Set up a new Zero

sudo -s
apt-get update 
apt-get upgrade
rpi-update
reboot

Fix the keyboard

Change the "gb" to "us" by doing this edit.

sudo vi /etc/default/keyboard

Bluetooth set up

Look up this article: https://www.cnet.com/how-to/how-to-setup-bluetooth-on-a-raspberry-pi-3/ and read the Terminal section. Works like a champ. I paired with an Apple keyboard.

sudo bluetoothctl
agent on
default agent
scan on

Watch for the keyboard to show up, then enter

pair XX:XX:XX:XX:XX

If the keyboard is in pairing mode, then you will be prompted to enter the secret code on the keyboard. Enter the numbers and then hit Enter. The keyboard should pair. Not sure if you have to do the next step,

connect XX:XX:XX:XX:XX

but anyway then the keyboard should start

http://wiki.debian.org/BluetoothUser

Make the settings sticky.

Camera notes

Enable camera and reboot:

raspi-config
reboot

Test camera: raspistill -o foo.jpg


Getting OpenCV

After 20 hours compiling opencv, Pi ran out of memory. I had to bump up swap space from 100MB to 1024.

Cribbed from https://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/

All this just to allow building from source...

sudo apt -y install git cmake pkg-config
sudo apt -y install libjpeg-dev libtiff5-dev libpng-dev
sudo apt -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt -y install libxvidcore-dev libx264-dev
sudo apt -y install libgtk2.0-dev
sudo apt -y install libatlas-base-dev gfortran
sudo apt -y install python2.7-dev python3-dev
sudo apt -y install python3-setuptools

This part is for either source build or pip install

sudo apt -y install libhdf5-dev libhdf5-serial-dev libwebp-dev
sudo apt -y install libqtwebkit4 libqt4-test
sudo apt -y install python-pip

On Zebra and Bellman,

pip install virtualenv virtualenvwrapper

vs Murre (Windows 10 Debian in WSL)

sudo apt install virtualenvwrapper

Add these lines to ~/.profile

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
  1. on windows
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh 

Then source them.

source .profile

mkvirtualenv cv -p python3
pip install numpy

This leaves you in the cv venv, on subsequent logins

workon cv

In one line, install the entire OpenCV package and skip 20 hours of compilation.

pip install opencv-contrib-python-headless

This fails, because there is a library missing.

ImportError: libImath-2_2.so.12: cannot open shared object file: No such file or directory

Build commands

Get the source

cd ~/src/
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip
unzip opencv.zip
wget -O opencv-contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip
unzip opencv-contrib.zip

Set up the makefile

cd opencv-4.0.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D INSTALL_PYTHON_EXAMPLES=OFF \
   -D OPENCV_EXTRA_MODULES_PATH=~/src/opencv_contrib-4.0.0/modules \
   -D BUILD_EXAMPLES=ON ..

Do the actual build, it will take about 21 hours on a Pi Zero. Do more than just drink coffee. :-)

make
sudo make install
sudo ldconfig
cd /usr/local/python
sudo python3 setup.py install

While I was waiting for the build on the Pi, I installed OpenCV from source on Bellman and plugged in a spare webcam. :-)

  • Zebra: 1290 minutes (21 hours), with 'make' (just one CPU)
  • Bellman: 48 minutes with 'make -j 8'
  • Murre: 5 minutes with 'make -j 20' (10 hyperthreaded cores) more than 250 times as fast as a Pi Zero. :-)

Zebra is at 14% and for testing on Murre I had time to eat Thanksgiving dinner, then download and install the build toolchain, download sources, and do the build on Murre. I could hear the fans speed up on Murre, the first time ever.

or Cross compile BUILD on a REAL computer

Have not tried this yet though I probably have time...

https://raspberrypi.stackexchange.com/questions/69169/how-to-install-opencv-on-raspberry-pi-3-in-raspbian-jessie

Now for the app software

https://github.com/HackerShackOfficial/Smart-Security-Camera

Camera related links

MotionEyeOS https://www.raspberrypi-spy.co.uk/2017/04/raspberry-pi-zero-w-cctv-camera-with-motioneyeos/

MotionEye on Raspian https://www.techcoil.com/blog/how-i-setup-a-raspberry-pi-zero-w-cctv-camera-with-motioneye-and-raspbian-stretch-lite/

Built into a dummy camera case: https://www.instructables.com/id/Zero-Security-Camera/ Pi Zero (not wireless) + USB - LAN adapter

Cool 3D-printed case: https://www.thingiverse.com/thing:2544275