How to write, in an html page, an if else condition using serialport.on() response?
I have the following server:
var express = require('express'); // include express.js
var app = express(); // a local instance of it
// serial port initialization:
var SerialPort = require("serialport"),
Readline = require('@serialport/parser-readline')
var portName = process.argv[2];
var portConfig = {baudRate: 9600};
// open the serial port:
var myPort = new SerialPort(portName, portConfig);
// look for return and newline at the end of each data packet:
var parser = myPort.pipe(new Readline({ delimiter: 'n' }));
parser.on('data', console.log);
// get an analog reading from the serial port.
// This is a callback function for when the client requests /device/channel:
function getSensorReading(request, response) {
// the parameter after /device/ is the channel number:
var channel = request.params.channel;
// send the channel number out the serial port
//and wait for a response:
myPort.write(channel, function(){
// when you get a response from the serial port,
//write it out to the client:
myPort.on('data', function(data) {
// send the data and close the connection:
response.end(data);
});
});
}
// start the server:
var server = app.listen(8080);
// start the listeners for GET requests:
app.use('/',express.static('public'));
app.get('/device/:channel', getSensorReading);
Express serves an index.html page which is:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16 /p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16/addons/p5.dom.min.js"></script>
<script>
var label, serverResponse; // UI elements
function setup() {
label = createSpan("Volume:"); // the label
label.position(10,20); // position it
serverResponse = createSpan(); // create a div for server responses
serverResponse.position(label.width + 15, 20); // position it
getData(); // make a request back to the server
}
// this function makes a call to the server:
function getData(channel) {
httpGet('/device/' + channel, update);
}
// update the page when the server responds:
function update(data) {
serverResponse.html(data); // put the response in the span
getData(); // make another call to the server
}
var flush1 = new Audio('1110.ogg');
var flush2 = new Audio('1115.ogg');
if(serverResponse == 'H') {
flush1.play();
}else{
flush2.play();
}
</script>
</head>
<body>
An Arduino sketch is providing 'H' an 'L'' to the server:
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
if (buttonState ==1)
{
// print out the state of the button:
Serial.println('H');
}else{
// print out the state of the button:
Serial.println('L');
}
delay(1); // delay in between reads for stability
}
I would like to get alternatively, depending on the letter H or L, the two sounds flush1 and flush2, whereas I get always flush2. I suppose the if condition is not right. What is the good format? Thanks for your help!
javascript if-statement node-serialport
add a comment |
I have the following server:
var express = require('express'); // include express.js
var app = express(); // a local instance of it
// serial port initialization:
var SerialPort = require("serialport"),
Readline = require('@serialport/parser-readline')
var portName = process.argv[2];
var portConfig = {baudRate: 9600};
// open the serial port:
var myPort = new SerialPort(portName, portConfig);
// look for return and newline at the end of each data packet:
var parser = myPort.pipe(new Readline({ delimiter: 'n' }));
parser.on('data', console.log);
// get an analog reading from the serial port.
// This is a callback function for when the client requests /device/channel:
function getSensorReading(request, response) {
// the parameter after /device/ is the channel number:
var channel = request.params.channel;
// send the channel number out the serial port
//and wait for a response:
myPort.write(channel, function(){
// when you get a response from the serial port,
//write it out to the client:
myPort.on('data', function(data) {
// send the data and close the connection:
response.end(data);
});
});
}
// start the server:
var server = app.listen(8080);
// start the listeners for GET requests:
app.use('/',express.static('public'));
app.get('/device/:channel', getSensorReading);
Express serves an index.html page which is:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16 /p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16/addons/p5.dom.min.js"></script>
<script>
var label, serverResponse; // UI elements
function setup() {
label = createSpan("Volume:"); // the label
label.position(10,20); // position it
serverResponse = createSpan(); // create a div for server responses
serverResponse.position(label.width + 15, 20); // position it
getData(); // make a request back to the server
}
// this function makes a call to the server:
function getData(channel) {
httpGet('/device/' + channel, update);
}
// update the page when the server responds:
function update(data) {
serverResponse.html(data); // put the response in the span
getData(); // make another call to the server
}
var flush1 = new Audio('1110.ogg');
var flush2 = new Audio('1115.ogg');
if(serverResponse == 'H') {
flush1.play();
}else{
flush2.play();
}
</script>
</head>
<body>
An Arduino sketch is providing 'H' an 'L'' to the server:
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
if (buttonState ==1)
{
// print out the state of the button:
Serial.println('H');
}else{
// print out the state of the button:
Serial.println('L');
}
delay(1); // delay in between reads for stability
}
I would like to get alternatively, depending on the letter H or L, the two sounds flush1 and flush2, whereas I get always flush2. I suppose the if condition is not right. What is the good format? Thanks for your help!
javascript if-statement node-serialport
add a comment |
I have the following server:
var express = require('express'); // include express.js
var app = express(); // a local instance of it
// serial port initialization:
var SerialPort = require("serialport"),
Readline = require('@serialport/parser-readline')
var portName = process.argv[2];
var portConfig = {baudRate: 9600};
// open the serial port:
var myPort = new SerialPort(portName, portConfig);
// look for return and newline at the end of each data packet:
var parser = myPort.pipe(new Readline({ delimiter: 'n' }));
parser.on('data', console.log);
// get an analog reading from the serial port.
// This is a callback function for when the client requests /device/channel:
function getSensorReading(request, response) {
// the parameter after /device/ is the channel number:
var channel = request.params.channel;
// send the channel number out the serial port
//and wait for a response:
myPort.write(channel, function(){
// when you get a response from the serial port,
//write it out to the client:
myPort.on('data', function(data) {
// send the data and close the connection:
response.end(data);
});
});
}
// start the server:
var server = app.listen(8080);
// start the listeners for GET requests:
app.use('/',express.static('public'));
app.get('/device/:channel', getSensorReading);
Express serves an index.html page which is:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16 /p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16/addons/p5.dom.min.js"></script>
<script>
var label, serverResponse; // UI elements
function setup() {
label = createSpan("Volume:"); // the label
label.position(10,20); // position it
serverResponse = createSpan(); // create a div for server responses
serverResponse.position(label.width + 15, 20); // position it
getData(); // make a request back to the server
}
// this function makes a call to the server:
function getData(channel) {
httpGet('/device/' + channel, update);
}
// update the page when the server responds:
function update(data) {
serverResponse.html(data); // put the response in the span
getData(); // make another call to the server
}
var flush1 = new Audio('1110.ogg');
var flush2 = new Audio('1115.ogg');
if(serverResponse == 'H') {
flush1.play();
}else{
flush2.play();
}
</script>
</head>
<body>
An Arduino sketch is providing 'H' an 'L'' to the server:
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
if (buttonState ==1)
{
// print out the state of the button:
Serial.println('H');
}else{
// print out the state of the button:
Serial.println('L');
}
delay(1); // delay in between reads for stability
}
I would like to get alternatively, depending on the letter H or L, the two sounds flush1 and flush2, whereas I get always flush2. I suppose the if condition is not right. What is the good format? Thanks for your help!
javascript if-statement node-serialport
I have the following server:
var express = require('express'); // include express.js
var app = express(); // a local instance of it
// serial port initialization:
var SerialPort = require("serialport"),
Readline = require('@serialport/parser-readline')
var portName = process.argv[2];
var portConfig = {baudRate: 9600};
// open the serial port:
var myPort = new SerialPort(portName, portConfig);
// look for return and newline at the end of each data packet:
var parser = myPort.pipe(new Readline({ delimiter: 'n' }));
parser.on('data', console.log);
// get an analog reading from the serial port.
// This is a callback function for when the client requests /device/channel:
function getSensorReading(request, response) {
// the parameter after /device/ is the channel number:
var channel = request.params.channel;
// send the channel number out the serial port
//and wait for a response:
myPort.write(channel, function(){
// when you get a response from the serial port,
//write it out to the client:
myPort.on('data', function(data) {
// send the data and close the connection:
response.end(data);
});
});
}
// start the server:
var server = app.listen(8080);
// start the listeners for GET requests:
app.use('/',express.static('public'));
app.get('/device/:channel', getSensorReading);
Express serves an index.html page which is:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16 /p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16/addons/p5.dom.min.js"></script>
<script>
var label, serverResponse; // UI elements
function setup() {
label = createSpan("Volume:"); // the label
label.position(10,20); // position it
serverResponse = createSpan(); // create a div for server responses
serverResponse.position(label.width + 15, 20); // position it
getData(); // make a request back to the server
}
// this function makes a call to the server:
function getData(channel) {
httpGet('/device/' + channel, update);
}
// update the page when the server responds:
function update(data) {
serverResponse.html(data); // put the response in the span
getData(); // make another call to the server
}
var flush1 = new Audio('1110.ogg');
var flush2 = new Audio('1115.ogg');
if(serverResponse == 'H') {
flush1.play();
}else{
flush2.play();
}
</script>
</head>
<body>
An Arduino sketch is providing 'H' an 'L'' to the server:
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
if (buttonState ==1)
{
// print out the state of the button:
Serial.println('H');
}else{
// print out the state of the button:
Serial.println('L');
}
delay(1); // delay in between reads for stability
}
I would like to get alternatively, depending on the letter H or L, the two sounds flush1 and flush2, whereas I get always flush2. I suppose the if condition is not right. What is the good format? Thanks for your help!
javascript if-statement node-serialport
javascript if-statement node-serialport
asked Nov 22 '18 at 12:16
Francois BaretFrancois Baret
63
63
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430842%2fhow-to-write-in-an-html-page-an-if-else-condition-using-serialport-on-respon%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430842%2fhow-to-write-in-an-html-page-an-if-else-condition-using-serialport-on-respon%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown