Sunday, December 13, 2020

Raspberry Pi Setup Notes

 PeopleSpace Raspberry Pi Setup Notes


Currently, there are 13 Raspberry Pi’s:

  • octo-1, octo-2, …, and octo-9 are running OctoPrint

    • They are connected to the Wi-Fi Router via ethernet cable to ensure stable connectivity.

    • Their LAN DHCP-reserved IP addresses are assigned to match the device number for easy identification (i.e., octo-1 gets 192.168.193.1, octo-2 gets 192.168.193.2, and so on).

    • Their Zerotier IP addresses also follow the same pattern (i.e. octo-1 gets 192.168.195.1, octo-2 gets 192.168.195.2, and so on.)

    • Each device communicates with the NAS via the LAN instead of the Zerotier network to avoid encryption overhead.

  • ps-nas is running as network attached storage

    • Runs SAMBA server to provide network access to its shared folder.

  • zerotier-pi is running as a layer-3 router to make devices on the 192.168.193.254/24 accessible to clients connected to the Zerotier network "peoplespace" (ID: a84ac5c10a70ccf7).

  • timelapse-0 and timelapse-1 are running as time lapse cameras

    • They are configured to connect to "lph-wifi" by default and with "ps-vip" as fallback. When a device connects to "ps-vip" (instead of "lph-wifi") as fallback, you will only be able to connect to the device via its Zerotier IP address.

Setting up and Raspberry Pi to run OctoPrint

There are two options to set up OctoPrint on a Raspberry Pi— via OctoPi or via manually configuration. OctoPi is a SD card image of the Raspberry Pi OS that includes the OctoPrint software. It is the recommended way to set up OctoPrint on a Raspberry Pi. This document also provides instructions on how to set up a Raspberry Pi OS installation with OctoPrint. Aside from setting up OctoPrint on Raspberry Pi OS, we need a few other additional customizations to fit our physical environment.

OctoPi Setup

This section provides instructions on how to configure a Raspberry Pi to run OctoPrint via the OctoPi image.


  1. Download and install Etcher (from Balena) — it is a piece of software that helps you properly write pre-built images onto SD cards. It's recommended by OctoPi author(s).
    https://etcher.io/

  2. Download latest version of OctoPi from OctoPrint's website:
    https://octoprint.org/download/

  3. Use Etcher to write the downloaded OctoPi image onto an SD card. When done, Etcher auto-unmounts the SD card.

  4. Eject and re-insert the SD card to re-mount the SD card.

  5. Modify the content of the SD card with custom settings.

    1. Setup WiFi by editing octopi-wpa-supplicant.txt.

      1. Update the network entry with the desired WiFi's SSID and PSK (i.e., Pre-Shared Key— or password). PSK can be in plaintext form or in raw-key form (raw-key form conceals the original plaintext password). You can make raw keys with https://www.wireshark.org/tools/wpa-psk.html. For example, for a WiFi AP (SSID: "waffle-house", PSK: "hunter2"), you define the network entry like below:

        1. Plaintext form (Note: PSK is in quotes.):
          network={
            ssid="waffle-house"
            psk="hunter2"
          }

        2. Raw-key form (Note: PSK is not in quotes.):
          network={
            ssid="waffle-house"
            psk=177cef9593<...redacted...>891ac0cce9
          }

      2. Change the country code from GB to US:
        country=US

    2. Enable SSH by creating a file called ssh in the root folder (if it doesn't already exist). Execute the following command in SD's card's root folder
      touch ./ssh

    3. Assign a unique hostname by creating a file called octopi-hostname.txt. The first line of the text file will be the hostname. (e.g., "octo-1"):
      echo "<device-hostname>" > ./octopi-hostname.txt

    4. Assign a unique password (e.g, "charlie-alpha-tango"):
      echo "<login-password-for-pi>" > ./octopi-password.txt

  6. Unmount and eject SD card.

  7. Insert SD card into Raspberry Pi.

  8. Turn on the Raspberry Pi.

  9. Verify the Pi is reachable via the network using the following commands (See: https://raspberrypi.stackexchange.com/questions/13936):

ping <device-hostname>

arp -na | grep -i b8:27:eb

sudo nmap -sP 192.168.1.0/24 | awk '/^Nmap/{ip=$NF}/B8:27:EB/{print ip}'

  1. Login to Pi via SSH.
    ssh pi@<device-hostname-or-ip>

  2. Exit the SSH session
    exit

Manual Setup

  1. Download and install Etcher (from Balena) — it is a piece of software that helps you properly write pre-built images onto SD cards. It's recommended by OctoPi author(s).
    https://etcher.io/

  2. Download latest version of Raspberry Pi OS (32-bit) Lite:
    https://www.raspberrypi.org/downloads/raspberry-pi-os/

  3. Use Etcher to write the downloaded OctoPi image onto an SD card. When done, Etcher auto-unmounts the SD card.

  4. Eject and re-insert the SD card to re-mount the SD card.

  5. Modify the content of the SD card with custom settings.

    1. Setup Wi-Fi by creating a file called wpa_supplicant.conf.

      1. Enter the following as its content:

country=US

update_config=1

ctrl_interface=/var/run/wpa_supplicant
network={

ssid="<your_ssid_case_sensitive>"

psk="<your_password>"

}

  1. Replace <your_ssid_case_sensitive> with your desired Access Point's SSID.

  2. Replace <your_password> with your desired Access Point's PSK (i.e., Pre-Shared Key— or password). It can be in plaintext form or in raw-key form (raw-key form conceals the original plaintext password). You can make raw keys with https://www.wireshark.org/tools/wpa-psk.html. For example, for a WiFi AP (SSID: "waffle-house", PSK: "hunter2"), you define the network entry like below:

    1. Plaintext form (Note: PSK is in quotes.):
      network={
        ssid="waffle-house"
        psk="hunter2"
      }

    2. Raw-key form (Note: PSK is not in quotes.):
      network={
        ssid="waffle-house"
        psk=177cef9593<...redacted...>891ac0cce9
      }

  1. Enable SSH by creating a file called ssh in the root folder (if it doesn't already exist). Execute the following command in SD's card's root folder
    touch ./ssh

  2. Assign a unique hostname by creating a file called octopi-hostname.txt. The first line of the text file will be the hostname. (e.g., "octo-1"):
    echo "<device-hostname>" > ./octopi-hostname.txt

  3. Assign a unique password (e.g, "charlie-alpha-tango"):
    echo "<login-password-for-pi>" > ./octopi-password.txt

  1. Unmount and eject SD card.

  2. Insert SD card into Raspberry Pi.

  3. Turn on the Raspberry Pi.

  4. Verify the Pi is reachable via the network using the following commands (See: https://raspberrypi.stackexchange.com/questions/13936):

ping <device-hostname>

arp -na | grep -i b8:27:eb

sudo nmap -sP 192.168.1.0/24 | awk '/^Nmap/{ip=$NF}/B8:27:EB/{print ip}'

  1. Login to Pi via SSH. Enter "raspberry" when prompted for password.
    ssh pi@<device-hostname-or-ip>

  2. Change default password for pi
    passwd

  3. install OctoPrint (see https://community.octoprint.org/t/setting-up-octoprint-on-a-raspberry-pi-running-raspbian/2337)
    sudo apt install python-pip \
                    python-dev \
                    python-setuptools \
                    python-virtualenv \
                    git libyaml-dev build-essential

cd ~

virtualenv oprint

source oprint/bin/activate

pip install pip --upgrade

pip install octoprint

sudo usermod -a -G tty pi

sudo usermod -a -G dialout pi

  1. Configure OctoPrint to start automatically on boot

    1. Add start script template

wget https://github.com/foosel/OctoPrint/raw/master/scripts/octoprint.init

sudo mv octoprint.init /etc/init.d/octoprint

wget https://github.com/foosel/OctoPrint/raw/master/scripts/octoprint.default

sudo mv octoprint.default /etc/default/octoprint

sudo chmod +x /etc/init.d/octoprint

  1. Modify /etc/default/octoprint

    1. Open /etc/default/octoprint for editing:
      sudo vi /etc/default/octoprint

    2. Change the DAEMON variable to match the following:
      DAEMON=/home/pi/oprint/bin/octoprint

    3. Comment out the following line (if it exists) to avoid making the OctoPrint run at the highest priority:
      # NICELEVEL=-2

    4. Save and exit

  2. Add the scripts to autostart using the following command
    sudo update-rc.d octoprint defaults

  3. Execute the following commands to start/monitor the service
    sudo systemctl start octoprint.service

sudo systemctl status octoprint.service

  1. Install rng-tools to use hardware generated random number
    sudo apt-get install rng-tools

  2. Install mjpg-streamer for accessing cameras attached to the raspberry pi

    1. Download source and compile it
      sudo apt-get install cmake libjpeg62-turbo-dev git

cd ~

git clone https://github.com/jacksonliam/mjpg-streamer.git

cd mjpg-streamer/mjpg-streamer-experimental

make

sudo make install

  1. Setup daemon to start it up on boot (see: https://github.com/ntoff/mjpeg-streamer-octoprint-daemon)
    cd ~

git clone https://github.com/ntoff/mjpeg-streamer-octoprint-daemon

cd ~/mjpeg-streamer-octoprint-daemon/_root_

sudo cp ./etc/default/webcamd /etc/default/webcamd

sudo cp ./etc/init.d/webcamd /etc/init.d/webcamd

sudo cp ./bin/webcamd /bin/webcamd

sudo chmod +x /etc/init.d/webcamd

sudo chmod +x /bin/webcamd

sudo update-rc.d webcamd defaults

sudo systemctl start webcamd.service

sudo systemctl status webcamd.service

  1. Customize daemon with configuration values (see: https://github.com/jacksonliam/mjpg-streamer/blob/master/mjpg-streamer-experimental/plugins/input_raspicam/README.md)
    sudo tee /boot/octopi.txt << EOL

#camera="raspi"

camera="auto"

camera_usb_options="-r 1280x720 -f 30"

camera_raspi_options="-x 1280 -y 720 -fps 30"

EOL

  1. Install Haproxy (reverse proxy) so that we can access OctoPrint on port 80.

    1. Install haproxy
      sudo apt install haproxy

    2. backup existing configuration file
      sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy

    3. create new configuration file
      sudo tee /etc/haproxy/haproxy.cfg << EOL

global

        maxconn 4096

        user haproxy

        group haproxy

        daemon

        log 127.0.0.1 local0 debug


defaults

        log     global

        mode    http

        option  httplog

        option  dontlognull

        retries 3

        option redispatch

        option http-server-close

        option forwardfor

        maxconn 2000

        timeout connect 5s

        timeout client  15min

        timeout server  15min


frontend public

        bind :::80 v4v6

        use_backend webcam if { path_beg /webcam/ }

        default_backend octoprint


backend octoprint

        reqrep ^([^\ :]*)\ /(.*)     \1\ /\2

        option forwardfor

        server octoprint1 127.0.0.1:5000


backend webcam

        reqrep ^([^\ :]*)\ /webcam/(.*)     \1\ /\2

        server webcam1  127.0.0.1:8080

EOL

  1. Modify OctoPrint's configuration file so that its built-in Discovery plugin reports itself as accessible on port 80 instead of port 5000 (default).

    1. Open OctoPrint's configuration file
      vi ~/.octoprint/config.yaml

    2. Add the following line under the "discovery:" section with the proper indentations
      publicPort: 80

    3. Save and exit

  2. Install FFMPEG
    sudo apt-get install ffmpeg

  3. Install lsof so we can check for list of open files in htop
    sudo apt-get install lsof

  4. Exit the SSH session
    exit

Post-Setup Configurations

  1. Copy SSH key to remote Pi
    ssh-copy-id pi@<device-hostname-or-ip>

  2. Login to Pi via SSH
    ssh pi@<device-hostname-or-ip>

  3. Configure environment settings:

    1. Open raspi-config by executing the following command:
      sudo raspi-config

    2. Change hostname from default of "raspberrypi" to something unique (e.g. octo-1)

    3. Change timezone to local time zone (e.g., America/Los Angeles)

    4. Change locale to en_GB.UTF-8 to en_US.UTF-8.

    5. For interfacing options, enable camera interface (if available).

  4. Update software listings and installed packages
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get dist-upgrade

  5. Install ZeroTier:
    curl -s https://install.zerotier.com | sudo bash

  6. Join ZeroTier Network
    zerotier-cli join <network-id>

  7. Install Vim
    sudo apt-get install vim

  8. Configure default text editor to use vim.basic instead of nano:
    sudo update-alternatives --config editor

  9. Disable prompting of password when executing command via sudo:

    1. Run visudo to bring up editor to edit sudoers.d files
      sudo visudo

    2. Append the following line to the end of file. Save then exit.
      pi ALL=(ALL:ALL) NOPASSWD:ALL

    3. Or, do the same with the following command and uncomment the line in it:
      sudo visudo -f /etc/sudoers.d/010_pi-nopasswd

  10. Increase Raspberry Pi camera video resolution (from 640x480 to 1280x720)

    1. Open octopi.txt for editing
      sudo vi /boot/octopi.txt

    2. Include the following line

camera_usb_options="-r 1280x720 -f 30"

  1. Save and exit

  1. Setup SAMBA mount to network attached storage (i.e., ps-nas) using autofs instead of fstab (autofs connects on-demand).

sudo mkdir /mnt/octolapse-timelapse

sudo apt-get install autofs

sudo tee ~/.smbcredentials < EOL

username=<samba_account_username>

password=<samba_account_password>

EOL

sudo tee /etc/auto.master.d/smb.autofs << EOL

/- /etc/auto.smb.shares --timeout 15 browse

EOL

sudo tee /etc/auto.smb.shares << EOL

/mnt/public-data/                -fstype=cifs,uid=1000,gid=1000,rw,noperm,dir_mode=0775,file_mode=0664,credentials=/home/pi/.smbcredentials,iocharset=utf8 ://192.168.193.100/public-data/

/mnt/octoprint-timelapse/        -fstype=cifs,uid=1000,gid=1000,rw,noperm,dir_mode=0775,file_mode=0664,credentials=/home/pi/.smbcredentials,iocharset=utf8 ://192.168.193.100/octoprint-timelapse/octo-<#>/

/home/pi/.octoprint/uploads/nas/ -fstype=cifs,uid=1000,gid=1000,rw,noperm,dir_mode=0775,file_mode=0664,credentials=/home/pi/.smbcredentials,iocharset=utf8 ://192.168.193.100/octoprint-data/printer-<#>/

EOL

sudo systemctl restart autofs

  1. Configure OctoPrint

    1. Visit http://octo-pi-hostname-or-ip/

    2. Configure initial settings

      1. Enable login security

      2. Provide login and password

      3. Disable connectivity check

      4. Disable anonymous usage reporting

      5. Disable plugin blacklisting

      6. Create printer profile

    3. Update OctoPrint Settings values

      1. Click the Wrench icon to open the Settings menu.

      2. Make the following changes

        1. Features >

          1. Features > Analyze gcode for time and model size estimate >
            Never

          2. Webcam and Timelapse >

            1. Webcam >

              1. Check "Enable webcam support"

              2. Stream URL
                /webcam/?action=stream

            2. Timelapse Recordings >

              1. Snapshot URL:
                http://127.0.0.1:8080/?action=snapshot

              2. Path to FFMPEG
                /usr/bin/ffmpeg

        2. OctoPrint >

          1. Server >

            1. Restart OctoPrint:
              sudo service octoprint restart

            2. Restart system:
              sudo shutdown -r now

            3. Shutdown system:
              sudo shutdown -h now

          2. Plugin Manager > Get More >

            1. Fullscreen Webcam

            2. Firmware Updater

          3. Software update >

            1. Perform update

      3. Save

    4. Click the Power button > Reboot system

PeopleSpace International