suchmememanyskill / CYD-Klipper

An implementation of a wireless Klipper status display on an ESP32 + screen
https://suchmememanyskill.github.io/CYD-Klipper/
GNU General Public License v3.0
251 stars 33 forks source link

Feature Request? Serial Connection to Pi instead of Wifi #100

Open thomasfjen opened 6 months ago

thomasfjen commented 6 months ago

Hello there! Thank you for the Project! I really the like simple Display option without having to mess klipperscreen and their in relation expensive display.

I'm having frequent wifi dropouts with my screen and i was wondering if a serial connection to the pi would be also possible instead of relying on wifi. A quick google search sadly doesn't gave any results. Do you happen to know if that is possible? And if that's not too much work that would be nice a feature to have.

suchmememanyskill commented 6 months ago

It's not that easy to do so

First i'd have to rewrite all networking code (it's built from the ground up for wifi), second i'd need to write a linux/pi program that actually communicates over serial.

I don't see this to be in scope of the project.

If you want a hardwired connection, use klipperscreen.

thomasfjen commented 6 months ago

Thanks for the answer! That's totally understandable and I already thought that this is too much work and out of scope.

trueserve commented 4 months ago

I disagree with the "use klipperscreen" response. I have a use for this.

While it may not be within your scope of this project, I can see several use cases for this.

I think this could be done fairly easily. Would need some software on the printer side that links the Klipper API over UART to the CYD... can probably modify whconsole.py easily enough for this. Modifying CYD-Klipper to abstract API requests which can then be serial or HTTP doesn't look like it would be that difficult.

This is a feature I've wanted, and now I know others want it. I'll try to find some time to hack this together.

suchmememanyskill commented 4 months ago

@trueserve don't get me wrong, i completely understand the use case.

Issue is that http usage runs pretty deep in the app, as i have to account for the low amount of memory quite a bit. Most responses are parsed while being streamed to save on memory usage. The project is built from the ground up for wifi

I don't see this easily being possible in the current codebase. You'd likely have to abstract quite a bit

trueserve commented 4 months ago

What I looked at in the code, it doesn't look difficult. Maybe there's something I missed but it doesn't look difficult to create an abstraction. Once I get some free cycles, likely mid August I'll work on it.

zefir-o commented 2 months ago

Nice suggestion. I want to use this project and connect using serial instead of wifi, as it could be laggy/unstable. Do you know if there's any progress on this topic? Were you able to implement an abstraction @trueserve?

suchmememanyskill commented 1 week ago

Reopening this issue. Backend is being rewritten and abstracted in #131 Serial implementation is now possible :)

@zefir-o @trueserve @thomasfjen if still interested

zefir-o commented 1 week ago

Thank you @suchmememanyskill great improvement! Could you point to the documentation how to connect the display to the board? I was not able to find such documentation. Also, if I see correctly, the latest release doesn't contain this feature and I should compile it and flash manually, right?

zefir-o commented 1 week ago

And also, it would be nice to describe, which changes are required from the klipper side (how to setup it). Thank you

suchmememanyskill commented 1 week ago

@zefir-o it's still in active development, so no release has been made yet.

It uses usb serial for communication. That way a single usb cable can be used for power and communication. You do lose the logs and serial console while using the serial connection for printer communication.

See https://github.com/suchmememanyskill/CYD-Klipper/blob/printer-interface/Manual_Install.md or compile from source from the 'printer-interface' branch if you want to try it in it's current state.

The pc-side server script still needs some work, namely in the way of auto discovery. Clone this repository (again, 'printer-interface' branch), install requests pip3 install requests, open the script serial_server.py, and adjust parameters to your environment. Then python3 serial_server.py to run the server

thomasfjen commented 1 week ago

Reopening this issue. Backend is being rewritten and abstracted in #131 Serial implementation is now possible :)

@zefir-o @trueserve @thomasfjen if still interested

Wow nice! Can't wait to try it out. Thank you for implementing that!

zefir-o commented 1 week ago

@suchmememanyskill I tested it out. Great work, and thank you again for developing it!

Good news: I was able to connect to the printer by connecting the display via USB to Windows. It works even if I configure the port to 80 instead of Moonraker's port. Is this intended?

However, when I connected the display to Ubuntu, I'm encountering errors almost all the time. The script (serial_server) fails in various places (I think there are 2-3 different errors).

Would you like a detailed report on the errors in Ubuntu?

suchmememanyskill commented 1 week ago

@zefir-o usually on printers port 80, endpoints past /api get re-routed to 7125, so should be fine

I haven not tested this yet on linux. It would be great if you could send me the errors.

I have pushed a new commit recently that should have better auto-detection capabilities, and also does configuration via environment variables rather than in the script. Would you mind testing that too?

zefir-o commented 5 days ago

@suchmememanyskill The errors, which I meant were caused by using the script on WSL. Now, I want to report, that I've been using the script under the docker container on orange pi for 2+ days without any issues. Great improvement!

Also, I wonder, if I can connect the display to the USB on the host machine and use RX-TX on the board directly, to make the connection more compact. Will such a setup work or do I need an adapter?

suchmememanyskill commented 5 days ago

@zefir-o Good to hear!

if I can connect the display to the USB on the host

Can you elaborate what you mean by that? I mean, yes, as long as you can run the script on the host, and connect it over usb, you can use cyd-klipper's serial interface.

For connecting through RX/TX, skipping the usb serial adapter, you can also technically do this. You'd have to hard specify the path to the serial interface though in the script through environment variables.

thomasfjen commented 5 days ago

grafik

So while trying to install the host serial server i get this error. Should a virtual enviroment be installed?

As additional info i have shake&tune installed which does alot of python stuff and might be the reason for that issue?

suchmememanyskill commented 5 days ago

@thomasfjen Oh, i forgot ubuntu devs are completely insane and don't use python's built in package manager.

If you can install packages via pip3 in a venv, then set up a venv. If you do figure that out, could you send me the needed commands?

thomasfjen commented 4 days ago

@thomasfjen Oh, i forgot ubuntu devs are completely insane and don't use python's built in package manager.

If you can install packages via pip3 in a venv, then set up a venv. If you do figure that out, could you send me the needed commands?

#!/bin/bash

KLIPPER_VENV_PATH="${HOME}/klippy-env"

if [ "$EUID" -eq 0 ]; then
    echo "Please do not run as root"
    exit
fi

set -e

# Install dependencies
source "${KLIPPER_VENV_PATH}/bin/activate"
pip3 install -r requirements.txt
deactivate

# Create systemd unit file
mkdir -p ~/.config/systemd/user
echo "[Unit]" > ~/.config/systemd/user/cyd-klipper-serial.service
echo "Description=CYD Klipper serial server" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "After=network.target" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "[Service]" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "ExecStart=python3 $(pwd)/serial_server.py" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "WorkingDirectory=$(pwd)" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "Restart=always" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "[Install]" >> ~/.config/systemd/user/cyd-klipper-serial.service
echo "WantedBy=multi-user.target" >> ~/.config/systemd/user/cyd-klipper-serial.service

# Start the service
systemctl --user daemon-reload
systemctl --user enable cyd-klipper-serial
systemctl --user start cyd-klipper-serial

I got it working. TBH i don't know anything regarding python but i looked at the shake&tune install script and copied over three commands from there. If i read that correctly it uses the python venv from it and it did work.

from here

zefir-o commented 3 days ago

@zefir-o Good to hear!

if I can connect the display to the USB on the host

Can you elaborate what you mean by that? I mean, yes, as long as you can run the script on the host, and connect it over usb, you can use cyd-klipper's serial interface.

For connecting through RX/TX, skipping the usb serial adapter, you can also technically do this. You'd have to hard specify the path to the serial interface though in the script through environment variables.

Hi, The idea is to connect the USB to the host, cut the wire and connect to rx, tx, gnd, 5v pins to jst connector. Do you think it will work?

zefir-o commented 3 days ago

I think, I found a bug. From time to time (quite often) the bed preheat buttons doesn't work. If I press "set" and enter the temp manually it works, but usage of presets 60/70 doesn't work.

suchmememanyskill commented 2 days ago

@zefir-o Cannot reproduce, can you send what it outputs in the serial console log? I expect something along the lines of

>>> HTTP_REQUEST 5000 GET /printer/gcode/script?script=M104%20S70
<<< 200 {"result": "ok"}
zefir-o commented 2 days ago

HTTP_REQUEST 1000 GET /printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan&display_status <<< 200 {"result": {"eventtime": 550.244650762, "st... [LOG] Deserialization result: Ok HTTP_REQUEST 5000 GET /printer/gcode/script?script=M140%20S60 <<< 200 {"result": "ok"} [LOG] Deserialization result: Ok HTTP_REQUEST 1000 GET /printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan&display_status <<< 200 {"result": {"eventtime": 551.24677497, "sta... [LOG] Deserialization result: Ok HTTP_REQUEST 1000 GET /printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan&display_status <<< 200 {"result": {"eventtime": 552.249479679, "st... [LOG] Deserialization result: Ok HTTP_REQUEST 1000 GET /printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan&display_status <<< 200 {"result": {"eventtime": 553.252086388, "st... [LOG] Deserialization result: Ok HTTP_REQUEST 5000 GET /printer/gcode/script?script=M104%20S70 <<< 200 {"result": "ok"} [LOG] Deserialization result: Ok HTTP_REQUEST 1000 GET /printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan&display_status <<< 200 {"result": {"eventtime": 554.25497518, "sta...

70 is set via the "set" bed temp button, whereas 60- using the button "60". Also, I found, that the bug is reproducible when I first set the extruder temp via any button. Otherwise, it works fine.

suchmememanyskill commented 2 days ago

Again, cannot reproduce. Clicking 200c for extruder, then 60c for bed works fine. Using the mainsail test printer docker image also shows popup messages for set temperatures. Your printer also accepts it (200 OK). You sure it's not on the printer side? The commands sent look fine

zefir-o commented 2 days ago

🤔 I have no idea what happens. The error code seems to be ok, however setting bed temp works using "set" button and doesn't work from 60/70/0 button.

suchmememanyskill commented 2 days ago

There isn't any difference in the codes sent via serial between setting it using the set button and setting it using the preset buttons, right?

suchmememanyskill commented 2 days ago

Wait, @zefir-o does it by chance set the nozzle temperature? (like the bed temp button mistakenly sets the nozzle temp?)

zefir-o commented 2 days ago

Yes, it does.

suchmememanyskill commented 1 day ago

@zefir-o Fixed in the latest commit