Internet of Things
TinkerforgeCode in ActionAbout Me
  • Course Outline
  • 1 - Getting Started
    • Overview
    • Connect to the LED
    • Getting Started
      • Glitch
      • The Application Template
    • Concepts in Programming
      • What is Programming?
      • Variables
      • Functions and Commands
      • Control Structures
      • Loops
      • Objects and Libraries
    • Programming Simple Web Apps
    • Exercises
      • 1.1 Buttons and Inputs
  • 2 - Internet of Things
    • Overview
    • IoT in our Apps
      • Getting Started
        • Hardware Kit
        • Brick Viewer and Daemon
      • Connect to the Devices
        • The Tinkerforge Device Manager
      • Program the Devices
        • RGB LED
        • RGB LED Button
        • OLED Display
        • Sensors
          • Humidity Sensor
          • Ambient Light Sensor
    • Components and Use Cases
    • Exercises
      • 2.1 Lights and Buttons
      • 2.2 Sensors
      • 2.3 Display
  • 3 - Artificial Intelligence
    • Overview
    • AI in our Apps
      • Google's Teachable Machine
      • Face Recognition
      • Training a Custom Model
    • Rules vs. Learning
    • Learning from Data
    • Use Cases
      • Computer Vision
        • Image Classification
        • Handwriting Recognition
    • Machine Learning Algorithms
      • Artificial Neural Networks
      • Decision Trees
      • Logistic Regression
    • Exercises
      • 3.1 Rules vs. Learning
      • 3.2 Fruits and Vegetables
      • 3.3 Face Recognition
      • 3.4 A Classifier for Iris
  • 4 - Cloud & APIs
    • Overview
    • APIs in our Apps
    • Cloud and APIs
      • Weather API
      • NASA Open APIs
      • EDAMAM Nutrition and Recipes API
    • Push Notifications
    • Exercises
  • 5 - App Project
    • Overview
    • Summer 2021
    • Summer 2022
  • Appendix
    • Other Devices
      • Motorized Linear Poti
      • Sound Pressure Sensor
      • NFC Reader
      • Motion Detector
    • UI Features
      • Realtime Charts
      • Countdown Timer
    • Digital Computers
      • Overview
      • The Binary System
      • Code Systems
      • Logic Gates
      • Binary Addition
      • From Analog to Digital
    • Cheat Sheets
    • Projects
      • IoT @ Pickup-Boxes
Powered by GitBook
On this page
  • General approach
  • Initialize devices
  • Getting and storing the sensor
  • Listen to sensor updates
  • Defining how often we want to be informed
  • Finished code

Was this helpful?

  1. 2 - Internet of Things
  2. IoT in our Apps
  3. Program the Devices

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.

PreviousOLED DisplayNextHumidity Sensor

Last updated 4 years ago

Was this helpful?

General approach

When programming sensors, we apply the same general approach:

  1. using the .

  2. Store a reference to the sensor on a .

  3. Register a listener function to get informed about new values from the sensor.

  4. Within the listener function, we can then work with the value we received.

Initialize devices

The following code connects to all devices on our local USB port:

// A global variable to store all devices
var devices;

// This initializes the connections
tf.initDevices(initDone);

// Call this when all connections are established
function initDone(connectedDevices) {
    // Store the devices on the global variable
    devices = connectedDevices;
}

Getting and storing the sensor

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:

// A global variable to store all devices
var devices;

// A global variable to store the sensor
var humiditySensor;

// This initializes the connections
tf.initDevices(initDone);

// Call this when all connections are established
function initDone(connectedDevices) {
    // Store the devices on the global variable
    devices = connectedDevices;
    
    // Get one specific device via its UID
    humiditySensor = devices.getDeviceByIdentifier(283);
}

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.

Listen to sensor updates

// Call this when all connections are established
function initDone(connectedDevices) {
    // Store the devices on the global variable
    devices = connectedDevices;
    
    // Get one specific device via its UID
    humiditySensor = devices.getDeviceByIdentifier(283);
    
    // We want to be informed when a new sensor value arrives
    humiditySensor.registerListener(humidityChanged);
}

function humidityChanged(val) {
  log(val.getValue());
}

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:

{
    "type": "temperature",
    "value": 2321
}

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.

There are sensors that send more than one value with this object. In this case we can pass an argument to the getValue() function to specify the value type we want.

Defining how often we want to be informed

We can add a line of code and tell the sensor in which frequency we want to be updated:

// We only want to be informed every 5 seconds
humiditySensor.setCallbackInterval(5000);

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.

Finished code

Here is the complete code from this article:

// A global variable to store all devices
var devices;

// A global variable to store the sensor
var humiditySensor;

// This initializes the connections
tf.initDevices(initDone);

// Call this when all connections are established
function initDone(connectedDevices) {
    // Store the devices on the global variable
    devices = connectedDevices;
    
    // Get one specific device via its UID
    humiditySensor = devices.getDeviceByIdentifier(283);
    
    // We want to be informed when a new sensor value arrives
    humiditySensor.registerListener(humidityChanged);
    
    // We only want to be informed every 5 seconds
    humiditySensor.setCallbackInterval(5000);
}

function humidityChanged(val) {
  log(val.getValue());
}

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:

look up the identifiers here
observer pattern
Connect to the sensor
Tinkerforge Device Manager
global variable