Connect Matlab to Octoprint API
11 views (last 30 days)
Show older comments
I am trying to connect with Octoprint using Matlab code.
From the Octoprint REST API web page I have found this information to connect:
POST /api/connection HTTP/1.1
Host: example.com
Content-Type: application/json
X-Api-Key: abcdef...
{
"command": "connect",
"port": "/dev/ttyACM0",
"baudrate": 115200,
"printerProfile": "my_printer_profile",
"save": true,
"autoconnect": true
}
Based on previous information I am using this code to connect. Unfortunatelly it does not work. Also I do not find the way to specify the request line in order to be "POST /api/connection HTTP/1.1" instead of the usual "POST".
import matlab.net.*
import matlab.net.http.*
import matlab.net.http.field.*
api_key = "some key";
api_url = "some url";
header1 = ContentTypeField('application/json');
header2 = matlab.net.http.HeaderField("X-Api-Key", api_key);
commands= struct('command', 'connect', 'port', 'AUTO', ...
'baudrate', 115200, 'printerProfile', 'Default', ...
'save', false, 'autoconnect', false);
body = matlab.net.http.MessageBody(jsonencode(commands));
request = RequestMessage('POST', [header1, header2], body);
[resp, c, h] = request.send(api_url);
1 Comment
Geoff Hayes
on 17 Jun 2022
Edited: Geoff Hayes
on 17 Jun 2022
@Álvaro González Menéndez - when you run the above code, what error are you observing? Presumably it is an HTTP error code of some kind. I've edited out the api key and url as I don't think that is something that you want to be posting publicly (unless already junk data?). Also, I'm not sure if the URL was complete as it just seemed to be a host.
Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!