gpt4 book ai didi

node.js - 对 BeagleBone 进行编程以打开 LED,出现 NodeJS 错误

转载 作者:行者123 更新时间:2023-12-04 18:59:02 46 4
gpt4 key购买 nike

作业是关于嵌入式系统的。我们正在学习如何使用 BeagleBone Black,以及如何使用它来制造小型设备,例如可以测量温度和脉搏的设备。
我们第一项任务的一部分是遵循本指南:https://randomnerdtutorials.com/programming-the-beaglebone-black-with-bonescript/
我们需要在 Node JS 中创建一个服务器,并在 html 中创建一个索引。该站点提供按钮来控制通过 BeagleBone Black 连接到面包板的 LED 灯。
我已将 LED、引脚和电线连接到 BeagleBone Black。安装了 Ubuntu 18.14、NodeJS、npm socket.io 和 Bonescript(专用于 BeagleBone 的脚本)。
我是不是 使用 Cloud 9 IDE 运行 server.js 和 index.html。
但我在 Ubuntu 中使用终端。
要启动服务器,我使用以下命令:node server.js
我尝试了几天让服务器和 index.html 运行,
但我收到此错误或什么也没有发生:

/home/ubuntu/bonescript/server.js:42
var io = require('socket.io').listen(server);
^

[TypeError: require(...).listen is not a function
at Object.<anonymous> (/home/ubuntu/bonescript/server.js:42:31)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
谁能帮我查明问题?我真的被困在这个阶段了。谢谢。
index.html 代码:
<!DOCTYPE html>
<html>
<head>
<title>Home Automation Web Server with BeagleBone</title>
<script src = "/socket.io/socket.io.js" ></script>
<script>
// Establishing connection with server
var socket = io.connect();

// Changes the led state
function changeState(state){
if (state==1){
// Emit message changing the state to 1
socket.emit('changeState', '{"state":1}');
// Change led status on web page to ON
document.getElementById("outputStatus").innerHTML = "Status: ON";
}
else if (state==0){
// Emit message changing the state to 0
socket.emit('changeState', '{"state":0}');
// Change led status on web page to OFF
document.getElementById("outputStatus").innerHTML = "Status: OFF";
}
}
</script>
</head>
<h2>LED</h2>
<p id="outputStatus">Status</p>
<button type="button" onclick="changeState(1);">ON</button>
<button type="button" onclick="changeState(0);">OFF</button>
</div>
</body>
</html>
server.js 代码:
//Loading modules
var http = require('http');
var fs = require('fs');
var path = require('path');
var b = require('bonescript');

// Create a variable called led, which refers to P9_14
var led = "P9_14";
// Initialize the led as an OUTPUT
b.pinMode(led, b.OUTPUT);

// Initialize the server on port 8888
var server = http.createServer(function (req, res) {
// requesting files
var file = '.'+((req.url=='/')?'/index.html':req.url);
var fileExtension = path.extname(file);
var contentType = 'text/html';
// Uncoment if you want to add css to your web page
/*
if(fileExtension == '.css'){
contentType = 'text/css';
}*/
fs.exists(file, function(exists){
if(exists){
fs.readFile(file, function(error, content){
if(!error){
// Page found, write content
res.writeHead(200,{'content-type':contentType});
res.end(content);
}
})
}
else{
// Page not found
res.writeHead(404);
res.end('Page not found');
}
})
}).listen(8888);

// Loading socket io module
var io = require('socket.io').listen(server);

// When communication is established
io.on('connection', function (socket) {
socket.on('changeState', handleChangeState);
});

// Change led state when a button is pressed
function handleChangeState(data) {
var newData = JSON.parse(data);
console.log("LED = " + newData.state);
// turns the LED ON or OFF
b.digitalWrite(led, newData.state);
}

// Displaying a console message for user feedback
server.listen(console.log("Server Running ..."));

最佳答案

我有同样的问题但已经解决了这个网站上的代码是基于旧 Node 版本的,你必须在线更改代码

var io = require('socket.io').listen(server); 
var io = require('socket.io')(server);
并编辑变量或删除此行,因为较新的 Node 不能使用 .listen 函数两次(代码已经在服务器 var 上使用它来打开端口 8888)
server.listen(console.log("Server Running ..."));

关于node.js - 对 BeagleBone 进行编程以打开 LED,出现 NodeJS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69790149/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com