当前位置:网站首页>Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform
Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform
2022-06-12 13:47:00 【TMS320VC5257H】
Abstract : Alibaba cloud HaaS510 Development board to 4G Network form networking , Can get rid of wifi rely on , So it can be widely used in the development of all kinds of Internet of things devices . As a module specially used to communicate with Alibaba cloud Internet of things platform , It has a basic and powerful function is to pass the data or instructions obtained through the serial port 4G The network sends it to the Internet of things platform , adopt JavaScript In very short code .

The experiment purpose : adopt haas510 Receive the serial port instructions from the upper computer , Send it as the attribute value of the IOT device .
Hardware :haas510 (2.0 edition ),USB A serial port
Software :win7 x64,vscode
Catalog
1. Common Internet of things platforms JavaScript Method
2. Report device attributes json String handling methods
3. Write the report attribute source code
4. Observe the experimental effect
1. Common Internet of things platforms JavaScript Method
Common methods are “ Escalation properties ”“ Report the incident ”, And then there is “ Close instance ”“ Register callback function ” etc. .

This experiment uses postProps Method . The function of this method is to report device attributes . Parameter only 1 individual , It's attribute json Format string . This is in a fixed format , If the format is incorrect, it cannot be reported normally . Generally, it is the attribute name -- The colon --- Property value , But it has to be handled , Please continue to read this article for specific handling methods .

2. Report device attributes json String handling methods
Suppose our device attribute is text type , The identifier is beizhu.

So a standard attribute string is {"beizhu":"xxxxxxxx"}
Use JavaScript When a program reports this property , You cannot predict in advance what data the serial port receives , Therefore, a complete string cannot be directly sent to the IOT platform .
The correct way is to get the serial port data , And half a string . The specific code is
JSON.stringify({beizhu:ArrayToString(data)})data Is the data received by the serial port , adopt stringify Repeatedly convert the string including curly braces into a complete string json String .
json What is it? ?
JSON = JavaScript Object Notation, Some translated as JS Object shorthand
It can be seen from the literal meaning that it is JavaScript My son ,JavaScript It is supported by default .
This means that JavaScript In dealing with JSON Data doesn't need any special API Or toolkit .
This experiment uses stringify Method , Because it's native , So the amount of code is very small .
3. Write the report attribute source code
This article directly contributes the debugged source code , You can copy and paste .
var iot = require('iot');
var network = require('network');
var uart = require('uart');
var net = network.openNetWorkClient();
/* For information about device triples, please refer to common In the catalog iot Component description */
var productKey = 'gcXXXXXvu2';
var deviceName = 'cewSSSSS001';
var deviceSecret = 'ee213aSSSSSSSSSadcdedb3a9';
// Build Internet of things connection instances
var device;
var lightSwitch = 0;
// Open the user serial port , stay HaaS in , The serial port number of the user is UART2
var serial = uart.open({
id: 'UART2'
});
var msgbuf = [97, 98, 99, 100];
function ArrayToString(fileData) {
var dataString = "";
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i]);
}
return dataString;
}
//net.on It is used to connect the IOT platform with triple data , The core is to implement a MQTT client
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');
}
});
})
// Timing procedure , This program implements 5 Second timing , Turn the status of the main light switch every time , Send an array of data to the user serial port
// Then send the main light switch status to the Internet of things platform
setInterval(function () {
lightSwitch = 1 - lightSwitch;
// user uart send data
serial.write(msgbuf);
/* 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);
// serial.on Used to respond to serial port data receiving events , What does the user receive through the serial port , Just send notes to the IOT platform through the debugging serial port .
// Be careful , The two serial ports are not the same .
serial.on('data', function(data) {
console.log('uart receive data is ' + ArrayToString(data));
device.postProps(
JSON.stringify({
beizhu: ArrayToString(data)
})
);
});4. Observe the experimental effect
The hardware connection is shown in the figure below

The experimental results are presented in the following aspects .
First, the device will be online , This is net.on Realized
Secondly, the serial port of the user will be timed 5 Seconds to send 4 Characters ( It's actually an array ) To the upper computer , This is setInterval Realized .

Then when the upper computer sends a string through the serial port ,HaaS510 This string will be sent to the Internet of things platform , As beizhu The value of the property .

thus , Successful experiment .
One deficiency is that : When the Internet of things devices are designed only 1 When there are two attributes , In this way, the serial port data can be directly sent to the Internet of things platform . But in the actual R & D work , The attributes of each device cannot have only one attribute , It often has many properties . If you simply report the serial port data as a string , Obviously, it is not what a mature device should have .
therefore , We also need to parse the data sent from the serial port , Extract the value of each attribute , And then send it to the cloud platform .
This is what we need to do next .
边栏推荐
- Multi source BFS problem template (with questions)
- Convert the string to hexadecimal string and display it
- 阿里云开发板HaaS510连接物联网平台--HaaS征文
- Application of short circuit expression (||) in C language
- Formal analysis of Woo Lam protocol with scyther tool
- 1004: character triangle
- 一种快速创建测试窗口的方法
- Paw advanced user guide
- VGA display color bar and picture (FPGA)
- Innovation training (XI) summary of some bugs in the development process
猜你喜欢

一种快速创建测试窗口的方法

【mysql进阶】索引分类及索引优化方案(五)

Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem

Record some settings for visual studio 2019

There was an error installing mysql. Follow the link below to CMD

Innovation training (x) advanced interface beautification

简述CGI与FASTCGI区别

数据类型转换和条件控制语句

通过loganalyzer展示数据库中的日志

Application of list and Dict
随机推荐
Innovation training (XI) summary of some bugs in the development process
【mysql进阶】mysql索引数据结构的演变(四)
Paw 高级使用指南
Codeforces 1629 D. pecuriar movie preferences - simple thinking, palindrome strings
Codeforces 1637 A. sorting parts - simple thinking
m1 pod install pod lint 失败解决方案
AVFoundation
一种快速创建测试窗口的方法
How to solve the problem of data table query error when SQLite writes the registration function?
Web3.0,「激发创造」的时代
Codeforces 1637 C. Andrew and stones - simple thinking
Application of short circuit expression (||) in C language
Recursion of subviews of view
Innovation training (x) advanced interface beautification
Acwing: topology sequence
【mysql进阶】查询优化原理与方案(六)
Code debugging - print log output to file
jsp跳转问题,不能显示数据库数据,并且不能跳转
Symbolic constant, const qualifier
jupyternotebook有汉字数据库吗。在深度学习中可以识别手写中文吗