Mqtt传输的停车位数据接收

Context

公司服务器 emqttd: mqtt代理服务器(负责接收并转发)
设备:发送mqtt数据包(topic+message)

接收程序

技术是nodejs脚本,借助mqtt和bitwise库实现。
传输方式是websocket,所以host地址填的是ws://:8083/mqtt, 8083是emqttd开放给websocket的端口。

var client = mqtt.connect(MQTT_ADDR, { clientId: 'BeCarefulexclusiveID', connectTimeout: 1000, debug: true }, (err) => {
    if (err) {
        console.log('Client established error');
    } else {
        console.log('Client established');
    }
});

如果协议中数据是按位分配字段,需要了解bitwise的使用:

var array_l = bitwise.readByte(data[3]);  //array_l is a Number(0/1) array, thus could be operated in bitwise layer

但是每一个Byte读出的8位数组需要拆分:

var msgType = MV.slice(0, 4); // get 0 ~3 four bits

Array2String:

msgType = msgType.join("");

上面的代码双引号表示数组每个元素间没有字符间隔 组成字符串

接着将字符串以2进制的方式转换为Int,如字符串”011“就是3

msgType = parseInt(msgType, 2);

实际情况 公司cai'y

你可能感兴趣的:(Mqtt传输的停车位数据接收)