In a previous tutorial, we covered how to connect and read from DS18B20 sensors with your Beaglebone. I’d now like to cover the process of displaying live-updating DS18B20 temperatures in your browser using a Beaglebone Black. We’ll be using Socket.IO, a simple library that can be used to quickly communicate between a web browser and the server using the web sockets protocol. This tutorial will display all connected DS18B20 sensors in a nicely formatted table. This is geared towards developing a basic understanding how to use nodeJS with Socket.IO and the DS18B20 sensor. It can be used as a groundwork to use temperature readings in graphs and other formats. I use very similar code to turn on or off my connected SSR and pump relays.

DS18B20 Temperatures In Your Browser


I will assume you already have the following installed:


The Beaglebone and Raspberry Pi nodeJS development community is quite active. This means there are a lot of libraries available that make it simple to do tasks on your Beaglebone. For example, we will be using a package specifically for reading DS18B20 temperatures.

Install NodeJS Packages

In a SSH session, move the current directory to your webserver’s document root (i.e .e /var/www) and install the necessary Node packages.

The necessary packages should now be installed into the directory ./node_modules/

Create app.js

Using your favorite text editor or IDE, let’s create the app.js file. This file is the node script that will be performing most of the work. It is responsible for the periodic reading of sensor values and sending them to connected web browsers.

Copy and paste the following into the file app.js:

Save and close as app.js.


Next you will need to create the file index.html. This file is loaded by your web browser and contains code to handle variables emitted from socket.IO.

Save and close as index.html.

Wrapping it up

You’re ready to test out your node script! Start the script using node app.js. You’ll need to leave open your current terminal window as the process is linked to your login when you run it like this.

Open up index.html in your web browser. In a few seconds, you should see live updating temperature readings from each connected DS18B20 sensors.

Giving sudo to PHP is one of the several ways to execute a shell command from a web interface. It is probably not one of the most secure methods, so I recommend not doing this on a server which has outside (WAN) internet access because of the possibility of malicious code being executed. A person with ill intent could then access your entire network, including any shared files, network attached storage, etc. You need to be careful with this.

First, I’ll cover alternative methods and then I’ll jump into how to do it with PHP.

Alternative Methods

  1. Use a text file to serve as the current command for the GPIO pin. Use a cron job to execute a python script every minute that reads the text file and parses the instructions for each relay.
    • Cons
    • Slow. Cron jobs can only be executed as frequently as every minute. That’s how the system was designed. There are work arounds to this by using a while loop in python code, though.
    • Requires more coding
  2. Granting super user to www-data
    • Cons
    • Easily the least secure method of doing this. By granting superuser to www-data, you are giving superuser to every file that is executed by apache. This is a huge security risk that probably will result in compromise of your server. Don’t do it.
  3. A variant of #1 is storing the desired state of the system in a MySQL database and reading it from python. This may be the simplest method because interacting with MySQL in python is very easy to do.
  4. Running NodeJS and using to execute commands. This involves running a webserver in Node and interacting with the Node script with I may cover how to do this in a future write-up.

Editing Your sudoers

Now, on to how to give permission to PHP. You are modifying /etc/sudoers to allow a specific PHP file to have sudo rights. I’ll mention security again. If you have an unsecure script, say you are using HTTP GET to send the command to the script, users with access will have free power to execute any command they wish.

  1. Edit the sudoers file to include your script. Note that you really should use visudo instead of editing the file directly as this is a safer method. You obviously need root do this.
  2. You will see your sudoers file in a text editor. Here is a screenshot of mine:
    sudo to PHP
  3. Now we are going to add the permission for www-data (Apache user) to our sudoers file. We don’t want to prompt for a password, so we add NOPASSWD. Add the following text under the #User privilege specification section:
  4. Your sudoers should now look something like this:
  5. You can get very specific in your sudoers permissions by only allowing specific commands to be executed. This is a very secure method of ensuring nothing malicious gets executed:

    Note that this is not file-specific, so any file executable by www-data will be able to run this command. This would be useful if you are controlling your GPIO pins in multiple scripts, but you could do this easier using a function() to call this command.
  6. Close and save the file. How to do this will depend on the editor. I am running debian, so nano was the editor used. Press ctrl+x followed by “y” to save the file. If you are using vi, hit esc and then type “:x”.

We’ll cover how to do the actual control in a later post.

Using a DS18B20 temperature sensor on a BeagleBone isn’t difficult, but requires a bit of coding. Let’s get started with an introduction on the different types of sensors.

Choose A Sensor

The actual DS18B20 sensor is around the size of a pea. You can order it pre-wired in a few different configurations, or purchase just the sensor itself. For my application, I am using the already waterproof sensor shown here. It is enclosed in a stainless thermowell and wrapped with shrinkwrap.

Wire It Up


Each manufacturer seems to use a different color for the data wire, so yours may differ. Here is how mine was setup:

  • Red – 3V
  • White – Data
  • Black – GND


You will need to connect a 4.7k resistor between the data wire and 3V wire. This diagram shows 5V, but we can still have it work using 3V. You only need one if you are using multiple sensors. More on this below.


Connect Sensor to the BeagleBone

Next, you will need to choose a GPIO pin to plug your DS18B20 into. I used this image to help visualize where each pinout was located . For this example, I used the P8 header, pin number 11.
Notice that the number of the pin does not correspond to the GPIO number! Also of importance, you cannot simply choose any pin. Some of the pins are reserved- for example, most of the higher numbered pins on the P9 header are taken up by the HDMI interface unless you have disabled it.

Which pins are free are detailed in the next step.


Preparing to Build the Device Tree

If you’re familiar with raspian (the optimized debian distro running on Raspberry Pi’s), you’ll recall the setup is essentially plug and play thanks to raspian’s kernel. On the Beaglebone, it’s consistent with the newer linux kernel so you will need to compile a device tree. At it’s most basic level, a device tree simply informs the kernel about the location and type of a specific piece of hardware.
Now that we are wired up, boot your Beaglebone and login with SSH. We now need to compile a device tree for the one-wire interface. This is where noting the GPIO pin becomes important. Open the P8 or P9 PDF below and find the head pin you used. You may need to click ‘Raw’ if you are taken to Github.


P8 Header


P9 Header

Now, let’s get into some coding. Login via SSH. Using your favorite text editor, copy and paste the below code (I usually use nano). If you are using a different GPIO pin, you will need to make some changes. See below.

If you are using a pin other than P8_11, you will need to make a few changes. See the PDF header tables linked above for GPIO information.

  • On line 19, change exclusive-use = "P8.11"; to the corresponding header and header pin you used.
  • On line 25, change 0x34 to your corresponding GPIO offset. The offsets are found in the PDF’s above. For example, if I used P8_12, the code should be 0x30. Leave 0x37 where it is.

Save your edit and return to the shell.

Build the Device Tree

Now we can build the file using dtc:

Missing [email protected] flag error:
If you come across this error, you probably have a dated version of dtc. You can download a newer version that is compatible at:

Copy it to /lib/firmware and load:

You should be set now. To ensure everything has loaded correctly, run the following command. If everything is correctly configured, you should see Override Board Name in your list.

You will need to run the echo w1 > /sys/devices/bone_capemgr.9/slots command on every boot. To do this automatically, add the line to your /etc/rc.local file somewhere after the comments section.

Taking a Temperature Reading

Your temperature sensors are located at /sys/devices/w1_bus_master1/. They are of the format 28-00000xxxxxxx and correspond to your sensor’s unique serial number.
To view the current value just cat the file. You will have two lines of output. The temperature (in celsius) is at the very end.

The current reading is 16.4 degrees celsius.

Using Multiple DS18B20 Sensors

This is where the one wire interface really shines. You can connect up to about ten temperature probes and still use the same GPIO pin. This is incredibly useful and makes using multiple sensors clean and mess-free. You still only need to use one 4.7k resistor. The sensors are connected as shown in this diagram: