Alibaba Cloud HaaS100 IoT Development Board Study Notes (4) Preliminary Light Application-Use javascript to connect to Alibaba Cloud IoT platform

Abstract: This article explains how to use JavaScript "light application" to connect to the Alibaba Cloud IoT platform and report a piece of data. The structure of the previous articles is still continued, starting from the installation of the software environment, so that after reading this article, students with zero foundation can also know how to do each step.

table of Contents

Purpose

1 HaaS100 light application firmware operation steps

2. New test equipment steps for Alibaba Cloud Life IoT Platform

2.1 New project

2.2 New product

2.3 Function definition

2.4 Payment

2.5 View the "three elements" of cloud devices

3. Transfer javascript program to HaaS100 development board

3.1 Download the amp tool

3.2 Connect the USB serial port module

3.3 Modify the app.js and app.json files.

3.4 Compile the program

4. View the IoT cloud platform log

5 Conclusion


 


Hardware: HaaS100 IoT development board

Software: amp-win (command line version), aos-cube (command line version), win7 x64 system

Purpose

Use the "light application" method to program the HaaS100 development board to report attributes to the Alibaba Cloud IoT platform. The following figure is a screenshot of the log after the report is successful.

 

1 HaaS100 light application firmware operation steps

The purpose of the following 6 steps is to set up a programming environment to start burning the firmware that supports "light application" operation. This firmware is burned through the USB serial port that comes with HaaS100 (the Micro USB interface in the upper left corner of the board).


There are the following steps. For details, please refer to the link: Alibaba Cloud HaaS100 IoT Development Board Study Notes (3) Preliminary Light Application-Use js to make the lights flash


1.1 Set up aos-cube programming environment

1.2 Learn about the light application operating mode

1.3 Use aos-cube to compile light applications and run firmware

1.4 Burn into HaaS100 development board

 

The sign of successful programming is to short-circuit the GPIO47 pin with 3.3V. After pressing the reset button (short key handle), the LED2 in the double-row indicator light flashes intermittently.

At this time, the development board is in a state of waiting to receive the js program.

After this step, don't worry about writing the js program, because the cloud platform is not ready, and some settings are needed to make the "cloud" and "board" communicate successfully.

 

2. New test equipment steps for Alibaba Cloud Life IoT Platform

A new test device is added to the Alibaba Cloud Life Internet of Things platform, then there will be a "thing" corresponding to the HaaS100 development board in the cloud. This "thing" has several attributes. When the HaaS100 development board reports an attribute upwards , The “things” in the cloud have made corresponding data changes.

Let's look at the specific steps:

2.1 New project

We first log in to the " Alibaba Cloud Life Internet of Things Platform " and add a new project, with a name determined by ourselves, such as "haas life test" here.

During the writing of this article, the life IoT platform has been revised! ! ! It seems to be integrated with the Alibaba Cloud IoT platform.

Select the workbench and enter the IoT device management

 

2.2 New product

Category selection electrician lighting---light

The node type is directly connected to the device, the networking mode is wifi, and the data format is ICA standard data format.

After confirming, it prompts that the device can be added. In other words, you have defined your own product, but you have not yet established a "shadow" in the cloud for the real product.

2.3 Function definition

Select "Go to Defined Object Model".

The function definition is to make the "things" in the cloud have the same function description as the real IoT products, and to name each attribute. For example, "lights" can have attributes such as mode, brightness, type, and even color.

If your product has more attributes and functions, you can customize and add corresponding functions.

This step does not make any changes, just for testing purposes.

Then add the device. In the product, select the type of device you want to add from the drop-down menu. If you choose any device, then this device has the above-mentioned function definition.

2.4 Payment

The "things" on each cloud platform, just like the "things" in the real world, have value attributes, and each "thing" also requires a certain payment.

Purchase through the purchase interface and pay. If you use it in large quantities, please participate in Ali's ongoing corporate discount promotion activities.

 

 

2.5 View the "three elements" of cloud devices

The cloud device, that is, the "thing", has a digital "ID". This certificate has 3 pieces of information, namely ProductKey, DeviceName and DeviceSecret, just like a person has an address, name, and ID number. However, for communication security, the data transmission of the Internet of Things uses encrypted transmission, and this ID number is encrypted.

 

 

You can view the information of this ID card by selecting "View" on the device page.

You can see the following 3 important parameters, namely ProductKey, DeviceName and DeviceSecret.

After registering this "ID card" on the Alibaba Cloud IoT platform, only devices that also have this ID card are allowed to correspond to it. So to a certain extent, the "things" set up on the Alibaba Cloud IoT platform are actually like the "shadows" of devices in the real world in the cloud.

 

Remember this "ID card", and then burn the 3 key information inside the IoT development board, then this development board will correspond to the "shadow" of the Alibaba Cloud IoT platform.

So how to burn the ID card into the development board? Please see the next step.

 

3. Transfer javascript program to HaaS100 development board

Before there is no javascript "light application" development model, it is necessary to build a more complicated compilation and support environment. To implement this seemingly simple step, a lot of time and experience are required.


You can look at https://blog.csdn.net/youngwah292/article/details/104124314 to understand some knowledge of MQTT and the complex intermediate process.


Now, all this is simple.

The last article introduced in detail how to use the command line version of the amp tool to transfer the js program to the development board. You can refer to the link https://blog.csdn.net/youngwah292/article/details/109540717

This article only introduces the basic steps, by default everyone has mastered the use of the amp tool.

3.1 Download the amp tool

Please click the blue font below to download. Or click to download from the official Alibaba Cloud document, and unzip it to my document directory after downloading. C:\Users\Administrator\amp-win

Alibaba Cloud officially recommends light application command line tools

3.2 Connect the USB serial port module

Connect the wires as shown in the figure below

The physical connection diagram is as follows:

After connecting the development board, the "light application" preparation work is completed, and then start writing the "light application" program

3.3 Modify the app.js and app.json files.

The source code of the "light application" is distributed in two files.

App.js and app.json in the C:\Users\Administrator\amp-win\app directory.

This program is to provide networking and report attribute functions, app.js is the javascript source file.

The app.js file code is as follows

var iot = require('iot');
var network = require('network');

var net = network.openNetWorkClient();
//下面这些就是阿里云生活物联网平台注册的“灯”的“身份证”信息

var productKey = 'a19xxxxxxxx';      /* your productKey */
var deviceName = 'haas-ltv1';      /* your deviceName */
var deviceSecret = 'e22735xxxxxxxxxxxxxxxxxxxxxcc2';  /* your deviceSecret */

var device;

function createDevice() {
        device = iot.device({
        productKey: productKey,
        deviceName: deviceName,
        deviceSecret: deviceSecret,
        region: 'cn-shanghai',
        success: function () {
            console.log('iot: [success] connect');
            onConnect();
        },
        fail: function () {
            console.log('iot: [failed] connect');
        }
    });

    device.on('connect', function () {
        console.log('iot: [success] iot.on(\'connect\')');
    });
    
    /* 网络断开事件 */
    device.on('disconnect', function () {
        console.log('iot: [success] iot.on(\'disconnect\')');
    });
    
    /* 关闭连接事件 */
    device.on('close', function () {
        console.log('iot: [success] iot.on(\'close\')');
    });
    
    /* 发生错误事件 */
    device.on('error', function (err) {
        throw new Error('iot: [failed] iot.on(\'error\') ' + err);
    });
    
    /* 云端设置属性事件 */
    device.on('props', function (payload) {
        console.log('iot: [success] iot.on(\'props\'), payload: ' + JSON.stringify(payload));
    });
    
    /* 云端下发服务事件 */
    device.on('service', function (id, payload) {
        console.log('iot: [success] iot.on(\'service\'), id: ' + id + ', payload: ' + JSON.stringify(payload));
    });
}


var lightSwitch = 0;
function onConnect() {
    /** post properties */
    lightSwitch = 1 - lightSwitch;
    device.postProps({
        payload: {
            LightSwitch: 'lightSwitch'
        },
        success: function () {
            console.log('iot: [success] iot.postProps');
        },
        fail: function () {
            console.log('iot: [failed] iot.postProps');
        }
    });
    /** post events */
    device.postEvent({
        id: 'Error',
        params: {
            ErrorCode: 0
        },
        success: function () {
            console.log('iot: [success] iot.postEvent');

        },
        fail: function () {
            console.log('iot: [failed] iot.postEvent');
        }
    });
}

var status = net.getStatus();
console.log('net status is: ' + status);

if (status == 'connect') {
    createDevice();
} else {
    net.on('connect', function () {
        createDevice();
    });
}

//下面的ssid和password就是你要连接的wifi用户名和密码,手机怎么连上的,你就怎么填
net.connect({
	ssid:'TP-LINK_3XXB',
	password:'zXX000XXX'
});

Note that you have added your own wifi information at the end of the file, so that you can connect to the Internet immediately after the burning is completed.

At the top of the file, three key parameters are written, which is the "ID card" information.

The app.json file is relatively simple, with only a few sentences.

{
    "version": "1.0.0",
    "io": {},
    "debugLevel": "DEBUG"
}

 

3.4 Compile the program

The amp software is needed to compile "light application" programs. This software is invoked via the command line.

In step 3.1, the amp tool has been downloaded and decompressed to the C:\Users\Administrator\amp-win folder. The amp tool is the one selected by the red box in the figure below. It cannot be opened directly by double-clicking, you need cmd to enter the command line and then run it.

Open the windows command line

Use the cd command to enter the amp tool

 

Then check the serial port, note that there are two serial ports, select the one corresponding to the USB serial port

The two instructions used are:

The first command is to display the current serial port number

amp seriallist

The second command is to burn with the serial number corresponding to the USB serial port

amp serialput app com14

When programming, you need to confirm whether the LED light flashes intermittently, if not, please confirm whether GPIO47 is short-circuited at 3.3V, if short-circuited, then press the reset button again.

Only when the LED light flashes intermittently, can it be confirmed that the "light application" firmware has been successfully run, so as to wait for the arrival of the js program.

When the following interface appears, the programming is successful.

After the programming is successful, disconnect the connection between GPIO47 and 3.3V, let the development board be in normal operation, and then press the reset button once to start the program.

After this series of steps, the program is running, but how to confirm that it successfully communicates with the Alibaba Cloud IoT platform? You also need to log in to the background to view the log.

 

4. View the IoT cloud platform log

Recently, the life Internet of Things platform has been revised. Refer to the diagram below to select Internet of Things device management.

Select the device menu, you can see that the designed product is already online.

Check the log, you can see the message from the device to the cloud.

View the details, you can observe the sent messages in more detail.

5 Conclusion

The HaaS100 development board is connected to the Internet via wifi and is developed in a "light application" way, which can be faster and more efficient.

 

During the writing of this article, the life IoT platform was revised. It seems to be integrated with the Internet of Things platform, the interface is more unified and easier to operate.

There are also easter eggs, please find time to write about it later.

 

 

 

 

Guess you like

Origin blog.csdn.net/youngwah292/article/details/109542585
Recommended