Pi Zero W

From Wildsong
Jump to navigationJump to search

I got the first Pi Zero because I wanted to play with a webcam. Silly me I have not used the camera yet. I tested it, it works.

I have a cute C4Labs Zebra Zero+ protoboard setup from Adafruit at the same time so I named it "Zebra". That led me to name the second one "Okapi".

Status

Okapi is deployed on top of my kitchen cabinets as a media player.

Zebra is undergoing test and development as the core of a clock radio. Well actually today it's running Kali and FYI the same instructions apply.

2021-01-30 Now I am trying to interface it to an ESP8266 today.

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 Raspberry Pi OS LITE image from https://www.raspberrypi.org/downloads/
  3. Download Raspberry Pi Imager.
  4. Run the imager to copy image to a 32GB card.
  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 BeagleBone Black 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! Do this by running raspi-config and setting everything else up, too.

Headless mode

Overview:

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

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

cd d:/
touch ssh
vi wpa_supplicant.conf

REMEMBER Pi Zero W is a 2.4 GHz ONLY radio!!! No 5 Ghz!

wpa_supplicant.conf should contain

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

network={
   ssid="wildsong" # or whatever YOUR ssid is...
   psk="YOURPASSWORD"
}

Raspian moves the wpa_supplicant.conf file 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: ''raspberry''
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.

Serial console

This works for any Pi including my older Model B which has no built in WiFi or Bluetooth

  1. Install driver for Mac
  2. Add "enable_uart=1" to config.txt in the DOS boot partition
  3. Connect
  4. Connect USB to Mac
  5. Start a session with screen /dev/cu.SLAB_USBtoUART 115200
  6. Power up PI
  7. Observe and rejoice:
[    7.878533] sysrq: SysRq : Resetting
[    7.883666] CPU2: stopping
[    7.888170] CPU0: stopping
[    7.892645] CPU1: stopping

Raspbian GNU/Linux 9 raspberrypi ttyAMA0
raspberrypi login: pi
password: raspberry

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. Enable ssh.
      cd /etc/rc3.d && ln -s ../init.d/ssh S01ssh
    2. sudo vi home/pi/btserial.sh
    3. chown 1000.1000 home/pi/btserial.sh
    4. vi etc/rc.local
    5. add line: sh /home/pi/btserial.sh
    6. Change gb to us in /etc/default/keyboard and /etc/default/locale
    7. Edit wpa_supplicant.conf as above
  6. Unmount the image; cd / && umount /mnt/root
  7. Shutdown the vagrant machine: vagrant halt
  8. Copy the image to a card using Etcher.

After booting the Pi connect on Bluetooth or SSH and then run updater and raspi-config.

btserial.sh:

#!/bin/bash -e

echo PRETTY_HOSTNAME=zebra > /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 update 
apt upgrade
reboot
apt install emacs-nox

LCD set up

This article shows the settings that work for my Adafruit 5" LCD with its attached HDMI decoder. Setting Up an 800X480 5inch HDMI LCD for Raspberry Pi

Add this to config.txt

hdmi_group=2
hdmi_mode=1 
hdmi_mode=87
hdmi_cvt=800 480 60 6 0 0 0

Add swap space

Edit /etc/dphys-swapfile and change CONF_SWAPSIZE I changed it from 100 to 1000 so that I can run a compiler SLOWLY but have it not crash.

Fix the various settings

You can run raspi-config to set the hostname, locale, keyboard, and time zone; this is easier than poking around in the various configuration files.

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


Red Bear IoT pHAT board

I got one of these for my Model 3 B, so that it can have WiFi and Bluetooth too! I have not set it up yet. It was on sale for $5, I could not pass it up.

I2S audio

I am working to get the MEMS Mic and the I2S Bonnet to work at the very same time. So far only the bonnet works.

https://raspberrypi.stackexchange.com/questions/90647/i2s-recording-combined-with-i2s-playback

Microphone

Adafruit has a nifty I2S MEMS microphone breakout, get 2 for stereo! ;-) about $7

Using the mic with RPi: https://github.com/nejohnson2/rpi-i2s

DAC

Adafruit has an I2S audio DAC bonnet with 3.5mm/rca line outs. About $10; they also have a $7 breakout board using the same DAC if you don't want a bonnet.

While you are shopping at Adafruit consider this "20W" class D stereo amp for $20.

See HiFiBerry for more interesting choices for audio output.

Pimoroni Inky pHAT

RED/BLACK/WHITE electronic paper, which I intend to use with a Pi Zero W. Might use as part of my boombox project. First though I am building a Pi clock.

Setting up OpenCV

Build host set up

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

Don't try to build on the Zero. Use a real computer. :-) In my case Bellman with an Intel NUC with an i5 4 core CPU. After getting a card set up on the Zero, shutdown and move it to a USB card reader. Put that on the Linux box.

I found a tip searching Stack Exchange and build these instructions.

sudo apt-get install -y qemu qemu-user-static binfmt-support
sudo mkdir /media/sdcard
sudo mount /dev/sdc2 /media/sdcard
sudo mount /dev/sdc1 /media/sdcard/boot
sudo cp /usr/bin/qemu-arm-static /media/sdcard/usr/bin/
sudo chroot /media/sdcard bash --login
root@bellman:/boot# uname -a
Linux bellman 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 armv7l GNU/Linux

Awesome, Bellman now thinks he is a Raspberry Pi.

root@bellman:/etc/apt# apt update
Get:1 http://packages.microsoft.com/repos/code stable InRelease [10.4 kB]
Get:2 http://archive.raspberrypi.org/debian buster InRelease [32.7 kB]           
Get:3 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]
. 
.
.

Once the SD card is mounted and we're chroot'ed, we can just proceed as if we're running a pi.

Install prerequisites

Following instructions were cribbed from https://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/ but so far are still working on buster.

All this just to allow building from source... at least it's happening now at Core i5 speed with 32GB of ram.

apt -y install git cmake pkg-config
apt -y install libjpeg-dev libtiff5-dev libpng-dev \
  libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
  libxvidcore-dev libx264-dev libgtk2.0-dev
apt -y install libatlas-base-dev python2.7-dev python3-dev python3-setuptools libpython3-dev
apt -y install libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-dev protobuf-compiler

These are for caffe

apt-get install -y --no-install-recommends libboost-all-dev
apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev

Pretty sure these are not needed

apt -y install libwebp-dev libqtwebkit4 libqt4-test

When working in chroot'ed environment at this point, I can drop root privileges by changing to the user 'pi', so that I can use virtualenv properly. Sort of the opposite of "sudo".

su - pi
pi@bellman:~$ pwd
/home/pi

At this point I tried using conda instead of virtualenv and it was a debacle. The available packages for conda are too old. I'm thinking I should mount an SSD on this machine to make it go faster before I do the opencv build, the SD card is slowing me down. Okay, I have a "workspace" ssd on Bellman that is unused mounted at /workspace so I did this

cd /media/sdcard
sudo cp -rp home_real/pi /workspace
sudo touch /workspace/pi/THIS_IS_THE_SSD_DRIVE
sudo mount --bin /workspace /media/sdcard/home

This masks the /home drive but that's okay for now.

Installing a newer pip

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py 

I was working on getting opencv going, I vaguely recall.

virtualenv -p python3 cv
source cv/bin/activate
pip install numpy

Build commands

Get the source

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

Set up the makefile

cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D INSTALL_PYTHON_EXAMPLES=ON \
   -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/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. :-) Instead set up a build environment on an Intel based machine as noted above.

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)
  • 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.

Okay, so I guess I should try setting up a virtual environment on Murre for this.

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

Mouse and keyboard and X

There is only one USB port on a Pi Zero and it's OTG.

I found that I can use an OTG cable purchased 100 years ago for a phone, but only to connect one thing at a time. (A cable to a hub could only see the hub and nothing plugged into it so I am assuming that it's the OTG thing? Help me out here.)

So with just the OTG cable on the USB port I can pop the Logitech keeb & mouse dongle in and have them both work.

X11

I installed the wee no desktop image of Raspian with this.

sudo apt install xinit matchbox

Then I can type "startx" and see a black screen with a cursor. Very satisfying. Disappointing that I am not seeing matchbox though. I thought it would start, it did on Ubuntu on my Pi4.

I added lightdm, and now I get a blank screen for a time, then it blinks and goes black again, not what I want.

 sudo apt install lightdm-autologin-greeter

Uninstalling caused it to install a different greeter, lightdm-gtk-greeter, maybe that will work better? Soon I will be able to tell. Rebooting!

 sudo apt remove lightdm-autologin-greeter

This is better

 sudo apt remove lightdm-gtk-greeter
 sudo apt install nodm

Other images

Since then I have tried OSMC / Kodi and now I want to try PiCorePlayer.

Media Players

As mentioned above I have an a DAC with headphone output and an Adafruit 20W amp. I used these at first but then moved over to the HiFiBerry Amp2.

I installed the Circuitpython libraries to control the amp over I2C. Basically just setting the volume.

sudo apt-get install python3-pip
sudo pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo python3 raspi-blinka.py

HiFiBerry Amp2 + Pi Zero W

I purchased this Amp2 board in May 2021.

The amp connects to a pair of Atlantic Technology model 153.1 center channel speakers. It's working fabulously well.

When I got it, the Amp2 did not work with the standard Raspbian because the kernel was too old, and there was a bug in the driver in the one shipped with Raspian, so I had to look for other options.

The HiFiBerry people say it's not a good idea to pair this board with a Pi Zero W. In my configuration it has worked just fine. The image for HiFiBerryOS they distribute for the Zero still says "Not Tested" on it, and when I tried it I found it did not have support for WiFi so I gave up on it.

I tried out Max2Play. Max2Play is based on the Raspbian Buster image.I flashed a Max2Play image on Okapi.

Installing Max2Play and then disabling all the fluff like X11 and Apache is stupid. X11 is a total waste of resources when you don't even have a monitor hooked up. I can't understand why they install it by default.

apt remove desktop-base xinit lxde-common lxde-core
apt autoremove

I rebooted and the process table is looking a bit less intimidating.

I can't see any reason to keep Apache running other than to let it remind me that I have no Max2Play license. Honestly, the design of their pages looks like an online store and not a dashboard. I removed it.

Settings files for Squeezelite:

/opt/max2play/audioplayer.conf  is where the squeezelite environment is defined, I put the server IP in there:`
/opt/max2play/playername.txt    is where the squeezebox playername is declared, normally max2play.

It looks like the Max2Play "autostart" feature means try to start once per minute, it's to workaround unstable code. I turned it on and it started many copies of squeezelite until I noticed and killed them all. So I have turned it off! The crontab in use is for the pi user. Edit this file to control autostart settings

/opt/max2play/autostart.conf

Some HiFiBerryOs things:

  • MPD Music Player Daemon (need to look at it)
  • Squeezelite (yep, good idea)
  • Squeezebox Server aka LMS (already running on Bellman in Docker)
  • DLNA (useful to me?)

I'd like Spotify to work somehow

Switching to PiCorePlayer

Okay, some time has gone by and I am working on the clock radio project again. The idea right now is a complete standalone box. I can buy one for $40 on Amazon. Or build my own based on a Pi for $100. No contest-- making is fun.

The new Pi SD Imaging program offered to let me install OSMC, the "Open Source Media Center". I did that and discovered it wants to run Kodi on my 5" LCD screen, which is uselessly small.

Then I discovered PiCorePlayer and got that going. PiCorePlayer is tiny, runs in RAM, and is built around Squeezelite. Since it runs 100% in RAM, I can put the image on the crummiest 2GB SD card from my junk box, and it's fine.

Burn image, edit wpa_supplicant.conf to set up ssid and password. On Zebra, I was trying the 5" HDMI display so I set that up for X11 but on Okapi there is no screen, so I did not bother with it. I just had to change that one file, boot, and go! See notes above if you want to use a screen.

You can use PiCorePlayer's web interface or you can ssh in to it, use the username "tc" and password "piCore", note the uppercase "C" in piCore. The web interface suits me.

On Zebra, I got PiCorePlayer to recognize my Adafruit DAC by telling it I have a HiFiBerry DAC+; I think that's right. I plugged in a powered speaker and music came streaming out from my Logitech Media Server. GREAT.

I tried JiveLite, which is a controller for LMS, and dumped it because it only wants to read media from the Zero and I have a standalone LMS server on Bellman. The X11 GUI on the 5" screen was fine.

I tried Bluetooth speakers as output but could not get any sound out. Not a big deal. I disabled Bluetooth, rumor is that using both WiFi and Bluetooth on the Pi Zero W is a bad idea. Bluetooth on Linux is meh anyway.

For Okapi, I set up a card on Zebra. Then I just moved the card up to Okapi and booted. I changed the name of the Squeezelite player to "okapi". That's it. Boots much faster now, and it's now a music appliance instead of a computer - I can't shell into it and build OpenCV, or run Alexa but that's how it should be. It won't tempt me! Much.

Alsa player equalizer

Turn on the equalizer on the Tweaks page. The instructions there tell you this:

Login via ssh.
Use "sudo alsamixer -D equal" and adjust the settings, press escape.
Backup ALSA settings by typing "sudo filetool.sh -b" or use "Backup button" on the Main page.

Zebra is now a media player, too

2022-10-23

I want to set up another player. I'd get another HiFiBerry AMP 2 but they are not available now. I got an Inno-Maker RPI AMP from Amazon.

I installed PiCorePlayer on a 2GB card and edited wpa_supplicant.conf, hooked up a 12V power brick, and set the audio player to be a HiFiBerry AMP+ (not AMP 2). It works.