我正在我的覆盆子pi上运行nodejs webserver,我正在建立一个Web界面,用户可以根据预先设定的时间输入gpio引脚触发的日期和时间。我用html和java制作了一个简单的闹钟,但我不知道如何在闹钟初始化时将1和0发送到树莓派。
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Alarm Clock</title>
</head>
<body>
<input type="datetime-local" id="alarmTime" />
<button onclick="setAlarm(this);" id="alarmButton">Set Alarm</button>
<div id="alarmOptions" style="display: none;">
<button onclick="snooze();">Snooze 5 minutes</button>
<button onclick="stopAlarm();">Stop Alarm</button>
</div>
<script>
var alarmSound = new Audio();
alarmSound.src = 'alarm.mp3';
var alarmTimer;
function setAlarm(button) {
var ms = document.getElementById('alarmTime').valueAsNumber;
if(isNaN(ms)) {
alert('Invalid Date');
return;
}
var alarm = new Date(ms);
var alarmTime = new Date(alarm.getUTCFullYear(), alarm.getUTCMonth(), alarm.getUTCDate(), alarm.getUTCHours(), alarm.getUTCMinutes(), alarm.getUTCSeconds());
var differenceInMs = alarmTime.getTime() - (new Date()).getTime();
if(differenceInMs < 0) {
alert('Specified time is already passed');
return;
}
alarmTimer = setTimeout(initAlarm, differenceInMs);
button.innerText = 'Cancel Alarm';
button.setAttribute('onclick', 'cancelAlarm(this);');
};
function cancelAlarm(button) {
clearTimeout(alarmTimer);
button.innerText = 'Set Alarm';
button.setAttribute('onclick', 'setAlarm(this);')
};
function initAlarm() {
alarmSound.play();
document.getElementById('alarmOptions').style.display = '';
console.log("ALAAAARM");
};
function stopAlarm() {
alarmSound.pause();
alarmSound.currentTime = 0;
document.getElementById('alarmOptions').style.display = 'none';
cancelAlarm(document.getElementById('alarmButton'));
};
</script>
</body>
</html>
&#13;
我尝试过使用
var socket = io();
window.addEventListener("load", function(){
var Alarm = document.getElementById("alarmTime");
Alarm.addEventListener("change", function() {
socket.emit("alarmTime", Number(this.checked));
&#13;
这是js文件:
var http = require('http').createServer(handler);
var fs = require('fs');
var io = require('socket.io')(http)
var Gpio = require('onoff').Gpio;
var LED = new Gpio(4, 'out');
http.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/test.html', function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}
io.sockets.on('connection', function (socket) {
var lightvalue = 0;
socket.on('alarmTime', function(data) {
lightvalue = data;
if (lightvalue != LED.readSync()) {
LED.writeSync(lightvalue);
}
});
});
process.on('SIGINT', function () {
LED.writeSync(0);
LED.unexport();
pushButton.unexport();
process.exit();
});
&#13;
什么都没有用。 nodejs新手