当前位置:网站首页>Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
2022-06-12 13:48:00 【TMS320VC5257H】
Abstract : This paper introduces the use of HaaS510 Receive data from serial port , Extract the required attribute values from the data , And send it to the IOT platform . The data format received from the serial port is JSON Format .

The experiment purpose : Through the upper computer to haas510 send out JSON character string , from HaaS510 Parse and extract the specified attribute value , And send it to the cloud platform .
Hardware :haas510 (2.0 edition ),USB A serial port
Software :win7 x64,vscode
Catalog
1. Introduction to key knowledge points
3. Observe the operation effect
1. Introduction to key knowledge points
In this experiment, the computer is used as the upper computer , Upper computer passes USB Serial port and HaaS510 Connect . If you use other microprocessors , As long as it can be sent JSON Format string , The effect is the same . The key knowledge points of the experiment are 2 individual .
First point , How to convert the data received by the serial port into a string . We know , Serial communication , yes 8 position 8 Receive data bit by bit , Equivalent to one by one 8 An array of bit data , This array is not a string at first , We can understand it as a series of hexadecimal data . So we need to restore this data to a string , And then we can parse it .
See documentation
Alibaba cloud development board HaaS510 Respond to UART Serial port instruction https://blog.csdn.net/youngwah292/article/details/120377994?spm=1001.2014.3001.5501
https://blog.csdn.net/youngwah292/article/details/120377994?spm=1001.2014.3001.5501
Second point , How to parse a string into key attribute values . This knowledge point is also explained in detail through documents .
Alibaba cloud development board HaaS510 analysis JSON data
2. Write source code
This article provides the source code directly
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 = 'gcCCCCCCu2';
var deviceName = 'ceCCCCCCC01';
var deviceSecret = 'eeSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSb3a9';
// Build Internet of things connection instances
var device;
var lightSwitch = 0;
// The test parses a json character string , The string of the serial port is not parsed here , Instead, it parses a fixed array of strings .
var json = '{"uid":"123456","beizhu":"5557589","shijian":"20210919"}';
var obj = JSON.parse(json);
// user uart
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('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;
// user uart send data
serial.write(msgbuf);
// The parsed content is shown below
console.log(obj);
console.log(obj.uid);
console.log(obj.beizhu);
console.log(obj.shijian);
/* 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);
// user uart data receive
serial.on('data', function(data) {
console.log('uart receive data is ' + ArrayToString(data));
json = ArrayToString(data);
// Then parse the string
obj = JSON.parse(json);
// Here are a few key attribute values to send json = '{"uid":"123456","beizhu":"5557589","shijian":"20210919"}'
device.postProps(
JSON.stringify({
uid:obj.uid,beizhu:obj.beizhu,shijian:obj.shijian
})
);
});
The key code is after the serial port receives the data , The operation of parsing data .
json = ArrayToString(data);
// Then parse the string
obj = JSON.parse(json);
// Here are a few key attribute values to send json = '{"uid":"123456","beizhu":"5557589","shijian":"20210919"}'
device.postProps(
JSON.stringify({
uid:obj.uid,beizhu:obj.beizhu,shijian:obj.shijian
})
);3. Observe the operation effect
First connect the hardware
After the source code provided in this experiment runs , There will be multiple experimental effects , As shown in the figure below . No more screenshots , Interested readers can check out my other HaaS510 article .

This experiment focuses on the 5 Experimental results , That is, send a message through the upper computer JSON character string , see HaaS510 Whether the value can be parsed and reported to the cloud in real time .

From the computer to HaaS510 Send data through the user serial port of
{"uid":"888888","beizhu":"666666","shijian":"20210920"}
It can be observed that this data is parsed , Through debugging, the serial port also has corresponding display .

The data has been uploaded to the cloud . remarks 、 Time 、uid、 The main light switches have been changed accordingly .

thus , Successful experiment .
Work here , You can use HaaS510 Designed an Internet of things device . Use one MCU Write functional code . Upload the data to the cloud to JSON Format sent to HaaS510, The attribute value reporting function can be realized . What about? , Easy !
If you still haven't mastered everything , Then I suggest you refer to other articles of the author .
Give people roses , Fragrance in hand .
边栏推荐
- Top 10 tips for visual studio code on Google
- When the byte jumps, the Chinese 996 is output in the United States
- Tensorrt, onnx to tensorrt in mmclas
- There was an error installing mysql. Follow the link below to CMD
- Install RPM package offline using yum
- 阿里云开发板HaaS510将串口获取数据发送到物联网平台
- Simple implementation of gpuimage chain texture
- [WUSTCTF2020]颜值成绩查询-1
- Codeforces 1629 C. Mexico array - simple greed
- Explanation of static and extern keywords
猜你喜欢
随机推荐
[wustctf2020] selfie score query -1
GPUImage链式纹理的简单实现
Octopus network progress monthly report | may 1-May 31, 2022
Automatic Generation of Visual-Textual Presentation Layout
Application of short circuit expression (||) in C language
xcode 调试openGLES
Cmake basic tutorial - 01 a-hello-cmake
Pytorch to onnx, onnxruntime reasoning in mmclas
高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法
Getting started with NVIDIA Jetson nano Developer Kit
Simple implementation of gpuimage chain texture
Comparator summary
Language skills used in development
颜色编码格式介绍
Does jupyternotebook have a Chinese character database. Can you recognize handwritten Chinese in deep learning
go-zero 微服务实战系列(二、服务拆分)
Innovation training (XII) project summary
Application of bit operation in C language
简述CGI与FASTCGI区别
一种快速创建测试窗口的方法


![2061: [example 1.2] trapezoidal area](/img/83/79b73ca10615c852768aba8d2a5049.jpg)




