Community Blog JavaScript IoT: Leveraging JavaScript for IoT Development

JavaScript IoT: Leveraging JavaScript for IoT Development

In this Javascript IoT blog, you will learn to extend objects with new prototypes and understand the concept of variables in Javascript for developing IoT platform.

Smart homes and many other Internet of Things (IoT) applications are rapidly gaining popularity this year, thanks to the rise of technologies such as 5G. More and more products are being labeled as "smart", such as smart watches and TVs, which shows that people have transformed the word "smart" into a way of life.

Common smart home solutions include the device (thing), the cloud, and the application. Some solutions also involve big data and AI. Traditional IoT development emphasizes the sequence; that is, data must be processed sequentially on the device, cloud, and application. Today, based on the "Thing Specification Language (TSL)" of the Alibaba Cloud IoT Platform, the two ends of the IoT development can run in parallel, significantly saving labor and material costs.

It is obviously tempting to run in parallel, but can we go one step further and let all development be handled by one person? The answer is yes! With all the technologies we have at our disposal, we can now easily design and build a smart home by ourselves, similar to building a website. By using the Alibaba Cloud IoT Platform, we can complete a smart home solution with 30 lines of code.

Currently, many Internet developers have stopped at the gate of the IoT due to lack of embedded development capabilities, such as C/C++ language basics. With Embedded Javascript Tool provided by the Alibaba Cloud IoT Platform, device development can be carried out quickly using JavaScript, which seamlessly resolves the biggest problem facing these developers. For embedded developers who are unfamiliar with front-end and back-end development, Alibaba Cloud IoT Platform also provides the "Visual Building" application and other quick-start functions for application development with zero code, greatly reducing the learning required.

In this article, we will use the Embedded Javascript Tool and the Visual Building function of the Alibaba Cloud IoT Development Platform to quickly develop smart home systems that consist of lighting and temperature and humidity meters, with just 30 lines of code.

Enabling the Service

First, apply for an Alibaba Cloud account, then enable and log in to the one-stop development platform, IoT Studio.

Next, Choose Create Project (you can use any name you like) > Device Development > Add Product > Category. Select "Lighting" or "Temperature & Humidity Meter" as required, select Wi-Fi for the communication method, and select Alink for the data format.

Choose Device Development > Add Debugging Devices, and note the device trituples.

Device Development

Open the embedded JavaScript online workbench (the development environment does not need to be built) and create a new project. Replace the index.js code:


var deviceShadow = require('deviceShadow');
var ledHandle = GPIO.open("led1");

  "productKey": "123",
  deviceName: "",
  deviceSecret: ""

function main(err){
    console.log("failed to connect to the platform");
    console.log("the main program started");
    deviceShadow.addDevSetPropertyNotify("LightSwitch", function (lightStatus) {
      GPIO.write(ledHandle, 1-lightStatus);
    var mainLoop = setInterval(function () {
      var ledStatus = GPIO.read(ledHandle);
      deviceShadow.postProperty("LightSwitch", 1-ledStatus);
    }, 2000);


You can review Completing a Smart Home System with 30 Lines of JavaScript Code to get the following code.

Related Blogs

Implementing an IoT Network Using Only JavaScript

This article provides briefly explains how embedded software developers can build an Internet of Things (IoT) network using JavaScript on Alibaba Cloud's IoT platform. This tutorial is ideal for developers with front-end experience, who typically prefer JavaScript over C and Java.


This example uses Developer Kit (with AliOS Things ported) as the development hardware to implement data uploading from a board-mounted temperature sensor to Link Develop, and uses Web App for demonstration.

Development Process

Configuring the Environment

AliOS Things is an IoT-oriented and highly scalable IoT operating system under developed by Alibaba Cloud's IoT team. To meet front-end developers' requirements, AliOS Things integrates BoneEngine, a Java engine that supports JavaScript for IoT development, which was previously only implemented in C and C++. Meanwhile, BoneEngine simplifies the syntax, allowing embedded devices with limited resources to run JavaScript applications as well.

You need to configure the environment before you use BoneEngine to perform device-side development. The configuration steps are shown as follows:

  1. Install Node.js (version 6.4.0 or later; for front-end development, version 8.9.0 or later is recommended).
  2. Install the be-cli tool to provide support for updating app.bin via serial port or over the network: `
  3. i be-cli -g -P

. If you are still prompted that the command cannot be found even when you run
after the preceding global installation is done, run the
npm prefix -g

3. (For Windows users only) Install Python, Visual Studio, and the AliOS plug-in. Then install [Hardware USB driver](https://www.silabs.com/documents/public/software/CP210x_Windows_Drivers.zip).
4. Install Git and download AliOS Things source code: ```
git clone git@github.com:alibaba/AliOS-Things.git

Now that the environment is configured, we are ready to begin development.

###[JavaScript Variables, Scope, and Hoisting](https://www.alibabacloud.com/blog/javascript-variables-scope-and-hoisting_594080)

This tutorial seeks to examine the differences between var, let, and const and also the way to declare them, name and define what exactly they are.

It is mandatory to understand the concept of variables if you are seeking to learn any programming language. In fact, it is recommended that newbies emphasis on this one aspect to grasp the basics of coding. JavaScript variables have wide-ranging properties as well as rules governing their naming. In particular, var, let, and const are the three keywords to be used in declaring JavaScript variables. The anticipated interpretation of the variable will depend on how these keywords are used. This tutorial seeks to examine the differences between var, let, and const and also the way to declare them, name and define what exactly they are. The other thing covered in this tutorial are hoisting impacts, local and global scope effects on how variables behave.

What Are Variables?
In simple terms, variables are containers that store values. For instance, if we want to store some information that we refer to several times, we could store it in a variable to access it later or modify it. We could store a number, string, or object, or any other data types in JavaScript variable containers. Modern JavaScript is based on the ECMAScript 2015 (ES6) language specification, which expanded variable declarations from the var keyword that was the only one available initially. That is why you will only find the var in older learning resources. In this tutorial, we have dedicated an entire section to explore the differences between var, let, and const keywords below. We shall use just one type o define the variable concept by declaring it and then assigning a value to it:

// Assign the string value Sam to the username identifier
var username = "sam_shark";

###[Prototypes and Inheritance in JavaScript](https://www.alibabacloud.com/blog/prototypes-and-inheritance-in-javascript_594078)

In this article, we will discuss JavaScript object prototypes, their inheritance and chains, as well as learn how constructors are used to extend objects with new prototypes.

JavaScript is a language that uses prototypes very widely to share methods and properties of objects. As a consequence, it is very easy to clone and extend generalized objects, a method that is referred to as prototypical inheritance. Notably, it is a very different technique from the class inheritance and should not be confused. Most object-oriented programming languages such as PHP, Python, and Java are class-based, a stark difference from the prototypical inheritance of JavaScript. Those languages usually have object blueprints for classes. This article is dedicated for the understanding of JavaScript object prototypes, their inheritance and chains. You will also learn how constructors are used to extend objects with new prototypes.

##Prototypes in JavaScript
It is assumed that you already are acquainted with object data types in JS, creating objects and how to modify object properties. Prototypes allow us to extend objects based on the fact that each JavaScript object includes [[Prototype]] as an internal property. 

JavaScript initially searches on an object whenever you attempt to access its property or method, and if not found, it then proceeds to in its [[Prototype]]. If there still are no matches for the property at the [[Prototype]], JavaScript then tries checking from the linked object's prototype until all objects in the chain are all covered n the search. Each chain contains Object.prototype at the end, whereby, there is an inheritance of methods and properties from Object. Searching beyond the chain always returns a null output. In our case above we have an empty object x, that bears inheritance from Object.

##Related Products
###[Simple Application Server](https://www.alibabacloud.com/product/swas)

A single server-based service for application deployment, security management, O&M monitoring, and more

Provides a wide selection of popular images to choose from (WordPress, LAMP, etc.)

###[Elastic Compute Service](https://www.alibabacloud.com/product/ecs)

Deploy ECS instances with just a few clicks from the easy-to-use console and scale capacity up or down based on real-time demands.

##Related Courses
###[JAVA Coding Standards in Alibaba Group](https://edu.alibabacloud.com/certification/clouder_java)

As one of the most popular programming languages, Java's usage in cloud application development is also very high. This certification exam is based on Alibaba's Java Coding Guidelines.

###[Network Series Courses](https://edu.alibabacloud.com/certification/clouder_network)

After learning this network series courses, the trainees will have a entry level understanding of basic relevant conceps, then knowing the routing and switching knowledge system, load balancing working theory and widely used network security solutions, thus having a comprehensive understanding of computing network knowledge.

##Related Documentation
###[Add signatures on the client by using JavaScript and upload data to OSS](https://www.alibabacloud.com/help/doc-detail/31925.html)

This topic describes how to use JavaScript to add signatures on the client based on the POST policy and then upload data to OSS by using form upload.

###Usage notes
After you use JavaScript to add signatures on the client, you can directly upload data to OSS. However, your AccessKey ID and AccessKey secret may be exposed because they are included in the JavaScript code. We recommend that you add signatures on the server to upload data. For more information, see [Add signatures on the server for object upload](https://www.alibabacloud.com/help/doc-detail/31926.htm).

The application server code provided in this topic supports protocols such as HTML4, HTML5, Flash, and Silverlight. Ensure that your browser supports these protocols. If a prompt such as "Your browser does not support Flash, Silverlight, or HTML5!" appears, upgrade your browser.

###[Introduction to ApsaraDB for Memcache SDK for NodeJS](https://www.alibabacloud.com/help/faq-detail/38612.html)

Alibaba Cloud SDK for NodeJS is a unified entry for NodeJS developers to use various Alibaba Cloud services. This SDK is developed and maintained by the Alibaba Cloud UED team. Currently, this SDK integrates three Alibaba Cloud services: ApsaraDB for Memcache, OSS, and RDS (MySQL). Code address: https://github.com/aliyun-UED/aliyun-sdk-js.

By using ApsaraDB for Memcache SDK for NodeJS, you can easily access ApsaraDB for Memcache. After obtaining Alibaba Cloud instances through a trial or purchase, you can obtain the corresponding ocsKey and ocsSecret. 

##Related Market Products
###[Node.js powered by Websoft9(Ubuntu16.04)](https://marketplace.alibabacloud.com/products/56680003/cmjj016795.html)

Websoft9 Node.js stack is a pre-configured, ready to run image for running Node.js application on Alibaba Cloud.Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

###[Hadoop Cluster Installation on Alibaba Cloud ECS](https://marketplace.alibabacloud.com/products/201076001/sgcmgj024698.html)

0 0 0
Share on

Alibaba Clouder

2,084 posts | 484 followers

You may also like