当前位置:网站首页>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 .
边栏推荐
- Application of binary search -- finding the square root sqrt of a number
- Codeforces 1637 A. sorting parts - simple thinking
- Binary tree traversal
- Django note 21: querying databases using native SQL
- 阿里云开发板HaaS510报送设备属性
- 一种快速创建测试窗口的方法
- Implementing singleton mode of database under QT multithreading
- Go zero micro Service Practice Series (II. Service splitting)
- FFmpeg 学习指南
- [wustctf2020] selfie score query -1
猜你喜欢
阿里云开发板HaaS510报送设备属性
Greed issues - Egypt scores
事件的传递和响应以及使用实例
A method of quickly creating test window
618 entered the second half of the period, apple occupied the high-end market, and the domestic mobile phones finally undercut the price competition
Scyther工具形式化分析Woo-Lam协议
阿里云开发板HaaS510响应UART串口指令
Qt5 plug-in production
When the byte jumps, the Chinese 996 is output in the United States
Codeforces 1637 D. yet another minimization problem - Mathematics, DP
随机推荐
Automatic Generation of Visual-Textual Presentation Layout
"Non" reliability of TCP
字节序数据读写
Understanding recursion
Cmake basic tutorial - 02 b-hello-cmake
Player screen orientation scheme
将字符串转为16进制字符串并显示出来
Seekg, tellg related file operations
2062: [example 1.3] movie tickets
C language array and pointer
Greed issues - Egypt scores
编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构
Cmake basic tutorial - 01 a-hello-cmake
[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX
一种快速创建测试窗口的方法
618进入后半段,苹果占据高端市场,国产手机终于杀价竞争
Application of short circuit expression (||) in C language
[brush title] probability of winning a draw
MySQL 查询 limit 1000,10 和 limit 10 速度一样快吗? 深度分页如何破解
事件的传递和响应以及使用实例