项目委员会是:
Git文件:https://github.com/artikcloud/sample-iot-MonitorFlameTemp
var webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
var device_id = "<00840e8c030c4482b29c01bb431aa41ee>";
var device_token = "<9e973185912d4680b8cbaa34098c7f67>";
var isWebSocketReady = false;
var ws = null;
var serialport = require("serialport")
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyACM0", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
var WebSocket = require('ws');
/**
* Gets the current time in millis
*/
function getTimeMillis(){
return parseInt(Date.now().toString());
}
/**
* Create a /websocket bi-directional connection
*/
function start() {
//Create the websocket connection
isWebSocketReady = false;
ws = new WebSocket(webSocketUrl);
ws.on('open', function() {
console.log("Websocket connection is open ....");
register();
});
ws.on('message', function(data, flags) {
console.log("Received message: " + data + '\n');
});
ws.on('close', function() {
console.log("Websocket connection is closed ....");
});
}
/**
* Sends a register message to the websocket and starts the message flooder
*/
function register(){
console.log("Registering device on the websocket connection");
try{
var registerMessage = '{"type":"register", "sdid":"'+device_id+'", "Authorization":"bearer '+device_token+'", "cid":"'+getTimeMillis()+'"}';
console.log('Sending register message ' + registerMessage + '\n');
ws.send(registerMessage, {mask: true});
isWebSocketReady = true;
}
catch (e) {
console.error('Failed to register messages. Error in registering message: ' + e.toString());
}
}
/**
* Send one message to ARTIK Cloud
*/
function sendData(onFire){
try{
ts = ', "ts": '+getTimeMillis();
var data = {
"onFire": onFire
};
var payload = '{"sdid":"'+device_id+'"'+ts+', "data": '+JSON.stringify(data)+', "cid":"'+getTimeMillis()+'"}';
console.log('Sending payload ' + payload);
ws.send(payload, {mask: true});
} catch (e) {
console.error('Error in sending a message: ' + e.toString());
}
}
/**
* All start here
*/
start(); // create websocket connection
sp.on("open", function () {
sp.on('data', function(data) {
if (!isWebSocketReady){
console.log("Websocket is not ready. Skip sending data to ARTIK Cloud (data:" + data +")");
return;
}
console.log("Serial port received data:" + data);
var flameDigitalValue = parseInt(data);
// flameDigitalValue = 1 ==> no fire is detected
// flameDigitalValue = 0 ==> fire is detected
var onFire = false;
if (flameDigitalValue == 0) {
onFire = true;
}
sendData(onFire);
});
});
由于某种原因它无法正常工作我从这个网站获得了代码:https://www.artik.io/blog/2016/05/monitor-fire-temperature-using-artik-cloud-open-source-iot-hardware-android/
但是给我这个错误:
pi@raspberrypi:~ $ node index.js
/home/pi/index.js:12
parser: serialport.parsers.readline("\n")
^
TypeError: serialport.parsers.readline is not a function
at Object.<anonymous> (/home/pi/index.js:12:32)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
at startup (internal/bootstrap/node.js:201:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
我做错了什么?
答案 0 :(得分:0)
serialport模块(解析器实现)在版本4.x之后发生了变化。
您有2个选项。
a)降级您的serialport模块;如果您知道使用了哪个版本号(请询问GitHub项目的作者)
npm uninstall serialport
npm install serialport@<version>
b)将您的代码升级到串口模块的当前版本(解析器的使用),请参阅https://node-serialport.github.io/parsers/ReadLineParser.html
查看代码,您需要使用以下内容替换串口创建:
const SerialPort = require('serialport');
const Readline = require('parser-readline');
const sp = new SerialPort('/dev/ttyACM0', {
baudRate: 9600
});
const parser = sp.pipe(new Readline({ delimiter: "\n"}));
注意,在代码末尾需要替换
sp.on('data' ...
带
parser.on('data' ...
希望这有帮助。