将数据从serialport发送到socket.io

时间:2014-07-29 16:30:07

标签: javascript node.js socket.io arduino

我愿意在我的Arduino和网络界面之间进行简单的对话。 我已经能够在命令行中执行此操作,所以现在我尝试使用某些按钮执行相同的操作。

我一直在使用socket.io来管理套接字,我可以向Arduino发出一个套接字但是当我尝试从Arduino向服务器发出一个套接字时,它似乎不起作用。

这是我的代码。

server.js:

var fs = require('fs');
var http = require('http');
var io = require('socket.io');
var url = require("url"); 
var SerialPort = require("serialport").SerialPort;

var socketServer;
var serialport;
var portName = '/dev/ttyACM0'; //change this to your Arduino port
var sendData = "";

function startServer(route,handle,debug) {
    function onRequest(request, response) {
      var pathname = url.parse(request.url).pathname; 
      console.log("Request for " + pathname + " received");
      var content = route(handle,pathname,response,request,debug);
    }
    var httpServer = http.createServer(onRequest).listen(1337, function(){
        console.log("Listening at: http://localhost:1337");
    });
    initSocketIO(httpServer,debug);
}

function initSocketIO(httpServer,debug) {
    socketServer = io.listen(httpServer);
    if(debug == false) socketServer.set('log level', 1);
    socketServer.on('connection', function (socket) {
        console.log("Connected");
        socket.emit('onconnection', {msg:sendData});    
        socket.on('buttonval', function(data) {
            console.log('A letter ' + data + ' was sent');
            serialport.write(data);
        });
        socket.on('update', function(data) {
            console.log('Yaaaaayaaa');
            socket.emit('updateData',{msg:data.buffer});
        });
        socket.on('error', function(err) {
            console.log(err);
        });
        serialListener(debug, socket);
    });
}

// Listen to serial port
function serialListener(debug, socket) {
    var receivedData = "";
    serialport = new SerialPort(portName, {
         baudrate: 9600,
         dataBits: 8,
         parity: 'none',
         stopBits: 1,
         flowControl: false
    });

    serialport.on('open', function(){
        var buffer = "";
        serialport.on('data', function(data){
            var match = /\r|\n/.exec(data);
            if (match) {
                buffer += data;
                console.log("%s", buffer); 
                sendData = buffer;
                socket.emit('update', {msg:buffer});
                buffer = "";
            } else buffer += data;
        });
    });
}

exports.start = startServer;

的index.html:

<!DOCTYPE HTML>
 <html>
   <head>    
    <style>
    </style>
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
    var iosocket;
    function initSocketIO() {
        iosocket = io.connect();
        iosocket.on('onconnection', function(value) {
            initButton();
            //recieve changed values by other client from server
            iosocket.on('updateData', function (recievedData) {
                alert(recievedData.msg);
                $("#result").html(recievedData.msg);
            });
        });
    }

    function initButton(){$( "#check" ).button();}
    window.onload = function() {initSocketIO();};   
      $(document).ready(function() {
        $('#check').click(function() {
        iosocket.emit('buttonval', 'S');
     });
    });
    </script>
  </head>
  <body>
   <div id="rData">
    <h2>Data from Arduino</h2>
    <div id="result"></div>
   </div>
    <input type="button" id="check" value="Measure"/></div>
   </div> 
  </body>
</html>

arduino.ino:

float rP = 0;
float lP = 0;
float cP = 0; 

void initMeasurement() {
    Serial.println("Ready for measures");
}

void doMeasure() {
  rP = 0.1;
  lP = 0.2;
  cP = 0.35;
}

boolean start = false;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // init something
  initMeasurement();

}

void loop() {
   // Recieve data from Node and write it to a String
  if (Serial.available() && !start) {
    char inChar = (char)Serial.read();
    if(inChar == 'S'){ // end character for led
     start = true;
    }
  } else if (start) {
    Serial.println("Measuring...");
    doMeasure();
    Serial.println("Results are:");
    Serial.print("Rp = ");
    Serial.println(rP);
    Serial.print("Lp = ");
    Serial.println(lP);
    Serial.print("Cp = ");
    Serial.println(cP);
    start = false;
    Serial.println("Ready again");
  }  
  //delay(50); // give the Arduino some breathing room.
}

0 个答案:

没有答案