Moonraker is a powerful web API service commonly used with Klipper in 3D printing. One of its useful features is the ability to control GPIO pins directly for managing power devices. This guide explains how to set up raspberry GPIO pins in Moonraker and use them for power control, including handling button inputs.


Configure GPIO power switch in Moonraker

To use a GPIO pin for power control, you must define it as a power_device in the Moonraker configuration file (moonraker.conf). Here’s an example setup:

Example Configuration

[power printer]
type: gpio
#PC14
pin: !gpio78
initial_state: off
#off_when_shutdown: True
#off_when_shutdown_delay: 30
locked_while_printing: True
on_when_job_queued: True
bound_services: klipper

Key Options Explained

  • type: Defines the device type, in this case, GPIO.
  • pin: Specifies the GPIO pin number with modifiers such as ! for active-low.
  • initial_state: Determines the device’s state at startup (on or off).
  • locked_while_printing: Prevents the power state from being toggled while a print job is active.
  • on_when_job_queued: Automatically turns on the device when a print job is queued.
  • bound_services: Specifies the services this device depends on (e.g., klipper).

Adding a Button for GPIO Control

You can integrate a physical button to control the state of a GPIO pin. To do this, define a button in the moonraker.conf file.

Example Button Configuration for power switch

[button power_btn]
type: gpio
#PC6
pin: ^!gpio70 
on_press:
  {% do call_method("machine.device_power.post_device", device="printer", action="toggle") %}

Example with button for emergency stop

[button emergency_stop]
type: gpio
#PC7
pin: ^!gpio71
on_press:
{% do call_method("printer.emergency_stop") %}

Methods that can be invoked can be found in the Moonraker API documentation: Moonraker API Documentation

Key Options Explained

  • type: Defines the button type, in this case, GPIO.
  • pin: Specifies the GPIO pin the button is connected to with modifiers like ! for active-low and ^ for pull-up.
  • on_press: Executes a Jinja2-based action to toggle the state of the printer device.

Troubleshooting Tips

  1. Check GPIO Pin Numbering Ensure you’re using the correct GPIO numbering scheme (e.g., BCM for Raspberry Pi).

  2. Monitor GPIO State Use the following command to view GPIO pin states:

    cat /sys/kernel/debug/gpio
    
  3. Inspect Moonraker Logs Review logs to debug button or device issues:

    tail -f /var/log/moonraker.log