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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | function matlab_example_enumerate()
import com.tinkerforge.IPConnection;
HOST = 'localhost';
PORT = 4223;
ipcon = handle(IPConnection(), 'CallbackProperties'); % Create IP connection
ipcon.connect(HOST, PORT); % Connect to brickd
% Register Enumerate Callback
set(ipcon, 'EnumerateCallback', @(h, e) cb_enumerate(e));
% Trigger Enumerate
ipcon.enumerate();
input('Press any key to exit...\n', 's');
ipcon.disconnect();
end
% Print incoming enumeration
function cb_enumerate(e)
ipcon = e.getSource();
fprintf('UID: %s\n', char(e.uid));
fprintf('Enumeration Type: %d\n', e.enumerationType);
if e.enumerationType == ipcon.ENUMERATION_TYPE_DISCONNECTED
fprintf('\n');
return;
end
fprintf('Connected UID: %s\n', char(e.connectedUid));
fprintf('Position: %s\n', e.position);
fprintf('Hardware Version: ');
fprintf('%d', rot90(e.hardwareVersion));
fprintf('\n');
fprintf('Firmware Version: ');
fprintf('%d', rot90(e.firmwareVersion));
fprintf('\n');
fprintf('Device Identifier: %d\n', e.deviceIdentifier);
fprintf('\n');
end
|
Download (matlab_example_authenticate.m)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | function matlab_example_authenticate()
import com.tinkerforge.IPConnection;
global SECRET;
HOST = 'localhost';
PORT = 4223;
SECRET = 'My Authentication Secret!';
ipcon = handle(IPConnection(), 'CallbackProperties'); % Create IP connection
% Disable auto reconnect mechanism, in case we have the wrong secret.
% If the authentication is successful, reenable it.
ipcon.setAutoReconnect(false)
% Register Connected Callback
set(ipcon, 'ConnectedCallback', @(h, e) cb_connected(e));
% Register Enumerate Callback
set(ipcon, 'EnumerateCallback', @(h, e) cb_enumerate(e));
ipcon.connect(HOST, PORT); % Connect to brickd
input('Press any key to exit...\n', 's');
ipcon.disconnect();
end
% Authenticate each time the connection got (re-)established
function cb_connected(e)
ipcon = e.getSource();
global SECRET;
if e.connectReason == ipcon.CONNECT_REASON_REQUEST
fprintf('Connected by request\n');
elseif e.connectReason == ipcon.CONNECT_REASON_AUTO_RECONNECT
fprintf('Auto-Reconnect\n');
end
% Authenticate first...
try
ipcon.authenticate(SECRET);
fprintf('Authentication succeeded\n');
catch ex
fprintf('Could not authenticate\n');
return
end
% ...reenable auto reconnect mechanism, as described above...
ipcon.setAutoReconnect(true)
% ...then trigger enumerate
ipcon.enumerate();
end
% Print incoming enumeration
function cb_enumerate(e)
fprintf('UID: %s, Enumeration Type: %g\n', char(e.uid), e.enumerationType);
end
|
Download (octave_example_enumerate.m)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | function octave_example_enumerate()
more off;
HOST = "localhost";
PORT = 4223;
ipcon = javaObject("com.tinkerforge.IPConnection"); % Create IP connection
ipcon.connect(HOST, PORT); % Connect to brickd
% Register Enumerate Callback
ipcon.addEnumerateCallback(@cb_enumerate);
% Trigger Enumerate
ipcon.enumerate();
input("Press any key to exit...\n", "s");
ipcon.disconnect();
end
% Print incoming enumeration
function cb_enumerate(e)
ipcon = e.getSource();
fprintf("UID: %s\n", e.uid);
fprintf("Enumeration Type: %d\n", java2int(e.enumerationType));
if java2int(e.enumerationType) == java2int(ipcon.ENUMERATION_TYPE_DISCONNECTED)
fprintf("\n");
return;
end
hardwareVersion = e.hardwareVersion;
firmwareVersion = e.firmwareVersion;
fprintf("Connected UID: %s\n", e.connectedUid);
fprintf("Position: %s\n", e.position);
fprintf("Hardware Version: %d.%d.%d\n", java2int(hardwareVersion(1)), ...
java2int(hardwareVersion(2)), ...
java2int(hardwareVersion(3)));
fprintf("Firmware Version: %d.%d.%d\n", java2int(firmwareVersion(1)), ...
java2int(firmwareVersion(2)), ...
java2int(firmwareVersion(3)));
fprintf("Device Identifier: %d\n", e.deviceIdentifier);
fprintf("\n");
end
function int = java2int(value)
if compare_versions(version(), "3.8", "<=")
int = value.intValue();
else
int = value;
end
end
|
Download (octave_example_authenticate.m)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | function octave_example_authenticate()
more off;
global SECRET;
HOST = "localhost";
PORT = 4223;
SECRET = "My Authentication Secret!";
ipcon = javaObject("com.tinkerforge.IPConnection"); % Create IP connection
% Disable auto reconnect mechanism, in case we have the wrong secret.
% If the authentication is successful, reenable it.
ipcon.setAutoReconnect(false)
% Register Connected Callback
ipcon.addConnectedCallback(@cb_connected);
% Register Enumerate Callback
ipcon.addEnumerateCallback(@cb_enumerate);
ipcon.connect(HOST, PORT); % Connect to brickd
input("Press any key to exit...\n", "s");
ipcon.disconnect();
end
% Authenticate each time the connection got (re-)established
function cb_connected(e)
ipcon = e.getSource();
global SECRET;
if java2int(e.connectReason) == java2int(ipcon.CONNECT_REASON_REQUEST)
fprintf("Connected by request\n");
elseif java2int(e.connectReason) == java2int(ipcon.CONNECT_REASON_AUTO_RECONNECT)
fprintf("Auto-Reconnect\n");
end
% Authenticate first...
try
ipcon.authenticate(SECRET);
fprintf("Authentication succeeded\n");
catch ex
fprintf("Could not authenticate\n");
return
end
% ...reenable auto reconnect mechanism, as described above...
ipcon.setAutoReconnect(true)
% ...then trigger enumerate
ipcon.enumerate();
end
% Print incoming enumeration
function cb_enumerate(e)
fprintf("UID: %s, Enumeration Type: %d\n", e.uid, e.enumerationType);
end
function int = java2int(value)
if compare_versions(version(), "3.8", "<=")
int = value.intValue();
else
int = value;
end
end
|
IPConnection
()¶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");
IPConnection.
connect
(String host, int port)¶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.
IPConnection.
disconnect
()¶Disconnects the TCP/IP connection from the Brick Daemon or the WIFI/Ethernet Extension.
IPConnection.
authenticate
(String secret)¶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.
IPConnection.
getConnectionState
()¶Can return the following states:
IPConnection.
setAutoReconnect
(boolean autoReconnect)¶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.
IPConnection.
getAutoReconnect
()¶Returns true if auto-reconnect is enabled, false otherwise.
IPConnection.
setTimeout
(int timeout)¶Sets the timeout in milliseconds for getters and for setters for which the response expected flag is activated.
Default timeout is 2500.
IPConnection.
getTimeout
()¶Returns the timeout as set by setTimeout()
.
IPConnection.
enumerate
()¶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.
IPConnection.
EnumerateCallback
¶Parameters: |
|
---|
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:
enumerate()
). This enumeration type can occur multiple times
for the same device.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.
IPConnection.
ConnectedCallback
¶Parameters: | 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:
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.
IPConnection.
DisconnectedCallback
¶Parameters: | 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:
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.