Beaglebone and Websockets, a full example that turn on/off a led

After the first “hello world” example with ExtJS – I’ve written a complete example in node.js that turns on and off a led controlled by the board and sends, in broadcast, the status of the led changing the background color of the web page.

The Web App is hosted by the Angstrom distribution – – the default OS that comes with the Beaglebone –

Using Cloud9 (you can access with the url http://beaglebone.local:3000) it’s possible to write a really simple node.js application that exposes via websockets on 8090 (or another port) a web page (index.html) and serves as Websockates gateways using module.

Once the button on is pressed a message led on is sent to the board; the server intercept it and set to high the port P8_3. The code is based on the library bonescript included in Cloud9.

When the led is changed a websocket broadcast message ledstatus on is sent to all browsers connected. The browsers intercept the message and to change the background of the web page in green.

With this demo you can try by yourself the two-way communication using websockets. The cpu load on the board is near to zero.


    <script src = "/" > </script>
    <script type=text/javascript src=""></script>
        var socket = io.connect();
            socket.on('ledstatus', function (data) {
                $("body").css("background-color", data);
    function ledOn(){
      socket.emit('led', 'on');
    function ledOff(){
      socket.emit('led', 'off');

    <input type="button" name="on" id="onButton" value="on" onClick="ledOn();">
    <input type="button" name="off" id="offButton" value="off" onClick="ledOff();">

sock123.js. The main program to execute.

var app = require('http').createServer(handler);
var io = require('').listen(app);
var fs = require('fs');
var bb = require('./bonescript');

var ledPin = bone.P8_3;

setup = function() {
    pinMode(ledPin, OUTPUT);

function handler (req, res) {
  function (err, data) {
    if (err) {
      return res.end('Error loading index.html');


io.sockets.on('connection', function (socket) {
  socket.on('led', function (data) {
    if (data == 'on'){
        digitalWrite(ledPin, HIGH);
          socket.emit('ledstatus', 'green');
          socket.broadcast.emit('ledupdate', 'green');

        digitalWrite(ledPin, LOW);
        socket.emit('ledstatus', 'red');
         socket.broadcast.emit('ledupdate', 'red');

To run the application just press run on your Cloud9 and select sock123.js.

Now you can access to the application and turn on/off the led.
Check the video 🙂
You will see Chrome on MacOS Lion that updates the background color simultaneously according with the led status.

4 thoughts on “Beaglebone and Websockets, a full example that turn on/off a led

    • Thanks jkridner!
      It’s true! If I would have had time to go to my store and buy some resistors… for the first test it’s ok but I promise I’ll prepare a better test platform in the near future 🙂

  1. The simplest way to launch the script on boot is to add the line

    node sock123.js

    on your


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s