Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Post Reopened by Phrancis, Jamal
deleted 24 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

EDIT: Code now working

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}. I

I am exposing a function called getTemperatures() that I currently call from elsewhere every 1000ms. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am filtering the data that comes back over the serial and then assigning temperatures responses to a temperatures variable that I continuously overwrite. I then give a 999 ms delay before calling back that variable. This

This works, but seems fragile (and not elegant).

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

var temperatures = {};

machine.getTemperatures = function(callback) {
    sendCommand('9');
    setTimeout(function(){
      callback(temperatures);
    }, 999)
}

function sendCommand(data) {
  machine.write(data);
}

function handleData(dataString){
  data = JSON.parse(dataString);

  if ('temperatures' in data) {
    temperatures = data;
  } 
} 

module.exports.getTemperatures = getTemperatures;

EDIT: Code now working

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}. I am exposing a function called getTemperatures() that I currently call from elsewhere every 1000ms. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am filtering the data that comes back over the serial and then assigning temperatures responses to a temperatures variable that I continuously overwrite. I then give a 999 ms delay before calling back that variable. This works, but seems fragile (and not elegant).

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

var temperatures = {};

machine.getTemperatures = function(callback) {
    sendCommand('9');
    setTimeout(function(){
      callback(temperatures);
    }, 999)
}

function sendCommand(data) {
  machine.write(data);
}

function handleData(dataString){
  data = JSON.parse(dataString);

  if ('temperatures' in data) {
    temperatures = data;
  } 
} 

module.exports.getTemperatures = getTemperatures;

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}.

I am exposing a function called getTemperatures() that I currently call from elsewhere every 1000ms. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am filtering the data that comes back over the serial and then assigning temperatures responses to a temperatures variable that I continuously overwrite. I then give a 999 ms delay before calling back that variable.

This works, but seems fragile (and not elegant).

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

var temperatures = {};

machine.getTemperatures = function(callback) {
    sendCommand('9');
    setTimeout(function(){
      callback(temperatures);
    }, 999)
}

function sendCommand(data) {
  machine.write(data);
}

function handleData(dataString){
  data = JSON.parse(dataString);

  if ('temperatures' in data) {
    temperatures = data;
  } 
} 

module.exports.getTemperatures = getTemperatures;
Make sure it is clear that code is now working, to get it off hold hopefully
Source Link

EDIT: Code now working

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}. I am exposing a function called getTemperatures() that I currently call from elsewhere every 1000ms. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am filtering the data that comes back over the serial and then assigning temperatures responses to a temperatures variable that I continuously overwrite. I then give a 999 ms delay before calling back that variable. This works, but seems fragile (and not elegant).

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

var temperatures = {};

machine.getTemperatures = function(callback) {
    sendCommand('9');
    setTimeout(function(){
      callback(temperatures);
    }, 999)
}

function sendCommand(data) {
  machine.write(data);
}

function handleData(dataString){
  data = JSON.parse(dataString);

  if ('temperatures' in data) {
    temperatures = data;
  } 
} 

module.exports.getTemperatures = getTemperatures;

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}. I am exposing a function called getTemperatures() that I currently call from elsewhere every 1000ms. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am filtering the data that comes back over the serial and then assigning temperatures responses to a temperatures variable that I continuously overwrite. I then give a 999 ms delay before calling back that variable. This works, but seems fragile (and not elegant).

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

var temperatures = {};

machine.getTemperatures = function(callback) {
    sendCommand('9');
    setTimeout(function(){
      callback(temperatures);
    }, 999)
}

function sendCommand(data) {
  machine.write(data);
}

function handleData(dataString){
  data = JSON.parse(dataString);

  if ('temperatures' in data) {
    temperatures = data;
  } 
} 

module.exports.getTemperatures = getTemperatures;

EDIT: Code now working

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}. I am exposing a function called getTemperatures() that I currently call from elsewhere every 1000ms. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am filtering the data that comes back over the serial and then assigning temperatures responses to a temperatures variable that I continuously overwrite. I then give a 999 ms delay before calling back that variable. This works, but seems fragile (and not elegant).

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

var temperatures = {};

machine.getTemperatures = function(callback) {
    sendCommand('9');
    setTimeout(function(){
      callback(temperatures);
    }, 999)
}

function sendCommand(data) {
  machine.write(data);
}

function handleData(dataString){
  data = JSON.parse(dataString);

  if ('temperatures' in data) {
    temperatures = data;
  } 
} 

module.exports.getTemperatures = getTemperatures;
deleted 115 characters in body; edited tags
Source Link

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}. I am exposing a function called getTemperatures() that I want to be able tocurrently call from elsewhere and have it return that JSONevery 1000ms. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am trying to figure out how to send back the temperature data from a given request in the callback but it seems challenging because I need to have a listener forfiltering the next piece of temperature data that comes back over the wire or maybe some sort of buffer/stackserial and then assigning temperatures responses to a temperatures variable that it can pull off ofI continuously overwrite. However, I amthen give a 999 ms delay before calling back that variable. This works, but seems fragile (and not sure what pattern would achieve this or how to code itelegant).

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

functionvar temperatures = {};

machine.getTemperatures = function(callback) {
    sendToMachinesendCommand('9');
    listenForResponsesetTimeout(function(data){
        callback(datatemperatures);
    }, 999);
}

function sendToMachinesendCommand(data) {
  machine.write(data);
}

function handleData(datadataString){
    // If the data object is temperatures, send= itJSON.parse(dataString);

 back withif the('temperatures' lastin temperaturedata) request{
    // Otherwise deal with whatevertemperatures type= ofdata;
 data it} is

} 

module.exports.getTemperatures = getTemperatures;

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}. I am exposing a function called getTemperatures() that I want to be able to call from elsewhere and have it return that JSON. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am trying to figure out how to send back the temperature data from a given request in the callback but it seems challenging because I need to have a listener for the next piece of temperature data that comes over the wire or maybe some sort of buffer/stack that it can pull off of. However, I am not sure what pattern would achieve this or how to code it.

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

function getTemperatures(callback) {
    sendToMachine('9');
    listenForResponse(function(data){
        callback(data);
    });
}

function sendToMachine(data) {
  machine.write(data);
}

function handleData(data){
    // If the data object is temperatures, send it back with the last temperature request
    // Otherwise deal with whatever type of data it is

} 

module.exports.getTemperatures = getTemperatures;

I have a machine that is being controlled by an Arduino. If I send the machine the command '9' it will send back JSON with some sensor temperatures in the format {"temperatures":{"bean_temp":110.75,"env_temp":98.15}}. I am exposing a function called getTemperatures() that I currently call from elsewhere every 1000ms. The serialport library provides a listener for all the data that gets sent back over the wire from the machine (machine.on('data', handleData)). There are other types of data that will get sent back from the machine, such as {"action":"handle_pulled"}. I am filtering the data that comes back over the serial and then assigning temperatures responses to a temperatures variable that I continuously overwrite. I then give a 999 ms delay before calling back that variable. This works, but seems fragile (and not elegant).

var serialport = require('serialport');

var ports = require('./ports'),
    port = ports.arduino;

var machine = new serialport(port, {
  baudRate: 9600,
  parser: serialport.parsers.readline("\n")
});

machine.on('data', handleData);

var temperatures = {};

machine.getTemperatures = function(callback) {
    sendCommand('9');
    setTimeout(function(){
      callback(temperatures);
    }, 999)
}

function sendCommand(data) {
  machine.write(data);
}

function handleData(dataString){
  data = JSON.parse(dataString);

  if ('temperatures' in data) {
    temperatures = data;
  } 
} 

module.exports.getTemperatures = getTemperatures;
Post Closed as "Not suitable for this site" by Jamal
Source Link
Loading