Sensors
Sensors are important parts of an Internet of Things application. They allow machines to take data from the physical world and transform it into a digital and thus computer processable form.
Last updated
Was this helpful?
Sensors are important parts of an Internet of Things application. They allow machines to take data from the physical world and transform it into a digital and thus computer processable form.
Last updated
Was this helpful?
When programming sensors, we apply the same general approach:
using the .
Store a reference to the sensor on a .
Register a listener function to get informed about new values from the sensor.
Within the listener function, we can then work with the value we received.
The following code connects to all devices on our local USB port:
To illustrate the process, let's assume we have a humidity sensor connected to our Master Brick. In this case, we can get and store a reference to that sensor by extending the code as follows:
The humidity sensor has the device identifier 283, and if the sensor is actually connected, line 16 in the code above will return a reference to that sensor. We then store that reference on our global variable humiditySensor
, so that we can access the sensor from anywhere in our code.
In line 10, we call the registerListener()
function and pass our local function humidityChanged()
as an argument. This is the callback function the sensor calls when there is a new value available. As an argument of the callback function, the sensor passes an object that contains information about the new value (the value itself, along with a timestamp and some other metadata).
To obtain only the value from this object, we can call the getValue()
function, which returns a JSON object with two fields:
In the callback function above, we don't do much with the new sensor value. We simply print it to the console. But you can imagine that we can do much more with it, for example control the A/C or the sun blinds of a greenhouse.
We can add a line of code and tell the sensor in which frequency we want to be updated:
In the example above, we set the callback interval to 5000 milliseconds or 5 seconds. This means our callback function will only be called every 5 seconds if there was a new value in that time interval.
Here is the complete code from this article:
The function getDevicesByIdentifier()
is part of the devices
object that we get back from the initialization process. We need to know the device identifier of the sensor and pass it to the function. You can , or you can simply take a look at the console, where all connected devices are listed:
Now that we have the sensor in our hands (or in our variable), we can tell the sensor to inform us about new values. The way we do that in programming is called the listener or . You can image it in this way: We tell the sensor: "Hey, let me know anytime there is a new value. You can do this by calling this function I give you". We call the function the sensor should call a callback function, and we must define the function accordingly: