8

I have data measured from ESP8266 which I store in a string array, and I display to my JavaScript HTML using Node.js, but I just get data as one array not as two array.

My Arduino code:

...
int temp1, hum1, temp2, hum2;
...
void setup(){
serial.begin(9600);
serial1.begin(115200);
...
}``

void loop(){
...
float t1 = bme1.readTemperature();
float h1 = bme1.readHumidity();
float t2 = bme2.readTemperature();
float h2 = bme2.readHumidity();

temp1 = t1;
hum1 = h1;
temp2 = t2;
hum2 = h2;

getStrings();
delay(3000);
...
}

void getStrings(){
String measure1=" "; String measure2=" ";
  measure1 += temp1;
  measure1 += " ";
  measure1 += hum1;
  measure1 += " ";
  Serial.println(measure1);
  measure2 += temp2;
  measure2 += " ";
  measure2 += hum2;
  measure2 += " ";
  Serial.println(measure2);
}

My serial monitor shows:

measure1: 12.34 23.45
measure2: 34.56 45.67

My Node.js code:

    var exs = require('express');
        var app = exs();
        var http = require('http').Server(app);
        var io = require('socket.io')(http);
        var port = require('serialport');
        var parsers = port.parsers;
        var parser = new parsers.Readline({delimiter:'\n'});
        var ports = new port("/dev/ttyUSB0", {baudRate:115200,parser:parser}, false);

        process.setMaxListeners(Infinity);
        ports.pipe(parser);
        ports.on('open', function(){
            console.log('portOpened');
            io.on('connection', function(socket){
                ports.on('data', function(data){
                console.log('data: '+data);
                    parser.on('data', function(data){
                    var result = data.split(" ");
                    var temp-1 = result[1]
                    var hum-1 = result[2]
                    var temp-2 = result[3]
                    var hum-2 = result[4];
                        io.sockets.emit('update', {
                            tmp_1:temp-1.toString()+' *C',
                            hum_1:hum-1.toString()+' %',
                            tmp_2:temp-2.toString()+' *C',
                            hum_2:hum-2.toString()+' %'
                        });
                    }); 
                });
            });
        });
        ...

When I call npm start, I just get all data as one array log:

data: 12.34 23.45 34.56 45.67

not a two array log as I want, like this:

string array 'measure1' as:

data1: 12.34 23.45

string array 'measure2' as:

data2: 34.56 45.67

data parser only one string array not as two string array!

sorry my bad english.

  • What do you mean by two array log ? Give me an example please – Nicolas Menettrier Jul 23 '18 at 12:36
  • on my arduino variable has string array of "measure-1" and "measure-2", and my nodejs to get a "measure" and store to argument function javascript "data", =>> parser.on('data', function(data){}. but the result "measure-1" and "measure-2" as one array on my console.log, i don/t know how to string array "measure-1" as some "data-1" and string array "measure-2" as some "data-2"??? – abu-ahmed al-khatiri Jul 24 '18 at 05:48
  • Try to send it in an object like this { measure-1: « blabla », measure-2: « some data » } it’ll be much easier to parse – Nicolas Menettrier Jul 25 '18 at 06:43
  • what there into socket emit, Sir?. my first step i want to store "measure" to console as two data array, and then i can store two data array to socket.emit .. – abu-ahmed al-khatiri Jul 27 '18 at 09:32
  • You'r serial encoding is problematic. You need to add more specific delimiters as @menett_a mentions above. Also, you are parsing it correctly in the nodejs code as a json object, so you should be able to receive it just fine on your websocket. – Sebastian Scholle Sep 18 '18 at 14:09
  • @SebastianScholle where my serial encoding is problematic exactly? i also tried as menett_a mentions but the output still one array log. – abu-ahmed al-khatiri Sep 18 '18 at 14:18
  • 1
    There is a typographical error : " hum-2 = result[2]" should read " hum-1 = result[2]" –  Sep 20 '18 at 00:53
  • @abu-ahmedal-khatiri note that serial output will buffer and send according to protocol rules, you could try using `Serial.flush()` to terminate the output before sending the next measure string. – Sebastian Scholle Sep 20 '18 at 08:04
  • problem is with node code or Arduino code? – Rohit Dhiman Sep 20 '18 at 11:45
  • @RohitDhiman i think both, i tried SebastianScholle Answered, when i flush serial `measure1` and `measure2` will be only data store on the data1 not to my `data2` JS, i just want store `measure1` to `data1` JS and `measure2` to `data2` JS. – abu-ahmed al-khatiri Sep 20 '18 at 11:51
  • from where you are getting data here " ports.on('data', function(data){ " – Gaurav Gupta Sep 20 '18 at 12:24
  • @GauravGupta of course data ports will store to my `data` arguments. i cannot find a shortcut way to make two data parser sir – abu-ahmed al-khatiri Sep 20 '18 at 12:29

3 Answers3

2
var hum-2 = result[2]

should be

var hum-1 = result[2]

Full code will be

ports.on('data', function(data){
console.log('data: '+data);
    parser.on('data', function(data){
    var result = data.split(" ");
    var temp-1 = result[1]
    var hum-1 = result[2]
    var temp-2 = result[3]
    var hum-2 = result[4];
    var data1 = temp-1+' '+hum-1;
    var data2 = temp-2+' '+hum-2;
    console.log('data1: '+data1);
    console.log('data2: '+data2);
        io.sockets.emit('update', {
            tmp_1:temp-1.toString()+' *C',
            hum_1:hum-1.toString()+' %',
            tmp_2:temp-2.toString()+' *C',
            hum_2:hum-2.toString()+' %'
        });
    }); 
});

Now you will have data in two different vars.

Rohit Dhiman
  • 2,691
  • 18
  • 33
1

ensure that serial is flushed out the buffer before starting the next serial output:

void getStrings(){
String measure1=" "; String measure2=" ";
  measure1 += temp1;
  measure1 += " ";
  measure1 += hum1;
  measure1 += " ";
  Serial.println(measure1);
  Serial.flush(); // FLUSH
  measure2 += temp2;
  measure2 += " ";
  measure2 += hum2;
  measure2 += " ";
  Serial.println(measure2);
  Serial.flush(); // FLUSH
}
  • i try it sir, and now when i run `npm start` the output said `data1: 12.34 23.45 and data1: 34.56 45.67` no data output on data2. how to get data parser serial `measure1` to `data1` JS and data parser serial `measure2` to `data2` JS ? – abu-ahmed al-khatiri Sep 20 '18 at 10:55
  • i think when i flush serial `measure1` and `measure2` will be only data store on the `data1` not to my `data2` JS – abu-ahmed al-khatiri Sep 20 '18 at 11:28
0

The parser is not finding the deliminator "\n" between messages.

In your arduino code add the line measure2 += "\n";

void getStrings(){
  String measure1=" "; String measure2=" ";
  measure1 += temp1;
  measure1 += " ";
  measure1 += hum1;
  measure1 += " ";
  Serial.println(measure1);
  measure2 += temp2;
  measure2 += " ";
  measure2 += hum2;
  measure2 += " ";
  measure2 += "\n";
  Serial.println(measure2);
}
  • Serial.println() does that already, thats why his serial output is showing new lines. His receiver gets the serial output in one stream (single line) – Sebastian Scholle Sep 20 '18 at 08:08