Programming the BeagleBone to turn on LED, Got NodeJS errors

The assignment is about embedded system. We are learning how to use BeagleBone Black and how we can use it to make small-size devices e.g., devices that can measure temperature and pulse.

A part of our first assignment is to follow this guide: https://randomnerdtutorials.com/programming-the-beaglebone-black-with-bonescript/

We need to make a server in Node JS and a index in html. The site provides button to control a LED light that is connected to a breadboard via BeagleBone Black.

I have connected the LED, pins and wires to the BeagleBone Black. Installed Ubuntu 18.14, NodeJS, npm socket.io and Bonescript(Script dedicated to BeagleBone).

I am not using Cloud 9 IDE to run the server.js and index.html. But I use terminal in Ubuntu.

To start the server i use this command: node server.js

I tried for several days to make the server and index.html to run, but I get this error or nothing happends:

/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

Can anyone help me pinpoint the problem? I am really stuck on this stage. Thanks.

index.html code:

<!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 code:

//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 ..."));

Upvotes: 0

Views: 424

Answers (2)

Settasak
Settasak

Reputation: 26

I have same problem but solve it already the code on this website is based on older node version, you have to change the code on line

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

to

var io = require('socket.io')(server);

and edit variable or remove this line since newer node cant use .listen function twice (the code already use it on server var to open port 8888)

server.listen(console.log("Server Running ..."));

Upvotes: 1

veryverde
veryverde

Reputation: 454

socket.io is an internal library, not an external one. Therefore, when you ran npm install socket.io, you downloaded something that is not the socket.io that you want.

Delete your node_modules, and remove socket.io from package.json, and reinstall bonescript via npm install. Then it should work.

Upvotes: 1

Related Questions