開發與維運

HaaS輕應用(JavaScript)- DTU功能示例

來源 | HaaS技術社區

1、示例簡介

在HaaS610開發板上把用戶串口接收到的數據,通過物模型把數據上傳到阿里雲物聯網平臺。本示例適合實時性要求不高、數據量不大的應用場景。

1.1、連接示例

image.png

通過USB轉TLL串口線(3.3V ),把PC與HaaS610的用戶串口(上圖序號 8處)連接起來。

1.2、流程

在PC上通過串口調試工具發送數據,HaaS610 Kit收到數據後觸發uart監聽事件。HaaS610 Kit在uart監聽事件回調函數中把數據通過MQTT發送到物聯網平臺指定Topic。

2、示例代碼

app.js

var iot = require('iot');
var network = require('network');
var uart = require('uart');
 
var net = network.openNetWorkClient();
var serial3 = uart.open({
    id: 'serial3'
});
 
function ArrayToString(fileData){
    var dataString = "";
    for (var i = 0; i < fileData.length; i++) {
      dataString += String.fromCharCode(fileData[i]);
    }
    return dataString;
}
 
serial3.on('data', function(data) {
    var data_str = ArrayToString(data);
    console.log('serial3:' + data_str);
 
    /* post props */
    device.postProps('{\"serial3\": ' + '\"' + data_str + '\"' + '}');
 
});
 
var productKey = 'xxxx';      /* your productKey */
var deviceName = 'xxxx';    /* your deviceName */
var deviceSecret = 'xxxx';    /* your deviceSecret */
 
var device;
var topic = '/sys/' + productKey + '/' + deviceName + '/user/haas/info';
 
function createDevice() {
    device = iot.device({
        productKey: productKey,
        deviceName: deviceName,
        deviceSecret: deviceSecret,
    });
 
    device.on('connect', function () {
        console.log('(re)connected');
 
        /* 雲端設置屬性事件 */
        device.onProps(function (res) {
            console.log('cloud req msg_id is ' + res.msg_id);
            console.log('cloud req params_len is ' + res.params_len);
            console.log('cloud req params is ' + res.params);
        });
 
        /* 雲端下發服務事件 */
        device.onService(function (res) {
            console.log('received cloud msg_id is ' + res.msg_id);
            console.log('received cloud service_id is ' + res.service_id);
            console.log('received cloud params_len is ' + res.params_len);
            console.log('received cloud params is ' + res.params);
        });
    });
 
    /* 網絡斷開事件 */
    device.on('disconnect', function () {
        console.log('disconnect ');
    });
 
    /* mqtt消息 */
    device.on('message', function (res) {
        console.log('mqtt message')
        console.log('mqtt topic is ' + res.topic);
        console.log('mqtt payload is ' + res.payload);
    })
 
    /* 關閉連接事件 */
    device.on('end', function () {
        console.log('iot client just closed');
    });
 
    /* 發生錯誤事件 */
    device.on('error', function (err) {
        console.log('error ' + err);
    });
}
 
var status = net.getStatus();
console.log('net status is: ' + status);
 
if (status == 'connect') {
    createDevice();
} else {
    net.on('connect', function () {
        createDevice();
    });
}

app.json

{
    "version": "1.0.0",
    "io": {
      "serial3":{
        "type":"UART",
        "port":2,
        "dataWidth":8,
        "baudRate":9600,
        "stopBits":1,
        "flowControl":"disable",
        "parity":"none"
      }
    },
    "debugLevel": "DEBUG",
    "repl": "enable"
}

開發者支持

如需更多技術支持,可加入釘釘開發者群,或者關注微信公眾號。

image.png

更多技術與解決方案介紹,請訪問HaaS官方網站https://haas.iot.aliyun.com

Leave a Reply

Your email address will not be published. Required fields are marked *