Skip to content

Instantly share code, notes, and snippets.

@CODeRUS
Last active October 2, 2023 09:27
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save CODeRUS/a5ec4a456f5b58186cbebb66a8542a2e to your computer and use it in GitHub Desktop.
Save CODeRUS/a5ec4a456f5b58186cbebb66a8542a2e to your computer and use it in GitHub Desktop.
Klipper + moonraker + KlipperScreen linuxdeploy android install

Prerequisites

  • Disto: Debian
  • Version: stable
  • Size: at least 4000
  • Username and password: please set
  • Autorun: enable
  • Init system: sysv
  • Mount folders: one can enable android folder mount for config and gcode
  • SSH: enable
  • Graphics: enable
  • Graphics subsystem: VNC or X11
  • Desktop environment: XTerm

Install

sudo apt install curl -y

curl -sL https://gist.githubusercontent.com/CODeRUS/a5ec4a456f5b58186cbebb66a8542a2e/raw/klipper-install-debian.sh | bash -

#!/bin/bash
if [ "$(id -u)" = "0" ]
then
echo "Start script as user!"
exit 1
fi
read -d . DEBIAN_VERSION < /etc/debian_version
if [ "$DEBIAN_VERSION" != "11" ]
then
echo "You should run Debian 11!"
exit 1
fi
if ! grep xterm "$HOME/.xsession" > /dev/null
then
echo "Configure container with XTerm graphics over VNC or X11!"
exit 1
fi
### environment
echo "Initializing environment variables"
KIAUH="$HOME/kiauh"
KLIPPER="$HOME/klipper"
MOONRAKER="$HOME/moonraker"
KLIPPERSCREEN="$HOME/KlipperScreen"
KLIPPER_START="/etc/init.d/klipper"
MOONRAKER_START="/etc/init.d/moonraker"
KLIPPER_CONFIG="$HOME/klipper_config"
GCODE_FILES="$HOME/gcode_files"
KLIPPERSCREEN_XTERM="/usr/local/bin/xterm"
TTYFIX="/usr/bin/ttyfix"
TTYFIX_START="/etc/init.d/ttyfix"
### Mounting /tmp
echo "Re-mounting /tmp from tmpfs"
sudo mount -o mode=1777,nosuid,nodev -t tmpfs tmpfs /tmp
### packages
echo "Installing required packages"
sudo apt update
sudo apt install -y \
git inotify-tools virtualenv libffi-dev build-essential libncurses-dev libusb-dev stm32flash libnewlib-arm-none-eabi gcc-arm-none-eabi binutils-arm-none-eabi libusb-1.0-0 pkg-config dfu-util \
python3-virtualenv python3-dev python3-libgpiod liblmdb0 libsodium-dev zlib1g-dev libjpeg-dev libcurl4-openssl-dev libssl-dev \
python3-tornado python3-serial python3-pillow python3-lmdb python3-libnacl python3-paho-mqtt python3-pycurl curl \
libopenjp2-7 python3-distutils python3-gi python3-gi-cairo gir1.2-gtk-3.0 wireless-tools libatlas-base-dev fonts-freefont-ttf python3-websocket python3-requests python3-humanize python3-jinja2 python3-ruamel.yaml python3-matplotlib
sudo apt install -f
sudo apt clean
### git
echo "Clonning klipper software"
git clone https://github.com/th33xitus/kiauh.git $KIAUH
git clone https://github.com/KevinOConnor/klipper.git $KLIPPER
git clone https://github.com/Arksine/moonraker.git $MOONRAKER
git clone https://github.com/jordanruthe/KlipperScreen.git $KLIPPERSCREEN
### folders
echo "Creating klipper folders"
mkdir -p $KLIPPER_CONFIG
mkdir -p $GCODE_FILES
### configs
echo "Fixing config files"
cp -f $KIAUH/resources/printer.cfg $KLIPPER_CONFIG
cp -f $KIAUH/resources/kiauh_macros.cfg $KLIPPER_CONFIG
sed -i "s#serial:.*#serial: /dev/ttyACM0#" $KLIPPER_CONFIG/printer.cfg
sed -i "1 i [include kiauh_macros.cfg]" $KLIPPER_CONFIG/printer.cfg
cp -f $KLIPPERSCREEN/ks_includes/defaults.conf $KLIPPER_CONFIG/KlipperScreen.conf
### klipper
echo "Installing klipper"
sudo python3 -m pip install setuptools wheel
sudo python3 -m pip install -r $KLIPPER/scripts/klippy-requirements.txt
sudo python3 -m pip cache purge
### moonraker
echo "Installing moonraker"
sudo pip3 install -U pip setuptools wheel
sudo pip3 install --no-use-pep517 "$(grep "streaming-form-data" ${MOONRAKER}/scripts/moonraker-requirements.txt)"
sudo pip3 install --no-use-pep517 "$(grep "distro" ${MOONRAKER}/scripts/moonraker-requirements.txt)"
sudo pip3 install --no-use-pep517 "$(grep "inotify-simple" ${MOONRAKER}/scripts/moonraker-requirements.txt)"
for n in tornado pyserial pillow lmdb libnacl paho-mqtt pycurl
do
sed -i "s#$n.*#$n#" ${MOONRAKER}/scripts/moonraker-requirements.txt
done
sudo pip3 install --no-use-pep517 -r ${MOONRAKER}/scripts/moonraker-requirements.txt
### KlipperScreen
echo "Installing KlipperScreen"
for n in humanize jinja2 matplotlib requests websocket-client
do
sed -i "s#$n.*#$n#" ${KLIPPERSCREEN}/scripts/KlipperScreen-requirements.txt
done
sudo pip3 install --no-use-pep517 "$(grep "netifaces" ${KLIPPERSCREEN}/scripts/KlipperScreen-requirements.txt)"
sudo pip3 install --no-use-pep517 "$(grep "vext" ${KLIPPERSCREEN}/scripts/KlipperScreen-requirements.txt)"
sudo pip3 install -r ${KLIPPERSCREEN}/scripts/KlipperScreen-requirements.txt
sudo pip3 cache purge
sudo tee "$KLIPPERSCREEN_XTERM" <<EOF
#!/bin/bash
cd $KLIPPERSCREEN
exec python3 ./screen.py
EOF
sudo chmod +x "$KLIPPERSCREEN_XTERM"
cat <<EOF > ${KLIPPER_CONFIG}/moonraker.conf
[server]
host: 0.0.0.0
port: 7125
enable_debug_logging: False
config_path: $KLIPPER_CONFIG
database_path: ~/.moonraker_database
klippy_uds_address: /tmp/klippy_uds
[authorization]
trusted_clients:
127.0.0.1
192.168.0.0/16
::1/128
FE80::/10
cors_domains:
*.lan
*.local
*://my.mainsail.xyz
*://app.fluidd.xyz
*://dev-app.fluidd.xyz
[octoprint_compat]
[history]
EOF
### autostart
echo "Creating autostart entries for sysv"
sudo tee "$TTYFIX" <<EOF
#!/bin/bash
inotifywait -m /dev -e create |
while read dir action file
do
[ "\$file" = "ttyACM0" ] && chmod 777 /dev/ttyACM0
done
EOF
sudo chmod +x "$TTYFIX"
sudo tee "$TTYFIX_START" <<EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: ttyfix
# Default-Start: 2 3 4 5
# Default-Stop:
# Required-Start: \$local_fs \$remote_fs
# Short-Description: ttyfix
# Description: ttyfix
### END INIT INFO
. /lib/lsb/init-functions
N="$TTYFIX_START"
PIDFILE=/run/ttyfix.pid
EXEC="$TTYFIX"
set -e
f_start ()
{
start-stop-daemon --start --background --make-pidfile --pidfile \$PIDFILE --exec \$EXEC
}
f_stop ()
{
start-stop-daemon --stop --pidfile \$PIDFILE
}
case "\$1" in
start)
f_start
;;
stop)
f_stop
;;
restart)
f_stop
sleep 1
f_start
;;
reload|force-reload|status)
;;
*)
echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
EOF
sudo chmod +x "$TTYFIX_START"
sudo mv /usr/bin/systemctl /usr/bin/systemctl2
sudo tee /usr/bin/systemctl <<EOF
#!/bin/bash
if [ "\$1" = "list-units" ]
then
echo "klipper.service"
echo "moonraker.service"
exit 0
fi
/usr/sbin/service "\$2" "\$1"
EOF
sudo chmod +x /usr/bin/systemctl
sudo tee $KLIPPER_START <<EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: klipper
# Default-Start: 2 3 4 5
# Default-Stop:
# Required-Start: \$local_fs \$remote_fs
# Short-Description: klipper
# Description: klipper
### END INIT INFO
. /lib/lsb/init-functions
N=$KLIPPER_START
PIDFILE=/run/klipper.pid
USERNAME=$USER
EXEC="/usr/bin/python3"
EXEC_OPTS="/home/\$USERNAME/klipper/klippy/klippy.py /home/\$USERNAME/klipper_config/printer.cfg -l /tmp/klippy.log -a /tmp/klippy_uds"
set -e
f_start ()
{
chmod 777 /dev/ttyACM0 ||:
mount -o mode=1777,nosuid,nodev -t tmpfs tmpfs /tmp
start-stop-daemon --start --background --chuid \$USERNAME --make-pidfile --pidfile \$PIDFILE --exec \$EXEC -- \$EXEC_OPTS
}
f_stop ()
{
start-stop-daemon --stop --pidfile \$PIDFILE
}
case "\$1" in
start)
f_start
;;
stop)
f_stop
;;
restart)
f_stop
sleep 1
f_start
;;
reload|force-reload|status)
;;
*)
echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
EOF
sudo chmod +x $KLIPPER_START
sudo tee $MOONRAKER_START <<EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: moonraker
# Default-Start: 2 3 4 5
# Default-Stop:
# Required-Start: \$local_fs \$remote_fs klipper
# Short-Description: moonraker
# Description: moonraker
### END INIT INFO
. /lib/lsb/init-functions
N=$MOONRAKER_START
PIDFILE=/run/moonraker.pid
USERNAME=$USER
EXEC="/usr/bin/python3"
EXEC_OPTS="/home/\$USERNAME/moonraker/moonraker/moonraker.py -c /home/\$USERNAME/klipper_config/moonraker.conf -l /tmp/moonraker.log"
set -e
f_start ()
{
start-stop-daemon --start --background --chuid \$USERNAME --make-pidfile --pidfile \$PIDFILE --exec \$EXEC -- \$EXEC_OPTS
}
f_stop ()
{
start-stop-daemon --stop --pidfile \$PIDFILE
}
case "\$1" in
start)
f_start
;;
stop)
f_stop
;;
restart)
f_stop
sleep 1
f_start
;;
reload|force-reload|status)
;;
*)
echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
EOF
sudo chmod +x $MOONRAKER_START
sudo update-rc.d klipper defaults
sudo update-rc.d moonraker defaults
sudo update-rc.d ttyfix defaults
echo "Starting klipper and moonraker services now"
sudo service ttyfix start
sudo service klipper start
sleep 10
sudo service moonraker start
echo "Starting KlipperScreen instead of XTerm"
sudo pkill xterm
export DISPLAY=:0
xterm >/dev/null 2>&1 &
### complete
echo "Installation completed!"
@pandel
Copy link

pandel commented Nov 9, 2021

Many thanks for the script! FWIW, after installing Debian 10 via linuxdeploy and upgrading manually to Debian 11, I had to add the following packages to the list of required packages. Before, Klipper wasn't able to compile the firmware.

gcc-avr avr-libc avrdude libftdi1 libhidapi-libusb0 libusb-1.0-0-dev libusb-1.0-doc python-dev-is-python2 python-is-python2

@CODeRUS
Copy link
Author

CODeRUS commented Nov 9, 2021

@pandel nice, if someone is using AVR can get list of packages from your post. But i'm using STM32 boards only.

@RyanEwen
Copy link

This looks great, but I have a couple of questions:

  • Does this require choosing the sysv init system in Linux Deploy (as opposed to run-parts)?
  • Why is virtualenv installed and then not used? I'm worried that klipper or moonraker might get an update one day and end up needing conflicting dependencies, causing me to have to set them up again in their own venvs.

@CODeRUS
Copy link
Author

CODeRUS commented Aug 21, 2022

@RyanEwen hi there
Just added info about setup prerequisites for LD. As of vitrualenv, iirc some of the pip3 packages was refusing to be builded, so we're using ones from linux repos instead. It would be cool if you can try with venv and tell if it works nowadays or not.

@RyanEwen
Copy link

Thank you!

I have everything working using virtualenvs already. I used KIAUH to install everything on Debian 10 in Linux Deploy. Everything installed and just kinda worked. All I was missing were some sysv init scripts which I managed to piece together. Your scripts here were helpful in getting to understand some things as well.

I documented my method using KIAUH here if curious: https://gist.github.com/RyanEwen/ae81fc48ad00397f1026915f0e6beed9

In my case my device's kernel doesn't support CH34x drivers but I managed to find a way to use Octo4a as the driver. Next is to learn how to compile the driver for my device myself so that I don't need Octo4a at all.

@gaifeng8864
Copy link

Great job, it's your script that got me back"
klipper-on-android" confidence.
I used part of your script, combined with the script developed by @d4rk50ul1(https://github.com/d4rk50ul1/klipper-on-android), to rewrite a post-processing script for one-click configuration.
After installing services such as klipper using the kiauh script, this script can quickly configure the initialization of related services, while also ensuring that basic upgrades, restarts, and other functions are available.
My level is limited, are you interested in optimizing it together.
Project address: https://github.com/gaifeng8864/klipper-on-android

@CODeRUS
Copy link
Author

CODeRUS commented Nov 11, 2022

@gaifeng8864 very good!

@CODeRUS
Copy link
Author

CODeRUS commented Nov 12, 2022

@gaifeng8864 can you add readme_en to your project? much appreciated

@gaifeng8864
Copy link

@gaifeng8864 can you add readme_en to your project? much appreciated

OK, I'll do it asap. Thank you for your suggestion.

@gaifeng8864
Copy link

@gaifeng8864 can you add readme_en to your project? much appreciated

An English version of the tutorial:README_en.md has been added . My English is very poor, so I used machine translation, if there are deficiencies, please correct me. much appreciated

@CODeRUS
Copy link
Author

CODeRUS commented Nov 16, 2022

@gaifeng8864 thank you!

@Saurabh251-cmd
Copy link

image
how to fix this :<

@CODeRUS
Copy link
Author

CODeRUS commented Dec 11, 2022

@Saurabh251-cmd I don't think this is good place to ask. Try klipper discourse and facebook groups

@ozcet
Copy link

ozcet commented Jul 24, 2023

Thank you!

I have everything working using virtualenvs already. I used KIAUH to install everything on Debian 10 in Linux Deploy. Everything installed and just kinda worked. All I was missing were some sysv init scripts which I managed to piece together. Your scripts here were helpful in getting to understand some things as well.

I documented my method using KIAUH here if curious: https://gist.github.com/RyanEwen/ae81fc48ad00397f1026915f0e6beed9

In my case my device's kernel doesn't support CH34x drivers but I managed to find a way to use Octo4a as the driver. Next is to learn how to compile the driver for my device myself so that I don't need Octo4a at all.

did you compiled kernel for this driver. ? I am also looking for this.

@CODeRUS
Copy link
Author

CODeRUS commented Jul 24, 2023

@ozcet use octo4a for ch340 serial driver

@ozcet
Copy link

ozcet commented Jul 24, 2023

I am still using this. But It would be better to direct access usb port in linux deploy...thank you..

@gaifeng8864
Copy link

Thank you!
I have everything working using virtualenvs already. I used KIAUH to install everything on Debian 10 in Linux Deploy. Everything installed and just kinda worked. All I was missing were some sysv init scripts which I managed to piece together. Your scripts here were helpful in getting to understand some things as well.
I documented my method using KIAUH here if curious: https://gist.github.com/RyanEwen/ae81fc48ad00397f1026915f0e6beed9
In my case my device's kernel doesn't support CH34x drivers but I managed to find a way to use Octo4a as the driver. Next is to learn how to compile the driver for my device myself so that I don't need Octo4a at all.

did you compiled kernel for this driver. ? I am also looking for this.

I also don't know how to compile the Android kernel at the moment. To compile the kernel driver, you need to find the source code of the kernel, preferably the kernel version currently used by the Android system. If you can’t find the same one, at least the version number must be very close.

@RyanEwen
Copy link

Sorry I did not end up compiling the kernel as it was way more complicated than I hoped and the kernel sources weren't public for many devices.

Also my gist has been replaced by https://github.com/d4rk50ul1/klipper-on-android

I ended up selling my printers and bought some P1Ps instead so sadly this is all in the past for me

@gaifeng8864
Copy link

Sorry I did not end up compiling the kernel as it was way more complicated than I hoped and the kernel sources weren't public for many devices.

Also my gist has been replaced by https://github.com/d4rk50ul1/klipper-on-android

I ended up selling my printers and bought some P1Ps instead so sadly this is all in the past for me

Your choice also matches my thinking. To play with a 3D printer, you only need to DIY one at first, learn the internal principles and debugging skills, and sum up experience. However, after all, self-assembled models are not as stable as industrially produced finished models. For most people, the ultimate goal is to use a 3D printer to make something. Therefore, the end result of most people is to buy a finished machine that is worry-free and has more stable performance and guaranteed after-sales. Just like those who play model airplanes, if they want to take some beautiful aerial shots, they still have to choose DJI in the end.

@RyanEwen
Copy link

RyanEwen commented Aug 1, 2023

@gaifeng8864 that's basically what happened. It was very fun to learn and tinker and upgrade, but in the end I needed machines that just worked and would continue working without my attention. The P1Ps have been amazing. I've had them since February and haven't looked back. Before them I had a Prusa MK3S+ and an Ender 3 S1 Pro. Many other machines came across my desk before those as well. I don't miss any of them. I do miss playing with Klipper and I'm tempted to one day build a Voron 2.4, but I know I'm more likely to buy additional P1Ps or P1Ss

@gaifeng8864
Copy link

@gaifeng8864 that's basically what happened. It was very fun to learn and tinker and upgrade, but in the end I needed machines that just worked and would continue working without my attention. The P1Ps have been amazing. I've had them since February and haven't looked back. Before them I had a Prusa MK3S+ and an Ender 3 S1 Pro. Many other machines came across my desk before those as well. I don't miss any of them. I do miss playing with Klipper and I'm tempted to one day build a Voron 2.4, but I know I'm more likely to buy additional P1Ps or P1Ss

I agree very much. I also have a DIY machine at the moment. If I have enough money and new needs, the next machine must be a finished machine, P1S or X1 series, or a later updated model. So far, Bambu Lab has done a good job in this regard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment