Thursday 3 September 2020

Klipper On Android - Galaxy S3 - Ender 3 Pro


 I've succesfully installed klipper on my old phone Galaxy S3. If you haven't read  first part of this topic "Octoprint on Android" please read it, cause i'm assuming you already accomplished the tasks below: 

Prerequisites

  • Android with printer serial driver installed (ch341 serial driver for Ender3 Pro).
  • Debian installed on Android  (using Linux Deploy) 
  • Always on phone (Check my other posts for an always on phone

Setting Klippy-Env Python Version

Klipper needs newer python like octoprint as Debian Jessie is too old. Klipper creates a "klippy-env" folder when installing it, so creating a virtual-env and changing python version in advance helps overcoming problems: 
mkdir /home/pi/klippy-env
virtualenv --python=/usr/local/lib/python2.7.17/bin/python klippy-env
sudo apt-get install libffi-dev
sudo apt-get install git
source /home/pi/klippy-env/bin/activate

Check python and pip version inside virtualenv (python 2.7.17, pip 20.x)
python --version
pip install --upgrade "pip < 21.0" 
pip install --upgrade setuptools
pip install greenlet
pip install cffi
pip install pyserial

Installing Klipper

You could now start installing klipper (exit virtualenv first) 
deactivate
git clone https://github.com/KevinOConnor/klipper
You need to start installing using klipper scripts (Which is standard installation of klipper). 
/home/pi/klipper/scripts/install-octopi.sh

Klipper Configuration File

In service start file we show printer.cfg file location in /home/pi.  Copy the sample config file to "/home/pi/printer.cfg" and make it readable by any user so that octoprint can read it 
cp /home/pi/klipper/config/printer-creality-ender3-2018.cfg /home/pi/printer.cfg 
chmod 777 /home/pi/printer.cfg 
If you unplug and plug usb, sometimes serial connection changes from TTYUSB0 to TTYUSB1 (like below picture) so it is better to connect to device by its id.

Enable virtual-sdcard from scratch so that you could start printing from lcd screen, and it works faster since klipper is directly sending commands not octoprint to klipper and klipper to printer.

You are still not ready to start klipper, next action is compiling: 

Compiling Klipper for Mainboard

If you can not succeed compiling klipper on a phone, you could do it on a linux and then you could copy the "klipper" folder to your phone. If you don't compile it you won't have the necessary dictionaries etc. and you will have problems. In my case it seems like it was working but when i checked "/tmp/klippy.log", i saw errors of missing build.dict  logs. 

You need to compile klipper according to your printer board, which for my case ATMEGA 1284P 16Mhz for Ender3 Pro standart board (not the quiet one) 

cd /home/pi/klipper/
make menuconfig


then you need to "make": 


So if everything up to here is successful, start klipper and check for logs whether it works without any warning logs etc. Below commands clear log file, restart klipper and check it's status and log files: 
pi@localhost:~/klipper$ sudo rm -rf /tmp/*
pi@localhost:~/klipper$ sudo service klipper restart
Restarting klipper: klipperStopping klipper: klipper.
Starting klipper: klipper.
pi@localhost:~/klipper$ sudo service klipper status
pi@localhost:~/klipper$ cat /tmp/klippy.log


Binding Octoprint to Klipper

Before changing the firmware of your board we need to be sure of 2 things: 
  • klipper and octoprint services are running (above) 
  • Octoprint is connected to klipper 
So you need to bind octoprint to klipper as below: 


Next action is changing firmware of your printer and if klipper & octoprint does not work you will loose time either finding the problem or converting your printer back to Marlin.  

Flashing Firmware to Mainboard

To flash klipper to printer, you need determine the serial connnected port by its id. First determine the id: 
ls /dev/serial/by-id/*
It should report something similar to the following:
/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0
You can then flash klipper to printer. 
make flash FLASH_DEVICE=/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0
After flashing, connect your phone (Which klipper is active), and then restart klipper service. 

Fixing Permissions 

If you try connecting to printer, this time you would face an error like "/tmp/printer permission denied." Because symlinks are sticky in tmp folder and if you install octoprint and klipper with different users, octoprint can not reach it. Here is the enhancement link: https://github.com/KevinOConnor/klipper/issues/1902 

Possible solutions are:
- Remove sticky bit and make tmp folder permissions readable (i used this one) 
ls -ld /tmp/printer
sudo chmod a=rwX /tmp/printer
- Remove symlink protection (it is not possible in this debian version unfortunately) 

Temporary Files 

If temporary files uses too much space, at the end your 4gb image could fill and octoprint/klipper might stop working. Keep an eye on the octoprint main page, under upload file menu you could see the. 

Folders you need to erase are: 
  • /home/pi/.octoprint/logs/*
  • /tmp/*
  • /var/log/*

PYTHON SCRIPT 

 I created a python script  that clears all log files, restarts klipper and then change the permission of "/tmp/printer" links. ("sudo nano /home/pi/printer.py")
import os,time
os.system(r'rm -rf /var/log/*') 
print r'/var/log cleaned' 
os.system(r'rm -rf /tmp/*')
print r'/tmp cleaned'
os.system (r'rm -rf /home/pi/.octoprint/logs/*')
print 'octoprint logs cleaned'
os.system (r'service klipper restart') 
print 'klipper service restarted'
time.sleep(2)
os.system(r'sudo chmod a=rwX /tmp/printer')
print 'klipper tmp-printer chmod changed'
Then add a system button to restart klipper by using "system command editor" plugin.


I also recommend a Tplink smartplug and its octoprint plugin to remotely power on/off your printer (red thunder icon).

0 comments: