Skip to content

Instantly share code, notes, and snippets.

@elxris
Created March 1, 2023 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elxris/ee65062352f40c3b92688a000aad7425 to your computer and use it in GitHub Desktop.
Save elxris/ee65062352f40c3b92688a000aad7425 to your computer and use it in GitHub Desktop.
How to use a Raspberry PI Zero 2 as WIFI USB for Anycubic Photon Ultra

March 1, 2023: This is a draft, I plan to extend this guide.

How to use a Raspberry PI Zero as WIFI USB for Anycubic Photon

I have only tested this in a Raspberry PI Zero 2 with an Anycubic Photon Ultra and works fine. This setup allows to connect remotely to your Raspberry PI to upload some files and then just go to your printer and hit print. This is not intented to act as a remote orchestrator for your printer. It is just a commodity because I hated to move the usb.

Install Raspberry PI OS

Using the Raspberry PI Imager install Raspberry Pi OS on a microSD card. I picked Raspberry PI OS Lite (64-bit) as we don't need the desktop environment. In the imager you can configure a wifi network that the RP will automatically connect on startup. I used a hostname ultra.local and username pi to later connect via ssh.

Access to your RP remotely

Plug the RP to a power supply, and wait for it to start. Then you can connect to the PI via SSH. In my case ssh pi@ultra.local

Copy startup_script.sh

After you have connected to your PI. Create a file named startup_script.sh. Then copy the contents of the file as below. You can use nano startup_script.sh to create and paste the contents of the file.

Create a binary storage

Depending on your microSD capacity, I recommend you to not exceed the 70% of it with the next commands.

sudo dd if=/dev/zero of=/home/pi/piusb_fat32.bin bs=1 count=0 seek=128M
sudo mkdosfs /home/pi/piusb_fat32.bin -n FAT32VOL11

This will create a binary file that will later act as the mounted storage you will use with your printer.

The seek=128M will create a 128 megabyte file. Thas is enough and fast. Be aware that bigger files will take a lot of time to create and format.

Mounting the storage

After creating and mounting the storage we will need to mount the storage. To do this we need to create a folder that will act as a mount place for our drive. sudo mkdir /home/pi/.thumbserve

Then we need to mount the binary file as a fat drive, but we want that to stay like that restart after restart, so lets save that to the /etc/fstab. sudo echo "/home/pi/piusb_fat32.bin /home/pi/.thumbserve/uploads vfat users,uid=pi,gid=pi,umask=000 0 2 " >> /etc/fstab

After that we need to put the following line to the /etc/rc.local file so everytime we power the pi, the USB can act as a USB Thumbdrive. sh /home/pi/startup_script.sh For reference you can look at the /etc/rc.local file I attached.

You're done

After last step you can use sudo reboot to reboot your RP and see the changes.

I connected my PI to a power supply and then the microUSB port to the printer.

I used an SFTP client to upload my files to the RP.

Then I go to my printer and just plug-in the cable and hit print. I CAN upload more files WHILE printing but be careful when doing that. I plug it off the printer when not in use because my cable provides some voltage to my printer and wakes it up. You need to wait the file is fully uploaded before starting a print. I havent encountered any big problems. I've had tons of sucessful prints with this setup. Ocacionally one faliure, but I think I was moving files around.

Thanks

Thanks to JayKay5532 at StackExchange post that guide me a lot on this journey. The startup_script.sh is provided as-is from this post. StackExchange - Change Raspberry Pi Zero USB Gadget name from Linux File-Stor Gadget

## This is an example file of /etc/rc.local, the important line is the 21.
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sh /home/pi/startup_script.sh
exit 0
#!/bin/bash
# libcomposite USB mass storage device gadget for raspberry pi zero
# Call this script on Pi Zero startup by adding its full path to /etc/rc.local
# Assumes you have created the backing file for the storage device with something like this:
# sudo dd if=/dev/zero of=/home/pi/piusb_fat32.bin bs=1 count=0 seek=128M
# sudo mkdosfs /home/pi/piusb_fat32.bin -n FAT32VOL11
# And done these two steps once:
# sudo echo "dtoverlay=dwc2" | sudo tee -a /boot/config.txt
# sudo echo "dwc2" | sudo tee -a /etc/modules
# Do some simple logging so we know the script is launching at startup
date | tee -a /home/pi/startup_script.log
echo Starting startup_script | tee -a /home/pi/startup_script.log
#Load the kernel module
modprobe libcomposite
#Gadget name (gadget1 is just an arbitrary name)
mkdir /sys/kernel/config/usb_gadget/gadget1
cd /sys/kernel/config/usb_gadget/gadget1
#hardware id
echo 0x0419 > bcdDevice
echo 0x0200 > bcdUSB
echo 0x0781 > idVendor # Sandisk
echo 0x556e > idProduct # Cruzer Edge
#device config
echo 0xEF > bDeviceClass
echo 0x02 > bDeviceSubClass
echo 0x01 > bDeviceProtocol
echo 0x08 > bMaxPacketSize0
#More stuff
mkdir strings/0x409
mkdir strings/0x407
cd strings/0x409
echo 'Sandisk' > manufacturer
echo 'Cruzer Edge' > product
echo 'Serial123456789' > serialnumber
cd ../../
cd strings/0x407
echo 'Sandisk' > manufacturer
echo 'Cruzer Edge' > product
echo 'Serial123456789' > serialnumber
cd ../../
mkdir functions/mass_storage.usb0
#Mass Storage 0 config settings
cd functions/mass_storage.usb0
echo 0 > stall
echo 1 > lun.0/removable
echo 0 > lun.0/ro
echo /home/pi/piusb_fat32.bin > lun.0/file
#Note, the next line is important. Host OS will tokenize it as: vendor(len 8) + model(len 16) + rev(len 4)
#Example here: vendor = ABCDEFGH model = 123456789ABCDEFG rev = WXYZ
echo 'SanDisk' > lun.0/inquiry_string
cd ../../
#OS Descriptor for Windows
cd os_desc
echo 1 > use
echo 0xcd > b_vendor_code
echo MSFT100 > qw_sign
cd ../
#Bus Hardware Config
mkdir configs/c.1
cd configs/c.1
echo 0x80 > bmAttributes
echo 100 > MaxPower
mkdir strings/0x409
echo "0" > strings/0x409/configuration
mkdir strings/0x407
echo "1" > strings/0x407/configuration
cd ../../
ln -s functions/mass_storage.usb0 configs/c.1
ln -s configs/c.1 os_desc
#Activate the gadget
ls /sys/class/udc > UDC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment