У меня есть следующий сервер:
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);
Экспресс обслуживает страницу index.html, которая:
<!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>
Эскиз Arduino предоставляет 'H'an' L '' для сервера:
// 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
}
Я хотел бы получить альтернативно, в зависимости от буквы H или L, два звука flush1 и flush2тогда как я всегда получаю flush2.Я полагаю, если условие не правильно.Какой хороший формат?Спасибо за вашу помощь!