Connect Matlab to Octoprint API

11 visualizaciones (últimos 30 días)
Álvaro González Menéndez
Álvaro González Menéndez el 17 de Jun. de 2022
Comentada: Álvaro González Menéndez el 20 de Jun. de 2022
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 comentario
Geoff Hayes
Geoff Hayes el 17 de Jun. de 2022
Editada: Geoff Hayes el 17 de Jun. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Álvaro González Menéndez
Álvaro González Menéndez el 20 de Jun. de 2022
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 comentarios
Álvaro González Menéndez
Álvaro González Menéndez el 20 de Jun. de 2022
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 el 20 de Jun. de 2022
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);

Iniciar sesión para comentar.

Categorías

Más información sobre Call Web Services from MATLAB Using HTTP en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by