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.

12
  • What do you mean by two array log ? Give me an example please Jul 23, 2018 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"??? Jul 24, 2018 at 5:48
  • Try to send it in an object like this { measure-1: « blabla », measure-2: « some data » } it’ll be much easier to parse Jul 25, 2018 at 6: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 .. Jul 27, 2018 at 9:32
  • 1
    There is a typographical error : " hum-2 = result[2]" should read " hum-1 = result[2]"
    – user10316640
    Sep 20, 2018 at 0:53

3 Answers 3

2
+100
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.

7
  • nice advice sir, your answer is almost correct, i have data serial as measure1: 12.34 23.45 measure2: 34.56 45.67 without flush serial, when i data store at my parser, it's only appears as one array log as parser.on('data', function(data){} , and i tried your answered and the output will print two variable on my console, i don't know make two data parser like data store measure1 to data1 JS and measure2 to data2. Sep 20, 2018 at 12:22
  • do you want to emit both data1 and data2 var separately? Sep 20, 2018 at 12:26
  • how are you sending data to socket from Arduino Sep 20, 2018 at 12:28
  • from getStrings void to my data parser function sir Sep 20, 2018 at 12:32
  • 1
    There should be a solution, You can differentiate data from client on server by passing an extra parameter, and checking it on socket connection. Sep 20, 2018 at 13:25
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
}
2
  • 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 ? Sep 20, 2018 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 Sep 20, 2018 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);
}
1
  • 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) Sep 20, 2018 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.