Connect Matlab to Octoprint API

11 vues (au cours des 30 derniers jours)
Álvaro González Menéndez
Álvaro González Menéndez le 17 Juin 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 commentaire
Geoff Hayes
Geoff Hayes le 17 Juin 2022
Modifié(e) : Geoff Hayes le 17 Juin 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.

Connectez-vous pour commenter.

Réponses (1)

Álvaro González Menéndez
Álvaro González Menéndez le 20 Juin 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 commentaires
Álvaro González Menéndez
Álvaro González Menéndez le 20 Juin 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 le 20 Juin 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);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call Web Services from MATLAB Using HTTP dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by