Alibaba Cloud HaaS100 IoT development board study notes (6) Be a smart light --- a complete development example

Abstract: This article combines several topics in the previous period and designs and produces a smart light based on the new firmware of Alibaba Cloud HaaS100. This smart light consists of a cloud platform, a mobile APP terminal and a device terminal, and basically covers the main steps required for a small IoT project.

table of Contents

1. Register the device on the Alibaba Cloud IoT platform

2. Write HaaS100 light application and burn it

3. Use IoT Studio to design and control mobile pages

4. Use android studio to design Android APP

5. Test

6 Conclusion

 

Reference article:

1. Basic steps for beginners to get started with Alibaba Cloud Haas100 development board

2. How to adapt the source code of Alibaba Cloud haas100 development board to python3.7 version

3. Alibaba Cloud HaaS100 IoT development board study notes (1) Introduction to hardware resources

4. Alibaba Cloud HaaS100 IoT development board study notes (2) Preliminary hardware control-let the small lights flash

5. Alibaba Cloud HaaS100 IoT development board study notes (3) Preliminary light application-use js to make the lights flash

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

7. Alibaba Cloud HaaS100 IoT development board study notes (5) Steps to push js code-based on the updated firmware

8. What is Aliyun amp tool?

9. How to add a new device to the Alibaba Cloud IoT platform

10. Introduction to the Internet of Things Control APP (1) --- Introduction to several modes of making Internet of Things APP

11. The topic of getting started with the Internet of Things control APP (2) --- Basic operation of the mobile visualization function of Alibaba Cloud iot studio

12. Introduction to the Internet of Things Control APP (3) --- Use a third-party platform to package web pages into APP

13. The topic of getting started with the Internet of Things control APP (4) --- Use android studio to make an APP framework for the control page

15. The topic of getting started with the Internet of Things control APP (5) --- Use android studio to directly write the Internet of Things control APP


Hardware platform: HaaS100.

Software platform: win7 x64, amp-win, notepad++, vscode, IoT Studio

 

1. Register the device on the Alibaba Cloud IoT platform

Detailed steps light reference: How to add a device to the Alibaba Cloud IoT platform

Pay attention to the difference between "product" and "equipment". When a new product is added, the "attribute" of this type of product will be set, and the new "equipment" under this type of product will have the same "product attribute" no matter how much it is added. ". After the "equipment" is successfully added, there will be a "triple" of equipment. When debugging the equipment of the Internet of Things, sometimes the "quadruple" is used. In any case, these strings represent the "ID card" of a special device. With the "ID card", it can prove that there is only one "I" in the world to walk legally on the rivers and lakes, right?

In particular, new devices registered on the Aliyun Life Internet of Things platform (Feiyan platform) can also be viewed and managed on the Aliyun Internet of Things platform.

2. Write HaaS100 light application and burn it

With the virtual "things" of the Alibaba Cloud IoT platform ---devices, it is necessary to make a real device in the real world and make it correspond to the devices on Alibaba Cloud.

In order for real devices to correspond with Alibaba Cloud virtual devices, there are several prerequisites: one is to be connected to the Internet, whether it is wifi or gprs; the other is to comply with the same communication rules, and Alibaba Cloud uses mqtt (in fact, IBM back then I didn’t expect this stuff to become an international standard in the true sense of today’s Internet of Things); the third is to comply with certain encryption rules. The MQTT client has certain encryption rules to ensure that Internet of Things devices (not a simple Small lights may be control devices on large machinery) safety and reliability; fourth, they have the same "attributes", for example, virtual devices have "switch" attributes, and this real device must also have a corresponding "switch" response program; Fifth, it has the same "identity card" as the virtual device-that is, triple or quadruple data.

To achieve the above five points, it can be achieved through a variety of hardware. This article is based on the super convenient HaaS100 Internet of Things development board, using the "light application" method to develop.

Light application is an IoT development technology developed by the Alibaba Cloud IoT team, which can be developed with JavaScript.

The light application source code consists of two files, app.js and app.json. The js file is the business logic entry, which can be understood as the source code; the json file is the global configuration, which can be understood as the hardware resource configuration, using the popular json format.

The source code of the js file of this article is as follows:

/* iot - 阿里云IoT连接平台连接组件示例,产品功能定义:
 * [功能类型] ------ [功能名称] ------ [标志符] ------ [数据类型]
 *   属性            主灯开关        LightSwitch      bool (0-关闭 1-开启) 
 *   服务            开关翻转     ToggleLightSwitch   -
 *   事件            故障上报           Error         参数标志符:ErrorCode
 * */

var iot = require('iot');
var network = require('network');
var gpio = require('gpio');
var net = network.openNetWorkClient();

var productKey = 'a19x7NT4nEn';
var deviceName = 'haas-ltv1';
var deviceSecret = 'e22735c2d28da99c1c9b85**********';

var lightSwitch = 0;

var led1 = gpio.open({
    id: 'LED3',
    success: function() {
        console.log('gpio: open led success')
    },
    fail: function() {
        console.log('gpio: open led failed')
    }
  });

var device;

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

    device.on('connect', function () {
        console.log('(re)connected');
    });
    
    /* 网络断开事件 */
    device.on('disconnect', function () {
        console.log('disconnect ');
    });
    
    /* 关闭连接事件 */
    device.on('close', function () {
        console.log('iot client just closed');
    });
    
    /* 发生错误事件 */
    device.on('error', function (err) {
        console.log('error ' + err);
    });
    
    /* 云端设置属性事件 */
    device.on('props', function (payload) {
        console.log('cloud req data is ', payload);
        console.log('LightSwitch ', payload.LightSwitch ? 'ON' : 'OFF');
        /**/
        if (payload.LightSwitch == 1) {
        led1.writeValue(1);
        device.postProps({
            payload: {
                LightSwitch: 1
            }
        })
    }
    if (payload.LightSwitch == 0) {
        led1.writeValue(0);
        device.postProps({
            payload: {
                LightSwitch: 0
            }
        })
    } 
    });
    
    /* 云端下发服务事件 */
    device.on('service', function (id, payload) {
        console.log('received cloud serviceid is ' + id);
        console.log('received cloud req_data is ' + payload);
    });
}

function onConnect() {
    setInterval(function () {
        /** post properties */
        /*device.postProps({
            payload: {
                LightSwitch: 0
            },
            success: function () {
                console.log('postProps success');
            },
            fail: function () {
                console.log('postProps failed');
            }
        });*/
        /** post events */
        /*device.postEvent({
            id: 'Error',
            params: {
                ErrorCode: 'error'
            },
            success: function () {
                console.log('postEvent success');

            },
            fail: function () {
                console.log('postEvent failed');
            }
        });*/
    }, 3000);
}

var status = net.getStatus();

if (status == 'disconnect') {
    net.on('connect', function () {
        console.log('========wifi connected========');
        createDevice();
      });
} else {
    createDevice();
}

net.connect({
  ssid: 'xiao****',
  password: 'li*****'
});

Special reminder, please use suitable editing software to edit, and make sure the file encoding format is UTF-8. The wrong one may be unsuccessful. . .

The code of the json file used in this article is as follows:

{
    "version": "1.0.0",
    "io": {"LED3": {
      "type": "GPIO",
      "port": 36,
      "dir": "output",
      "pull": "pulldown"
}},
    "debugLevel": "ERROR"
  }
  

After the source code is edited, use the amp tool to push.

For specific steps, please refer to: Alibaba Cloud HaaS100 IoT Development Board Study Notes (5) Steps to push js code-based on the updated firmware

After downloading the amp-win tool, you can call teminal through vscode to operate, which is more convenient than the command line operation under win7.

 

3. Use IoT Studio to design and control mobile pages

An IoT device without a mobile phone application is incomplete. The Alibaba Cloud IoT platform provides mobile visualization operation functions, which is the "mobile visualization" of IoT studio.

A few months ago, this function also supported online generation of app installation packages. Later, this function was revised. The current version supports the generation of "page version" apps. After the successful release, you can write it through a third party or by yourself using android studio An app framework, just let the framework open is the designed page.

Specific steps can refer to

Introduction to the Internet of Things Control APP (2) --- Basic operation of the mobile visualization function of Alibaba Cloud iot studio

The picture below is a screenshot of the design process. The iot studio mobile visualization development provides mobile APP design in a nearly zero-code manner. This function is currently in public beta, and some functions have limitations.

If this page is published, the public IP of the domain name is required to access it normally. Add the domain name in iot studio (it is required to have been successfully filed, this process is relatively slow, it may take about 20 days), click publish.

This development method has an advantage. You only need to modify the page in iot studio, click Publish, and then visit this page to become the latest page.

4. Use android studio to design Android APP

The presentation form of the mobile visualization development of Alibaba Cloud Internet of Things iot studio is just a page that can be accessed through a domain name. For example, in the figure below, there is a switch control on the page. Click this control to control the HaaS100 device, and open or close the development board. Specify LED lights. By accessing this page through a mobile browser, you can control the real device, but this method is not suitable for real project development.

If you want to truly control IoT devices in APP mode, you can package this page into an APK installation package through a third-party platform. If you know Android Studio, you can also write your own framework.

This kind of framed APP theoretically does not need to be upgraded after installation-only the page needs to be updated. After publishing, the latest page will be automatically displayed next time it is opened. After all, APP is a browser with a fixed address.

For specific steps, please refer to the following two articles.

Introduction to the Internet of Things Control APP (3) --- Use a third-party platform to package web pages into APP

Internet of Things control APP entry topic (4) --- Use android studio to make a control page APP framework

5. Test

The purpose of the test is to control the on and off of LED3 on the HaaS100 development board through the mobile APP. The corresponding attribute is "LightSwitch".

After testing, LED3 can be turned on and off through the mobile APP.

6 Conclusion

Alibaba Cloud HaaS100 IoT Development Board is a very good board, suitable for beginners and professional developers. The workmanship of the board is also very good. Some wifi projects can be directly fixed to the target shell for use. The power supply is very stable and can work for a long time. This article uses a simple case to explain step by step how to make an IoT smart light that can be controlled by a mobile phone APP. It seems simple, but you still need to conscientiously overcome every possible difficulty in the development process. Once mastered, it will become a development weapon.

 

The following is a detailed step-by-step introduction of some basic operations, suitable for getting started, and you can skip it directly if you have the basics.

1. Basic steps for beginners to get started with Alibaba Cloud Haas100 development board

2. How to adapt the source code of Alibaba Cloud haas100 development board to python3.7 version

3. Alibaba Cloud HaaS100 IoT development board study notes (1) Introduction to hardware resources

4. Alibaba Cloud HaaS100 IoT development board study notes (2) Preliminary hardware control-let the small lights flash

5. Alibaba Cloud HaaS100 IoT development board study notes (3) Preliminary light application-use js to make the lights flash

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

7. Alibaba Cloud HaaS100 IoT development board study notes (5) Steps to push js code-based on the updated firmware

8. What is the Aliyun amp tool?

9. How to add a new device to the Alibaba Cloud IoT platform

10. Introduction to the Internet of Things Control APP (1) --- Introduction to several modes of making Internet of Things APP

 

11. The topic of getting started with the Internet of Things control APP (2) --- Basic operation of the mobile visualization function of Alibaba Cloud iot studio

12. Introduction to the Internet of Things Control APP (3) --- Use a third-party platform to package web pages into APP

13. The topic of getting started with the Internet of Things control APP (4) --- Use android studio to make an APP framework for the control page

15. The topic of getting started with the Internet of Things control APP (5) --- Use android studio to directly write the Internet of Things control APP

 

Welcome

 

Guess you like

Origin blog.csdn.net/youngwah292/article/details/112194632