gpt4 book ai didi

json - 传感器数据未上传至 artik 云

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:07 27 4
gpt4 key购买 nike

我正在尝试通过node.js 将传感器数据发送到artik cloud。 (使用网络套接字和串行端口)。但它发送空。有人知道原因吗?我刚刚复制了教程中的代码,因此没有语法错误。

var webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
var device_id = "####";
var device_token = "#####";

var isWebSocketReady = false;
var ws = null;
var serialport = require("serialport");


var portName = 'COM5';
var sp= new serialport.SerialPort(portName, {
baudRate: 9600,
parser: serialport.parsers.readline("\r\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(temperature){
try{
// ts = ', "ts": '+getTimeMillis();
var data = {
"temp": temperature

};
var payload = '{"sdid":"'+device_id+'", "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 parsedStrs = data.split(",");
var temperature = parseInt(data);




sendData(temperature);
});
});

最佳答案

如果您引用我们的第一个 IoT 示例: https://developer.artik.cloud/documentation/tutorials/your-first-iot-device.html

node.js 示例发送来自温度传感器的值。作为依赖项,它需要连接 Arduino、Raspberry Pi 和位于右侧引脚的 DHT 温度传感器。如果您在将数据发送到 ARTIK Cloud 之前看到 null,则说明您没有从传感器获得任何值。

特别是,如果出现任何解析错误,请输出并打印以控制台以下函数的“温度”值:函数 sendData(温度)//...

如果您需要更多信息,请发送电子邮件至developer@artik.cloud。

谢谢!

关于json - 传感器数据未上传至 artik 云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43044673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com