Tag Archives: CPU temperature

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

So, three things to improve on the last post:

First, I’ve created a github repository which contains the source for our project. It can be found here: https://github.com/bitstobrowser/socketTemp.

To run the code, you can download it either directly to your BeagleBone Black or to your desktop and use a utility, like WinSCP, to transfer it to your BBB. Once on your BBB, navigate to the directory and start the server by executing the following command: node server.js.

Second, let’s quickly review the code to get an understanding of how it works:

BBB Temperature Server

BBB Temperature Server

  • Lines 3-6: Define the set of libraries that we need to use. Importantly, we tell socket.io to listen for any connections on the server’s port.
  • Line 8: Here we listen for any HTTP requests on port 8888.
  • Lines 10-21: Serve up a client.html file to all browser HTTP requests
  • Line 23: Sets a timer to go off every 2500ms or 2.5 seconds. We’ll use this to read and update the attached browsers.
  • Lines 25-26: Is the user file which contains the temperature information
  • Lines 28-32: This is the function that is called every 2500ms and it reads the contents of the temperature file and then calls another function to send the information out to any clients.
  • Lines 34-39: This puts the temperature in a JSON object and then emits (sends) the data to all attached socket connections.

At the client end, it’s even less lines of code:

BBB Socket Temp Client

BBB Socket Temp Client

  • Lines 3-4: We specify the JavaScript libraries that we will need to reference.
  • Line 6: Creates a socket connection, using the same port as the specified in the browser access.
  • Line 7: Defines what we do in case we get a socket message of type ‘temperature’ – we will call the handleTemperature function.
  • Lines 9-14: When we receive a ‘temperature’ function, we unpack the JSON object to get the value sent to us. We convert to Fahrenheit for our display. Then finally on line 13, we update the HTML as marked by the “temp” ID with the latest temperature information.
  • Lines 18-22: This is our basic HTML display – line 22 will be adjusted whenever we receive a message by the handleTemperature function.

Third, I’ve put a better version of the video up – much better resolution:

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: