..
 #############################################################
 # This file was automatically generated on 2015-05-08.      #
 #                                                           #
 # If you have a bugfix for this file and want to commit it, #
 # please fix the bug in the generator. You can find a link  #
 # to the generators git repository on tinkerforge.com       #
 #############################################################


:breadcrumbs: <a href="../../index.html">Home</a> / <a href="../../index.html#software-javascript">Software</a> / JavaScript - Temperature Bricklet

.. |device_identifier_constant| replace:: There is also a :ref:`constant <temperature_bricklet_javascript_constants>` for the device identifier of this Bricklet.

.. _temperature_bricklet_javascript:

JavaScript - Temperature Bricklet
=================================

This is the description of the :ref:`JavaScript API bindings <api_bindings_javascript>` for the :ref:`Temperature Bricklet <temperature_bricklet>`. General information and technical
specifications for the Temperature Bricklet are summarized in its :ref:`hardware description
<temperature_bricklet_description>`.

An :ref:`installation guide <api_bindings_javascript_install>` for the JavaScript API
bindings is part of their general description.

.. _temperature_bricklet_javascript_examples:


Examples
--------

The example code below is `Public Domain (CC0 1.0)
<http://creativecommons.org/publicdomain/zero/1.0/>`__.

Simple (Node.js)
^^^^^^^^^^^^^^^^

`Download (ExampleSimple.js) <https://github.com/Tinkerforge/temperature-bricklet/raw/master/software/examples/javascript/ExampleSimple.js>`__

.. literalinclude:: Temperature_Bricklet_JavaScript_ExampleSimple.js
 :language: javascript
 :linenos:
 :tab-width: 4

Callback (Node.js)
^^^^^^^^^^^^^^^^^^

`Download (ExampleCallback.js) <https://github.com/Tinkerforge/temperature-bricklet/raw/master/software/examples/javascript/ExampleCallback.js>`__

.. literalinclude:: Temperature_Bricklet_JavaScript_ExampleCallback.js
 :language: javascript
 :linenos:
 :tab-width: 4

Threshold (Node.js)
^^^^^^^^^^^^^^^^^^^

`Download (ExampleThreshold.js) <https://github.com/Tinkerforge/temperature-bricklet/raw/master/software/examples/javascript/ExampleThreshold.js>`__

.. literalinclude:: Temperature_Bricklet_JavaScript_ExampleThreshold.js
 :language: javascript
 :linenos:
 :tab-width: 4

Simple (HTML)
^^^^^^^^^^^^^

`Download (ExampleSimple.html) <https://github.com/Tinkerforge/temperature-bricklet/raw/master/software/examples/javascript/ExampleSimple.html>`__, `Test (ExampleSimple.html) <http://www.tinkerforge.com/en/doc/Software/Examples/JavaScript/Temperature_Bricklet_JavaScript_ExampleSimple.html>`__

.. literalinclude:: Temperature_Bricklet_JavaScript_ExampleSimple.html
 :language: html
 :linenos:
 :tab-width: 4

Callback (HTML)
^^^^^^^^^^^^^^^

`Download (ExampleCallback.html) <https://github.com/Tinkerforge/temperature-bricklet/raw/master/software/examples/javascript/ExampleCallback.html>`__, `Test (ExampleCallback.html) <http://www.tinkerforge.com/en/doc/Software/Examples/JavaScript/Temperature_Bricklet_JavaScript_ExampleCallback.html>`__

.. literalinclude:: Temperature_Bricklet_JavaScript_ExampleCallback.html
 :language: html
 :linenos:
 :tab-width: 4

Threshold (HTML)
^^^^^^^^^^^^^^^^

`Download (ExampleThreshold.html) <https://github.com/Tinkerforge/temperature-bricklet/raw/master/software/examples/javascript/ExampleThreshold.html>`__, `Test (ExampleThreshold.html) <http://www.tinkerforge.com/en/doc/Software/Examples/JavaScript/Temperature_Bricklet_JavaScript_ExampleThreshold.html>`__

.. literalinclude:: Temperature_Bricklet_JavaScript_ExampleThreshold.html
 :language: html
 :linenos:
 :tab-width: 4

.. _temperature_bricklet_javascript_api:

API
---

Generally, every method of the JavaScript bindings can take two optional
parameters, ``returnCallback`` and ``errorCallback``. These are two user
defined callback functions. The ``returnCallback`` is called with the return
values as parameters, if the method returns something. 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

The namespace for the JavaScript bindings is ``Tinkerforge.*``.




Basic Functions
^^^^^^^^^^^^^^^


.. javascript:function:: new BrickletTemperature(uid, ipcon)

 :param uid: string
 :param ipcon: IPConnection

 Creates an object with the unique device ID ``uid``:

 .. code-block:: javascript

    var temperature = new BrickletTemperature("YOUR_DEVICE_UID", ipcon);

 This object can then be used after the IP Connection is connected
 (see examples :ref:`above <temperature_bricklet_javascript_examples>`).


.. javascript:function:: BrickletTemperature.getTemperature([returnCallback], [errorCallback])


 :return temperature: int

 Returns the temperature of the sensor. The value
 has a range of -2500 to 8500 and is given in °C/100,
 e.g. a value of 4223 means that a temperature of 42.23 °C is measured.
 
 If you want to get the temperature periodically, it is recommended 
 to use the callback :javascript:attr:`CALLBACK_TEMPERATURE <BrickletTemperature.CALLBACK_TEMPERATURE>` and set the period with 
 :javascript:func:`setTemperatureCallbackPeriod() <BrickletTemperature.setTemperatureCallbackPeriod>`.
 


Advanced Functions
^^^^^^^^^^^^^^^^^^

.. javascript:function:: BrickletTemperature.setI2CMode(mode, [returnCallback], [errorCallback])

 :param mode: int

 :noreturn: undefined

 Sets the I2C mode. Possible modes are:
 
 * 0: Fast (400kHz, default)
 * 1: Slow (100kHz)
 
 If you have problems with obvious outliers in the
 Temperature Bricklet measurements, they may be caused by EMI issues.
 In this case it may be helpful to lower the I2C speed.
 
 It is however not recommended to lower the I2C speed in applications where
 a high throughput needs to be achieved.
 
 The following constants are available for this function:
 
 * BrickletTemperature.I2C_MODE_FAST = 0
 * BrickletTemperature.I2C_MODE_SLOW = 1
 
 .. versionadded:: 2.0.1~(Plugin)
 
.. javascript:function:: BrickletTemperature.getI2CMode([returnCallback], [errorCallback])


 :return mode: int

 Returns the I2C mode as set by :javascript:func:`setI2CMode() <BrickletTemperature.setI2CMode>`.
 
 The following constants are available for this function:
 
 * BrickletTemperature.I2C_MODE_FAST = 0
 * BrickletTemperature.I2C_MODE_SLOW = 1
 
 .. versionadded:: 2.0.1~(Plugin)
 
.. javascript:function:: BrickletTemperature.getAPIVersion()

 :rtype: [int, int, int]

 Returns the version of the API definition (major, minor, revision) 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.
 
.. javascript:function:: BrickletTemperature.getResponseExpected(functionId, [errorCallback])

 :param functionId: int
 :rtype: 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
 :javascript:func:`setResponseExpected() <BrickletTemperature.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 send and errors are silently ignored,
 because they cannot be detected.
 
 See :javascript:func:`setResponseExpected() <BrickletTemperature.setResponseExpected>`
 for the list of function ID constants available for this function.
 
.. javascript:function:: BrickletTemperature.setResponseExpected(functionId, responseExpected, [errorCallback])

 :param functionId: int
 :param responseExpected: boolean

 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 and callbacks it is always disabled.
 
 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 send and errors are silently ignored,
 because they cannot be detected.
 
 The following function ID constants are available for this function:
 
 * BrickletTemperature.FUNCTION_SET_TEMPERATURE_CALLBACK_PERIOD = 2
 * BrickletTemperature.FUNCTION_SET_TEMPERATURE_CALLBACK_THRESHOLD = 4
 * BrickletTemperature.FUNCTION_SET_DEBOUNCE_PERIOD = 6
 * BrickletTemperature.FUNCTION_SET_I2C_MODE = 10
 
.. javascript:function:: BrickletTemperature.setResponseExpectedAll(responseExpected)

 :param responseExpected: boolean

 Changes the response expected flag for all setter and callback configuration
 functions of this device at once.
 
.. javascript:function:: BrickletTemperature.getIdentity([returnCallback], [errorCallback])


 :return uid: string
 :return connectedUid: string
 :return position: char
 :return hardwareVersion: [int, int, int]
 :return firmwareVersion: [int, int, int]
 :return deviceIdentifier: int

 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' or 'd'.
 
 The device identifier numbers can be found :ref:`here <device_identifier>`.
 |device_identifier_constant|
 


Callback Configuration Functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


.. javascript:function:: BrickletTemperature.on(id, callback)

 :param id: int
 :param callback: function

 Registers a callback with ID *id* to the function *callback*. The available
 IDs with corresponding function signatures are listed
 :ref:`below <temperature_bricklet_javascript_callbacks>`.


.. javascript:function:: BrickletTemperature.setTemperatureCallbackPeriod(period, [returnCallback], [errorCallback])

 :param period: int

 :noreturn: undefined

 Sets the period in ms with which the :javascript:attr:`CALLBACK_TEMPERATURE <BrickletTemperature.CALLBACK_TEMPERATURE>` callback is triggered
 periodically. A value of 0 turns the callback off.
 
 :javascript:attr:`CALLBACK_TEMPERATURE <BrickletTemperature.CALLBACK_TEMPERATURE>` is only triggered if the temperature has changed since the
 last triggering.
 
 The default value is 0.
 
.. javascript:function:: BrickletTemperature.getTemperatureCallbackPeriod([returnCallback], [errorCallback])


 :return period: int

 Returns the period as set by :javascript:func:`setTemperatureCallbackPeriod() <BrickletTemperature.setTemperatureCallbackPeriod>`.
 
.. javascript:function:: BrickletTemperature.setTemperatureCallbackThreshold(option, min, max, [returnCallback], [errorCallback])

 :param option: char
 :param min: int
 :param max: int

 :noreturn: undefined

 Sets the thresholds for the :javascript:attr:`CALLBACK_TEMPERATURE_REACHED <BrickletTemperature.CALLBACK_TEMPERATURE_REACHED>` callback. 
 
 The following options are possible:
 
 .. csv-table::
  :header: "Option", "Description"
  :widths: 10, 100
 
  "'x'",    "Callback is turned off"
  "'o'",    "Callback is triggered when the temperature is *outside* the min and max values"
  "'i'",    "Callback is triggered when the temperature is *inside* the min and max values"
  "'<'",    "Callback is triggered when the temperature is smaller than the min value (max is ignored)"
  "'>'",    "Callback is triggered when the temperature is greater than the min value (max is ignored)"
 
 The default value is ('x', 0, 0).
 
 The following constants are available for this function:
 
 * BrickletTemperature.THRESHOLD_OPTION_OFF = 'x'
 * BrickletTemperature.THRESHOLD_OPTION_OUTSIDE = 'o'
 * BrickletTemperature.THRESHOLD_OPTION_INSIDE = 'i'
 * BrickletTemperature.THRESHOLD_OPTION_SMALLER = '<'
 * BrickletTemperature.THRESHOLD_OPTION_GREATER = '>'
 
.. javascript:function:: BrickletTemperature.getTemperatureCallbackThreshold([returnCallback], [errorCallback])


 :return option: char
 :return min: int
 :return max: int

 Returns the threshold as set by :javascript:func:`setTemperatureCallbackThreshold() <BrickletTemperature.setTemperatureCallbackThreshold>`.
 
 The following constants are available for this function:
 
 * BrickletTemperature.THRESHOLD_OPTION_OFF = 'x'
 * BrickletTemperature.THRESHOLD_OPTION_OUTSIDE = 'o'
 * BrickletTemperature.THRESHOLD_OPTION_INSIDE = 'i'
 * BrickletTemperature.THRESHOLD_OPTION_SMALLER = '<'
 * BrickletTemperature.THRESHOLD_OPTION_GREATER = '>'
 
.. javascript:function:: BrickletTemperature.setDebouncePeriod(debounce, [returnCallback], [errorCallback])

 :param debounce: int

 :noreturn: undefined

 Sets the period in ms with which the threshold callback
 
 * :javascript:attr:`CALLBACK_TEMPERATURE_REACHED <BrickletTemperature.CALLBACK_TEMPERATURE_REACHED>`
 
 is triggered, if the threshold
 
 * :javascript:func:`setTemperatureCallbackThreshold() <BrickletTemperature.setTemperatureCallbackThreshold>`
 
 keeps being reached.
 
 The default value is 100.
 
.. javascript:function:: BrickletTemperature.getDebouncePeriod([returnCallback], [errorCallback])


 :return debounce: int

 Returns the debounce period as set by :javascript:func:`setDebouncePeriod() <BrickletTemperature.setDebouncePeriod>`.
 


.. _temperature_bricklet_javascript_callbacks:

Callbacks
^^^^^^^^^

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

.. code-block:: javascript

    temperature.on(BrickletTemperature.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.

.. javascript:attribute:: BrickletTemperature.CALLBACK_TEMPERATURE

 :param temperature: int


 This callback is triggered periodically with the period that is set by
 :javascript:func:`setTemperatureCallbackPeriod() <BrickletTemperature.setTemperatureCallbackPeriod>`. The parameter is the temperature
 of the sensor.
 
 :javascript:attr:`CALLBACK_TEMPERATURE <BrickletTemperature.CALLBACK_TEMPERATURE>` is only triggered if the temperature has changed since the
 last triggering.
 
.. javascript:attribute:: BrickletTemperature.CALLBACK_TEMPERATURE_REACHED

 :param temperature: int


 This callback is triggered when the threshold as set by
 :javascript:func:`setTemperatureCallbackThreshold() <BrickletTemperature.setTemperatureCallbackThreshold>` is reached.
 The parameter is the temperature of the sensor.
 
 If the threshold keeps being reached, the callback is triggered periodically
 with the period as set by :javascript:func:`setDebouncePeriod() <BrickletTemperature.setDebouncePeriod>`.
 


.. _temperature_bricklet_javascript_constants:

Constants
^^^^^^^^^

.. javascript:attribute:: BrickletTemperature.DEVICE_IDENTIFIER

 This constant is used to identify a Temperature Bricklet.

 The :javascript:func:`getIdentity() <BrickletTemperature.getIdentity>` function and the
 :javascript:attr:`CALLBACK_ENUMERATE <IPConnection.CALLBACK_ENUMERATE>`
 callback of the IP Connection have a ``device_identifier`` parameter to specify
 the Brick's or Bricklet's type.

