Connect Matlab to Octoprint API
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
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.
Réponses (1)
Voir également
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!