CME64
CME64

Reputation: 1672

Arduino wireless and Node.js

I'm working on an Arduino project to control motors and read sensors. I decided to use the web view using Node.js as a medium channel to read/write from the serial port to the browser using either libraries (SerialPort and SerialPort2).

Both are working fine when I connect the Arduino directly to the USB device using a wire, but Node.js can't seem to read anything when I connect the Arduino to the USB device through my wireless adapter** (APC220) even though I can read everything received on it using the Arduino serial monitor.

I checked every possible reason behind that; I checked the baud-rate I'm using for the Arduino communication with the wirelss serial and the APC220 and the bridge connector (USB-to-serial converter). They all have the same settings: 9600 baud-rate, no parity /flowcontrol, data bits: 8 , stop bits: 1.

The behavior is as follows. It connects to the COM port without trouble, and then I tried printing the errors, but it seems there are none identified by either SerialPort libraries. Then no reading comes to the event (data), which means it (Node.js) is not interacting with the serialport even though it is open.

Note: I know I can use another Arduino as a medium between the USB port and the wireless adapter, but I want to understand this problem and solve it cleanly without such work around.

What could the problem be?

server [node.js]:

var SerialPort  = require('serialport2').SerialPort;
var portName = 'COM15';

var io = require('socket.io').listen(8000); // Server listens for socket.io communication at port 8000
io.set('log level', 1); // Disables debugging. This is optional. You may remove it if desired.

var sp = new SerialPort(); // Instantiate the serial port.
sp.open(portName, { // portName is instatiated to be COM3, replace as necessary
   baudRate: 9600, // This is synchronised to what was set for the Arduino code
   dataBits: 8, // This is the default for Arduino serial communication
   parity: 'none', // This is the default for Arduino serial communication
   stopBits: 1, // This is the default for Arduino serial communication
   flowControl: false // This is the default for Arduino serial communication
});

io.sockets.on('connection', function (socket) {
    // If socket.io receives message from the client browser then
    // this call back will be executed.
    socket.on('message', function (msg) {
        console.log(msg);
    });
    // If a web browser disconnects from Socket.IO then this callback is called.
    socket.on('disconnect', function () {
        console.log('disconnected');
    });
});

var cleanData = ''; // This stores the clean data
var readData = '';  // This stores the buffer
sp.on('data', function (data) { // Call back when data is received
    readData = data.toString(); // Append data to buffer.
    // If the letters '[' and ']' are found on the buffer then isolate what's in the middle
    // as clean data. Then clear the buffer.
    console.log(readData); // **Here you should be able to print the data if you receive any**
     if (readData.indexOf(']') >= 0 && readData.indexOf('[') >= 0) {
        cleanData = readData.substring(readData.indexOf('[') + 1, readData.indexOf(']'));
        readData = '';
        console.log("-- "+cleanData);
        io.sockets.emit('message', cleanData);
     }else if(readData.indexOf('[') >= 0){
        cleanData = readData.substring(readData.indexOf('[') + 1, readData.length);
        readData = '';
     }else if(readData.indexOf(']') >= 0){
        cleanData += readData.substring(0, readData.indexOf(']'));
        readData = '';
        console.log("-- "+cleanData);
        io.sockets.emit('message', cleanData);
     }else{
        cleanData += readData;
        readData = '';
     }
    //console.log(readData);
    //io.sockets.emit('message', readData);
});

Upvotes: 3

Views: 2616

Answers (2)

zmo
zmo

Reputation: 24812

Well, both your codes look all right, so I'm pretty sure your problem is something obvious (like the nose in the middle of your face) that you don't see because you're too focused on details. So here's a checklist I'd do first:

  • Are you sure your serial interface is the COM15, and never changes?
  • Are you sure both APC devices have correct baudrate configured?
  • Did you try making your Arduino send a simple code that sends the same thing over the channel?

Like:

void loop() {
  ...println("TEST");
  delay(1000);
}

And on your host:

sp.on('data', function (data) {
    console.log(data.toString());
});

When you get something buggy in your system, try to build the most simple use case of that buggy part, so you're sure that's nothing else in your codes that interferes with that. You don't need to make your Arduino work on the GPS stuff, as well as your Node.js stuff work on the web stuff.

Just make it the most simple as you can. (And don't forget to add a delay in your Arduino loop, or you may have difficulties reflashing the chip).

You may also want to add to your code the error catching part of serialport2:

port.on('error', function(err) {
  console.log("ERROR receiving serial data: ", err);
});

As well as for your open() statement:

sp.open(portName, portConfig, function (err) {
    console.log("ERROR opening serial port: ", err);
});

As you may be missing error reporting on the host side!

Upvotes: -1

Udo Klein
Udo Klein

Reputation: 6882

While the monitor is running no other program can read the serial port.

In case you do not open both at the same time then things are more tricky. My suggestion would be to spy on the wire. That is: install Wireshark and have a look at the data on the serial connection / USB bus.

You might also want to check how the serial port of the APC220 and the Arduino differ with regard to their serial/USB converters. Another idea would be to analyze this issue under Linux since may allow more insights into the low-level differences of the chip sets / USB activity. Of course if you do not have Linux experience this is hard to do, but maybe you know some Linux enthusiasts.

Upvotes: 0

Related Questions