×
Community Blog Completing a Smart Home System with 30 Lines of JavaScript Code

Completing a Smart Home System with 30 Lines of JavaScript Code

This article explores the growth of the IoT trend and shows you how to build a smart home system with just 30 lines of code.

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.

1

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);

Temperature and Humidity Meter

var deviceShadow = require('deviceShadow');
var shtc1 = require('shtc1');
var handle = new shtc1('shtc1');
var ledHandle = GPIO.open("led");

deviceShadow.bindDevID({
  productKey: "a17vi82MmxP",
  deviceName: "0001",
  deviceSecret: "tYUngSMqYeDxODgtX3DNKkQ7920I3t4T"
});

function main(err) {
  if (err) {
    console.log("failed to connect to the platform");
  } else {
    console.log("the main program started");
    var mainLoop = setInterval(function () {
      var val = handle.getTempHumi();
      console.log('shtc1:temp=' + val[0] + ' humi:' + val[1]);
      deviceShadow.postProperty("CurrentTemperature", val[0]);
      deviceShadow.postProperty("RelativeHumidity", val[1]);
    }, 2000);
  }
}

deviceShadow.start(main);

Connect the device(Such as ESP32) to the computer, and click on "Connect" and "Run." The device automatically loads and runs index.js after startup and reports the data to the Alibaba Cloud IoT Platform at the same time.

Application Development

Systems cannot operate without applications. We can use the Visual Building function to quickly complete an application with zero code. Just drag in the dashboard and switch components in sequence, replace the picture, and bind the device to complete all operations.

2

The end-to-end smart home system built with just 30 lines of code is completed and can be shared with others after being saved and released.

With Embedded Javascript Tool and IoT Studio, developers can seamlessly and quickly start development of the IoT without learning a new programming language, demonstrating the inclusiveness and originality of the Alibaba Cloud IoT Platform.

1 1 1
Share on

GXIC

16 posts | 3 followers

You may also like

Comments

Ikhbar Keba March 1, 2019 at 11:35 pm

Ikhbar keba