Tag Archives: cloud9

Project: BBB Admin Page – Connecting a BeagleBone node.js app to a browser using socket.io

Having established running a basic node.js application on the BeagleBone Black in the previous post, we can now progress to connecting our app to a browser. We’ll do this by running JavaScript on both the browser and our BBB, then allow them to share data in real-time using the socket.io library.

What we’ll do in this step is to read the on-board CPU temperature of the ARM 3359 processor, every second and inform any attached browser of the most recent value. In an obscure forum discussion, I was able to find out my Debian BBB-specific distro makes this CPU temperature register location available at the /sys/devices/ocp.3/44e10448.bandgap/temp1_input you may need to verify that this is available on your OS or if there is a slight variation of the location. Information on the bandgap register is available in the AM335x Technical Reference Manual (chapter 9). The temp_input file holds the current temperature (multiplied by 1000) – for example, in the image below, the CPU temperature is 53 C. On the web, you’ll see many caveats that this not an accurate measurement and should not be used for any serious application – but for our demo app, it serves the purpose of sending I/O changes from the BBB to any attached browser.

Location of CPU Temperature Information

Location of CPU Temperature Information

Our demo consists of two parts: the node.js server located on the BBB and a web page with a JavaScript connection to the BBB.

Node.Js CPU Temperature Server

Our node.js server does two things, first it serves up a web page, when requested, and secondly, every second it sends the latest temperature to all of the attached browsers.

Our first step to getting this working is to install socket.io using the node package manager (npm), which handles the transporting of temperature information to the browser.

Install socket.io using npm

Install socket.io using npm

Unexpected Detour to Fix Disk Size

I noticed that I was having problems doing any work, node started behaving weird and I started getting errors. I realized that the partition allocated for the Linux kernel on my SD card was full. While I had an 8GB SD lying around, I was able to stumble on this excellent set of instructions to expand the Linux kernel partition on my SD card, which allows me to continue to use my 4GB SD card, with a 10 minute procedure.

Here’s what my SD card’s Linux kernel partition looked like before:

Linux kernel partition on SD before

Linux kernel partition on SD before

Here’s the disk allocation after expanding the partition size:

Linux kernel partition on SD after

Linux kernel partition on SD after

And finally, after finishing up the resizing command:

Result of Linux kernel paritition resize

Result of Linux kernel partition resize

What I learned from this detour is that a 2GB SD card shouldn’t be used for Debian on BBB, the 4GB is fine, but consider right-sizing the partitions beforehand and, if available, start with a 8GB SD.

Temperature Sensor App

So back from our tangent, we’re ready to test out reading the on-board temperature measurement and displaying it in (near) real-time to a connected browser. I’ve written a very small node.js app called bbbTemp which performs this function.

I found a very useful utility, called WinSCP, to allow me to transfer files to the BBB from my Windows machine. WinSCP gives me the luxury of doing development and testing in Windows, then fine tuning and minor debugging on the BBB. (It’s one of the nice little tools that deserves a donation – I gave mine and I hope that you will too.)

WinSCP File Transfer

WinSCP File Transfer

In next week’s post, I’ll share the code, but in the meantime, here’s a video of our application running:

 

 

 

 

Advertisements

Project: BBB Admin Page – Running a Basic Node.js Program

The default Debian distro for BBB includes the Cloud9 Integrated Development Environment (IDE), which we can use to run a very basic program to blink LEDs. The Cloud9 IDE is available over the virtual USB to Ethernet port’s default IP address and port 3000:

Cloud9 IDE

Cloud9 Integrated Development Environment (IDE)

We’ll be using node.js technology to handle our requests, so let’s first confirm that we have node installed on our system. The best way to do this to simple ask for the version installed, if we get a response and the response is a latest build, then we’re ready to move on. We can issue a command directly from the Cloud9 bash window:

Get node.js version

Test node.js installation

We’ll be writing our programs in JavaScript within the node.js infrastructure. To get started we’ll load the demo/blinkled.js.

blinkled.js program

blinkled.js program

  • Line 1: Initialize the bonescript library
  • Line 3: Defines the leds variable, by referencing user space names for known outputs
  • Lines 5-7: Defines the leds as outputs
  • Lines 9-12: Sets the leds to off state
  • Line 14: Defines a timer that will go call the toggle function every 1000ms (1sec)
  • Line 16-22: If the led state is off, turn if on, else turn it off

We can now run the program by typing the following into the bash window: node demo/blinkled.js. The end result is the following:

Now that we have the programming environment in place, we’ll step up to create a web page that can allow us to control the LEDs.