Programming with BoneScript on the BeagleBone Black

en.pdf24.org    Send article as PDF   

This Article 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.

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

Introduction

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 http://192.168.7.2:8888, your web browser requests your index.html file that is stored on your BeagleBone.

framework web server beaglebone black

192.168.7.2 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:

bbb_bonescript_cloud9

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:

 

<br /> BeagleBone<br />

LED

Status

 

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.htmlfiles 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 http://192.178.7.2:8888. You see a web page similar to the following figure.

bbb_bonescript_web_page

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:

ifconfig

Our Ethernet IP address is highlighted in the following figure.

bbb_bonescript_ifconfig

Now you can type your Ethernet IP address followed by the port number (example http://192.168.1.80:8888) in a web browser of any device.

Now you can control any outputs remotely!

bonescript web server led

Previous Post

Install Cloud9 IDE on the BeagleBone Black

The Cloud9 IDE is an open-source web based programming platform that supports several programming languages. This great piece of software comes installed on your BeagleBone Black by default. And in ... Read more

Next Post

Programming the BeagleBone Black with Python

Building a surveillance system with a PIR sensor, the BeagleBone Black and Python. The BeagleBone Black is an outstanding tool for projects that involve the Internet. Access is easy (simply ... Read more

Short URL: http://tinyurl.com/juw76n6

Leave a Reply

Your email address will not be published. Required fields are marked *



Do NOT follow this link or you will be banned from the site!
error: Content is protected !!