Skip to content

Instantly share code, notes, and snippets.

@Blizzke
Last active January 31, 2021 14:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blizzke/9e13c4a7cbb91ec1976a0c50162fa323 to your computer and use it in GitHub Desktop.
Save Blizzke/9e13c4a7cbb91ec1976a0c50162fa323 to your computer and use it in GitHub Desktop.
OctoPrint CLI flasher for Prusa MK2, MK3 and MMU2
#!/usr/bin/env bash
#
# This script was created to flash firmwares for the MK2, MK3 and the MMU2,
# all from the command line of your OctoPrint installation.
#
# The script assumes that you have avrdude and jq installed on your raspbian and that you
# have filled the API key below.
#
# Usage: flash.sh mk2|mk3|mmu2 firmware.hex
#
# If you are flashing a printer, it will use the API to disconnect it first if necessary
# (and reconnect it afterwards)
#
# The MMU2 can only be flashed while in bootloader mode, which only 5 lasts seconds.
# That's why this script is started first, it then waits until you go press the reset button
# and then starts flashing as soon as the MMU2 reconnects.
#
# I have tested this on all my devices (MK2.5S, MK3 and MMU2) and it works, but:
# THIS SCRIPT COMES WITH NO WARRANTY WHATSOEVER, USE AT YOUR OWN RISK!
#
API_KEY=<YOUR_API_KEY_HERE>
function device {
searching=true
while [ $searching == true ]; do
# https://unix.stackexchange.com/a/144735
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
# Get rid of "dev"
syspath="${sysdevpath%/dev}"
# Translate from sys to dev
devname="$(udevadm info -q name -p $syspath)"
# Only keep devices, we don't want controllers/hubs
[[ "$devname" == "bus/"* ]] && continue
# Export the device properties as environment settings
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
if [[ $ID_SERIAL == *${1}* ]]; then
echo /dev/$devname
searching=false
fi
done
if [ $searching == true ]; then
sleep 1
fi
done
}
function api {
# http://docs.octoprint.org/en/master/api/connection.html
if [ "$2" == POST ]; then
curl -s --header "X-Api-Key: $API_KEY" --header "Content-Type: application/json" --data "$3" http://127.0.0.1:5000/api/$1
else
curl -s --header "X-Api-Key: $API_KEY" http://127.0.0.1:5000/api/$1
fi
}
if [ "$1" == MMU ] || [ "$1" == mmu ] || [ "$1" == MMU2 ] || [ "$1" == mmu2 ]; then
echo Flashing MMU2
echo Beware: If you have modem manager installed, flashing will fail
echo "(see https://github.com/prusa3d/MM-control-01#linux-1)"
DEV=$(device Multi_Material_2.0)
# Count USB DEVICES
ORIGINAL_USB_DEVICES=$(lsusb | wc -l)
USB_DEVICES=$ORIGINAL_USB_DEVICES
echo Device found at $DEV, please hit the reset button now...
while [ $USB_DEVICES -eq $ORIGINAL_USB_DEVICES ]; do USB_DEVICES=$(lsusb | wc -l) ; done
while [ $USB_DEVICES -ne $ORIGINAL_USB_DEVICES ]; do USB_DEVICES=$(lsusb | wc -l) ; done
DEV=$(device Multi_Material_2.0)
echo Reset detected, device now at $DEV
echo Starting flash process...
avrdude -v -p atmega32u4 -c avr109 -P ${DEV} -b 57600 -D -U flash:w:${2}:i
elif [ $1 == MK3 ] || [ $1 == mk3 ] || [ $1 == MK2 ] || [ $1 == mk2 ]; then
if [ $1 == MK3 ] || [ $1 == mk3 ]; then
NAME=MK3
DEV=$(device MK3)
elif [ $1 == MK2 ] || [ $1 == mk2 ]; then
NAME=MK2
DEV=$(device MK2)
fi
echo Flashing $NAME, found at $DEV
# Get the printer connection status
CONNECTION=$(api connection)
STATUS=$(echo $CONNECTION | jq -r .current.state)
if [ "$STATUS" != Closed ]; then
# Make sure the printer is disconnected before we do anything
echo Printer is connected, disconnecting
api connection POST '{"command": "disconnect"}'
else
echo "Printer not connected, disconnect not needed (will not auto-reconnect after flash)"
fi
echo Starting flash process via $DEV...
avrdude -v -p atmega2560 -c wiring -P ${DEV} -D -U flash:w:${2}:i
if [ $? -eq 0 ] && [ "$STATUS" == Operational ]; then
echo Flash successful, reconnecting $NAME
BAUDRATE=$(echo $CONNECTION | jq -r .current.baudrate)
PROFILE=$(echo $CONNECTION | jq -r .current.printerProfile)
COMMAND="{\"command\":\"connect\",\"port\":\"$DEV\",\"baudrate\":$BAUDRATE,\"printerProfile\":\"$PROFILE\",\"save\":true,\"autoconnect\":true}"
api connection POST "$COMMAND"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment