当前位置:网站首页>Alibaba cloud development board haas510 submission device attributes

Alibaba cloud development board haas510 submission device attributes

2022-06-12 13:47:00 TMS320VC5257H

Abstract :HaaS510 The development board can use mobile network communication , So please wifi Signal dependence . It also supports JavaScript Language , Very reliable cloud operations are implemented with very simple statements . This article shares with you after the cloud , How to submit device attributes to the Internet of things platform .

  The experiment purpose : adopt haas510 interval 5 Report the switch attribute of the smart light once every second LightSwitch, Each flip .

Hardware :haas510 (2.0 edition ),USB A serial port

Software :win7 x64,vscode

Catalog

1. Development environment preparation

2. Internet of things cloud platform device registration

3. Connect hardware

​4. Write code

5. Running effect

6. Extended reading :JSON.stringify


1. Development environment preparation

If you have already configured based on vscode Of haas-studio development environment , Please skip this step .

without , Please refer to the article

Alibaba cloud development board vscode Development environment construction icon-default.png?t=L892https://blog.csdn.net/youngwah292/article/details/120356887?spm=1001.2014.3001.5501

2. Internet of things cloud platform device registration

If registration is complete , Please skip this step .

without , Please refer to the article

Alibaba cloud development board HaaS510 Connect to the Internet of things platform --HaaS Solicitation icon-default.png?t=L892https://blog.csdn.net/youngwah292/article/details/120358210?spm=1001.2014.3001.5501

3. Connect hardware

Connect as shown in the figure below

4. Write code

  The code is as follows . Please note that , The author also shows the wrong code , It is convenient for us to compare and learn .Interval The Chinese meaning is “ interval ; ( In time ) The gap ; intermission ”. Here we set the interval 5 Second .

every other 5 The second operation is to send a message through the debug serial port , This message is equivalent to an indicator light , Tell the developer , The program is running normally .

every other 5 Second ,510 The development board will also submit attributes to the Internet of things platform once . The name of the attribute is LightSwitch, The value is 0 perhaps 1. sentence lightSwitch = 1 - lightSwitch It means that it will be flipped before each submission .

var iot = require('iot');
var network = require('network');
var net = network.openNetWorkClient();
/*  For information about device triples, please refer to common In the catalog iot Component description  */
var productKey = 'gcXXXXXjX';
var deviceName = 'light001';
var deviceSecret = 'b25XXXXXXXXXXXXXXXXXXXXXXXXXXX6db';
//  Build Internet of things connection instances 
var device;
var lightSwitch = 0;
net.on('connect', function () {
    device = iot.device({
        productKey: productKey,
        deviceName: deviceName,
        deviceSecret: deviceSecret,
        region: 'cn-shanghai',
        success: function () {
            console.log('iot: [success] connect');
        },
        fail: function () {
            console.log('iot: [failed] connect');
        }
    });
})


setInterval(function () {
    lightSwitch = 1 - lightSwitch;

    console.log('Hello World, time: ' + new Date());

/*  Error model  iot.postProps({ payload: {LightSwitch:'0'} })*/
/* Correct demonstration , To send attributes , You have to use stringify Talent */
    device.postProps(
        JSON.stringify({
            LightSwitch: lightSwitch
        })
    );
  
  }, 5000);
  

  Be careful , We used a Method . If you don't use this method , For example, we use payload: {LightSwitch:'0'} Will cause syntax errors . As shown in the figure below , This screenshot comes from connecting to DEBUG UART Serial port assistant program .

 

  The way to correct this error is simple , It's called JSON.stringify. What does this method do ?

Its function is to apply the method to JavaScript Value to JSON character string .

Using this method , You can easily implement attribute reporting .

5. Running effect

Log in to the Internet of things platform , Observe the specified equipment ( This article is about light001) Object model data , You can see that the attribute is successfully submitted .

  Open the serial port assistant , Observe DEBUG UART Output , You can see the interval 5 Second display hello world character string . If there is an error , interval 5 Second , except helloworld, There are other error messages .

  The object model can also be refreshed in real time , So you can observe “ Main light switch ” attribute , interval 5 Every second . Successful experiment .

haas-studio A relatively complete temperature test program is provided iot-sensor(LM75 Temperature detection procedure ). This routine provides IIC Interface driver code . It's more complicated . For starters , The simpler the code, the easier it is to learn . Therefore, this article makes a simple example of regularly sending flip attributes . The procedure is simple , Highlight only the key issues .

Reporting attribute is a basic function , It is also the key content of the Internet of things . After making IOT devices , The device status can be reported to the cloud platform , It is a difficulty in the development of Internet of things equipment , When programming , Often make mistakes .

Especially after referring to Alibaba cloud's documentation , Students who don't know how to solve their mistakes , Mastering the skills described in this article is a very necessary job .

6. Extended reading :JSON.stringify

References in this part 6. Extended reading :JSON.stringify

JSON.stringify() Method is used to JavaScript Value to JSON character string .

The syntax is

JSON.stringify(value, replacer, space)

To convert JavaScript Values are usually objects or arrays . For example, we use bool Type data 0 perhaps 1.

If replacer For the function , be JSON.stringify This function will be called , And pass in the key and value of each member . Use the return value instead of the original value . If this function returns undefined, Then exclude members . The key of the root object is an empty string :"".

If replacer Is an array , Only the members with key values in the array are converted . Members are converted in the same order as keys in the array .

space Is text indented 、 Spaces and line breaks , If space It's a number , The return value text is indented by a specified number of spaces at each level , If space Greater than 10, The text is indented 10 A space .space You can also use non numbers , Such as :\t.

Here's an example JSON.stringify()  Detailed usage and running effect of .

var str = {
    "name":" Novice tutorial ", "site":"http://www.runoob.com"}
str_pretty1 = JSON.stringify(str)
document.write( " Case with only one parameter :" );
document.write( "<br>" );
document.write("<pre>" + str_pretty1 + "</pre>" );
 
document.write( "<br>" );
str_pretty2 = JSON.stringify(str, null, 4) // Indent with four spaces 
document.write( " Usage parameters :" );
document.write( "<br>" );
document.write("<pre>" + str_pretty2 + "</pre>" ); // pre  Used to format output 

The operation effect is shown in the figure below .

 

原网站

版权声明
本文为[TMS320VC5257H]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121345059255.html