Reading data from arduino with nodejs and socketio

I have this simple code that I'm running in arduino

char inp;
void setup(){
  Serial.begin(9600);
}

void loop(){
  while(Serial.available()>0){
    inp = Serial.read();
    Serial.print(inp);
  }
  Serial.print("--");
  delay(200);
}

So it continiously writes "--" and if I send something to arduino it replies with that

Then I have in a js file

var strinInfo = "";
tempSerial.on("data", function(data) {
  console.log("data -> " + data);
  if (stringInfo.length < 37){
    stringInfo += data;
  }else{
    io.sockets.emit("message", stringInfo);
    stringInfo = ""
  }
});

That sends via sockets what I got from arduino. My problem is that, for example, if I send

"thisisanunusuallongandterriblestringofsymbolsblablablablablablabla"

There are missing characters:

---------thisisanunusuallongandterribles, 
gofsymbolsblablablablabla--blabla ----, 
-------------------------------------,

in this example I'm missing "trin". Is there a way of not losing that characters?

I'm starting with node so my guess is that between the time that it emits the message ... the content from that moment is gone, maybe I'm wrong.

Looking at your code, it could be a couple of things.

  1. You may be sending data before the listener is ready, the above libs below have that solved.
  2. The following line worries me if (stringInfo.length < 37){. If you get successive small packets of data, the packet that puts you over 37 will only print the stringInfo but will not print the data portion.

Libs that connect to Arduino:

  • [Johnny Five][1]
  • [Firmata][2]

Both programs interact with Arduinos. Firmata is the low level one with Johnny-Five running on top.