A Peek at Four JavaScript Based Li­braries for the In­ter­net of Things

Con­nect­ing ev­ery ‘thing’ is the lat­est trend in the world of IT. A prime chal­lenge in con­nect­ing het­ero­ge­neous things is the need for in­ter­op­er­a­ble de­vel­op­ment tools. This ar­ti­cle in­tro­duces readers to a few im­por­tant JavaScript based li­braries, frame­work

OpenSource For You - - Contents - By: Dr K.S. Kup­pusamy The au­thor is an as­sis­tant pro­fes­sor of com­puter sci­ence at Pondicherry Cen­tral Univer­sity. He has 11+ years of teach­ing and re­search ex­pe­ri­ence in academia and in­dus­try. He can be reached at kskuppu@gmail.com.

The In­ter­net has def­i­nitely changed the way we carry out our day to day ac­tiv­i­ties, be it some­thing as sim­ple as com­mu­ni­cat­ing with a friend, or the more crit­i­cal ap­pli­ca­tions such as bank­ing and health­care. The ben­e­fits of con­nect­ing more and more de­vices to the In­ter­net, pop­u­larly known as the In­ter­net of Things (IoT), are man­i­fold. As an ex­am­ple, your key­chain can broad­cast its lo­ca­tion, help­ing you to find the key you lost by click­ing on an app on your smart­phone.

How­ever, as more and more things get con­nected, there are new re­quire­ments in network pro­to­cols, se­cu­rity, pro­gramma­bil­ity, etc.

JavaScript and IoT

JavaScript has be­come the de-facto client-side script­ing lan­guage of the Web. One can call JavaScript the ‘C of the Web’. There are mil­lions of Web de­vel­op­ers with JavaScript skills who can mi­grate with rel­a­tive ease to

IoT. More­over, with the ex­po­nen­tial growth in IoT, there are spe­cialised JavaScript frame­works that can use the po­ten­tial of con­nected de­vices.

JavaScript can be utilised for IoT in the fol­low­ing dif­fer­ent ways (Fig­ure 1):

One of the ap­proaches is to run JavaScript in the host com­puter and send the sig­nals to the client (things).

This mode is op­ti­mal for sce­nar­ios in which the ac­tual ‘things’ don’t have the po­ten­tial to run even the slimmest JavaScript code­base.

An­other ap­proach is to ex­e­cute the JavaScript code through me­mory op­ti­mised en­gines in the de­vice it­self. Frame­works such as Jer­ryScript may be used to run the de­vices.

In sce­nar­ios where sin­gle board computers can be used, JavaScript or Node.js code is ex­e­cuted in these de­vices with­out any is­sues.

JavaScript is suit­able for de­vices be­cause of its abil­ity to re­act to events and asyn­chro­nous code ex­e­cu­tion. It could be a great op­tion for quick pro­to­typ­ing as well.

The abil­ity of JavaScript to pro­vide a uni­form in­ter­face across het­ero­ge­neous de­vices is also a key fac­tor in choos­ing it for IoT.

There are plenty of JavaScript based frame­works/li­braries avail­able for IoT and other re­lated do­mains (Fig­ure 2).

This ar­ti­cle in­tro­duces four of these tools, which are listed be­low:

Fa­vor

Jer­ryScript

IoT.js

Cy­clon.js

Fa­vor

Fa­vor is a sim­ple and pow­er­ful library for IoT, which hides the com­plex and in­con­sis­tent in­ter­faces pro­vided by het­ero­ge­neous de­vices, from the app de­vel­oper. The sin­gle API pro­vided to ac­cess these de­vices makes it sim­ple for the de­vel­oper to con­trol var­i­ous de­vices.

As nar­rated in the of­fi­cial doc­u­men­ta­tion (https://github. com/fa­vor/it), if you wish to have a func­tion like read­ing tem­per­a­ture from var­i­ous het­ero­ge­neous de­vices with­out con­sid­er­ing their hard­ware dif­fer­ences, us­ing com­mands sim­i­lar to what’s shown be­low, then Fa­vor is the library that will as­sist you.

$$(‘tem­per­a­ture’).get(func­tion(temp) { con­sole.log(‘The tem­per­a­ture is ‘, temp) };

The Fa­vor doc­u­men­ta­tion uses the term hard­ware ag­nos­tic, which im­plies that de­vel­op­ers can write the busi­ness logic of the app with­out think­ing about the ac­tual hard­ware in which the code is go­ing to get ex­e­cuted.

Presently, Fa­vor sup­ports de­vices of Linux ori­gin such as Rasp­berry Pi, Bea­glebone, etc. How­ever, any Linux de­vice with the abil­ity to run Node.js can be used.

Fa­vor is able to achieve the hard­ware ag­nos­tic fea­ture by main­tain­ing a con­fig­u­ra­tion file. This con­sists of de­tails de­scrib­ing the struc­ture of the hard­ware. Fa­vor queries this con­fig­u­ra­tion file when com­mu­ni­cat­ing with the ac­tual de­vice. This func­tion­al­ity could be com­pared with how jQuery parses and in­ter­acts with the DOM of a Web page. Once the pars­ing is done, then jQuery is ca­pa­ble of ac­cess­ing any DOM el­e­ment with the sim­ple

$ func­tion­al­ity. Sim­i­larly, Fa­vor is able to ex­hibit jQuery style func­tion­al­ity in ac­cess­ing the de­vices. As nar­rated ear­lier, any de­vice with a tem­per­a­ture sen­sor will in­ter­act with the code seg­ment shown be­low:

$$(‘tem­per­a­ture’).get(call­back)

The ad­van­tages of us­ing Fa­vor are:

Uni­form and sim­ple API for com­mu­ni­cat­ing across var­i­ous de­vices/pro­to­cols.

Clear de­mar­ca­tion be­tween hard­ware and soft­ware, which in­creases the mod­u­lar­ity of the ap­pli­ca­tion. It sep­a­rates the busi­ness logic from the hard­ware.

The ‘Write Once - Run Any­where’ fea­ture makes it more suit­able for a va­ri­ety of ap­pli­ca­tion sce­nar­ios.

Fa­vor can be in­stalled easily with Node.js, us­ing the fol­low­ing com­mand:

npm in­stall fa­vor

A sim­ple but ef­fec­tive demo of a sam­ple ap­pli­ca­tion with Fa­vor is avail­able at https://www.youtube.com/ watch?v=ujHa-I3ZRUM.

If you want to ex­plore Fa­vor in de­tail, then visit the of­fi­cial Fa­vor wiki at https://github.com/fa­vor/it/wiki.

Jer­ryScript

Jer­ryScript is a light­weight en­gine for JavaScript. Its prime ad­van­tage is its abil­ity to run in re­source-con­strained de­vices. Jer­ryScript can run on de­vices with less than 64KB of RAM. It even sup­ports de­vices with less than 200KB of Flash me­mory (https://github.com/jer­ryscript-project/jer­ryscript).

The ma­jor high­lights of Jer­ryScript are listed be­low (Fig­ure 3):

Op­ti­mised for mi­cro­con­trollers.

Code is ac­tively up­dated at fre­quent in­ter­vals.

Jer­ryScript is ECMAScript 5.1 stan­dards com­pli­ant. Abil­ity to pre­com­pile the JavaScript source code to byte code.

Highly op­ti­mised for low me­mory con­sump­tion. Jer­ryScript is open source and avail­able with the Apache Li­cense 2.0.

Writ­ten in C99 for max­i­mum porta­bil­ity.

Jer­ryScript was de­vel­oped from scratch by the Sam­sung Open Source Group.

De­tailed instructions re­gard­ing pre-req­ui­sites and set­ting up are pro­vided at https://github.com/jer­ryscript-project/ jer­ryscript/blob/master/docs/01.GET­TING-STARTED.md.

IoT.js

IoT.js is also tar­geted to­wards re­source-poor de­vices; it is built on top of Jer­ryScript. Its fea­tures are:

The IoT.js plat­form uses Jer­ryScript to ex­e­cute JavaScript. It uses libuv for asyn­chro­nous in­put and out­put (https:// github.com/Sam­sung/libuv). libuv is a multi-plat­form sup­port library to en­able asyn­chro­nous I/O.

Presently, IoT.js sup­ports Linux and NuttX. The lat­ter is a real-time op­er­at­ing sys­tem.

The API pro­vides fea­tures to han­dle buf­fers, events, con­soles, GPIO, streams, timers, etc.

De­tailed in­for­ma­tion re­gard­ing app de­vel­op­ment us­ing IoT.js is avail­able at https://github.com/Sam­sung/iotjs.

Cy­clon

Cy­clon.js is a JavaScript frame­work to sup­port robotics, phys­i­cal com­put­ing and IoT. One of its ma­jor ad­van­tages is the sup­port for var­i­ous plat­forms. As per the of­fi­cial doc­u­men­ta­tion (https://cy­lonjs.com/) it sup­ports 43 dif­fer­ent plat­forms.

Get­ting started with Cy­clon.js is straight­for­ward. Just in­stall the npm mod­ule, as fol­lows:

npm in­stall cy­clon

A sam­ple code snip­pet to tog­gle an LED ev­ery three sec­onds is shown be­low:

var Cy­lon = re­quire(“cy­lon”); // Ini­tial­ize the robot Cy­lon.robot({ // Change the port to the cor­rect port for your Ar­duino. con­nec­tions: { ar­duino: { adap­tor: ‘fir­mata’, port: ‘/dev/ttyACM0’ }

}, de­vices: {

led: { driver: ‘led’, pin: 13 } }, work: func­tion(my) { ev­ery((3).sec­ond(), func­tion() { my.led.tog­gle(); }); } }).start();

To run the code, type:

$ npm in­stall cy­lon­fir­mata cy­lon­gpio cy­lon­i2c $ node script.js

Cy­clon.js can be ex­e­cuted from a browser with the help of the browser­ify mod­ule of npm.

Ref­er­ences

[1] https://www.postscapes.com/javascript-and-thein­ter­net-of-things/ [2] https://github.com/fa­vor/it [3] http://iotjs.net/ [4] https://github.com/Sam­sung/iotjs [5] https://github.com/jer­ryscript-project/jer­ryscript [6] https://cy­lonjs.com/ [7] ‘JavaScript on Things’, a book by L.D. Gard­ner

Fig­ure 4: IoT.js

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.