Humidity Sensor

Goals

Get the humidity sensor and subscribe to updates

Working with a Humidity 2.0 sensor works as described in the general article on how to use sensors. In this article, we assume you have successfully initialized the devices with the Tinkerforge Device Manager, and you stored all connected devices on a global variable devices. We also declared a global variable called humiditySensor.

Next, we need to know the device identifier, which 283:

// Get the humidity sensor via its device identifer
humiditySensor = devices.getDeviceByIdentifier(283);

Once we have a reference to the sensor, we can register a callback function:

// We want to be informed when a new sensor value arrives
humiditySensor.registerListener(humidityChanged);

Of course, we have to actually define the function:

function humidityChanged(val) {
   // Do something with the value object
}

Distinguish between temperature and humidity

Since the sensor delivers two different value types, temperature and humidity, why not use the callback function to illustrate how we can distinguish both types:

function humidityChanged(val) {
    // Get the value
    var value = val.getValue():
    
    if(value.type == "temperature") {
        // Do something
    }
    else if (value.type == "humidity") {
        // Do something else
    }
}

That is basically it - we can't do anything else with a Humidity 2.0 sensor.

Last updated

Was this helpful?