Yoctopuce now supports Node.js

Yoctopuce now supports Node.js

Good news for Node.js users: we are introducing today a new version of our library, dedicated to this environment. Node.js is an easy-to-install environment that provides an interesting alternative to PHP, and known to be more efficient. You can now use it to drive your Yoctopuce devices as well, including in HTTP Callback mode.





Node.js is primarily a command-line tool based on Google Javascript runtime to run applications implemented using the Javascript language. It comes with a package manager named 'npm' that provides easy access to many powerful libraries. Together, these two simple tools make it really easy to run network applications and web services on all kind of hardware. You can even find free and paid Node hosting providers for your web applications.

Here is a Node.js example that logs every 5 seconds the temperature measured by all sensors connected to the YoctoHub-Ethernet whose IP address is 192.168.1.89:

var yoctolib = require('yoctolib');
var YAPI = yoctolib.YAPI;
var YTemperature = yoctolib.YTemperature;

// Connect to our YoctoHub-Ethernet
YAPI.RegisterHub('http://192.168.1.89');

setInterval( function() {
    var sensor = YTemperature.FirstTemperature()
    while(sensor) {
        console.log(sensor.get_friendlyName() + ": " +
                    sensor.get_currentValue() + " °C")
        sensor = sensor.nextTemperature();
    }
}, 5000); // every 5000[ms]



To try it, proceed as follow:

  1. In the directory of your choice, type: npm install yoctolib
  2. Save the code above into a file named test.js
  3. To run the code, type: node test.js

Simple, isn't it ? You will find similar examples showing how to use each one of our sensors and actuators in the directory example of our Node.js library, available on our libraries page and on GitHub.

The sample code works with any temperature sensor connected to a YoctoHub-Ethernet
The sample code works with any temperature sensor connected to a YoctoHub-Ethernet



If you are familiar with our library for other programming languages, you have probably noted that this library uses the same idioms and object names as all other languages. There is however something a bit different with Node.js: Node.js inherits from Javascript the non-blocking I/O model that makes it very lightweight... but also sometime quite painful to use. For any time-consuming operation, you should normally use an asynchronous function that will invoke a callback when completed. This tends to split procedural code into less readable event-driven chunks, but ultimately... you get used to it (sigh).

In the example above we did not have to do it, as some synchronous functions do still exist in Node.js. But in some cases, you will have to use them. One such case is when you want to implement a web service, and to use our library in HTTP Callback mode (as demonstrated with our past RSS reader and Automation gateway examples). Here is a simple web service example that receive temperature measurements from any YoctoHub-Ethernet configured to send HTTP Callback to it on port 8044:

var http     = require('http');
var yoctolib = require('yoctolib');
var YAPI     = yoctolib.YAPI;
var YTemperature = yoctolib.YTemperature;

// Instantiate a simple HTTP Server
http.createServer(function (message, response) {
    // Here you can filter the requests by URL if you want
    console.log('received '+message.method+' request for '+message.url);

    // Code below starts the library in HTTP Callback mode and interacts
    // with modules connected on the Hub that made the HTTP request
    YAPI.RegisterHub_async('http://callback/', function(ctx, retcode, errmsg) {
        if(retcode == YAPI.SUCCESS) {
            // Log all temperatures reported by Hub
            YAPI.UpdateDeviceList();
            var sensor = YTemperature.FirstTemperature()
            while(sensor) {
                console.log(sensor.get_friendlyName() + ": " +
                            sensor.get_currentValue() + " °C")
                sensor = sensor.nextTemperature();
            }
        }
        // Returns the API to its original state and close the connection
        YAPI.FreeAPI();
    }, null, message, response);
}).listen(8044);

console.log('Node.js temperature logger running on port 8044');



Note the use of RegisterHub_async instead of RegisterHub, required in HTTP callback mode. In this case, all other library functions can be used in the standard synchronous version, but for other applications you may want (or need) to use for instance sensor.get_currentValue_async() instead of the blocking sensor.get_currentValue(). In the Node.js library, all blocking functions are available in _async version.

The source code of our Node.js library is currently fully written in Javascript. It actually shares most of its code with the standard Javascript library. For this reason, it cannot connect directly to local USB devices without using the VirtualHub. It is however possible that in the future we add this functionality to the Node.js version, using a wrapper to our C library, as initiated some time ago by a Yoctopuce user.

Last but not least: a big Thanks to @mrose17 from @TheThingSystem, who initiated this port of the Javascript library to Node.js, and whose expertise in Node.js made it possible to publish this new library in such a short time.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.