Tuesday, 4 December, 2018 UTC


Today’s post is by Ziran Sun from Samsung Research UK as part of our JS Devs Zone blog series, which highlights technical tutorials and thought leadership on JS Foundation technologies and the greater JavaScript ecosystem written by outstanding members of the JavaScript community.
Are you a JavaScript developer? Have you ever thought of applying your JavaScript skills beyond the web and exploring its potentials in the exciting and fast growing Internet of Things (IoT) world? What about starting to building a simple end-to-end IoT system, from constrained devices to a secure gateway?
At this year’s Node + JS Interactive, I presented a talk on how to build your own Internet of Things using JavaScript. Now let’s walk through this.
A Simple IoT Demo Using JavaScript

The Demo

Imagine that you just arrived home on a late dark night. Rather than searching for the light switches on the wall, would you like to turn on the light by simply clapping your hands? The demo shown below pretty much does what we described here – control your lights via hand claps.
In this demo, we have –
  • Raspberry Pi Zero board as the end device.  
  • PIMORONI Blinkt! Light, a super slim line Raspberry Pi add-on board with eight super-bright RGB LED indicators. The light is mounted onto Raspberry Pi Zero board via GPIO pins.
  • Mozilla Things Gateway. The gateway has a clap-sensing add-on that takes audio inputs, e.g. sound of hand claps in this case.
  • Platform (a laptop PC or a Raspberry Pi 3) to run The Things Gateway framework.
The demo video is also available here if you are interested.

Why JavaScript?

This demo application was written fully with JavaScript. So why JavaScript?
  • Popularity. There’s a huge pool of JavaScript developers. According to StackOverflow’s annual survey, for the fifth year in a row, JavaScript is the most popular language with 62.5% of respondents claiming to use it.
  • Maturity – Javascript and frameworks around it are maturing.
  • Productivity: The language itself is pretty much platform and OS independent and there are a vast number of easy to use 3rd-party libraries available. These factors result in less time to introduce your products to markets.
  • It all sounds good! Bear in mind though that JavaScript is a scripting language. We can see it works in web, on servers. What about in the IoT, which has a lot deeply embedded or constrained devices?
On Constrained Device
“Constrained devices” here we are referring small devices with limited CPU, memory, and power resources. IoT is about to connect billions of “Things” devices, whose memory footprints could spread from 2KB to 1GB or over.  In our work, we are looking at having a JavaScript engine run on an IoT device, which does require a reasonable amount of memory. Devices with RAM size under 32KB are probably out of the picture in this case.
In the demo, we have introduced JerryScript to the end device.
  • JerryScript is an ultra-lightweight JavaScript engine, which can run on a device with RAM under 64KB and ROM under 200 KB.
  • JerryScript was created by Samsung from scratch in 2014.
  • The JerryScript project is now under the umbrella of JS Foundation and was one of the JS Foundation’s very first sponsored projects.  
At the moment there two leading IoT JS platforms based on JerryScript engine: IoT.js by Samsung and Zephyr.js by Intel. Both platforms are to provide JavaScript APIs to developers. One core goal of IoT.js and Zephyr.js is to bring the success of Node.js into the embedded world and retain backward compatibility with Node.js as much as possible. Comparing with IoT.js, Zephyr.js is more platform specific for the moment while hardware and OS supports in IoT.js are very much in line with JerryScript. To understand more on IoT.js and JerryScript, you can check our previous blog on IoT.js Architecture.  
The demo used IoT.js as the IoT platform shown below. With the IoT.js GPIO module in place, it allows us to access the Blinkt Light directly via GPIO JavaScript APIs.
Connect Device to Secure Gateway
For gateway, we have chosen The Things Gateway by Mozilla, an open gateway that anybody can now create with a Raspberry Pi to control Internet of Things devices. The Things Gateway has provided privacy and security features such as TLS encrypted tunneling service and third-party applications authorization using the de-facto authorization standard OAuth 2.0 etc. A more detailed discussion on these security features can be found in our previous blog here.
The backend of The Things Gateway uses the Web Things API from Mozilla IoT, to communicate with the front end. Web Things API is a proposal by Mozilla with “Web Thing Description” to describe things, “Web Thing REST API” and “Web Thing WebSocket API” to interact with ‘Things’.
In the demo, Blinkt! light is the “thing” for The Things Gateway and we need to describe Blinkt! Light following the “Web Things Description” format: 
 "name":"Blinkt! light",
 "type": "thing",
 "Description": "A Blinkt! light",
 "properties": {
   "on": {
     "type": "boolean",
     "description": "Whether the Blinkt! light is turned on",
     "href": "/things/blinkt/properties/on"
 "actions": {
   "toggle": {
     "description": "Turn the Blinkt! Light on and off"
My colleague, Phil Coval, has published “WebThing-IotJS“, an IoT.js implementation of WebThing API based on Mozilla-IoT’s webthing-node. It provides a baseline to add your own Web Thing on top of IoT.js. You can take Blinkt! Light Web Thing code as an example to try it out.
The Things Gateway has provided a rules engine. Through the rules engine, we can set rules between the clap-sensing add-on and Blinkt! Light webthing. In this demo, the Blinkt! Light state changes when the clap-sensing add-on detects an incoming hand-clapping sound.
There is a lot more you can do…
We have shown you an example on how to build a simple end to end IoT system and walked through technologies behind the scenes. Are you ready to give it go? We’d love to hear your stories on building IoT systems using JavaScript!
The post Building Your Own Internet of Things Using JavaScript appeared first on JS Foundation.