This is the description of the MATLAB/Octave API bindings for the IP Connection. The IP Connection manages the communication between the API bindings and the Brick Daemon or a WIFI/Ethernet Extension. Before Bricks and Bricklets can be controlled using their API an IP Connection has to be created and its TCP/IP connection has to be established.
An installation guide for the MATLAB/Octave API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
Download (matlab_example_enumerate.m)
1function matlab_example_enumerate()
2 import com.tinkerforge.IPConnection;
3
4 HOST = 'localhost';
5 PORT = 4223;
6
7 ipcon = handle(IPConnection(), 'CallbackProperties'); % Create IP connection
8
9 ipcon.connect(HOST, PORT); % Connect to brickd
10
11 % Register Enumerate Callback
12 set(ipcon, 'EnumerateCallback', @(h, e) cb_enumerate(e));
13
14 % Trigger Enumerate
15 ipcon.enumerate();
16
17 input('Press any key to exit...\n', 's');
18 ipcon.disconnect();
19end
20
21% Print incoming enumeration
22function cb_enumerate(e)
23 ipcon = e.getSource();
24
25 fprintf('UID: %s\n', char(e.uid));
26 fprintf('Enumeration Type: %d\n', e.enumerationType);
27
28 if e.enumerationType == ipcon.ENUMERATION_TYPE_DISCONNECTED
29 fprintf('\n');
30 return;
31 end
32
33 fprintf('Connected UID: %s\n', char(e.connectedUid));
34 fprintf('Position: %s\n', e.position);
35 fprintf('Hardware Version: ');
36 fprintf('%d', rot90(e.hardwareVersion));
37 fprintf('\n');
38 fprintf('Firmware Version: ');
39 fprintf('%d', rot90(e.firmwareVersion));
40 fprintf('\n');
41 fprintf('Device Identifier: %d\n', e.deviceIdentifier);
42 fprintf('\n');
43end
Download (matlab_example_authenticate.m)
1function matlab_example_authenticate()
2 import com.tinkerforge.IPConnection;
3
4 global SECRET;
5
6 HOST = 'localhost';
7 PORT = 4223;
8 SECRET = 'My Authentication Secret!';
9
10 ipcon = handle(IPConnection(), 'CallbackProperties'); % Create IP connection
11
12 % Disable auto reconnect mechanism, in case we have the wrong secret.
13 % If the authentication is successful, reenable it.
14 ipcon.setAutoReconnect(false)
15
16 % Register Connected Callback
17 set(ipcon, 'ConnectedCallback', @(h, e) cb_connected(e));
18
19 % Register Enumerate Callback
20 set(ipcon, 'EnumerateCallback', @(h, e) cb_enumerate(e));
21
22 ipcon.connect(HOST, PORT); % Connect to brickd
23
24 input('Press any key to exit...\n', 's');
25 ipcon.disconnect();
26end
27
28% Authenticate each time the connection got (re-)established
29function cb_connected(e)
30 ipcon = e.getSource();
31
32 global SECRET;
33
34 if e.connectReason == ipcon.CONNECT_REASON_REQUEST
35 fprintf('Connected by request\n');
36 elseif e.connectReason == ipcon.CONNECT_REASON_AUTO_RECONNECT
37 fprintf('Auto-Reconnect\n');
38 end
39
40 % Authenticate first...
41 try
42 ipcon.authenticate(SECRET);
43 fprintf('Authentication succeeded\n');
44 catch ex
45 fprintf('Could not authenticate\n');
46 return
47 end
48
49 % ...reenable auto reconnect mechanism, as described above...
50 ipcon.setAutoReconnect(true)
51
52 % ...then trigger enumerate
53 ipcon.enumerate();
54end
55
56% Print incoming enumeration
57function cb_enumerate(e)
58 fprintf('UID: %s, Enumeration Type: %g\n', char(e.uid), e.enumerationType);
59end
Download (octave_example_enumerate.m)
1function octave_example_enumerate()
2 more off;
3
4 HOST = "localhost";
5 PORT = 4223;
6
7 ipcon = javaObject("com.tinkerforge.IPConnection"); % Create IP connection
8
9 ipcon.connect(HOST, PORT); % Connect to brickd
10
11 % Register Enumerate Callback
12 ipcon.addEnumerateCallback(@cb_enumerate);
13
14 % Trigger Enumerate
15 ipcon.enumerate();
16
17 input("Press any key to exit...\n", "s");
18 ipcon.disconnect();
19end
20
21% Print incoming enumeration
22function cb_enumerate(e)
23 ipcon = e.getSource();
24
25 fprintf("UID: %s\n", e.uid);
26 fprintf("Enumeration Type: %d\n", java2int(e.enumerationType));
27
28 if java2int(e.enumerationType) == java2int(ipcon.ENUMERATION_TYPE_DISCONNECTED)
29 fprintf("\n");
30 return;
31 end
32
33 hardwareVersion = e.hardwareVersion;
34 firmwareVersion = e.firmwareVersion;
35
36 fprintf("Connected UID: %s\n", e.connectedUid);
37 fprintf("Position: %s\n", e.position);
38 fprintf("Hardware Version: %d.%d.%d\n", java2int(hardwareVersion(1)), ...
39 java2int(hardwareVersion(2)), ...
40 java2int(hardwareVersion(3)));
41 fprintf("Firmware Version: %d.%d.%d\n", java2int(firmwareVersion(1)), ...
42 java2int(firmwareVersion(2)), ...
43 java2int(firmwareVersion(3)));
44 fprintf("Device Identifier: %d\n", e.deviceIdentifier);
45 fprintf("\n");
46end
47
48function int = java2int(value)
49 if compare_versions(version(), "3.8", "<=")
50 int = value.intValue();
51 else
52 int = value;
53 end
54end
Download (octave_example_authenticate.m)
1function octave_example_authenticate()
2 more off;
3
4 global SECRET;
5
6 HOST = "localhost";
7 PORT = 4223;
8 SECRET = "My Authentication Secret!";
9
10 ipcon = javaObject("com.tinkerforge.IPConnection"); % Create IP connection
11
12 % Disable auto reconnect mechanism, in case we have the wrong secret.
13 % If the authentication is successful, reenable it.
14 ipcon.setAutoReconnect(false)
15
16 % Register Connected Callback
17 ipcon.addConnectedCallback(@cb_connected);
18
19 % Register Enumerate Callback
20 ipcon.addEnumerateCallback(@cb_enumerate);
21
22 ipcon.connect(HOST, PORT); % Connect to brickd
23
24 input("Press any key to exit...\n", "s");
25 ipcon.disconnect();
26end
27
28% Authenticate each time the connection got (re-)established
29function cb_connected(e)
30 ipcon = e.getSource();
31
32 global SECRET;
33
34 if java2int(e.connectReason) == java2int(ipcon.CONNECT_REASON_REQUEST)
35 fprintf("Connected by request\n");
36 elseif java2int(e.connectReason) == java2int(ipcon.CONNECT_REASON_AUTO_RECONNECT)
37 fprintf("Auto-Reconnect\n");
38 end
39
40 % Authenticate first...
41 try
42 ipcon.authenticate(SECRET);
43 fprintf("Authentication succeeded\n");
44 catch ex
45 fprintf("Could not authenticate\n");
46 return
47 end
48
49 % ...reenable auto reconnect mechanism, as described above...
50 ipcon.setAutoReconnect(true)
51
52 % ...then trigger enumerate
53 ipcon.enumerate();
54end
55
56% Print incoming enumeration
57function cb_enumerate(e)
58 fprintf("UID: %s, Enumeration Type: %d\n", e.uid, e.enumerationType);
59end
60
61function int = java2int(value)
62 if compare_versions(version(), "3.8", "<=")
63 int = value.intValue();
64 else
65 int = value;
66 end
67end
Creates an IP Connection object that can be used to enumerate the available devices. It is also required for the constructor of Bricks and Bricklets.
In MATLAB:
import com.tinkerforge.IPConnection;
ipcon = IPConnection();
In Octave:
ipcon = java_new("com.tinkerforge.IPConnection");
Creates a TCP/IP connection to the given host and port. The host and port
can refer to a Brick Daemon or to a WIFI/Ethernet Extension.
Devices can only be controlled when the connection was established successfully.
Blocks until the connection is established and throws an exception if there is no Brick Daemon or WIFI/Ethernet Extension listening at the given host and port.
Disconnects the TCP/IP connection from the Brick Daemon or the WIFI/Ethernet Extension.
Performs an authentication handshake with the connected Brick Daemon or WIFI/Ethernet Extension. If the handshake succeeds the connection switches from non-authenticated to authenticated state and communication can continue as normal. If the handshake fails then the connection gets closed. Authentication can fail if the wrong secret was used or if authentication is not enabled at all on the Brick Daemon or the WIFI/Ethernet Extension.
See the authentication tutorial for more information.
Can return the following states:
IPConnection.CONNECTION_STATE_DISCONNECTED = 0: No connection is established.
IPConnection.CONNECTION_STATE_CONNECTED = 1: A connection to the Brick Daemon or the WIFI/Ethernet Extension is established.
IPConnection.CONNECTION_STATE_PENDING = 2: IP Connection is currently trying to connect.
Enables or disables auto-reconnect. If auto-reconnect is enabled,
the IP Connection will try to reconnect to the previously given
host and port, if the currently existing connection is lost.
Therefore, auto-reconnect only does something after a successful
connect() call.
Default value is true.
Returns true if auto-reconnect is enabled, false otherwise.
Sets the timeout in milliseconds for getters and for setters for which the response expected flag is activated.
Default timeout is 2500.
Returns the timeout as set by setTimeout().
Broadcasts an enumerate request. All devices will respond with an enumerate callback.
Callbacks can be registered to be notified about events. The registration is done with "set" function of MATLAB. The parameters consist of the IP Connection object, the callback name and the callback function. For example, it looks like this in MATLAB:
function my_callback(e)
fprintf('Parameter: %s\n', e.param);
end
set(ipcon, 'ExampleCallback', @(h, e) my_callback(e));
Due to a difference in the Octave Java support the "set" function cannot be used in Octave. The registration is done with "add*Callback" functions of the IP Connection object. It looks like this in Octave:
function my_callback(e)
fprintf("Parameter: %s\n", e.param);
end
ipcon.addExampleCallback(@my_callback);
It is possible to add several callback functions and to remove them with the corresponding "remove*Callback" function.
The parameters of the callback are passed to the callback function as fields of
the structure e, which is derived from the java.util.EventObject class.
The available callback names with corresponding structure fields are described
below.
uid -- String
connectedUid -- String
position -- char
hardwareVersion -- short[]
firmwareVersion -- short[]
deviceIdentifier -- int
enumerationType -- short
The callback function receives seven parameters as fields of the
structure e:
uid: The UID of the device.
connectedUID: UID where the device is connected to. For a Bricklet this
is the UID of the Brick or Bricklet it is connected to. For a Brick it is
the UID of the bottommost Brick in the stack. For the bottommost Brick
in a stack it is "0". With this information it is
possible to reconstruct the complete network topology.
position: For Bricks: '0' - '8' (position in stack). For Bricklets:
'a' - 'h' (position on Brick) or 'i' (position of the Raspberry Pi (Zero) HAT)
or 'z' (Bricklet on Isolator Bricklet).
hardwareVersion: Major, minor and release number for hardware version.
firmwareVersion: Major, minor and release number for firmware version.
deviceIdentifier: A number that represents the device.
enumerationType: Type of enumeration.
Possible enumeration types are:
IPConnection.ENUMERATION_TYPE_AVAILABLE = 0: Device is available
(enumeration triggered by user: enumerate()). This enumeration type can occur multiple times
for the same device.
IPConnection.ENUMERATION_TYPE_CONNECTED = 1: Device is newly connected (automatically send by Brick after establishing a communication connection). This indicates that the device has potentially lost its previous configuration and needs to be reconfigured.
IPConnection.ENUMERATION_TYPE_DISCONNECTED = 2: Device is disconnected (only
possible for USB connection). In this case only uid and
enumerationType are valid.
It should be possible to implement plug-and-play functionality with this (as is done in Brick Viewer).
The device identifier numbers can be found here. There are also constants for these numbers named following this pattern:
<device-class>.DEVICE_IDENTIFIER
For example: BrickMaster.DEVICE_IDENTIFIER
or BrickletAmbientLight.DEVICE_IDENTIFIER.
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addEnumerateCallback() function. An added callback function can be removed with
the removeEnumerateCallback() function.
connectReason -- short
This callback is called whenever the IP Connection got connected to a Brick Daemon or to a WIFI/Ethernet Extension, possible reasons are:
IPConnection.CONNECT_REASON_REQUEST = 0: Connection established after request from user.
IPConnection.CONNECT_REASON_AUTO_RECONNECT = 1: Connection after auto-reconnect.
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addConnectedCallback() function. An added callback function can be removed with
the removeConnectedCallback() function.
disconnectReason -- short
This callback is called whenever the IP Connection got disconnected from a Brick Daemon or from a WIFI/Ethernet Extension, possible reasons are:
IPConnection.DISCONNECT_REASON_REQUEST = 0: Disconnect was requested by user.
IPConnection.DISCONNECT_REASON_ERROR = 1: Disconnect because of an unresolvable error.
IPConnection.DISCONNECT_REASON_SHUTDOWN = 2: Disconnect initiated by Brick Daemon or WIFI/Ethernet Extension.
In MATLAB the set() function can be used to register a callback function
to this callback.
In Octave a callback function can be added to this callback using the
addDisconnectedCallback() function. An added callback function can be removed with
the removeDisconnectedCallback() function.