OpenSource For You

A Peek at Four JavaScript Based Libraries for the Internet of Things

Connecting every ‘thing’ is the latest trend in the world of IT. A prime challenge in connecting heterogene­ous things is the need for interopera­ble developmen­t tools. This article introduces readers to a few important JavaScript based libraries, framework

- By: Dr K.S. Kuppusamy The author is an assistant professor of computer science at Pondicherr­y Central University. He has 11+ years of teaching and research experience in academia and industry. He can be reached at kskuppu@gmail.com.

The Internet has definitely changed the way we carry out our day to day activities, be it something as simple as communicat­ing with a friend, or the more critical applicatio­ns such as banking and healthcare. The benefits of connecting more and more devices to the Internet, popularly known as the Internet of Things (IoT), are manifold. As an example, your keychain can broadcast its location, helping you to find the key you lost by clicking on an app on your smartphone.

However, as more and more things get connected, there are new requiremen­ts in network protocols, security, programmab­ility, etc.

JavaScript and IoT

JavaScript has become the de-facto client-side scripting language of the Web. One can call JavaScript the ‘C of the Web’. There are millions of Web developers with JavaScript skills who can migrate with relative ease to

IoT. Moreover, with the exponentia­l growth in IoT, there are specialise­d JavaScript frameworks that can use the potential of connected devices.

JavaScript can be utilised for IoT in the following different ways (Figure 1):

One of the approaches is to run JavaScript in the host computer and send the signals to the client (things).

This mode is optimal for scenarios in which the actual ‘things’ don’t have the potential to run even the slimmest JavaScript codebase.

Another approach is to execute the JavaScript code through memory optimised engines in the device itself. Frameworks such as JerryScrip­t may be used to run the devices.

In scenarios where single board computers can be used, JavaScript or Node.js code is executed in these devices without any issues.

JavaScript is suitable for devices because of its ability to react to events and asynchrono­us code execution. It could be a great option for quick prototypin­g as well.

The ability of JavaScript to provide a uniform interface across heterogene­ous devices is also a key factor in choosing it for IoT.

There are plenty of JavaScript based frameworks/libraries available for IoT and other related domains (Figure 2).

This article introduces four of these tools, which are listed below:

Favor

JerryScrip­t

IoT.js

Cyclon.js

Favor

Favor is a simple and powerful library for IoT, which hides the complex and inconsiste­nt interfaces provided by heterogene­ous devices, from the app developer. The single API provided to access these devices makes it simple for the developer to control various devices.

As narrated in the official documentat­ion (https://github. com/favor/it), if you wish to have a function like reading temperatur­e from various heterogene­ous devices without considerin­g their hardware difference­s, using commands similar to what’s shown below, then Favor is the library that will assist you.

$$(‘temperatur­e’).get(function(temp) { console.log(‘The temperatur­e is ‘, temp) };

The Favor documentat­ion uses the term hardware agnostic, which implies that developers can write the business logic of the app without thinking about the actual hardware in which the code is going to get executed.

Presently, Favor supports devices of Linux origin such as Raspberry Pi, Beaglebone, etc. However, any Linux device with the ability to run Node.js can be used.

Favor is able to achieve the hardware agnostic feature by maintainin­g a configurat­ion file. This consists of details describing the structure of the hardware. Favor queries this configurat­ion file when communicat­ing with the actual device. This functional­ity could be compared with how jQuery parses and interacts with the DOM of a Web page. Once the parsing is done, then jQuery is capable of accessing any DOM element with the simple

$ functional­ity. Similarly, Favor is able to exhibit jQuery style functional­ity in accessing the devices. As narrated earlier, any device with a temperatur­e sensor will interact with the code segment shown below:

$$(‘temperatur­e’).get(callback)

The advantages of using Favor are:

Uniform and simple API for communicat­ing across various devices/protocols.

Clear demarcatio­n between hardware and software, which increases the modularity of the applicatio­n. It separates the business logic from the hardware.

The ‘Write Once - Run Anywhere’ feature makes it more suitable for a variety of applicatio­n scenarios.

Favor can be installed easily with Node.js, using the following command:

npm install favor

A simple but effective demo of a sample applicatio­n with Favor is available at https://www.youtube.com/ watch?v=ujHa-I3ZRUM.

If you want to explore Favor in detail, then visit the official Favor wiki at https://github.com/favor/it/wiki.

JerryScrip­t

JerryScrip­t is a lightweigh­t engine for JavaScript. Its prime advantage is its ability to run in resource-constraine­d devices. JerryScrip­t can run on devices with less than 64KB of RAM. It even supports devices with less than 200KB of Flash memory (https://github.com/jerryscrip­t-project/jerryscrip­t).

The major highlights of JerryScrip­t are listed below (Figure 3):

Optimised for microcontr­ollers.

Code is actively updated at frequent intervals.

JerryScrip­t is ECMAScript 5.1 standards compliant. Ability to precompile the JavaScript source code to byte code.

Highly optimised for low memory consumptio­n. JerryScrip­t is open source and available with the Apache License 2.0.

Written in C99 for maximum portabilit­y.

JerryScrip­t was developed from scratch by the Samsung Open Source Group.

Detailed instructio­ns regarding pre-requisites and setting up are provided at https://github.com/jerryscrip­t-project/ jerryscrip­t/blob/master/docs/01.GETTING-STARTED.md.

IoT.js

IoT.js is also targeted towards resource-poor devices; it is built on top of JerryScrip­t. Its features are:

The IoT.js platform uses JerryScrip­t to execute JavaScript. It uses libuv for asynchrono­us input and output (https:// github.com/Samsung/libuv). libuv is a multi-platform support library to enable asynchrono­us I/O.

Presently, IoT.js supports Linux and NuttX. The latter is a real-time operating system.

The API provides features to handle buffers, events, consoles, GPIO, streams, timers, etc.

Detailed informatio­n regarding app developmen­t using IoT.js is available at https://github.com/Samsung/iotjs.

Cyclon

Cyclon.js is a JavaScript framework to support robotics, physical computing and IoT. One of its major advantages is the support for various platforms. As per the official documentat­ion (https://cylonjs.com/) it supports 43 different platforms.

Getting started with Cyclon.js is straightfo­rward. Just install the npm module, as follows:

npm install cyclon

A sample code snippet to toggle an LED every three seconds is shown below:

var Cylon = require(“cylon”); // Initialize the robot Cylon.robot({ // Change the port to the correct port for your Arduino. connection­s: { arduino: { adaptor: ‘firmata’, port: ‘/dev/ttyACM0’ }

}, devices: {

led: { driver: ‘led’, pin: 13 } }, work: function(my) { every((3).second(), function() { my.led.toggle(); }); } }).start();

To run the code, type:

$ npm install cylonfirma­ta cylongpio cyloni2c $ node script.js

Cyclon.js can be executed from a browser with the help of the browserify module of npm.

References

[1] https://www.postscapes.com/javascript-and-theinterne­t-of-things/ [2] https://github.com/favor/it [3] http://iotjs.net/ [4] https://github.com/Samsung/iotjs [5] https://github.com/jerryscrip­t-project/jerryscrip­t [6] https://cylonjs.com/ [7] ‘JavaScript on Things’, a book by L.D. Gardner

 ??  ??
 ??  ?? Figure 4: IoT.js
Figure 4: IoT.js

Newspapers in English

Newspapers from India