3d printer api doesn't work

openhab2
Tags: #<Tag:0x00007f1e5603e780>
(JustAProgrammer) #1

So I have following code to send gcode to my ender 3:

rule "POST System Kommando"
when
	Item OctoprintPrinterCommand received update
then
var String PrinterCmd = OctoprintPrinterCommand.state.toString()
    var url = "http://octopi/api/job?apikey=`XXXXXXXXXXXXXXX"
    var contenttype = "application/json"
    var POSTrequest = '{"command":"'+PrinterCmd+'"}'
    logInfo(filename, "Ender3 kompiliert GCode: " + PrinterCmd )

    var output = sendHttpPostRequest(url, contenttype, POSTrequest)
    
    logInfo(filename, output);
end

the json api looks like this:


But the logs are showing the following when I do a HTTP post with “G28” (Auto home) to my printer:

2018-12-08 19:48:11.203 [INFO ] [arthome.model.script.Octoprint.rules] - Ender3 kompiliert GCode: G28

2018-12-08 19:48:11.262 [INFO ] [arthome.model.script.Octoprint.rules] - Expected valid command

Is my syntax incorrect ? Does the command maybe need to have those
inverted commas (")? if yes - how do I type them, without comment out my Code?

0 Likes

(Angelos) #2

I think? that you need to add double quotes to your POSTrequest (and maybe escape the double quotes within the content also)

try this:

var POSTrequest = "{\"command\":\"+PrinterCmd+\"}"

see example here: JSONPATH updating Item state

edit: you many need to play with single and double quotes to get the PrinterCmd working… my proposal may not pull correctly the PrinterCmd var… I have to re-think the syntax

1 Like

(JustAProgrammer) #3

thank you!
give me a second :slight_smile:

1 Like

(JustAProgrammer) #4

new Code:

    var POSTestRequest = "{\"command\":\"+PrinterCmd+\"}"
    logInfo(filename, "Ender3 kompiliert GCode: " + PrinterCmd )
    logInfo(filename, POSTestRequest)
    //Antwort der API
    var output = sendHttpPostRequest(url, contenttype, POSTestRequest)
    logInfo(filename, output);

Output:

2018-12-08 20:32:35.921 [INFO ] [arthome.model.script.Octoprint.rules] - Ender3 kompiliert GCode: G28
2018-12-08 20:32:35.936 [INFO ] [arthome.model.script.Octoprint.rules] - {"command":"+PrinterCmd+"}
2018-12-08 20:32:36.004 [INFO ] [arthome.model.script.Octoprint.rules] - Expected valid command

still the same behaviour…
If it helps, the old Code gave out the following

var POSTrequest = '{"command":"'+PrinterCmd+'"}':
2018-12-08 20:31:39.052 [INFO ] [arthome.model.script.Octoprint.rules] - {"command":"G28"}
0 Likes

(Angelos) #5

I don’t understand why your original rule is not working…
I checked 3D Printer Status and Commands from/to Octoprint via HTTP REST API also and I see that your syntax is correct. It seems that you don’t need the double quotes…

I am going to try to reproduce this on my system for deeper validation and let you know what I find.

1 Like

(Vincent Regaud) #6

@Nico111

Could you please ensure that you select a category for your posts?
Thanks

0 Likes

(JustAProgrammer) #7

I would be really grateful, if you take the time to do this :slight_smile:
Thank you alot!

Yes, sorry!

0 Likes

(namraccr) #8
var POSTestRequest = "{\"command\":\"+PrinterCmd+\"}"

I think that should be

var POSTestRequest = "{\"command\":\"" + PrinterCmd + "\"}"
2 Likes

(JustAProgrammer) #9

still getting:

2018-12-09 10:45:12.067 [INFO ] [arthome.model.script.Octoprint.rules] - {"command":"G28"}

2018-12-09 10:45:12.146 [INFO ] [arthome.model.script.Octoprint.rules] - Expected valid command

this is weird, the syntax should be correct …

0 Likes

(JustAProgrammer) #11

Hey,
are you referencing a Epson printer?
I was talking about my ender 3, and everything worked in the end

0 Likes

(Rossko57) #12

I think it just wants you to click its link

0 Likes