Skip to main content
deleted 72 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Arduino SerailSerial data communication with NodejsNodeJS

I am using an Arduino Uno and a DTH11 temperature sensor to read tempearaturetemperature and then send that value to a computer via the serial port.

A nodejsNodeJS application is running inon the computer and capturecaptures that value and writewrites to a log file.

Here whenWhen I try to get values from my NodejsNodeJS application, sometimes the value is broken into two values. Eg: 25 -> 2 and 5

This is my arduinoArduino code:

dht DHT;

#define DHT11_PIN 7
float tem [4];
 

void setup() {
  Serial.begin(9600);
  pinMode(12, OUTPUT); 
 
}

void loop()
  {

 
  int chk = DHT.read11(DHT11_PIN);  //

  int val = (int)(DHT.temperature);
 
  Serial.println(val);
 
  delay(600000);
 
}

This is my NodejsNodeJS program.

var fs = require('fs')
var logger = fs.createWriteStream('/data/Temperature/temperature.csv', {
  flags: 'a' // 'a' means appending (old data will be preserved)
})

var SerialPort = require("serialport")
var serialPort = new SerialPort("/dev/ttyACM0", {
  baudRate: 9600,
});

 serialPort.on("open", function () {
 
         serialPort.on('data', function(data) {
        var moment = require('moment');
            var dayTime = moment().format('YYYY-MM-DD,hh:mm');
 
        var val1 = data.toString();
        var val = val1.trim();
        if (val == '') {
                console.log('no data');
        else {
            console.log(val);
            logger.write(dayTime + ',' + val + '\n');
        }
               });
               });

Is it a problem with my codescode?

Thanks in advance.

Arduino Serail data communication with Nodejs

I am using Arduino Uno and DTH11 temperature sensor to read tempearature and then send that value to a computer via serial port.

A nodejs application is running in the computer and capture that value and write to a log file.

Here when I try to get values from my Nodejs, sometimes value is broken into two values. Eg: 25 -> 2 and 5

This is my arduino code

dht DHT;

#define DHT11_PIN 7
float tem [4];
 

void setup(){
  Serial.begin(9600);
  pinMode(12, OUTPUT); 
 
}

void loop()
 {

 
 int chk = DHT.read11(DHT11_PIN);  //

 int val = (int)(DHT.temperature);
 
Serial.println(val);
 
  delay(600000);
 
}

This is my Nodejs program.

var fs = require('fs')
var logger = fs.createWriteStream('/data/Temperature/temperature.csv', {
  flags: 'a' // 'a' means appending (old data will be preserved)
})

var SerialPort = require("serialport")
var serialPort = new SerialPort("/dev/ttyACM0", {
  baudRate: 9600,
});

 serialPort.on("open", function () {
 
         serialPort.on('data', function(data) {
        var moment = require('moment');
            var dayTime = moment().format('YYYY-MM-DD,hh:mm');
 
        var val1 = data.toString();
        var val = val1.trim();
        if (val == ''){
                console.log('no data');
        else{
            console.log(val);
            logger.write(dayTime + ',' + val + '\n');
        }
               });
               });

Is it a problem with my codes?

Thanks in advance

Arduino Serial data communication with NodeJS

I am using an Arduino Uno and a DTH11 temperature sensor to read temperature and then send that value to a computer via the serial port.

A NodeJS application is running on the computer and captures that value and writes to a log file.

When I try to get values from my NodeJS application, sometimes the value is broken into two values. Eg: 25 -> 2 and 5

This is my Arduino code:

dht DHT;

#define DHT11_PIN 7
float tem [4];

void setup() {
  Serial.begin(9600);
  pinMode(12, OUTPUT); 
}

void loop() {
  int chk = DHT.read11(DHT11_PIN);
  int val = (int)(DHT.temperature);
  Serial.println(val);
  delay(600000);
}

This is my NodeJS program.

var fs = require('fs')
var logger = fs.createWriteStream('/data/Temperature/temperature.csv', {
  flags: 'a' // 'a' means appending (old data will be preserved)
})

var SerialPort = require("serialport")
var serialPort = new SerialPort("/dev/ttyACM0", {
  baudRate: 9600,
});

serialPort.on("open", function () {
  serialPort.on('data', function(data) {
    var moment = require('moment');
    var dayTime = moment().format('YYYY-MM-DD,hh:mm');
    var val1 = data.toString();
    var val = val1.trim();
    if (val == '') {
      console.log('no data');
    else {
      console.log(val);
      logger.write(dayTime + ',' + val + '\n');
    }
  });
});

Is it a problem with my code?

Thanks in advance.

Source Link

Arduino Serail data communication with Nodejs

I am using Arduino Uno and DTH11 temperature sensor to read tempearature and then send that value to a computer via serial port.

A nodejs application is running in the computer and capture that value and write to a log file.

Here when I try to get values from my Nodejs, sometimes value is broken into two values. Eg: 25 -> 2 and 5

This is my arduino code

dht DHT;

#define DHT11_PIN 7
float tem [4];


void setup(){
  Serial.begin(9600);
  pinMode(12, OUTPUT); 

}

void loop()
{


 int chk = DHT.read11(DHT11_PIN);  //

 int val = (int)(DHT.temperature);

Serial.println(val);
 
 delay(600000);

}

This is my Nodejs program.

var fs = require('fs')
var logger = fs.createWriteStream('/data/Temperature/temperature.csv', {
  flags: 'a' // 'a' means appending (old data will be preserved)
})

var SerialPort = require("serialport")
var serialPort = new SerialPort("/dev/ttyACM0", {
  baudRate: 9600,
});

 serialPort.on("open", function () {

         serialPort.on('data', function(data) {
        var moment = require('moment');
            var dayTime = moment().format('YYYY-MM-DD,hh:mm');

        var val1 = data.toString();
        var val = val1.trim();
        if (val == ''){
                console.log('no data');
        else{
            console.log(val);
            logger.write(dayTime + ',' + val + '\n');
        }
               });
               });

Is it a problem with my codes?

Thanks in advance