-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gcode macro moonraker call for quiet times.
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') |