Random Nerd Tutorials

Programming the BeagleBone Black with BoneScript

This entry is part 3 of 4 in the series Getting Started with BeagleBone Black. 

This post was written by Rui Santos and Luís Perestrelo authors of BeagleBone For Dummies.

This part introduces BoneScript, a Node.js library containing functions specially created for the BeagleBone Black. This library simplifies the process of accessing and configuring the pins of your BeagleBone Black.

Controlling an output, reading a button/sensor, controlling a motor are quite easy with BoneScript. If you used an Arduino before, you’ll find most functions very similar.


The BeagleBone is quite a powerful tool when it comes to home automation, because it provides easy access to the web and lots of GPIOs.

In this project you’ll create a web server that controls outputs and it can be accessed remotely with your smartphone, tablet or laptop.

The Framework

Your BeagleBone will be hosting a web server, establishing a communication between your device’s web browser and your BeagleBone GPIOs.

Your web server is created with some Node.js code, and when you access IP address, your web browser requests your index.html file that is stored on your BeagleBone.

framework web server beaglebone black is the local USB address of your BeagleBone and 8888 is the port that you’ll be using.

When you click one of the buttons on your web page, an event triggers and talks with your Node.js code, which has the package socket.io listening for an event to occur. Based on that event message, your Node.js code interacts your BeagleBone’s pins.

Parts required

For this project you need:

Wiring your circuit

Turning an LED on or off is the first thing you do when experimenting with a new technology. After completing this project you control any electronic component — such as relay — through a web page that you can access with your smartphone from the comfort of your sofa.

Web Server LED

Installing socket.io


You have to install a Node.js package called socket.io. Having your BBB connected to the internet, type the following commands in your terminal:

sudo npm install update
 sudo npm install -g socket.io

The first command updates the list of all available packages to the latest version, and the second one installs socket.io globally.

This package allows real-time communication between your web browser events and your BeagleBone. In other words, as soon as you click a button on your web page, your Node.js code that was listening acts immediately according to the message sent in that event.

Creating a folder and your files

We encourage you to use the Cloud9 IDE as often as possible to program your BeagleBone. First read the previous part: Cloud 9 IDE on the BeagleBone Black.

With the Cloud9 IDE open, you can create a folder and file in your workspace. Follow these steps:

  1. Right-click the cloud9 folder and choose New Folder from the shortcut menu.
  2. Name the new folder Projects.
  3. Right-click the Projects folder and choose New File from the shortcut menu.
  4. Name your file index.html.
  5. Repeat step number 3. and name your new file server.js.

This is how it should look like in the end:


Writing your Web Page

Your index.html file is very straight forward. It contains: a heading 2, a paragraph to output the current status of the LED and two buttons. Then you load your socket.io module and write a simple JavaScript script to send a message to your server.js. The code is well commented below:

Creating your Web Server

Your server.js file is the last piece of the puzzle. First you have to load all the required modules and  configure your BeagleBone Black’s pin. You also initialize a web server on port 8888. Then you establish the communication between server.js and index.html files using socket.io.

The  function called handleChangeState() turns your LED either on or off according to the message received from your index.html. The code is also commented so you can understand exactly what each line is doing:

Download all the code

Instead of copying and pasting you can click here to download all the code used in this project. Then you unzip that folder and upload the code into your BeagleBone Black.

Launching your web server

Launching your web server is easy. You simply save all two files. Click the green button “Run” in the Cloud9 IDE, and you should see a message in your output window that says “Server Running . . . “.

That’s it, your web server is up and running! Open a tab in your web browser, and type You see a web page similar to the following figure.


This web page only opens only if your computer is connected directly to the BeagleBone Black by USB. If that’s not the case, read the next section to see how to access the BeagleBone through a device connected in your workspace network.

Accessing your Web Server with another device

To access your web page in any device inside your network, you need your BeagleBone Black’s Ethernet IP address. Open your terminal and type the following command:


Our Ethernet IP address is highlighted in the following figure.


Now you can type your Ethernet IP address followed by the port number (example in a web browser of any device.

Now you can control any outputs remotely!

bonescript web server led


That’s all for now! In the next part you’ll learn about programming the Beaglebone Black with Python. If you enjoy this series make sure you subscribe here so you don’t miss our next blogs posts using this board.

Special thanks to Luís Perestrelo for helping Rui Santos putting this series together!

This is part 3, read part 4 now! ->

Learn ESP32 with Arduino IDE

This our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registrations are open, so SIGN UP NOW »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our course even with no prior experience!

Leave a Comment:

Add Your Reply

Grab our Electronics and Programming Courses