Simple PowerShell script for leveling bed using Astroprint API

I put together a simple PowerShell script the other day to help with leveling my printbed. (I don’t tend to see a lot of Windows based scripts so i wanted to share.)

I thought i would share this here too.

You can either save the following text with a .ps1 extension or simply paste it into a PowerShell command prompt.

(In either case you will need to set your Astroprint URL and API key)

I am moving between the 4 corners (about 50mm in) but you can configure as many test points as you would like

I am NOT turning on the hot end or the bed (this is an important part of leveling, but I don’t want to risk issues with anyone else’s printer)

My process is:

  • Turn on the printer Manually heat the bed and hot end
  • Place a piece of paper on the bed
  • Run this script
  • Adjust the bed height until the paper is barely touching the print head
  • Press enter
  • Repeat until all 4 corners are adjusted perfectly

Hopefully someone else will find this helpful.

$PrintServerURL = “http://10.0.0.45#Your Astroprint URL

$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers.Add(“X-Api-Key”, ‘’) #Your Astroprint API key from Settings (Gear Icon) > Advanced

$Position = @(
“G1 X50 Y50 F6400”, #Front Left
“G1 X150 Y 150 F6400”, #Back Right
“G1 X50 Y150 F6400”, #Front Right
“G1 X150 Y50 F6400” #Back Left
)
function ExecuteGcode ([string]$GCODE)
{
$Body= New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$Body.Add(“command”, $GCODE)
Invoke-RestMethod -uri “$PrintServerURL/api/printer/comm/send” -Headers $headers -Method POST -Body $body
}

ExecuteGcode -GCODE “G28 XY” #start by homing the XY axis

$LocationCounter = 0

while ((Read-Host “Press enter to move to next position or X to Exit”) -ne “X”)
{
ExecuteGcode -GCODE “G1 Z1” # raise Z slightly before move
ExecuteGcode -GCODE $Position[$LocationCounter]
ExecuteGcode -GCODE “G1 Z0” # Return Z to home
$LocationCounter++
if ($LocationCounter -eq $Position.Count) {$LocationCounter = 0}
}

2 Likes