Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Display two String Array Esp8266 on data parser nodejs

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.

like image 937
abu-ahmed al-khatiri Avatar asked Jul 23 '18 11:07

abu-ahmed al-khatiri


1 Answers

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.

like image 189
Rohit Dhiman Avatar answered Nov 15 '22 00:11

Rohit Dhiman