WirelessPrinting icon indicating copy to clipboard operation
WirelessPrinting copied to clipboard

Work with printers without serial interface (USB Host Mode)

Open TheAssassin opened this issue 5 years ago • 63 comments

Some printers' boards, such as for instance the Creality Ender 2, don't have RX/TX GPIOs for serial communication, which means this project cannot be used with them. As these are mostly inexpensive printers, using OctoPrint with more expensive hardware is annoying due to the price.

The printers however mostly have USB sockets which can be used for serial communication. Therefore the question is: can an ESP board communicate via USB with those?

My hope is there's a USB-to-serial sort of adapter that provides a USB host and translates the USB stuff into raw serial again. The cheap UART adapters you can find online are functionally equivalent to the stuff in the printer board, they lack the host feature and cannot be used.

I know some Mega2560 based Arduino boards can provide a USB host feature and might do the translation, however they're quite bulky and also expensive. It might be cheaper to get something like an Orange Pi or so and try to run OctoPrint on there (UI won't run very well, I hope there will be a completely headless version in the future).

Perhaps there's a kind of USB host chip to do the translation which doesn't require a complex circuit that needs to be soldered on a PCB, as most people don't have access to the tools to realize that.

TheAssassin avatar Jun 11 '19 01:06 TheAssassin

Never say never... https://github.com/cnlohr/espusb/issues/45

In the meantime, this is where you can solder to the Creality mainboard: https://github.com/luc-github/ESP3D/wiki/Creality-CR-10-Ender-3

probonopd avatar Jun 11 '19 05:06 probonopd

I tried to connect my esp8266 usb to mega usb (my printer). On mega side I know it works because I usually use it with repetier esp8266 TX and RX are connected to ch340 (or similar) and I thought, why not to use it and avoid soldering? I failed...

GMagician avatar Jun 11 '19 07:06 GMagician

@probonopd thanks, I might try later with those pins.

TheAssassin avatar Jun 11 '19 09:06 TheAssassin

https://github.com/gdsports/esp8266-usb-host-demos

just-jason avatar Jun 12 '19 06:06 just-jason

Actually there will be a new ESP32 chip version with USB host mode soon: ESP32-S2. I think I am gonna support this one if possible, once boards for it are cheaply available.

Throwing in a USB host to serial chip is always an option of course, so this brings our options (at least) to:

  • ESP32-S2 WITH USB (should become available soon)
  • FT311D
  • FT312D
  • CH341A
  • MAX3421E
  • Possibly CY7C67200
  • Possibly custom firmware on 24FJ64GB002
  • Possibly custom firmware on STM32F4

probonopd avatar Jun 12 '19 06:06 probonopd

Ever tried either of these chips? I'm a bit sceptical, I couldn't find any quick examples for most of the chips that'd be equivalent to what shall be accomplished here.

TheAssassin avatar Jun 12 '19 11:06 TheAssassin

Ever tried either of these chips?

No. But any chip that can convert serial-over-USB to serial should do, at least in theory. In practice, most of this won't make sense from a cost perspective, compared to a Raspberry Pi Zero.

probonopd avatar Jun 12 '19 16:06 probonopd

A Raspberry Pi Zero however doesn't run OctoPrint sufficiently smoothly...

TheAssassin avatar Jun 12 '19 17:06 TheAssassin

Who said OctoPrint? ;-)

I wonder if we can get this to compile on https://github.com/epsilonrt/piduino. Probably the WLAN stuff would need to be ifdef'd away... since you know C++ quite well, would you like to give it a try?

probonopd avatar Jun 12 '19 18:06 probonopd

Who said OctoPrint? ;-)

Well...

It might be cheaper to get something like an Orange Pi or so and try to run OctoPrint on there (UI won't run very well, I hope there will be a completely headless version in the future).

https://github.com/probonopd/WirelessPrinting/issues/123#issue-454434678

Did I mention one of these cheap RasPi clones melted an SD card and it took me an hour to figure out what was wrong?

OctoPrint is great, if you have the right hardware to make use of all the features. However, if you can't, a small, energy-saving solution like WirelessPrinting is preferable.

TheAssassin avatar Jun 12 '19 18:06 TheAssassin

a small, energy-saving solution like WirelessPrinting is preferable

Agree! I was just wondering whether we can make it run on the 5 EUR Raspberry Pi Zero which has USB host mode...

probonopd avatar Jun 12 '19 19:06 probonopd

Maybe by making this app into a proper C++ library and then building two frontends, one for Arduino, one for Linux hosts. That's the easiest and most manageable way.

TheAssassin avatar Jun 12 '19 19:06 TheAssassin

A USB host solution by the way is also great for debugging, you don't have to solder to demonstrate this on arbitrary printers -> great for conferences.

TheAssassin avatar Jun 12 '19 19:06 TheAssassin

In September or so we will have ESP32-S2 :+1:

https://hackaday.com/2019/05/21/new-part-day-espressif-announces-esp32-s2-with-usb/

probonopd avatar Jun 12 '19 19:06 probonopd

I wonder if we can get this to compile on https://github.com/epsilonrt/piduino. Probably the WLAN stuff would need to be ifdef'd away...

Blocked by https://github.com/epsilonrt/piduino/issues/11 as it currently does not seem to support networking at all.

probonopd avatar Jun 15 '19 07:06 probonopd

Looks like the Geeetech 3D WiFi Module can do it.

Looking at http://www.geeetech.com/firmware/Upgrade3DWF/config.txt the hardware is based on ESP8266 but the firmware is proprietary.

I wonder what chip/electronics they are using for the USB host.

probonopd avatar Aug 04 '19 22:08 probonopd

Digging though their firmware gives Cannot Find the interface for Communication Interface Class. which is a string that appears in https://github.com/stm32duino/Arduino_Core_STM32/blob/9b6938fdc50f6d48b8f6f48eff4b1e96092fe666/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Src/usbh_cdc.c#L163 so I would not be surprised if they had a STM32F105xx or STM32F107xx with USB host mode attached to the ESP8266.

https://www.st.com/en/embedded-software/stsw-stm32046.html

For more details about all the components of a USB OTG host and device library, including examples for different types of devices, refer to STM32F105xx, STM32F107xx, STM32F2xx and STM32F4xx USB On-The-Go host and device library User manual (UM1021).

Intelligent guessing when digging through the firmware is fun:

me@host:~$ strings Downloads/*.bin | grep STM32F Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.The OTG_FS is a dual-role device (DRD) controller that supports both device and host functions and is fully compliant with the On-The-Go Supplement to the USB 2.0 Specification. It can also be configured as a host-only or device-only controller, fully compliant with the USB 2.0 Specification. USB host The STM32 Connectivity Line allows developers to take advantage of industry-standard 32-bit processing in designs requiring simultaneous Ethernet, USB, CAN and audio-class I2S capabilities. Two variants are available, including the STM32F105 series combining a Full-Speed USB 2.0 Host/Device/OTG peripheral and two CAN2.0B controllers with advanced filtering capabilities. The STM32F107 family adds a complete 10/100 Ethernet MAC with hardware support for the IEEE1588 Precise Time Protocol, enabling Ethernet connectivity for real-time applications. Dedicated buffers allow the USB OTG, the two CAN controllers and the Ethernet interfaces to operate simultaneously to satisfy communication-gateway applications, as well as a host of challenges requiring flexible, industry-standard connectivity.

So at this point I'd say that's the hardware they are using. Are they flashing the STM32F105xx from the ESP8266, or why does the STM32F105xx stuff show up in the ESP8266 firmware?

Actually, the manual says under "Specs": CPU: STM32F107 + ESP8266

The available STM32F107 boards/modules are more expensive than the Geeetech 3D WiFi Module, so if you need a solution now, then Geeetech 3D WiFi Module may be it.

probonopd avatar Aug 04 '19 22:08 probonopd

ESP32-S2 seems still not to be available yet on eBay.

probonopd avatar Aug 04 '19 23:08 probonopd

ESP32-S2

Has this board ever been released? I don't see any of those in the respective shops.

Regarding the combination of an STM32 and ESP8266, it's also a pretty cheap and very universal solution. Ideally, we could simply combine a bluepill board with a NodeMCU or WeMos D1 Mini. I think that such a solution would work out of the box, provided that the STM32 just bridges between the USB serial interface and its own UART pins. Using an STM32 might even allow for utilizing Marlin's "SD card reader" feature without having to use the serial interface, which can be painfully slow for larger G-Codes.

Edit: the vendor publishes a library which claims to support USB host mode for USB-CDC devices. This class is also used by the USB-to-RS232 bridges, which internally use chips from e.g., FTDI or (especially cheaper ones) Prolific. See https://www.st.com/resource/en/data_brief/stsw-stm32046.pdf for more information.

Edit 2: hosting CDC devices is not available on cheaper boards like the bluepill, you need a specific chip which contains a USB host.

TheAssassin avatar Feb 20 '20 20:02 TheAssassin

Yes, unfortunately the Blue Pill is lacking the needed USB OTG/host functionality.

The ESP32-S2 had been announced some time ago, but as far as I know there are no board with it on the market for sale yet. And also no code for the Arduino framework to use it as a USB OTG/host device yet.

probonopd avatar Feb 22 '20 14:02 probonopd

I've found some USB host shields for ~8USD, that still exceeds a RasPi Zero's price.

Perhaps this repository should be split up and modularized so that it's easier to build frontends for e.g., embedded devices, but also native ones? Some hardware abstraction wouldn't be too hard to implement, C++ makes it really easy (adding an abstract class which defines the serial communication interface as well as two or more implementations for the ESP8266/ESP32, Linux (for the RasPi Zero but also development machine), and in the future perhaps even a simple Arduino Nano (should work for LAN, see #125)). Another advantage is that you can develop on your computer to debug non-hardware-related problems, e.g., the communication protocols. Unit testing also becomes a lot easier.

TheAssassin avatar Feb 25 '20 08:02 TheAssassin

...or we just wait a bit more, then we will get Linux capable modules for the price of an ESP8266. It's only a question of time :-)

probonopd avatar Feb 26 '20 19:02 probonopd

Linux is not a real-time system. But I guess that doesn't really matter here, since we just forward commands to an actual real-time system.

TheAssassin avatar Feb 26 '20 20:02 TheAssassin

Yes. Actually, we can send a few lines "in advance" to what is being printed; hence the exact timing should not matter too much.

probonopd avatar Feb 27 '20 21:02 probonopd

I ordered an STM32F4 for evaluation, which could be combined with a WirelessPrinting device, serving as a bridge between a USB CDC device and UART.

TheAssassin avatar Mar 22 '20 02:03 TheAssassin

Which one did you order? Seems like STM32F401CCU6 boards are becoming cheaply available, but with that dreaded USB Type C plug (despite the chipset having USB 2.0 OTG FS).

Also note that not all printer boards implement USB CDC; some have chip-proprietary implementations like FTDI, Silabs, Prolific, WCH-IC CH340, etc., all of which you need host drivers for. Does STM32CubeMX contain all of those or "just" CDC?

probonopd avatar Mar 22 '20 09:03 probonopd

ESP32-S2 support is beginning to trickle into the libraries... https://github.com/hathach/tinyusb/commit/2ff3f765db7e62201d0491093111b0d649446046. Unfortunately,

The Host stack is under rework and largely untested.

And is likely missing the CDC/other serial modes.

probonopd avatar Apr 13 '20 00:04 probonopd

Someone mentioned the ESP32-S2? http://invidio.us/watch?v=L6IoSVdKwNM Looks like a nice device, let's hope for the best. The thing has just one core, but it's fine for WirelessPrinting, nothing time critical running really. What's a bit annoying is the need for an external SPI MicroSD interface. Maybe it's even a bit cheaper than the ESP32, that would be nice, too.

TheAssassin avatar Jun 18 '20 13:06 TheAssassin

I am interested in developing a pcb with either ESP32-S2 or maybe a USB-Hostcontroller chip like MAX3421 and make it especially for this library with onboard sd-card reader or larger onboard volatile memory if that is better.

Has any tests been made with ESP32-S2 as USB-Host?

Looking at this example makes me think that doing this with an ESP32-S2 will actually work. Atleast there is examplecode for arduino here but should also work in PlatformIO.

https://github.com/espressif/arduino-esp32/blob/master/libraries/USB/examples/USBSerial/USBSerial.ino

Does not take much time or money to get a develop-pcb up and running so I think I will give it a shoot

dlyckelid avatar Feb 02 '22 14:02 dlyckelid

image There you go! Should be a good first prototype and if it works I can spend more time making sure it is more solid EMC and probably a bit better placing of the parts. But for test purposes this should work just fine :)

The only way this makes sence is to buy a larger batch of them because otherwise they will be more expensive than a Raspberry Pi Zero 2 W that can run Octopie.

If I ordered 50 of them fully assembled the cost/piece is $7.4 + shipping and taxes

dlyckelid avatar Feb 02 '22 16:02 dlyckelid