Skip to content

Commit

Permalink
gcode macro moonraker call for quiet times.
Browse files Browse the repository at this point in the history
  • Loading branch information
oernster committed Aug 1, 2024
1 parent 2597510 commit 28b03b6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions moonraker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Moonraker API callback to Klipper with a macro example

## I have written a short python script that will ensure that you only play a sound when your print ends if it's not within the small hours of the morning.

## This might be useful if you happen to have parents or kids who are sleeping while you are printing at night and usually play a sound on completion of a print.

## This also serves as a useful guide (the python script that is) to calling a macro in klipper from a python script.

### Please see 'call_gcode_macro.py' in this directory.
31 changes: 31 additions & 0 deletions moonraker/call_gcode_macro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import requests
import json
from datetime import datetime

# Configuration
macro_name = 'PRINT_END_SOUND'
moonraker_api_url = f'http://0.0.0.0:7125/printer/gcode/script?script={macro_name}'

# Get the current hour
current_hour = datetime.now().hour

# Check if the current time is not between 1 AM and 8 AM
if not (1 <= current_hour < 8):
# Send a request to Klipper to execute the macro

payload = {
"jsonrpc": "2.0",
"method": "printer.gcode.script",
"params": {
"script": f"{macro_name}"
},
"id": 7466}

try:
response = requests.post(moonraker_api_url, data=json.dumps(payload))
response.raise_for_status()
print(f'Successfully executed macro: {macro_name}')
except requests.exceptions.RequestException as e:
print(f'Failed to execute macro: {macro_name}. Error: {e}')
else:
print(f'Time is between 1 AM and 8 AM. Skipping macro execution.')

0 comments on commit 28b03b6

Please sign in to comment.