Home Audience Developers A peek at four JavaScript based libraries for Internet of Things

A peek at four JavaScript based libraries for Internet of Things

0
11019

 

Connecting every ‘thing’ is the latest trend in the world of IT. A prime challenge in connecting heterogeneous things is the need for interoperable development tools. This article introduces readers to a few important JavaScript based libraries, frameworks and platforms associated with IoT development.

The Internet has definitely changed the way we carry out our day to day activities, be it something as simple as communicating with a friend, or the more critical applications 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 requirements in network protocols, security, programmability, 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 exponential growth in IoT, there are specialised 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 JerryScript 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 asynchronous code execution. It could be a great option for quick prototyping as well. The ability of JavaScript to provide a uniform interface across heterogeneous 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
  • JerryScript
  • IoT.js
  • Cyclon.js
Figure 1: JavaScript on devices

Favor

Favor is a simple and powerful library for IoT, which hides the complex and inconsistent interfaces provided by heterogeneous 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 documentation, if you wish to have a function like reading temperature from various heterogeneous devices without considering their hardware differences, using commands similar to what’s shown below, then Favor is the library that will assist you.

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

The Favor documentation 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 maintaining a configuration file. This consists of details describing the structure of the hardware. Favor queries this configuration file when communicating with the actual device. This functionality 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 $ functionality.

Similarly, Favor is able to exhibit jQuery style functionality in accessing the devices. As narrated earlier, any device with a temperature sensor will interact with the code segment shown below:

$$(‘temperature’).get(callback)

The advantages of using Favor are:

  • Uniform and simple API for communicating across various devices/protocols.
  • Clear demarcation between hardware and software, which increases the modularity of the application. It separates the business logic from the hardware.
  • The ‘Write Once – Run Anywhere’ feature makes it more suitable for a variety of application scenarios.
    Favor can be installed easily with Node.js, using the following command:
npm install favor

JerryScript

JerryScript is a lightweight engine for JavaScript. Its prime advantage is its ability to run in resource-constrained devices. JerryScript can run on devices with less than 64KB of RAM. It even supports devices with less than 200KB of Flash memory.

The major highlights of JerryScript are listed below (Figure 3):

  • Optimised for microcontrollers.
  • Code is actively updated at frequent intervals.
  • JerryScript is ECMAScript 5.1 standards compliant.
  • Ability to precompile the JavaScript source code to byte code.
  • Highly optimised for low memory consumption.
  • JerryScript is open source and available with the Apache License 2.0.
  • Written in C99 for maximum portability.

JerryScript was developed from scratch by the Samsung Open Source Group.

Figure 2: JavaScript for IoT
Figure 3: JerryScript features

IoT.js

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

  • The IoT.js platform uses JerryScript to execute JavaScript.
  • It uses libuv for asynchronous input and output, which is a multi-platform support library to enable asynchronous 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.
Figure 4: IoT.js

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 documentation, it supports 43 different platforms.

Getting started with Cyclon.js is straightforward. 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. connections: { 
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 cylon-firmata cylon-gpio cylon-i2c 
$ node script.js

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

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here