Connect Matlab to Octoprint API

조회 수: 11 (최근 30일)
Álvaro González Menéndez
Álvaro González Menéndez 2022년 6월 17일
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
Geoff Hayes
Geoff Hayes 2022년 6월 17일
편집: Geoff Hayes 2022년 6월 17일
@Á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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Álvaro González Menéndez
Álvaro González Menéndez 2022년 6월 20일
Hi Geoff, Thank you for removing the api_key, anyway I had changed some digits to avoid problems. Regarding the address, it is an address inside my home network, I am not trying to reach form outside of my home.
I have improve the code in the meantime, and I now I can reach the server but I get the following response into resp.StatusLineStatusCode: 'InternalServerError', and the following message in resp.Body.Data: 'The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application'. Obviously I have no active connection while I am trying this test and if I try to connect for example using the Octoprint webpage I can connect without errors using the same values for the connection parameters.
import matlab.net.*
import matlab.net.http.*
api_url = matlab.net.URI('http://host_addr/api/connection');
api_key = 'api_key';
method = matlab.net.http.RequestMethod.POST;
header = matlab.net.http.HeaderField('Content-Type', 'application/json', ...
'X-Api-Key', api_key);
commands= struct('command', 'connect',...
'port', '/dev/ttyUSB0', ...
'baudrate', 115200, ...
'printerProfile', '_default');
body = matlab.net.http.MessageBody(jsonencode(commands));
request = matlab.net.http.RequestMessage(method, header, body);
% Send request to login api
[resp, complreq, history] = request.send(api_url);
% La respuesta
resp.StatusLine
When I use this code to test that I reach Octoprint, it works. I receive an StatusCode: OK, and the different values available for the connection parameters.
import matlab.net.*
import matlab.net.http.*
api_url = matlab.net.URI('http://host_addr/api/connection');
api_key = 'api_key';
method = matlab.net.http.RequestMethod.GET;
header = matlab.net.http.HeaderField('X-Api-Key', api_key);
request = matlab.net.http.RequestMessage(method, header);
% Send request to login api
[resp, complreq, history] = request.send(api_url);
% La respuesta
resp.StatusLine
  댓글 수: 3
Álvaro González Menéndez
Álvaro González Menéndez 2022년 6월 20일
In order to avoid a potential problem of connection I have succesfully connect to Octoprint using this code in Python 3.8. This code is very similar to the one implementaed in Matlab.
import requests
api_url = "http://host_addr/api/connection"
api_key = 'apy_key'
command = 'connect'
baudrate = 115200
port = '/dev/ttyUSB0'
headers = ({'X-Api-Key': api_key, 'Content-Type': 'application/json'})
data = {'command': 'connect'}
data['port'] = port
data['baudrate'] = baudrate
r = requests.post(api_url, json=data, headers=headers)
Álvaro González Menéndez
Álvaro González Menéndez 2022년 6월 20일
Solved, finally I have connect to Octoprint using Matlab code. It seems that the key point is to use the following sentence
body = matlab.net.http.MessageBody(commands);
Instead of
body = matlab.net.http.MessageBody(jsonencode(commands));
So, the final code that it works is:
import matlab.net.*
import matlab.net.http.*
api_url = matlab.net.URI('http://host_addr/api/connection');
api_key = 'apy_key'
method = matlab.net.http.RequestMethod.POST;
header = matlab.net.http.HeaderField('Content-Type', 'application/json', ...
'X-Api-Key', api_key);
commands= struct('command', 'connect',...
'baudrate', 115200, ...
'port', '/dev/ttyUSB0');
body = matlab.net.http.MessageBody(commands);
request = matlab.net.http.RequestMessage(method, header, body);
[resp, complreq, history] = request.send(api_url);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by