SpellFoundry / Sleepy-Pi-Setup

9 stars 17 forks source link

Sleepy-Pi-Setup - Bookworm OS compatibility ? #24

Open Tchiimy opened 3 months ago

Tchiimy commented 3 months ago

Hi,

I've recently tried to run the Sleepy-Pi-Setup script on my RaspberryPi4 running the Bookworm Debian OS version.

I got an error like This script only works on Jessie, Stretch, Buster or Bullseye at this time

Is it planned to update this script for the new OS version ?

Tchiimy commented 3 months ago

Ok after some test and checking this from @nu00 I've corrected some syntax error and it worked using this:


set -eu

# trap "set +x; sleep 5; set -x" DEBUG

# Check whether we are running sudo
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

# Check the OS
osInfo=$(cat /etc/os-release)
Jessie=false
Stretch=false
Buster=false
Bullseye=false
Bookworm=false
if [[ $osInfo == *"jessie"* ]]; then
    Jessie=true
elif [[ $osInfo == *"stretch"* ]]; then
    Stretch=true
elif [[ $osInfo == *"buster"* ]]; then
    Buster=true
elif [[ $osInfo == *"bullseye"* ]]; then
    Bullseye=true
elif [[ $osInfo == *"bookworm"* ]]; then
    Bookworm=true
else
    echo "This script only works on Jessie, Stretch, Buster, Bullseye or Bookworm at this time"
    exit 1
fi

echo '================================================================================ '
echo '|                                                                               |'
echo '|      Sleepy Pi Installation Script - Jessie, Stretch, Buster or Bullseye      |'
echo '|                                                                               |'
echo '================================================================================ '

## Update and upgrade
# sudo apt-get update && sudo apt-get upgrade -y

## Detecting Pi model
## list available https://elinux.org/Rpi_HardwareHistory
RPi3=false
RPi4=false
RpiCPU=$(grep Revision /proc/cpuinfo | cut -d ':' -f 2 | sed 's/ //g')
case "$RpiCPU" in
    a02082|a22082|a32082) echo "Raspberry Pi 3 detected"; RPi3=true ;;
    a020d3) echo "Raspberry Pi 3 B+ detected"; RPi3=true ;;
    9020e0) echo "Raspberry Pi 3 A+ detected"; RPi3=true ;;
    9000c1) echo "Raspberry Pi Zero W detected"; RPi3=true ;;
    a03111|b03111|b03112|b03114) echo "Raspberry Pi 4 detected"; RPi4=true ;;
    c03111|c03112|c03114) echo "Raspberry Pi 4 detected"; RPi4=true ;;
    d03114) echo "Raspberry Pi 4 8GB detected"; RPi4=true ;;
    902120) echo "Raspberry Pi Zero 2W detected"; RPi3=true ;;
    *) echo "Non-Raspberry Pi 3 or 4 or Zero families detected"; RPi3=false; RPi4=false ;;
esac

echo 'Begin Installation ? (Y/n) '
read -r ReadyInput
if [[ "$ReadyInput" == "Y" || "$ReadyInput" == "y" ]]; then
    echo "Beginning installation..."
else
    echo "Aborting installation"
    exit 0
fi

if [[ $Bookworm != true ]]; then
    userFilePath="/home/pi"
else
    # List all directories in /home
    directories=($(ls -d /home/*/ | sed 's:/*$::'))

    # Get the number of directories
    dir_count=${#directories[@]}

    if [ "$dir_count" -eq 1 ]; then
        # If there's only one directory, set the variable to that directory
        userFilePath=${directories[0]}
        echo "Only one directory found: $userFilePath"
    else
        # If there are multiple directories, prompt the user to choose one
        echo "Multiple directories found:"
        for i in "${!directories[@]}"; do
            echo "$i) ${directories[$i]}"
        done

        # Ask the user to choose a directory
        while true; do
            read -r -p "Select a directory by number: " choice
            if [[ $choice =~ ^[0-9]+$ ]] && [ "$choice" -ge 0 ] && [ "$choice" -lt "$dir_count" ]; then
                userFilePath=${directories[$choice]}
                break
            else
                echo "Invalid choice. Please enter a number between 0 and $((dir_count-1))."
            fi
        done
    fi

    # Output the selected directory
    echo "Selected directory: $userFilePath"
fi

##-------------------------------------------------------------------------------------------------
## Test Area
# echo every line 
set +x

# exit 0
## End Test Area

##-------------------------------------------------------------------------------------------------

## Install Arduino
echo 'Installing Arduino IDE...'
program="arduino"
condition=$(which "$program" 2>/dev/null | grep -v "not found" | wc -l)
if [ "$condition" -eq 0 ] ; then
    apt-get install -y arduino
    # create the default Arduino and libraries that the IDE would normally create on first run
    mkdir -p "$userFilePath/Arduino/libraries"
else
    echo "Arduino IDE is already installed - skipping"
fi

##-------------------------------------------------------------------------------------------------

# filepath for the boot partition
# Prior to Bookworm, Raspberry Pi OS stored the boot partition at /boot/. Since Bookworm, the boot partition is located at /boot/firmware/.
if [[ $Bookworm != true ]]; then
    filepath="/boot"
else
    filepath="/boot/firmware"
fi

## Enable Serial Port
echo 'Enable Serial Port...'
if ! grep -q 'enable_uart=1' "$filepath/config.txt"; then
    echo 'enable_uart=1' | tee -a "$filepath/config.txt"
else
    echo 'enable_uart=1 is already set - skipping'
fi

if ! grep -q 'core_freq=250' "$filepath/config.txt"; then
    echo 'core_freq=250' | tee -a "$filepath/config.txt"
else
    echo 'The frequency of GPU processor core is set to 250MHz already - skipping'
fi

## Disable Serial login
echo 'Disabling Serial Login...'
if [[ $RPi3 != true && $RPi4 != true ]]; then
    # Non-RPi3 or 4
    systemctl stop serial-getty@ttyAMA0.service
    systemctl disable serial-getty@ttyAMA0.service
else
    # Rpi 3 or 4
    systemctl stop serial-getty@ttyS0.service
    systemctl disable serial-getty@ttyS0.service
fi

## Disable Boot info
echo 'Disabling Boot info...'
sed -i'bk' -e 's/console=serial0,115200.//' "$filepath/cmdline.txt"

## Link the Serial Port to the Arduino IDE
echo 'Link Serial Port to Arduino IDE...'
if [[ $RPi3 != true && $RPi4 != true ]]; then
    wget https://raw.githubusercontent.com/SpellFoundry/Sleepy-Pi-Setup/master/80-sleepypi.rules
    mv 80-sleepypi.rules /etc/udev/rules.d/
fi
# Note: On Rpi3 or 4 GPIO serial port defaults to ttyS0 which is what we want

##-------------------------------------------------------------------------------------------------

## Setup the Reset Pin
echo 'Setup the Reset Pin...'
program="autoreset"
condition=$(which "$program" 2>/dev/null | grep -v "not found" | wc -l)
if [ "$condition" -eq 0 ]; then
    wget https://github.com/spellfoundry/avrdude-rpi/archive/master.zip
    unzip master.zip
    cd avrdude-rpi-master || exit
    cp autoreset /usr/bin
    cp avrdude-autoreset /usr/bin
    mv /usr/bin/avrdude /usr/bin/avrdude-original
    cd "$userFilePath" || exit
    rm -f master.zip
    rm -R -f avrdude-rpi-master
    ln -s /usr/bin/avrdude-autoreset /usr/bin/avrdude
else
    echo "$program is already installed - skipping..."
fi

##-------------------------------------------------------------------------------------------------

## Getting Sleepy Pi to shutdown the Raspberry Pi
echo 'Setting up the shutdown...'
cd ~ || exit
if grep -q 'shutdowncheck.py' /etc/rc.local; then
    echo 'shutdowncheck.py is already setup - skipping...'
else
    mkdir -p "$userFilePath/bin/SleepyPi"
    wget https://raw.githubusercontent.com/SpellFoundry/Sleepy-Pi-Setup/master/shutdowncheck.py
    mv -f shutdowncheck.py "$userFilePath/bin/SleepyPi/"
    sed -i '/exit 0/i python $userFilePath/bin/SleepyPi/shutdowncheck.py &' /etc/rc.local
fi

##-------------------------------------------------------------------------------------------------

## Adding the Sleepy Pi to the Arduino environment
echo 'Adding the Sleepy Pi to the Arduino environment...'
# ...setup Arduino
mkdir -p "$userFilePath/Arduino"
# ...setup Arduino/libraries
mkdir -p "$userFilePath/Arduino/libraries"
# .../Arduino/hardware
mkdir -p "$userFilePath/Arduino/hardware"

# .../Arduino/hardware/sleepy_pi2
if [ -d "$userFilePath/Arduino/hardware/sleepy_pi2" ]; then
    echo "Arduino/hardware/sleepy_pi2 exists - skipping..."
else
    mkdir -p "$userFilePath/Arduino/hardware/sleepy_pi2"
    wget https://raw.githubusercontent.com/SpellFoundry/Sleepy-Pi-Setup/master/boards.txt
    mv boards.txt "$userFilePath/Arduino/hardware/sleepy_pi2/"
fi

# .../Arduino/hardware/sleepy_pi
if [ -d "$userFilePath/Arduino/hardware/sleepy_pi" ]; then
    echo "Arduino/hardware/sleepy_pi exists - skipping..."
else
    mkdir -p "$userFilePath/Arduino/hardware/sleepy_pi"
    wget https://raw.githubusercontent.com/SpellFoundry/Sleepy-Pi-Setup/master/boards.txt
    mv boards.txt "$userFilePath/Arduino/hardware/sleepy_pi/"
fi

## Setup the Sleepy Pi Libraries
echo 'Setting up the Sleepy Pi Libraries...'
cd "$userFilePath/Arduino/libraries/" || exit
if [ -d "SleepyPi2" ]; then
    echo "SleepyPi2 Library exists - skipping..."
else
    echo "Installing SleepyPi 2 Library..."
    git clone https://github.com/SpellFoundry/SleepyPi2.git
fi

if [ -d "SleepyPi" ]; then
    echo "SleepyPi Library exists - skipping..."
else
    echo "Installing SleepyPi Library..."
    git clone https://github.com/SpellFoundry/SleepyPi.git
fi

if [ -d "Time" ]; then
    echo "Time Library exists - skipping..."
else
    echo "Installing Time Library..."
    git clone https://github.com/PaulStoffregen/Time.git
fi

if [ -d "LowPower" ]; then
    echo "LowPower Library exists - skipping..."
else
    echo "Installing LowPower Library..."
    git clone https://github.com/rocketscream/Low-Power.git
    # rename the directory as Arduino doesn't like the dash
    mv "$userFilePath/Arduino/libraries/Low-Power" "$userFilePath/Arduino/libraries/LowPower"
fi

# Sleepy Pi 1
if [ -d "DS1374RTC" ]; then
    echo "DS1374RTC Library exists - skipping..."
else
    echo "Installing DS1374RTC Library..."
    git clone https://github.com/SpellFoundry/DS1374RTC.git
fi

# Sleepy Pi 2
if [ -d "PCF8523" ]; then
    echo "PCF8523 Library exists - skipping..."
else
    echo "Installing PCF8523 Library..."
    git clone https://github.com/SpellFoundry/PCF8523.git
fi

if [ -d "PinChangeInt" ]; then
    echo "PinChangeInt Library exists - skipping..."
else
    echo "Installing PinChangeInt Library..."
    git clone https://github.com/GreyGnome/PinChangeInt.git
fi
cd ~ || exit

##-------------------------------------------------------------------------------------------------

# Install i2c-tools
echo 'Enable I2C...'
if grep -q '#dtparam=i2c_arm=on' "$filepath/config.txt"; then
    # uncomment
    sed -i '/dtparam=i2c_arm/s/^#//g' "$filepath/config.txt"
else
    echo 'i2c_arm parameter already set - skipping...'
fi

echo 'Install i2c-tools...'
if hash i2cget 2>/dev/null; then
    echo 'i2c-tools are installed already - skipping...'
else
    apt-get install -y i2c-tools
fi

##-------------------------------------------------------------------------------------------------
echo "Sleepy Pi setup complete!"
echo "Would you like to reboot now? Y/n"
read -r RebootInput
if [[ "$RebootInput" == "Y" ]]; then
    echo "Now rebooting..."
    sleep 3
    reboot
fi
exit 0
##-------------------------------------------------------------------------------------------------