Raspberry Pi Zero W for SID monitoring

This receiver for SID monitoring consists of a Raspberry Pi board running linux and my receiver software in python. The hardware section lists the minimal items needed for the system itself. In this setup, the Pi Zero W computer is not connected to a keyboard, mouse, or monitor. Another computer (the logging computer) is instead used to log into the Pi Zero through X11 windows and your home wireless network. One talks to the Pi Zero through terminal screens. Graphical output from the programs running on the Pi Zero can be displayed on the logging computer via X11. The logging computer does not need to be linux. It only needs to have an X11 server and an ssh client running on it. On windows these can be xming and putty, respectively.

The hardware section describes the hardware I am using, and the setup section gives the steps to set up linux and other software on the Pi Zero. It has taken a lot of fiddling to get the Pi Zero up and running, and I am hoping that being specific about hardware and software these instructions might shorten the path for those of you who like myself are not linux savvy.

Later sections explain how to run sidmon in the background, and to run it as a service. The latter ensures that sidmon restarts when the Pi Zero boots, which prevents missing blocks of time after power dropouts. Finally there is info on power consumption, noise from switching power supplies, running on a battery with or without a solar panel, and remote siting.

Hardware

Minimal hardware for getting a Pi Zero W up and running over a wireless network. This configuration does not use a mouse or keyboard, but is instead controlled via ssh on a linux or windows computer (use ssh on linux, or, e.g., putty on a windows computer).

Setting up the Pi Zero W

  1. Download one of the Raspbian operating system images in a zip or gzip file: Raspbian Jessie also works.
  2. Installing operating system images on an SD card. Use etcher to write to the SD card. When done, unplug and then plug in the SD card.
  3. Configuring the operating system on the SD card. The important thing is to set up the Pi Zero to connect to the wireless network when it boots. Two files need to be created in the boot folder of the SD card. On my laptop with the SD card plugged in, the path to the boot folder was /media/<username>/boot, where <username> is pi on my Pi Zero.
    1. Create wpa_supplicant.conf containing wireless credentials and place in the boot folder.
    2. Create an empty ssh file and place in the same folder.
    On a linux computer, the commands might look like:
    sudo cp wpa-supplicant.conf /media/<username>/boot
    sudo cp ssh /media/<username>/boot
  4. Verify that the files are there, and be sure to they are in the right boot folder. If the folder does not have a lot of files, then it is the wrong folder. I was tripped up by both of these foibles.
  5. Unmount the SD card and plug it into the Pi Zero. Power up the Pi Zero. Give it a couple of minutes to boot up.
  6. Determine the IP address of the Pi Zero. This can be done by logging into your modem and viewing the connected devices. One of them should be the Raspberry Pi. If you are not able to use this method, perhaps you can determine the address by trial and error, i.e., 192.168.1.2, 192.168.1.3, etc., with help by pinging these addresses.
  7. From a linux computer, log in with
    ssh -Y pi@<ipaddress>
    The password is raspberry. From a windows computer with putty installed, use:
    putty -X pi@<ipaddress>
    The switches enable X11 port forwarding.
  8. Change the password with passwd.
  9. To install software, enter the following:
    sudo apt-get update && sudo apt-get upgrade -y
    sudo apt-get install software-properties-common
    sudo apt-add-repository universe
    sudo apt-get install python-pip
    python -m pip install -U pip
    # python 3
    sudo apt install python3.7 python3.7-dev python3.7-venv
    sudo apt-get install python3-pip
    sudo apt-get install python3-numpy
    sudo apt-get install python3-matplotlib
    sudo apt-get install libasound2-dev
    sudo apt-get install python3-alsaaudio
  10. It is essential that alsaaudio version 0.8.4 or later is installed. Test by checking for the function pcms().
    $ python
    >>> import alsaaudio
    >>> alsaaudio.pcms()
    A list of sound devices should be printed. If not, then go to this link.
  11. I've found that it is wise to shut down before plugging in the sound card.
    sudo shutdown now
    Unplug the Pi Zero, plug in the sound card, and then reapply power. Log in as before. Note that with DHCP, the IP address may have changed.
  12. Run arecord and check that the sound card is listed.
    arecord -L
    With an Xonar U5, among many device entries it should show
    ...
    plughw:CARD=U5,DEV=0
        ASUS XONAR U5, USB Audio
        Hardware device with all software conversions
    ...
  13. In the following I've assumed that an Asus Xonar U5 is present. To set up the sound card, run alsamixer.
    alsamixer
    1. Hit F6 and select the sound card.
    2. Hit F4.
    3. Cursor to the 'Line' column; select 'Capture' with the space bar; and cursor up to full volume.
    4. Cursor right to the 'Mic' column and deselect 'Capture' with the space bar.
    5. Cursor to 'auto gain control' and select off with the space bar or 'm' key.
    6. Hit F3.
    7. Cursor to 'PCM Capture Source' column and select 'Line' with the up and down keys.
    8. Exit with the escape key.
  14. matplotlib may be installed in a non-interactive mode, in which case no graphs will appear when running with the live-plot (-lp) switch. To check this, inspect the file
    /home/<user>/.local/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc
    where <user> is your user name. Look for the line
    backend = agg
    If present, replace it with
    backend = TkAgg
  15. You will want to create a directory in which to install the sidmon software. Locate this sidmon root directory to your liking somewhere in the pi user's directory space. Set the current directory to this root directory you have created. In this directory create the subdirectory hist.
  16. Copy the suite of python files and the file Header.txt to the sidmon root directory. The python files should include
    config.py
    iputil.py
    noaa_flares.py
    sidfile.py
    sidsplit.py
    supersid_plot.py
    ftp_to_Standford.py
    Moments.py
    OrthPoly.py
    sidmon.py
    sound.py
    Note that some of these files are associated with supersid_plot from the SuperSID distribution. Some of these files are modified significantly and are not interchangable with the originals.

Other packages and useful commands:

  • One can optionally install X-windows clients,

    sudo apt-get install xbase-clients

    which can be useful for testing X-windows (X11).

    xeyes
  • When you want to shut down the Pi Zero:
    sudo shutdown now
  • To check the operating-system version, enter
    lsb_release -a # or
    uname -a
  • curl for downloading files from the internet.
    curl -O <url>
  • lynx, an ascii web browser
    sudo apt-get install lynx
  • pandas
    sudo pip3 install pandas
  • scipy
    sudo apt-get install python3-scipy
  • ipython3
    sudo apt-get install ipython3
  • jupyter
    sudo apt-get install jupyter
  • sympy for symbolic mathematics
    sudo apt-get install python3-sympy
  • geany IDE
    sudo apt-get install geany
    Fast and works over X11.
  • Running the program in the background

    When running from a terminal, logging off or other disruption of the terminal session will kill sidmon. To avoid this problem, it is convenient to run sidmon in the backgound without connection to a terminal. Running without a terminal session is simple with nohup.

    nohup /usr/bin/python sidmon.py <options> &
    When run this way the user can log off without stopping the program. The sidmon -lp switch should not be specified so that no live plot is generated. Make a note of the process ID so that the process can be stopped with the kill command later. Alternatively, the process ID can be learned later using the ps command. The process by the name python is likely the correct one.
    ps -U pi
      PID TTY          TIME CMD
      428 ?        00:00:00 systemd
      431 ?        00:00:00 (sd-pam)
     1363 ?        1-10:36:32 python
     4059 ?        00:00:00 sshd
     4062 pts/0    00:00:01 bash
     4169 pts/0    00:00:00 ps

    Note in the earlier command that the path to the python interpreter might be different on other machines than shown.

    Installing sidmon as a service

    Power glitches and dropouts naturally disrupt sidmon. When the power comes back the computer will boot, but sidmon will not restart itself. To keep the data flowing it is necessary to have sidmon automatically restart on power up, and without a terminal session. It is possible to have the program run at startup by entering commands in /etc/rc.local, but installing as a service is nearly as easy. Instructions for a template towards this end are at this link. In summary
    1. Download the template, copy it to /etc/init.d, and give it a meaningful name, such as sidmon. Edit the file
      vi /etc/init.d/sidmon
      and after Provides: in the file enter that name.
      ...
      Provides: sidmon
      ...
    2. Enter the directory the program starts in, the command that starts the program, and the user name the program runs under in the dir, cmd, and user lines. An example is
      ...
      dir="/home/pi"
      cmd="./sidmon.py -t 4.4"
      user="pi"
      ...
      Again make sure to enter the correct path to the sidmon directory and that the python path at the top of sidmon.py is correct on your Pi Zero (check with whereis python). If you are already running sidmon from the command line, you should enter that in the cmd variable. There are also places to change the locations for stdout, stderr, and a file for the process ID. You might wish to enter locations for those files more convenient than the defaults in the template.
    3. Make the script executable and owned by root
      sudo chmod +x /etc/init.d/sidmon
      sudo chown root:root /etc/init.d/sidmon
    4. To have it run automatically at boot time, enter
      sudo update-rc.d sidmon defaults
      sudo update-rc.d sidmon enable
    5. To manage sidmon, there are four commands. Manually start the program as a service with
      sudo /etc/init.d/sidmon start
      check the status using
      sudo /etc/init.d/sidmon status
      restart the service (stop then restart) with
      sudo /etc/init.d/sidmon restart
      and stop the service with
      sudo /etc/init.d/sidmon stop

    Power consumption

    Measured current consumption while running sidmon oscillates between 0.41 and 0.47 mA at 5 V, including the Asus Xonar U5 sound card on the USB port.

    To conserve power, switch off the LED, and see this link. There are other useful links in the latter.

    To set the LED to undervoltage detect, enter

    echo input | sudo tee /sys/class/leds/led0/trigger
    Note 'led0' instead of the 'led1' from the first link. The savings look to be only a few mA.

    Powering off the HDMI port using the command

    /usr/bin/tvservice -o
    saves about 15 mA.

    With these reductions the average current consumption is about 0.42 A (2.1 W). This figure includes the wireless interface and the Xonar U5. Stopping sidmon dropped the current to 0.37 A, and halting dropped the current to 0.165 A.

    To save power it might be possible to disable the wireless adapter except at a predetermined time.

    sudo ifconfig wlan0 down
    and bring it back up for a time
    sudo ifconfig wlan0 up

    CPU temperature logging

    The CPU temperature is available in the file/sys/devices/virtual/thermal/thermal_zone0/temp. It can be useful to routinely log this temperature to a file as a means to monitor the receiver's environment. To do this:

    1. Create the file temperatureschedule with the content
      */5 * * * * <path>/temperature
      where the digit '5' is the number of minutes between recorded readings.
    2. Create the file temperature with the content
      cd <path>
      echo $(date -Iseconds)  $(cat /sys/devices/virtual/thermal/thermal_zone0/temp) >> temperature.log
      exit 0
      where <path> is the path to the sidmon directory.
    3. Then actually schedule logging with the command
      crontab temperatureschedule

    CPU temperature reflects power consumption, and wireless and bluetooth power consumption can be measured by switching them on and off and monitoring the cpu temperature. If the power consumption turns out to be lower, then one can write a script that switches off wireless and bluetooth most of the day, and then switches them back on at a predetermined time to enable download of data and other activity on the Pi Zero. Not an ideal scheme, but it might be useful in a remotely sited Pi Zero.

    Further information on sensors in linux systems. To install, enter

    sudo apt install lm-sensors
    To look for sensors, run
    sudo sensors-detect
    and note what sensors are found.

    Configuring a static IP address

    When run on a local area network (LAN) behind a modem, devices are ordinarily dynamically assigned IP addresses by the modem in the private domain, usually 192.168.x.x. A static (fixed) IP address on the LAN is a convenience in that it obviates the need to determine the Pi Zero's IP address. Your modem can be configured to provide one through its configuration menus. To do this first determine your Pi Zero's (or other sidmon host's) MAC address by entering on the sidmon host the command

    ifconfig -wlan0    # for wireless interface, or
    ifconfig -a        # for all interfaces
    One of the interfaces will be the wireless or wired interface you are using. Note the MAC address of the form hh:hh:hh:hh:hh:hh associated with that interface, where 'h' refers to a hexadecimal character. Log into your modem and navigate to the page used to assign static DHCP addresses. On my ZyXEL DSL modem it is under Network Settings -> Static DHCP. Add a new entry and enter the MAC address you noted. Then choose a new static address in the DHCP address range configured for the modem and enter that address in its place on that configuration page. Make sure that that IP address does not conflict with a device currently on your network.

    Links