简体   繁体   English

无法在数据解析器nodejs上显示两个String Array Esp8266

[英]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. 我有从ESP8266测得的数据,这些数据存储在一个字符串数组中,并使用Node.js显示到JavaScript HTML,但我只是将数据作为一个数组而不是作为两个数组来获取。

My Arduino code: 我的Arduino代码:

...
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: 我的Node.js代码:

    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: 当我调用npm start ,我只是将所有数据作为一个数组日志获取:

data: 12.34 23.45 34.56 45.67

not a two array log as I want, like this: 不是我想要的两个数组的日志,像这样:

string array 'measure1' as: 字符串数组“ measure1”为:

data1: 12.34 23.45

string array 'measure2' as: 字符串数组“ measure2”为:

data2: 34.56 45.67

data parser only one string array not as two string array! 数据解析器只有一个字符串数组而不是两个字符串数组!

sorry my bad english. 对不起,我英语不好。

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. 现在,您将拥有两个不同的变量中的数据。

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
}

The parser is not finding the deliminator "\\n" between messages. 解析器在消息之间找不到分隔符“ \\ n”。

In your arduino code add the line measure2 += "\\n"; 在您的arduino代码中,添加一行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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM