#Start Print (Cmd: 128) crashes printer
1 messages · Page 1 of 1 (latest)
import asyncio
import json
import time
import uuid
import websockets
PRINTER_IP = "myip"
FILE_NAME = "Cube.gcode"
MAINBOARDID = "myid"
async def start_print(ip: str, filename: str) -> None:
req = {
"Id": str(uuid.uuid4()),
"Data": {
"Cmd": 128,
"Data": {"Filename": filename, "StartLayer": 0},
"RequestID": str(uuid.uuid4()),
"MainboardID": MAINBOARDID,
"TimeStamp": int(time.time()),
"From": 0,
},
"Topic": f"sdcp/request/{MAINBOARDID}",
}
async with websockets.connect(f"ws://{ip}:3030/websocket") as ws:
await ws.send(json.dumps(req))
print(await asyncio.wait_for(ws.recv(), timeout=3))
if __name__ == "__main__":
asyncio.run(start_print(PRINTER_IP, FILE_NAME))
This is the code that I'm using to run this from the documentation.
https://docs.opencentauri.cc/software/api/
I did some more digging and tested whether I could get all the files on my printer, and it worked.
import asyncio
import json
import time
import uuid
import websockets
PRINTER_IP = "myip"
FILE_NAME = "Cube.gcode"
MAINBOARDID = "myid"
async def get_file(ip: int) -> dict:
req = {
"Id": str(uuid.uuid4()),
"Data": {
"Cmd": 258,
"Data": {"Url": "/local/"},
"RequestID": str(uuid.uuid4()),
"MainboardID": MAINBOARDID,
"TimeStamp": int(time.time()),
"From": 0,
},
"Topic": f"sdcp/request/{MAINBOARDID}",
}
async with websockets.connect(f"ws://{ip}:3030/websocket") as ws:
await ws.send(json.dumps(req))
resp_raw = await asyncio.wait_for(ws.recv(), timeout=5)
resp = json.loads(resp_raw)
print(resp)
if __name__ == "__main__":
asyncio.run(get_file(PRINTER_IP))
Which gave me something like this, which confirms that the G-code is on the printer, and I can pull it using their API
{'Id': 'myid', 'Data': {'Cmd': 258, 'Data': {'Ack': 0, 'FileList': [ {'name': '/local//Cube.gcode', 'type': 1, 'CreateTime': 71, 'FileSize': 152651, 'LayerHeight': 0, 'TotalLayers': 81, 'EstFilamentLength': 0},]}, 'RequestID': 'id', 'MainboardID': 'mainboardid', 'TimeStamp': 1772417589}, 'Topic': 'sdcp/response/mainboardid'}
Sorry for the bad code, and let me know what I can change to be able to run a print using their API
any idea to be able to fix this?
Sorry, did not see this.
Can you check how the webui sends this via the network tab? @copper raven
I was able to fix the issue, I was missing the extra data information.
This is what I got from the webui
{"Id":"","Data":{"Cmd":128,"Data":{"Filename":"/local/Cube.gcode","StartLayer":0,"Calibration_switch":0,"PrintPlatformType":1,"Tlp_Switch":1},"RequestID":"","MainboardID":"","TimeStamp":1773968213890,"From":1}}
This was my working version
import asyncio, json, time, uuid, websockets
PRINTER_IP = ""
MAINBOARDID = ""
FILE_NAME = "/local/Cube.gcode"
#{"Id":"","Data":{"Cmd":128,"Data":{"Filename":"/local/Cube.gcode","StartLayer":0,"Calibration_switch":1,"PrintPlatformType":1,"Tlp_Switch":0},"RequestID":"","MainboardID":"","TimeStamp":1773972595979,"From":1}}
async def start_print(ip, filename):
payload = {
"Id": str(uuid.uuid4()),
"Data": {
"Cmd": 128,
"Data": {
"Filename": filename,
"StartLayer": 0,
"Calibration_switch": 0,
"PrintPlatformType": 1,
"Tlp_Switch": 0
},
"RequestID": str(uuid.uuid4()),
"MainboardID": MAINBOARDID,
"TimeStamp": int(time.time()),
"From": 1,
},
"Topic": f"sdcp/request/{MAINBOARDID}"
}
async with websockets.connect(f"ws://{ip}:3030/websocket") as ws:
await ws.send(json.dumps(payload))
print(await asyncio.wait_for(ws.recv(), timeout=3))
if __name__ == "__main__":
asyncio.run(start_print(PRINTER_IP, FILE_NAME))
🙏