Arduino + node.js + socket.io - Object {valor:ArrayBuffer}

时间:2015-10-26 08:35:58

标签: javascript node.js sockets arduino

我通过node和socket.io在arduino上通过serialport发送1到web。问题是当我发送数据时:

Data: [object Object]
Data: [object Object]
...

但我想要那样的东西

Data: 1
Data: 1
...

没有价值" 1",我尝试发送字符串仍然相同。这是为什么? 程序工作正常,但这个值问题让我更进一步,我在控制台中有一条消息:

userk@user-k52j:~/sio$ node app.js
http.listen
Port OPEN.
express deprecated res.sendfile: Use res.sendFile instead app.js:10:9
Socket.io connected.

但是当我将sendfile更改为sendFile时程序崩溃。

这是我的节目:

Arduino的:

void setup(){Serial.begin(9600);}
void loop(){
 Serial.println(1);
 delay(2000); 
}

app.js

var app = require("express")();
var express = require("express");

app.use(express.static(__dirname + '/public'));

var http = require("http").Server(app);
var io = require("socket.io")(http);

app.get("/", function(req, res){
    res.sendfile("index.html");
});

var mySocket;

// Obsługa portu szeregowego
var SerialPort = require("serialport").SerialPort;
var mySerial = new SerialPort("/dev/ttyUSB0", {
        baudrate: 9600,
    });

mySerial.on("open", function(){
    console.log("Port OPEN.");
});
mySerial.on("data", function(data){
    //console.log("Data: " + data + 'ls/n');
    io.emit("dataArduino",{
        valor: data
    });
});
mySerial.on("error", function(error){
    console.log("Failed to open: " + error);
});

io.on("connection", function(socket){
    console.log("Socket.io polaczony.")
});

http.listen(3000, function(){
    console.log("http.listen");
});

的index.html

<!doctype html>
<html lang="pl">
<head>
  <meta charset="utf-8">
  <title>Arduino node.js test</title>
  <script src="/socket.io/socket.io.js"></script>
</head>
<body>
    <script type="text/javascript"> 
        var socket = io();
        socket.on("dataArduino", function(data){
            console.log(data);
            document.write("Data: "+ data + "<br />"); 
        });
    </script>  
</body>
</html>

0 个答案:

没有答案