×
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:

Lighting

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

deviceShadow.bindDevID({
  "productKey": "123",
  deviceName: "",
  deviceSecret: ""
});

function main(err){
  if(err){
    console.log("failed to connect to the platform");
  }else{
    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);
  }
}

deviceShadow.start(main);

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.

Overview

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.

Prototypes and Inheritance in JavaScript

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.

Inheritance

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

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

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

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

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

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.

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

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)

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

Through this course, you will learn about SSH protocol, the installation process of Java and Hadoop on ECS, the construction process of Hadoop cluster, and the use of Hadoop.

0 0 0
Share on

Alibaba Clouder

2,599 posts | 762 followers

You may also like

Comments