Android phone as octoprint server

I was also trying to install your app on another 2 devices (both rooted) but in both cases I got error - app stopped working after I hit install Octoprint button in the app.

I have a tx6 android tv box with real USB ports. I installed everything and tried connecting to the printer (lk4pro) but I see no printer
Where do I find it? Under /dev/what ?
Termux-usb -l shows no devices

Hey @Filip,

I built the app on my laptop and have installed it on an old phone. It starts the octoserver, it sees the connected printer, but octoprint doesn't see the serial port. I think it is a rights issue, could it be that the child process doesn't get the permission to access the serial devices ?

I'm investigating the issue right now, looks like it broke with some octoprint update. I'll try to upload the fix this week

Hi, I want to use my Android phone as print-server too, but sadly @Filip 's .apk crashed while installing octoprint. My phone in question is an ancient Galaxy S2 (almost 10 years old by now...), but it still gets unofficial LinageOS updates. Thing is the kernel doesn't supply a driver for /dev/ttyACM* devices, and I'm to lazy to recompile it. But thanks to a couple helpful pointers in here I came up with another solution:

I made an Octoprint plugin that uses termux-usb and libusb to talk to CDC-ACM usb-serial devices. I tested this on my Galaxy S2 with LineageOS 17.1 (=Android 10) and an Ender 3 with a SKR-Mini E3 board. It is a bit flaky, also when while printing the printer stutters because something is still not fast enough, and also after like a minute of printing the phone crashes :joy: But it works up to that point :partying_face:

If you want to try it, see code and basic instructions here: https://github.com/schuhumi/Termux_CDC_ACM

It's not exactly elegant, because it seems like you can only get the file descriptor for the usb device by running termux-usb -r -e myprogram.py /dev/bus/usb/001/011. So this Plugin opens a subprocess that starts the so called "cdcDaemon" with termux-usb. That Daemon uses the usb-fd to talk to the printer. The Daemon and Octoprint in turn are talking through two sockets (one for each direction).

2 Likes

Fixed a couple bugs and remembered I had a HTC 10 laying around as well -> Got my first smooth print off of an Android phone! :rocket: :grin: https://imgur.com/a/qw9E5Q3

This method should work with most devices as it neither needs root nor the dcd-acm driver in the kernel enabled.

1 Like

This is awesome! I think this is a very big deal. I have a new printer coming in and I'm going to set this up and report back.

Hey, @schuhumi

Trying to use the plugin in non rooted Honor 9. Almost there:

~ $ termux-usb -r /dev/bus/usb/001/002
Access granted.

Octoprint sees the port, but connections with 250000 baud rate failsl. 250000 is default for my printer, and I could communicate with it using other serial port connection apps. But here it fails:

2021-01-11 12:42:08,479 - octoprint.util.comm - ERROR - Unexpected error while connecting to serial port /dev/bus/usb/001/002, baudrate 250000 from hook Termux_CDC_ACM: timeout: 'timed out' @ comm.py:_open_serial:3666
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/octoprint/util/comm.py", line 3666, in _open_serial
    serial_obj = factory(
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/octoprint/util/__init__.py", line 1890, in wrapper
    return f(*args, **kwargs)
  File "/data/data/com.termux/files/home/.octoprint/plugins/Termux_CDC_ACM/__init__.py", line 95, in serial_printer_factory
    serial_obj = serial_printer(
  File "/data/data/com.termux/files/home/.octoprint/plugins/Termux_CDC_ACM/__init__.py", line 38, in __init__
    self.OOPIconn = self.OOPIlistener.accept()
  File "/data/data/com.termux/files/usr/lib/python3.9/multiprocessing/connection.py", line 468, in accept
    c = self._listener.accept()
  File "/data/data/com.termux/files/usr/lib/python3.9/multiprocessing/connection.py", line 614, in accept
    s, self._last_accepted = self._socket.accept()
  File "/data/data/com.termux/files/usr/lib/python3.9/socket.py", line 293, in accept
    fd, addr = self._accept()
socket.timeout: timed out

What could cause this?

After a reboot and a fresh restart I saw a hint. Looks like serialDaemon gets None from libusb1.get_backend(), and socket fails to start, that leads to problem in previous Traceback.

It appears libusb can not work in my setup, backend can not be initialized:

~/.../plugins/Termux_CDC_ACM $ export PYUSB_DEBUG=debug
~/.../plugins/Termux_CDC_ACM $ python
Python 3.9.1 (default, Jan  8 2021, 21:18:33)
[Clang 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import usb
>>> usb.core.find()
2021-01-12 00:45:49,299 DEBUG:usb.backend.libusb1:_LibUSB.__init__(<CDLL 'libusb-1.0.so', handle 8f400b6fe1046d6f at 0x707bc5abb0>)
2021-01-12 00:45:49,302 ERROR:usb.backend.libusb1:Error loading libusb 1.0 backend
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/usb/backend/libusb1.py", line 964, in get_backend
    _lib_object = _LibUSB(_lib)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/usb/_debug.py", line 62, in do_trace
    return f(*args, **named_args)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/usb/backend/libusb1.py", line 717, in __init__
    _check(self.lib.libusb_init(byref(self.ctx)))
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/usb/backend/libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 5] Input/Output Error
2021-01-12 00:45:49,307 DEBUG:usb.backend.libusb1:_LibUSB._finalize_object()
2021-01-12 00:45:49,737 ERROR:usb.libloader:'OpenUSB library' could not be found
2021-01-12 00:45:49,738 ERROR:usb.backend.openusb:Error loading OpenUSB backend
2021-01-12 00:45:50,984 ERROR:usb.libloader:'Libusb 0' could not be found
2021-01-12 00:45:50,985 ERROR:usb.backend.libusb0:Error loading libusb 0.1 backend
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/usb/core.py", line 1297, in find
    raise NoBackendError('No backend available')
usb.core.NoBackendError: No backend available

Somehow libusb_init fails with Input/Output Error. export LIBUSB_DEBUG=1 outputs libusb: error [sysfs_get_device_list] opendir devices failed, errno=13 which means that opendir('/sys/bus/usb/devices') fails with access denied.

Really strange, because "Serial USB Terminal" app works ok and outputs printer serial port initialization messages. Probably some kind of Termux environment issue. Out of ideas, right now.

I just wanted to say that this is amazing! Not only is it super cool to be able to control my 3D printer from an old phone, but the installation process is also so much easier than on Windows (at least for me).

Thank you so much.
Samsung Galaxy J7 Duo and Anycubic Mega X.

On a fresh install i get my printer to connect by setting this serial port manualy in settings/serial connections/additional serial port (/dev/bus/usb/001/002). Wehn connecting set the port from auto detect to /dev/ttySAC0.

I taped my 5v pin. (Worked without it but just to be safe). Then booted the Printer.(Ender 3 Pro 4.2.2 board with marlin 2.0.x) pluged in the usb cable into the printer as first and into the Phone as last(Samsung Galaxy S9+ with one ui 2.5) via the original OTG connecter provided with the Phone. The connection is stable but if not done in the proper order it won't connect even if repeated properly.
When it won't connect anymore deleting the cache of the Octoprint on Android app seems to solve the problem.

I managed to root my phone and got Termux_CDC_ACM working under root, but for Anycubic S3 Mega it says serialDaemon:: Error: Couldn't find matching driver for usb device hex(dev.idVendor)='0x10c4', hex(dev.idProduct)='0xea60' !

Looks like your Mega X uses chipset similar to one on S3 Mega, and if it works for you, it should work for me. What it your working setup for Anycubic Mega X?

Maybe it's because my phone wasn't rooted. I just installed the .apk as it was and everything worked out of the box.
I'm running an Android version from around february 2020.

Hi there. I love that you are working on this. Thanks a lot.

I installed @Filip 's app on an unrooted phone and hooked it up to my Ender3.
I was able to get the server running on the phone, and also did the setup using the web ui.

Do I need to install anything else?

The app sais it is connected to the printer on /dev/bus/usb/001/003. But it cannot connect to the printer in the web ui.

The serial log states
2021-01-30 17:53:19,142 - Performing autodetection with 0 port/baudrate candidates:
2021-01-30 17:53:19,144 - Changing monitoring state from "Detecting serial connection" to "Error: No more candidates to test, and no working port/baudrate combination detected."
2021-01-30 17:53:19,164 - Changing monitoring state from "Error: No more candidates to test, and no working port/baudrate combination detected." to "Offline (Error: No more candidates to test, and no working port/baudrate combination detected.)"

I added /dev/bus/usb/001/003 to the additional ports, but that does not change anything. What could I add instead?

I also tried setting the baudrate to 115200 manually. Did not help either.

Anything else I am missing?

@MoKaEins @3ddy84 Hi, sorry for late reply. I've released a new version that should fix the connection issues. Builds + discussion group are available on telegram: https://t.me/octo4achat

Hey, maybe it would be a good idea to integrate this solution with my app. Right now my solution works through two fifos communicating with Android SDK's API's, but using termux's libusb might be a better solution. I'd love to talk it out with you :slight_smile:

All new releases of my app are now available on github:

1 Like

Hey its possible to connect with OTG "One plus x e1001" Android 6.0.1 and Crealitiy Ender 3 v2?

State: Offline after error
No more candidates to test, and no working port/baudrate combination detected.

I have started to use this solution as my standard Octoprint host. TL;DR this is a great project that deserves more attention. Motivation was simply the unavailability of PIs.

Few experiences:
I installed it on an old Amazon Fire 2015 tablet. Unusable otherwise. The tablet is by benchmark roughly a Pi3. There are the already reported issues with installation and that you have to fix it by ssh. And the camera upon restart gets changed to the wrong one (solution: restart again).

The real issue with these old devices before USB-C is power supply in OTG mode. If the tablet becomes busmaster by plugging the OTG adapter, it stops charging. Also tried an old Samsung phone. Same. This behavior conforms to standards. The busmaster supplies the power.
I tried everything from soldering adapters with ensured 5V to Vin on the tablets USB to powered USB hubs. No chance. Seems to be deep in the kernel.
So I opened the tablet and found quite some space. I put a TP4056 LiIon charge board with USB into the tablet and wired it directly to the battery. This is ok as the TP4056 ensures that the battery will not be overcharged (beyond 4.2V). So I have a second USB port on that tablet now. That saves me an adapter (alternative would be to take Vin from the original USB and rewire). USB1 is normal where I connect the OTG adapter and the printer. USB2 is for a charger only.
So far this works fine. CPU-z shows that the battery voltage settles at approx 4.1V in steady state. The Android power management tolerates it. The tablet thinks it has 71% SoC and 8h remaining runtime. Same numbers for a week now :slight_smile:

Mod for reference; blue board is the TP4056