JavaScript - UV Light Bricklet

This is the description of the JavaScript API bindings for the UV Light Bricklet. General information and technical specifications for the UV Light Bricklet are summarized in its hardware description.

An installation guide for the JavaScript API bindings is part of their general description.

Examples

The example code below is Public Domain (CC0 1.0).

Simple (Node.js)

Download (ExampleSimple.js)

 1var Tinkerforge = require('tinkerforge');
 2
 3var HOST = 'localhost';
 4var PORT = 4223;
 5var UID = 'XYZ'; // Change XYZ to the UID of your UV Light Bricklet
 6
 7var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
 8var uvl = new Tinkerforge.BrickletUVLight(UID, ipcon); // Create device object
 9
10ipcon.connect(HOST, PORT,
11    function (error) {
12        console.log('Error: ' + error);
13    }
14); // Connect to brickd
15// Don't use device before ipcon is connected
16
17ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
18    function (connectReason) {
19        // Get current UV light
20        uvl.getUVLight(
21            function (uvLight) {
22                console.log('UV Light: ' + uvLight/10.0 + ' mW/m²');
23            },
24            function (error) {
25                console.log('Error: ' + error);
26            }
27        );
28    }
29);
30
31console.log('Press key to exit');
32process.stdin.on('data',
33    function (data) {
34        ipcon.disconnect();
35        process.exit(0);
36    }
37);

Callback (Node.js)

Download (ExampleCallback.js)

 1var Tinkerforge = require('tinkerforge');
 2
 3var HOST = 'localhost';
 4var PORT = 4223;
 5var UID = 'XYZ'; // Change XYZ to the UID of your UV Light Bricklet
 6
 7var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
 8var uvl = new Tinkerforge.BrickletUVLight(UID, ipcon); // Create device object
 9
10ipcon.connect(HOST, PORT,
11    function (error) {
12        console.log('Error: ' + error);
13    }
14); // Connect to brickd
15// Don't use device before ipcon is connected
16
17ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
18    function (connectReason) {
19        // Set period for UV light callback to 1s (1000ms)
20        // Note: The UV light callback is only called every second
21        //       if the UV light has changed since the last call!
22        uvl.setUVLightCallbackPeriod(1000);
23    }
24);
25
26// Register UV light callback
27uvl.on(Tinkerforge.BrickletUVLight.CALLBACK_UV_LIGHT,
28    // Callback function for UV light callback
29    function (uvLight) {
30        console.log('UV Light: ' + uvLight/10.0 + ' mW/m²');
31    }
32);
33
34console.log('Press key to exit');
35process.stdin.on('data',
36    function (data) {
37        ipcon.disconnect();
38        process.exit(0);
39    }
40);

Threshold (Node.js)

Download (ExampleThreshold.js)

 1var Tinkerforge = require('tinkerforge');
 2
 3var HOST = 'localhost';
 4var PORT = 4223;
 5var UID = 'XYZ'; // Change XYZ to the UID of your UV Light Bricklet
 6
 7var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
 8var uvl = new Tinkerforge.BrickletUVLight(UID, ipcon); // Create device object
 9
10ipcon.connect(HOST, PORT,
11    function (error) {
12        console.log('Error: ' + error);
13    }
14); // Connect to brickd
15// Don't use device before ipcon is connected
16
17ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
18    function (connectReason) {
19        // Get threshold callbacks with a debounce time of 10 seconds (10000ms)
20        uvl.setDebouncePeriod(10000);
21
22        // Configure threshold for UV light "greater than 75 mW/m²"
23        uvl.setUVLightCallbackThreshold('>', 75*10, 0);
24    }
25);
26
27// Register UV light reached callback
28uvl.on(Tinkerforge.BrickletUVLight.CALLBACK_UV_LIGHT_REACHED,
29    // Callback function for UV light reached callback
30    function (uvLight) {
31        console.log('UV Light: ' + uvLight/10.0 + ' mW/m²');
32        console.log('UV Index > 3. Use sunscreen!');
33    }
34);
35
36console.log('Press key to exit');
37process.stdin.on('data',
38    function (data) {
39        ipcon.disconnect();
40        process.exit(0);
41    }
42);

Simple (HTML)

Download (ExampleSimple.html), Test (ExampleSimple.html)

 1<!DOCTYPE html>
 2<html>
 3    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 4    <head>
 5        <title>Tinkerforge | JavaScript Example</title>
 6    </head>
 7    <body>
 8        <div style="text-align:center;">
 9            <h1>UV Light Bricklet Simple Example</h1>
10            <p>
11                <input value="localhost" id="host" type="text" size="20">:
12                <input value="4280" id="port" type="text" size="5">,
13                <input value="uid" id="uid" type="text" size="5">
14                <input value="Start Example" id="start" type="button" onclick="startExample();">
15            </p>
16            <p>
17                <textarea readonly id="text" cols="80" rows="24" style="resize:none;"
18                          >Press "Start Example" to begin ...</textarea>
19            </p>
20        </div>
21        <script src="./Tinkerforge.js" type='text/javascript'></script>
22        <script type='text/javascript'>
23            var ipcon;
24            var textArea = document.getElementById("text");
25            function startExample() {
26                textArea.value = "";
27                var HOST = document.getElementById("host").value;
28                var PORT = parseInt(document.getElementById("port").value);
29                var UID = document.getElementById("uid").value;
30                if(ipcon !== undefined) {
31                    ipcon.disconnect();
32                }
33                ipcon = new Tinkerforge.IPConnection(); // Create IP connection
34                var uvl = new Tinkerforge.BrickletUVLight(UID, ipcon); // Create device object
35                ipcon.connect(HOST, PORT,
36                    function(error) {
37                        textArea.value += 'Error: ' + error + '\n';
38                    }
39                ); // Connect to brickd
40                // Don't use device before ipcon is connected
41
42                ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
43                    function (connectReason) {
44                        // Get current UV light
45                        uvl.getUVLight(
46                            function (uvLight) {
47                                textArea.value += 'UV Light: ' + uvLight/10.0 + ' mW/m²\n';
48                            },
49                            function (error) {
50                                textArea.value += 'Error: ' + error + '\n';
51                            }
52                        );
53                    }
54                );
55            }
56        </script>
57    </body>
58</html>

Callback (HTML)

Download (ExampleCallback.html), Test (ExampleCallback.html)

 1<!DOCTYPE html>
 2<html>
 3    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 4    <head>
 5        <title>Tinkerforge | JavaScript Example</title>
 6    </head>
 7    <body>
 8        <div style="text-align:center;">
 9            <h1>UV Light Bricklet Callback Example</h1>
10            <p>
11                <input value="localhost" id="host" type="text" size="20">:
12                <input value="4280" id="port" type="text" size="5">,
13                <input value="uid" id="uid" type="text" size="5">
14                <input value="Start Example" id="start" type="button" onclick="startExample();">
15            </p>
16            <p>
17                <textarea readonly id="text" cols="80" rows="24" style="resize:none;"
18                          >Press "Start Example" to begin ...</textarea>
19            </p>
20        </div>
21        <script src="./Tinkerforge.js" type='text/javascript'></script>
22        <script type='text/javascript'>
23            var ipcon;
24            var textArea = document.getElementById("text");
25            function startExample() {
26                textArea.value = "";
27                var HOST = document.getElementById("host").value;
28                var PORT = parseInt(document.getElementById("port").value);
29                var UID = document.getElementById("uid").value;
30                if(ipcon !== undefined) {
31                    ipcon.disconnect();
32                }
33                ipcon = new Tinkerforge.IPConnection(); // Create IP connection
34                var uvl = new Tinkerforge.BrickletUVLight(UID, ipcon); // Create device object
35                ipcon.connect(HOST, PORT,
36                    function(error) {
37                        textArea.value += 'Error: ' + error + '\n';
38                    }
39                ); // Connect to brickd
40                // Don't use device before ipcon is connected
41
42                ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
43                    function (connectReason) {
44                        // Set period for UV light callback to 1s (1000ms)
45                        // Note: The UV light callback is only called every second
46                        //       if the UV light has changed since the last call!
47                        uvl.setUVLightCallbackPeriod(1000);
48                    }
49                );
50
51                // Register UV light callback
52                uvl.on(Tinkerforge.BrickletUVLight.CALLBACK_UV_LIGHT,
53                    // Callback function for UV light callback
54                    function (uvLight) {
55                        textArea.value += 'UV Light: ' + uvLight/10.0 + ' mW/m²\n';
56                        textArea.scrollTop = textArea.scrollHeight;
57                    }
58                );
59            }
60        </script>
61    </body>
62</html>

Threshold (HTML)

Download (ExampleThreshold.html), Test (ExampleThreshold.html)

 1<!DOCTYPE html>
 2<html>
 3    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 4    <head>
 5        <title>Tinkerforge | JavaScript Example</title>
 6    </head>
 7    <body>
 8        <div style="text-align:center;">
 9            <h1>UV Light Bricklet Threshold Example</h1>
10            <p>
11                <input value="localhost" id="host" type="text" size="20">:
12                <input value="4280" id="port" type="text" size="5">,
13                <input value="uid" id="uid" type="text" size="5">
14                <input value="Start Example" id="start" type="button" onclick="startExample();">
15            </p>
16            <p>
17                <textarea readonly id="text" cols="80" rows="24" style="resize:none;"
18                          >Press "Start Example" to begin ...</textarea>
19            </p>
20        </div>
21        <script src="./Tinkerforge.js" type='text/javascript'></script>
22        <script type='text/javascript'>
23            var ipcon;
24            var textArea = document.getElementById("text");
25            function startExample() {
26                textArea.value = "";
27                var HOST = document.getElementById("host").value;
28                var PORT = parseInt(document.getElementById("port").value);
29                var UID = document.getElementById("uid").value;
30                if(ipcon !== undefined) {
31                    ipcon.disconnect();
32                }
33                ipcon = new Tinkerforge.IPConnection(); // Create IP connection
34                var uvl = new Tinkerforge.BrickletUVLight(UID, ipcon); // Create device object
35                ipcon.connect(HOST, PORT,
36                    function(error) {
37                        textArea.value += 'Error: ' + error + '\n';
38                    }
39                ); // Connect to brickd
40                // Don't use device before ipcon is connected
41
42                ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
43                    function (connectReason) {
44                        // Get threshold callbacks with a debounce time of 10 seconds (10000ms)
45                        uvl.setDebouncePeriod(10000);
46
47                        // Configure threshold for UV light "greater than 75 mW/m²"
48                        uvl.setUVLightCallbackThreshold('>', 75*10, 0);
49                    }
50                );
51
52                // Register UV light reached callback
53                uvl.on(Tinkerforge.BrickletUVLight.CALLBACK_UV_LIGHT_REACHED,
54                    // Callback function for UV light reached callback
55                    function (uvLight) {
56                        textArea.value += 'UV Light: ' + uvLight/10.0 + ' mW/m²\n';
57                        textArea.value += 'UV Index > 3. Use sunscreen!\n';
58                        textArea.scrollTop = textArea.scrollHeight;
59                    }
60                );
61            }
62        </script>
63    </body>
64</html>

API

Generally, every function of the JavaScript bindings can take two optional parameters, returnCallback and errorCallback. These are two user defined callback functions. The returnCallback function is called with the results as arguments, if the function returns its results asynchronously. The errorCallback is called with an error code in case of an error. The error code can be one of the following values:

  • IPConnection.ERROR_ALREADY_CONNECTED = 11

  • IPConnection.ERROR_NOT_CONNECTED = 12

  • IPConnection.ERROR_CONNECT_FAILED = 13

  • IPConnection.ERROR_INVALID_FUNCTION_ID = 21

  • IPConnection.ERROR_TIMEOUT = 31

  • IPConnection.ERROR_INVALID_PARAMETER = 41

  • IPConnection.ERROR_FUNCTION_NOT_SUPPORTED = 42

  • IPConnection.ERROR_UNKNOWN_ERROR = 43

  • IPConnection.ERROR_STREAM_OUT_OF_SYNC = 51

  • IPConnection.ERROR_NON_ASCII_CHAR_IN_SECRET = 71

  • IPConnection.ERROR_WRONG_DEVICE_TYPE = 81

  • IPConnection.ERROR_DEVICE_REPLACED = 82

  • IPConnection.ERROR_WRONG_RESPONSE_LENGTH = 83

  • IPConnection.ERROR_INT64_NOT_SUPPORTED = 91

The namespace for the JavaScript bindings is Tinkerforge.*.

Basic Functions

new BrickletUVLight(uid, ipcon)
Parameters:
  • uid – Type: string
  • ipcon – Type: IPConnection
Returns:
  • uvLight – Type: BrickletUVLight

Creates an object with the unique device ID uid:

var uvLight = new BrickletUVLight("YOUR_DEVICE_UID", ipcon);

This object can then be used after the IP Connection is connected.

BrickletUVLight.getUVLight([returnCallback][, errorCallback])
Callback Parameters:
  • uvLight – Type: int, Unit: 1/10 mW/m², Range: [0 to 3280]
Returns:
  • undefined

Returns the UV light intensity of the sensor. The sensor has already weighted the intensity with the erythemal action spectrum to get the skin-affecting irradiation.

To get UV index you just have to divide the value by 250. For example, a UV light intensity of 500 is equivalent to an UV index of 2.

If you want to get the intensity periodically, it is recommended to use the CALLBACK_UV_LIGHT callback and set the period with setUVLightCallbackPeriod().

Advanced Functions

BrickletUVLight.getIdentity([returnCallback][, errorCallback])
Callback Parameters:
  • uid – Type: string, Length: up to 8
  • connectedUid – Type: string, Length: up to 8
  • position – Type: char, Range: ['a' to 'h', 'z']
  • hardwareVersion – Type: [int, ...], Length: 3
    • 0: major – Type: int, Range: [0 to 255]
    • 1: minor – Type: int, Range: [0 to 255]
    • 2: revision – Type: int, Range: [0 to 255]
  • firmwareVersion – Type: [int, ...], Length: 3
    • 0: major – Type: int, Range: [0 to 255]
    • 1: minor – Type: int, Range: [0 to 255]
    • 2: revision – Type: int, Range: [0 to 255]
  • deviceIdentifier – Type: int, Range: [0 to 216 - 1]
Returns:
  • undefined

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.

The device identifier numbers can be found here. There is also a constant for the device identifier of this Bricklet.

Callback Configuration Functions

BrickletUVLight.on(callback_id, function[, errorCallback])
Parameters:
  • callback_id – Type: int
  • function – Type: function
Returns:
  • undefined

Registers the given function with the given callback_id.

The available callback IDs with corresponding function signatures are listed below.

BrickletUVLight.setUVLightCallbackPeriod(period[, returnCallback][, errorCallback])
Parameters:
  • period – Type: int, Unit: 1 ms, Range: [0 to 232 - 1], Default: 0
Callback Parameters:
  • undefined
Returns:
  • undefined

Sets the period with which the CALLBACK_UV_LIGHT callback is triggered periodically. A value of 0 turns the callback off.

The CALLBACK_UV_LIGHT callback is only triggered if the intensity has changed since the last triggering.

BrickletUVLight.getUVLightCallbackPeriod([returnCallback][, errorCallback])
Callback Parameters:
  • period – Type: int, Unit: 1 ms, Range: [0 to 232 - 1], Default: 0
Returns:
  • undefined

Returns the period as set by setUVLightCallbackPeriod().

BrickletUVLight.setUVLightCallbackThreshold(option, min, max[, returnCallback][, errorCallback])
Parameters:
  • option – Type: char, Range: See constants, Default: 'x'
  • min – Type: int, Unit: 1/10 mW/m², Range: [0 to 232 - 1], Default: 0
  • max – Type: int, Unit: 1/10 mW/m², Range: [0 to 232 - 1], Default: 0
Callback Parameters:
  • undefined
Returns:
  • undefined

Sets the thresholds for the CALLBACK_UV_LIGHT_REACHED callback.

The following options are possible:

Option

Description

'x'

Callback is turned off

'o'

Callback is triggered when the intensity is outside the min and max values

'i'

Callback is triggered when the intensity is inside the min and max values

'<'

Callback is triggered when the intensity is smaller than the min value (max is ignored)

'>'

Callback is triggered when the intensity is greater than the min value (max is ignored)

The following constants are available for this function:

For option:

  • BrickletUVLight.THRESHOLD_OPTION_OFF = 'x'

  • BrickletUVLight.THRESHOLD_OPTION_OUTSIDE = 'o'

  • BrickletUVLight.THRESHOLD_OPTION_INSIDE = 'i'

  • BrickletUVLight.THRESHOLD_OPTION_SMALLER = '<'

  • BrickletUVLight.THRESHOLD_OPTION_GREATER = '>'

BrickletUVLight.getUVLightCallbackThreshold([returnCallback][, errorCallback])
Callback Parameters:
  • option – Type: char, Range: See constants, Default: 'x'
  • min – Type: int, Unit: 1/10 mW/m², Range: [0 to 232 - 1], Default: 0
  • max – Type: int, Unit: 1/10 mW/m², Range: [0 to 232 - 1], Default: 0
Returns:
  • undefined

Returns the threshold as set by setUVLightCallbackThreshold().

The following constants are available for this function:

For option:

  • BrickletUVLight.THRESHOLD_OPTION_OFF = 'x'

  • BrickletUVLight.THRESHOLD_OPTION_OUTSIDE = 'o'

  • BrickletUVLight.THRESHOLD_OPTION_INSIDE = 'i'

  • BrickletUVLight.THRESHOLD_OPTION_SMALLER = '<'

  • BrickletUVLight.THRESHOLD_OPTION_GREATER = '>'

BrickletUVLight.setDebouncePeriod(debounce[, returnCallback][, errorCallback])
Parameters:
  • debounce – Type: int, Unit: 1 ms, Range: [0 to 232 - 1], Default: 100
Callback Parameters:
  • undefined
Returns:
  • undefined

Sets the period with which the threshold callbacks

are triggered, if the thresholds

keep being reached.

BrickletUVLight.getDebouncePeriod([returnCallback][, errorCallback])
Callback Parameters:
  • debounce – Type: int, Unit: 1 ms, Range: [0 to 232 - 1], Default: 100
Returns:
  • undefined

Returns the debounce period as set by setDebouncePeriod().

Callbacks

Callbacks can be registered to receive time critical or recurring data from the device. The registration is done with the on() function of the device object. The first parameter is the callback ID and the second parameter the callback function:

uvLight.on(BrickletUVLight.CALLBACK_EXAMPLE,
    function (param) {
        console.log(param);
    }
);

The available constants with inherent number and type of parameters are described below.

Note

Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.

BrickletUVLight.CALLBACK_UV_LIGHT
Callback Parameters:
  • uvLight – Type: int, Unit: 1/10 mW/m², Range: [0 to 32800000]

This callback is triggered periodically with the period that is set by setUVLightCallbackPeriod(). The parameter is the UV Light intensity of the sensor.

The CALLBACK_UV_LIGHT callback is only triggered if the intensity has changed since the last triggering.

BrickletUVLight.CALLBACK_UV_LIGHT_REACHED
Callback Parameters:
  • uvLight – Type: int, Unit: 1/10 mW/m², Range: [0 to 32800000]

This callback is triggered when the threshold as set by setUVLightCallbackThreshold() is reached. The parameter is the UV Light intensity of the sensor.

If the threshold keeps being reached, the callback is triggered periodically with the period as set by setDebouncePeriod().

Virtual Functions

Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.

BrickletUVLight.getAPIVersion()
Returns:
  • apiVersion – Type: [int, ...], Length: 3
    • 0: major – Type: int, Range: [0 to 255]
    • 1: minor – Type: int, Range: [0 to 255]
    • 2: revision – Type: int, Range: [0 to 255]

Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.

BrickletUVLight.getResponseExpected(functionId[, errorCallback])
Parameters:
  • functionId – Type: int, Range: See constants
Returns:
  • responseExpected – Type: boolean

Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.

For getter functions this is enabled by default and cannot be disabled, because those functions will always send a response. For callback configuration functions it is enabled by default too, but can be disabled by setResponseExpected(). For setter functions it is disabled by default and can be enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For function_id:

  • BrickletUVLight.FUNCTION_SET_UV_LIGHT_CALLBACK_PERIOD = 2

  • BrickletUVLight.FUNCTION_SET_UV_LIGHT_CALLBACK_THRESHOLD = 4

  • BrickletUVLight.FUNCTION_SET_DEBOUNCE_PERIOD = 6

BrickletUVLight.setResponseExpected(functionId, responseExpected[, errorCallback])
Parameters:
  • functionId – Type: int, Range: See constants
  • responseExpected – Type: boolean
Returns:
  • undefined

Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For function_id:

  • BrickletUVLight.FUNCTION_SET_UV_LIGHT_CALLBACK_PERIOD = 2

  • BrickletUVLight.FUNCTION_SET_UV_LIGHT_CALLBACK_THRESHOLD = 4

  • BrickletUVLight.FUNCTION_SET_DEBOUNCE_PERIOD = 6

BrickletUVLight.setResponseExpectedAll(responseExpected)
Parameters:
  • responseExpected – Type: boolean
Returns:
  • undefined

Changes the response expected flag for all setter and callback configuration functions of this device at once.

Constants

BrickletUVLight.DEVICE_IDENTIFIER

This constant is used to identify a UV Light Bricklet.

The getIdentity() function and the IPConnection.CALLBACK_ENUMERATE callback of the IP Connection have a device_identifier parameter to specify the Brick's or Bricklet's type.

BrickletUVLight.DEVICE_DISPLAY_NAME

This constant represents the human readable name of a UV Light Bricklet.