Skip navigation

BeagleBoard

3 Posts authored by: CharlesGantt

For those of you who have been following along with my BeagleBone Black and Drupal series,  you could see this post coming. In my first installment I showed you how to build a website on the BeagleBone Black using the Drupal CMS, and followed it up with Controlling the BeagleBone Black's GPIO Pins with Drupal. This tutorial is going to take things a step further and show you how to take that knowledge, and use it to trigger some relays to turn mains powered electronics on and off. While the method I am going to use would not be ideal for conrtolling an entire home, it works quite well for controlling things like props in a haunted house and other low-amperage things like small fans, lights, and such. Just to make things easier, I am going to recap a good bit of my previous tutorial here. You will still need to go through the Drupal setup steps in the first tutorial though.

 

 

 

Prerequisites: What you need to know to be able to complete this project.

  • Understand how Drupal-based websites work.
  • Understand how PHP can be used to parse commands to the web server's terminal.
  • Understand the basics of how Transistors work, and why they should be used for applications like this.
  • Understand how the BeagleBone Black accesses its GPIO Pins.

 

Hardware Required: What hardware is needed to complete this project.

  • BeagleBone Black (Rev C recommended) BeagleBone Black (Rev C recommended) running the latest Debian image.
  • 5mm Green LED
  • 1x 460Ω 1/4w Resistor
  • 3x 3.3kΩ 1/4w Resistors
  • 3x 100Ω 1/4w Resistors
  • 3x 2n3904 General Purpose NPN Transistors
  • Female to Male Jumper Wires
  • Male to Male Jumper Wires
  • 5V Power Source for BeagleBone Black

 

 

Getting started: Building the discrete circuit and connecting to the Beaglebone Black.

 

Just like last time, we need to set up a transistor that will be used to supply the trigger voltage to the relay coils. The transistor circuit is needed because the BeagleBone Black is only able to supply about 8mAh to each of its GPIO pins and connecting something that draws more than 8mAh can damage the board's ARM chip. We can turn on a general-use PNP transistor with just a few mAh which will let us power the LED externally, and prevent any damage to the board. This is also handy for controlling relays and other components that can be triggered via the GPIO pins. Follow the diagram below to build the simple circuit. Instead of setting up the circuit to illuminate a LED, this time we will configure it to trigger the relay. We will be using a cheap 8-channel relay board to connecto to our circuit, so we will need a common ground, as well as two signal lines to the first two relays.

 

Relay-BBB.jpg

 

With the breadboard populated with our transistor circuits and connected to our BeagleBone Black, lets move onto the software side of things. We will come back and connect the relay board later in the tutorial.

 

 

Setting up the GPIO Pins in Debian: Using the terminal to configure specific GPIO pins in Debian.

 

To test the circuit we need to access our BeagleBone Black via SSH. I covered how to do this in the BeagleBone Black RSS News Reader tutorial, but will post it here as well.

 

To access the BeagleBone Black via SSH I will be using a terminal for Windows called Putty, as well as an add-on called MTPutty, which allows me to run multiple putty instances from within a single tabbed interface. Both Putty and MTPutty are available for free, and take just a few moments to download and install. Chances are, if you are reading this, you already know how to use Putty. If you are using a Mac you can simply use the built-in Terminal to access your Black Via SSH. To connect to your Black, you need to enter the following credentials.


Putty-Config.jpg

With these settings configured, click ok and then double click the BeagleBone Black name in the server list. This will open up a new terminal tab and you will be prompted to enter a username. Unless you have modified the root user, you should enter root as the user name.

 

Putty-connect-2.jpg

 

BeagleBone has made it easy here as there is no password and after entering the username, the connection will finish, and you will be placed in your "home" directory.

Putty-connect-3.jpg

 

Now that you are SSHed into the BeagleBone Black we will need to prepare GPIO_67 (P8:08), GPIO_68 (P8:10), and GPIO_44 (P8:12) . The pinout diagram can be seen below.

 

bbb-pinout.jpg

 

Before we get started with testing, lets take a moment and talk about how the BeagleBone Black's GPIO pins are addressed by the Linux-based Debian operating system. The BeagleBone Black addresses its GPIO Pins by reading the contents of files which are located in the sys/class/gpio directory in the board's operating system. The most basic functionality is to write the pin high or low, and is what we will be doing in this tutorial.

 

To use a GPIO Pin you first need to tell Debian that you are going to use a particular pin in a specific GPIO header. The following code uses GPIO 8.8 as an example, but any GPIO Pin can be accessed by creating these files. Enter the following commands into the BeagleBone Black's terminal.

 

The first thing we need to do is create a few new directories in the /sys/class/gpio/sys/class/gpio directory.

echo 67 > /sys/class/gpio/export
echo 68 > /sys/class/gpio/export
echo 44 > /sys/class/gpio/export



















 

At this point you will need to let the BeagleBone Black know whether or not the specified GPIO pins are input or output.

echo out > /sys/class/gpio/gpio67/direction
echo out > /sys/class/gpio/gpio68/direction
echo out > /sys/class/gpio/gpio44/direction




















 

With this command, the BeagleBone Black no longer supplies 2.3V to the header and it is considered to be turned off. In order for us to set the GPIO Pins high or low we need to set their value to  either 1 or 0. We can do that with the following commands where the gpio# variable is set to the corresponding pin #. An example for gpio_67 is listed below.

 

Set the GPIO Pin High

echo 1 > /sys/class/gpio/gpio67/value




















 

Set the GPIO Pin Low

echo 0 > /sys/class/gpio/gpio67/value




















 

Remove control of the GPIO Pin when finished.

echo 67 > /sys/class/gpio/unexport




















 

You will also need to chown and chmod the directory for each GPIO pin used. This can be done by entering the following commands.

chown -R www-data:www:data /sys/class/gpio/gpio67/value
chown -R www-data:www:data /sys/class/gpio/gpio68/value
chown -R www-data:www:data /sys/class/gpio/gpio44/value
chmod 777 /sys/class/gpio/gpio67/value
chmod 777 /sys/class/gpio/gpio68/value
chmod 777 /sys/class/gpio/gpio44/value


















 

Testing The Circuit: Using the terminal to trigger the GPIO Pin.

 

With those basics out of our way, we can test out setup by turning the LED connected to GPIO_67 on and off. Connect a 9-volt battery to our breadboard and head back to the terminal window and follow the steps below to turn the LED on and off via the command line.

 

9-volt-power.jpg

 

With everything connected, we should be able to use the command line to set GPIO Pin 67 high, which should in turn activate the transistor and allow the LED to light up.

Set the GPIO Pin High

echo 1 > /sys/class/gpio/gpio67/value




















 

If everything was followed correctly, you will see the LED light up

 

LED-Illuminated.jpg

 

Now set the GPIO Pin to low to turn off the circuit.

Set the GPIO Pin Low

echo 0 > /sys/class/gpio/gpio67/value




















 

 

Setting up the Relay Board: Connecting the Relay Board to the BeagleBone Black.

 

If those commands worked, then everything is set up as expected. We can now connect the 8-channel relay board and test to see if it is working correctly. I'm using a cheap relay board I bought on Ebay a while back, but Newark and MCM Electronics both have several you can purchase. My board is quite simple and only requires a VCC input of 5V and a GND connection. Each relay has a trigger pin broken out, and these are where you will attach the GPIO pins via the transistor circuits we built earlier. Since my board only requires 5V to trigger the relay coils, I am going to power it from the BeagleBone Black. Generally this is frowned upon, but since I am only using two relays at once I am letting it slide. If you try triggering more than 2 relays at once, they could try to pull more current from the BeagleBone Black than it can supply and the relays will not trigger and the board could be damaged. If you plan on triggering all 8-relays I would suggest powering it with an external 5V 2amp power source. You could also use the 9-volt battery and a L7805 +5V Linear Voltage Regulator to trim 9-12V down to 5V. Follow the instructions below. I could not find my resistor assortment, and had to risk connecting the relays directly to the BeagleBone Black since they would only be used for the duration of this tutorial.

 

IMAG0662.jpg

 

  • For this tutorial,  connect the two relays to the BeagleBone Black by connecting the VDD_5V (P9:05) pin to the relay boards VCC connection header.
  • Connect the BeagleBone Black's Ground DGND (P9:01) to the Relay Boards GND pin via the transistor circuit.

 

As I mentioned above, I could not find my resistors to complete the transistor circuits, so follow the directions below to connect the relays to the transistor circuits.

 

  • Connect the transistor that is connected to the  BeagleBone Black GPIO_68 (P8:10) pin to the relay 1 pin on the relay board.
  • Connect the transistor that is connected to the  BeagleBone Black GPIO_44 (P8:12) pin to the relay 2 pin on the relay board.

 

Now with those relays connected, its time to test that they work. Click back into the terminal and enter the following commands. If all is right, both relays should click on and off. Note that some relay boards are set as normally closed, and will appear to function the opposite you think they should. You will have to reverse the on and off commands for the relays to function correctly. This is as easy as swapping the 0 and 1 around in the code.

 

Turn Relay 1 on:

echo 0 > /sys/class/gpio/gpio67/value

















 

Turn Relay 1 off:

echo 1 > /sys/class/gpio/gpio67/value

















 

Turn Relay 2 on:

echo 0 > /sys/class/gpio/gpio67/value

















 

Turn Relay 2 off:

echo 1 > /sys/class/gpio/gpio67/value

















 

If they clicked on and off, then proceed to the next step. If they did not then repeat the process to see if you missed something.

 

 

Configuring the Drupal website: Adding the additional control buttons to the website.


With all of the hardware working its now time to configure the website that will let us control each relay as well as the LED remotely. Instead of writing 2000 more words and making this tutorial impossibly long, I have recorded another video to guide you through the configuration process. Check it out below and remember to subscribe to the channel if you like what I am trying to do!

 

 

When you get to the page creation phase, you can find the needed code examples pasted below.

 

LED Code:

 

Relay 1 Code:

 

Relay 2 Code:

 

With the website now configured, you should have a working remote automation system. You can use this to control a number of things such as lamps, irrigation systems, and even props in things like haunted houses, or film sets. I first designed this system to control a simple fog machine for my home on Halloween via my smartphone, but sadly the fog machine burnt out a week before the project was complete. So as you can see in the video below, I am using it to control a light bulb on my office desk. I plan on streamlining the system before next Halloween, and writing a few tutorials here on automation with the BeagleBone Black using triggers such as an Android app, motion sensors, and even pressure plates. If there is enough demand, I would consider turning it into a monthly series so leave me a comment if this is something you might like to see.

 

bbb-web-gpio-banner.jpg

 

In my last tutorial I covered the installation of Drupal onto the BeagleBone Black and how to setup an Apache-based web server. In this tutorial, I am going to continue building on that foundation and cover the basics of using that Drupal-based website to control the GPIO pins on the BeagleBone Black using PHP to run bash scripts. This will, in turn, allow you to use the internet to control anything that is connected to the BeagleBone Black's GPIO pins, and will further the foundation for some basic home automation tutorials I will write in the coming months.

 

Prerequisites: What you need to know to be able to complete this project.

  • Understand how Drupal-based websites work.
  • Understand how PHP can be used to parse commands to the web server's terminal.
  • Understand how LEDs work, and what the terms Anode and Cathode mean.
  • Understand the basics of how Transistors work, and why they should be used for applications like this.
  • Understand how the BeagleBone Black accesses its GPIO Pins.
  • Basic knowledge of soldering.

 

Hardware Required: What hardware is needed to complete this project.

  • BeagleBone Black (Rev C recommended) BeagleBone Black (Rev C recommended) running the latest Debian image.
  • 5mm Green LED
  • 1x 460Ω 1/4w Resistor
  • 1x 3.3kΩ 1/4w Resistors
  • 1x 100Ω 1/4w Resistors
  • 1x 2n3904 General Purpose NPN Transistors
  • Female to Male Jumper Wires
  • Male to Male Jumper Wires
  • 5V Power Source for BeagleBone Black
  • 5V Power Source for BeagleBone Black (Optional without BB View cape, Required if BB View Cape is used.)
  • Solder
  • Flux
  • Soldering Iron

 

Getting started: Soldering the resistor to the LED



For the purpose of this tutorial we are going to use an LED to indicate when a specific GPIO pin has been toggled on and off. As I am sure many of you reading this already know, Light Emitting Diodes (or LEDs) are one of the most revolutionary electronic components to have ever been created. In fact, this year's Nobel Prize in Physics was awarded to the team of scientists who created the efficient blue LED, which has revolutionized the way humans light the world. Since LEDs are so efficient, and sip very little power, the 3.3v coming out of the BeagleBone Black's GPIO pins would fry the LED if we tried to connect it without a current limiting resistor. While this is old-hat stuff for most of you, there will be some readers who will find this section interesting and helpful, and that is why I am including it. Below is a diagram that shows exactly what makes up your typical 5mm LED.

 

led-anatomy.jpg

Since the LED is a diode, electricity will only flow one direction. This is why a LED is polarized, unlike a resistor that works in any orientation. In the diagram above, you will notice that the Anode (+ side) is the smaller element inside the LED, and that the Cathode is actually the leg that houses the crystal that emits the light. Another way to tell the Anode from the Cathode is that the Anode will usually be longer than the Cathode. Finally, the Cathode is usually closest to the flat spot on the LED's base ring.

 

Placing the resistor on the Anode or Cathode will work, but I prefer to place it on the Cathode. This is just personal preference, but I find it to be considered best practice in several textbooks and guides. There is also no set standard for attaching a resistor directly to the leg of the LED, but I generally tend to go for the "wrap it around neatly" method. I also like to trim the Cathode down so that the resistor's lead matches up with the Anode leg length. For breadboard work I like to build easily reusable LED modules that include the resistor and are soldered to a 2-pin male header.

 

LED-Breadboard.jpg

 

With that finished, we can now move on to setting up the transistor circuit on a breadboard. The transistor circuit is needed because the BeagleBone Black is only able to supply about 8mAh to each of its GPIO pins and connecting something that draws more than 8mAh can damage the board's ARM chip. We can turn on a general-use PNP transistor with just a few mAh which will let us power the LED externally, and prevent any damage to the board. This is also handy for controlling relays and other components that can be triggered via the GPIO pins.

 

bbb_breadboard_led.jpg

 

Here is a quick diagram of how I wired up the transistor circuit and attached the LED.

 

Breadboard_actual.jpg

 

You will notice that I have a 9Volt battery powering the LED via the transistor. You do not have to use a 9V battery, and anything above 5V will work, but I would not use a power supply over 12v for this application.


bbb_circuit_breadboard.jpg

 

With the Transistor Circuit wired up, lets connect the BeagleBone Black. Basically we simply need to connect one of the GPIO Pins to the Transistor, and carry over the BeagleBone Black's ground circuit to our bread board. With all of this connected, we can now test the LED using the BeagleBone Black's terminal.

 

Testing The Circuit: Using the terminal to trigger the GPIO Pin.

 

To test the circuit we need to access our BeagleBone Black via SSH. I covered how to do this in the BeagleBone Black RSS News Reader tutorial, but will post it here as well.

 

To access the BeagleBone Black via SSH I will be using a terminal for Windows called Putty, as well as an add-on called MTPutty, which allows me to run multiple putty instances from within a single tabbed interface. Both Putty and MTPutty are available for free, and take just a few moments to download and install. Chances are, if you are reading this, you already know how to use Putty. If you are using a Mac you can simply use the built-in Terminal to access your Black Via SSH. To connect to your Black, you need to enter the following credentials.


Putty-Config.jpg

With these settings configured, click ok and then double click the BeagleBone Black name in the server list. This will open up a new terminal tab and you will be prompted to enter a username. Unless you have modified the root user, you should enter root as the user name.

 

Putty-connect-2.jpg

 

BeagleBone has made it easy here as there is no password and after entering the username, the connection will finish, and you will be placed in your "home" directory.

Putty-connect-3.jpg

 

Now that you are SSHed into the BeagleBone Black we will need to prepare GPIO 67 (P8:08). The pinout diagram can be seen below.

 

bbb-pinout.jpg

 

Before we get started with testing, lets take a moment and talk about how the BeagleBone Black's GPIO pins are addressed by the Linux-based Debian operating system. The BeagleBone Black addresses its GPIO Pins by reading the contents of files which are located in the sys/class/gpio directory in the board's operating system. The most basic functionality is to write the pin high or low, and is what we will be doing in this tutorial.

 

To use a GPIO Pin you first need to tell Debian that you are going to use a particular pin in a specific GPIO header. The following code uses GPIO 8.8 as an example, but any GPIO Pin can be accessed by creating these files. Enter the following commands into the BeagleBone Black's terminal.

 

The first thing we need to do is create a new directory called gpio67 in the /sys/class/gpio/sys/class/gpio directory.

echo 67 > /sys/class/gpio/export







































































































 

At this point you will need to let the BeagleBone Black know whether or not the specified GPIO pin is input or output.

echo out > /sys/class/gpio/gpio67/direction







































































































 

With this command, the BeagleBone Black no longer supplies 2.3V to the header and it is considered to be turned off. In order for us to turn on GPIO Pin 67 we need to set its value to 1. To turn it back off again, the value needs to be 0. We can do that with the following commands.

 

Set the GPIO Pin High

echo 1 > /sys/class/gpio/gpio67/value







































































































 

Set the GPIO Pin Low

echo 0 > /sys/class/gpio/gpio67/value







































































































 

Remove control of the GPIO Pin when finished.

echo 67 > /sys/class/gpio/unexport







































































































 

With those basics out of our way, let's move on to testing. Since we already have GPIO Pin 67 setup, and the rest of the circuit above is in order, we just need to give our breadboard power via a 9-volt battery. Connect the 9-volt to the breadboard as shown in the image below.

 

9-volt-power.jpg

 

With everything connected, we should be able to use the command line to set GPIO Pin 67 high, which should in turn activate the transistor and allow the LED to light up.

Set the GPIO Pin High

echo 1 > /sys/class/gpio/gpio67/value

































































































 

If everything was followed correctly, you will see the LED light up

 

LED-Illuminated.jpg

 

Now set the GPIO Pin to low to turn off the circuit.

Set the GPIO Pin Low

echo 0 > /sys/class/gpio/gpio67/value
































































































 

Configuring The Website: Using the internet to control the GPIO Pin.


If you have been following along in my BeagleBone Black series you will recall that my last tutorial demonstrated how to install an apache-based web server on the BeagleBone Black, and how to install and configure a Drupal-based website on top of that web server. For this next part, you will need to follow the steps in that tutorial and get a Drupal 7 website up and running on your BeagleBone Black before continuing with this tutorial.

 

Pull up the previously created website, and login as the administrator by visiting the (my_website_url)/user.

 

There are a few different ways that we can build functionality into our website to control the GPIO pins on the BeagleBone Black. Note that the following methods only work when the website is hosted on the BeagleBone Black. We are going to begin with the two simplest methods. Method one will create two separate pages that run a php script when visited, while method two creates toggle buttons within a single page or block. While these methods work, they are highly vulnerable to security breaches, and should only be used on sites that are not directly open to the public.

 

 

Method One: Controlling the GPIO via separate web pages.

 

Since the BeagleBone Black GPIO Pins are accessible using the command line, we can write a simple PHP script that turns any of the pins on and off using the commands listed above. First, we need to turn on the PHP filter in Drupal. (Note that turning this filter on in Drupal exposes your website to security vulnerabilities, and as such it should only be done on sites not publicly available) Navigate to (my_website_url)/admin/modules/ and scroll down until you see the PHP Filter module under the Core heading. Enable this module by ticking the box to the left, then scroll to the bottom of the page and click save.

 

phpfilter.jpg

Creating the LED On Page

 

With the PHP filter enabled create a new basic page by navigating to (my_website_url)/node/add/page. Fill out the page with the following information, or watch the video embedded below and follow along.

  • Title: LED ON
  • Body:
    • <?php
      exec("/bin/echo \"1\" > /sys/class/gpio/gpio67/value");
      ?>
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
  • Text Format: PHP Code
  • Menu Settings:  Tick the box for Provide Menu Link
    • Menu Link Title: LED On
    • Parent Item: <Main Menu>
    • Weight: 1
  • Publishing Options:  Tick the box for "Published" and make sure the other two options are not checked.

 

Now click the "Save" button at the bottom of the page. If everything was entered correctly, you can return to the home page and there will be a new main menu link called "LED On."

LED ON Menu.jpg

 

Creating the LED Off Page

Now we need to create a web page that turns the LED off. Create a new basic page by navigating to (my_website_url)/node/add/page. Fill out the page with the following information, or watch the video embedded below and follow along.

  • Title: LED Off
  • Body:
    • <?php
      exec("/bin/echo \"0\" > /sys/class/gpio/gpio67/value");
      ?>
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
  • Text Format: PHP Code
  • Menu Settings:  Tick the box for Provide Menu Link
    • Menu Link Title: LED Off
    • Parent Item: <Main Menu>
    • Weight: 2
  • Publishing Options:  Tick the box for "Published" and make sure the other two options are not checked.

 

Now click the "Save" button at the bottom of the page. If everything was entered correctly, you can return to the home page and there will be a new main menu link called "LED Off."

LED Off Menu.jpg

Setting Permissions

With both control pages created, we need to modify the permissions on the gpio67/value directory so that the www-data user (Apache) can write to it. Run the following command from within the terminal.

chmod 666 /sys/class/gpio/gpio67/value






















































 

With this done, we are able to test that everything works. With the transistor's base pin connected to GPIO67 and the 9-volt battery powering the breadboard, you should be able to click on the LED On menu link and the LED will turn on. Clicking the LED Off Menu Link will turn the LED Off. If your LED is not turning on and off, revisit the steps above. Alternatively, you can watch the video below and follow along.

 

 

Method Two: Controlling the GPIO via toggle buttons on a single page or block.


While turning the LED on and off with separate web pages works, it is not the most efficient or best way to do things. This method could come in handy if you wanted to trigger a GPIO event whenever someone visits a webpage, but for general user triggered On and Off use, buttons would be a far better method. To trigger the GPIO pins via web buttons, we will utilize some existing code that I found during a quick search. This PHP code creates two basic buttons that fire off php scripts when pressed. You can view the original code at http://www.berriman.co.uk/beaglebone-black-bbb-simple-web-io/.

 

To make the code work we will need to modify two lines to point to the specific GPIO pin we are using to turn our LED circuit on and off. I have pasted the code you will need to use in the Page Building section below.

 

Creating the LED On / Off Toggle Button Page

Now we need to create a web page that turns the LED off. Create a new basic page by navigating to (my_website_url)/node/add/page. Fill out the page with the following information.

  • Title: LED On and Off Buttons
  • Body:
  • Text Format: PHP Code
  • Menu Settings:  Tick the box for Provide Menu Link
    • Menu Link Title: LED On and Off Toggle Buttons
    • Parent Item: <Main Menu>
    • Weight: 3
  • Publishing Options:  Tick the box for "Published" and make sure the other two options are not checked.

 

Now click the "Save" button at the bottom of the page. If everything was entered correctly, you can return to the home page and there will be a new main menu link called "LED On and Off Toggle Buttons." Click on that page and click the LED On button and then the LED Off Button. The LED should illuminate and then turn off. If this is correct, we can move on, otherwise revisit the steps above.

 

LED bittons.jpg

 

Creating the LED On / Off Toggle Button Block

Create a new block by navigating to (my_website_url)/admin/structure/block/add. Fill out the block with the following information, or watch the video embedded below and follow along.

  • Title: Access LED
  • Block Description: GPIO67 LED Buttons
  • Body:
  • Text Format: PHP Code
  • Region Settings:
    • Bartic (Default Theme)
      • Sidebar First

Now click save block. You can change the weight of this block by visiting the Block Administration page, and navigating to  (my_website_url)/admin/structure/block. Now you will see the LED Toggle buttons when visiting any page on the website. The beauty of this block / button method is that you can now place your GPIO buttons anywhere on the website, and not have to display them as a page.

 

button block.jpg

 

There are many ways to do this that are specific to Drupal, but the PHP code used in this last example will work on any web framework, and can be used independently as well. One of the safer ways to execute this code would be to set up a rule and use a flag to control when each script is run. That is more advanced Drupal development work than I want to get into in this tutorial, but I would be glad to write a separate tutorial on it if there is enough interest. For the advanced Drupal developers out there, you can head over to my Github and download a feature module with this functionality built in. You will still have to create the node, and install some modules though.  In the mean time, I urge everyone to play around with modifying this code, and making some awesome stuff.

 

 

My next tutorial will involve modifying this code to trigger a set of relays, and notification LEDs that can be used to control anything from a basic lamp to a more full featured home automation system.

Banner.jpg

 

When the BeagleBone BlackBeagleBone Black was first released, I was one of the first journalist in the country to receive one, and impressed did not begin to describe how I felt about the little black board. At the time, I was still riding a high from my experiences with the Raspberry Pi, and while the Black lacked the camera and LCD connectors, it made up for it in on-board flash and processing power. Unfortunately I was very disappointed in the Angstrom Linux distro that shipped with the Black, but did manage to flash the board to Ubuntu with the help of a friend at my local MakerSpace. Over the next several months, I played with the Black here and there, and made a few small projects here and there. When revision C of the BeagleBone Black came out running Debian from the factory, I knew I had to have one. It was also around this time that I made a major career change, and left tech journalism for a more lucrative position as a Drupal developer at a friends web development firm. While playing around with my new BeagleBone Black one night, I began wondering how hard it would be to get a web server up and running and if the Black would be able to handle MySQL and Apache2 along with a Drupal Install.

 

Prerequisites: What you need to know to be able to complete this project.

  • Understand how to install packages from the terminal.
  • Understand how a LAMP-based web server works.
  • Understand how Drupal is installed and configured.
  • Understand how RSS feeds work.
  • Understand basic HTML and XHTML.

 

SSHing into the BeagleBone Black via Putty

 

To access the BeagleBone Black via SSH I will be using a terminal for Windows called putty, as well as an addon called MTPutty which allows me to run multiple putty instances from within a single tabbed interface. Both Putty and MTPutty are available for free, and take just a few moments to download and install. Chances are, if you are reading this, you already know how to use Putty. If you are using a Mac you can simply use the built-in terminal to access your Black Via SSH. To connect to your Black, you need to enter the following credentials.


Putty-Config.jpg

With these settings configured, click ok and then double click the BeagleBone Black name in the server list. This will open up a new terminal tab and you will be prompted to enter a username. Unless you have modified the root user, you should enter root as the user name.

 

Putty-connect-2.jpg

 

BeagleBone has made it easy here as there is no password and after entering the username, the connection will finish, and you will be placed in your "home" directory.

Putty-connect-3.jpg


To get things started, I first needed to get MySQL installed and running on the BeagleBone Black, and I will be perfectly honest in saying that it was no easy task starting off. Since revision C of the Black runs Debian Wheezy from the factory, I knew that apt-get install would be used to install new packages now where as opkg was used with the Angstrom distro on earlier models. Unfortunately that is where I began running into problems. When I would run anything via apt-get install the whole install process would run, but then quickly error out due to a script called led_aging.sh causing an error. After almost an hour of pulling my hair out, and searching the web, I found a thread in a mail archive where someone was having the same problem. As it turns out the script was missing some of its functions, and that was causing the dkpg error, but why a seemingly random script was causing apt-get install to fail is beyond my pay-grade. If you are interested in seeing the original mail archive with the information, it can be found here. I am going to cover this fix below for convenience though.


If you get an error surrounding the led_aging.sh script when running apt-get install on your BeagleBone Black, or during any terminal process, you will need to modify that script to include some missing functions. The process to do this is found below, but you will need a basic understanding of the Linux file structure and how to modify files via the terminal to complete this. You can also check out this code on github.

 

  • From your home directory navigate to /etc/init.d and then nano the led_aging.sh file that is inside the init.d directory.
  • Replace all of the code inside the led_aging.sh file with the following code.
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: led_aging.sh
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start LED aging
# Description: Starts LED aging (whatever that is)
### END INIT INFO

x=$(/bin/ps -ef | /bin/grep "[l]ed_acc")
if [ ! -n "$x" -a -x /usr/bin/led_acc ]; then
  /usr/bin/led_acc &
fi


























































































 

  • Then exit out of nano, keeping the filename the same as it was.

 

This should have fixed the issue that pops up when trying to run an apt-get install command. Now that we have that error taken care of, lets move on to the rest of the project.

 

Installing and Configuring the Web Server

 

There are a few packages we need to install in order to get our web server up and running to be able to serve a Drupal installation. The packages we need to install are listed below.

  • MySQL Server
  • MySQL Client
  • Apache2

 

Before we get started, we need to make sure that Debian is up to date with the latest libraries, and doing that is as simple as running the following command.

 

apt-get update


























































































 

This will automatically download and install the latest update for the version of Debian that is running on the BeagleBone Black.

 

With the distro updated, we can now install MySQL Server and MySQL Client with a single command. For those of you following along at home, MySQL is the database of choice for Drupal installations, but you could run MariaDB, postgresql, or another variant of MySQL. To keep things simple and standardized, we will be running MySQL. To install both packages, use the command below.

apt-get install mysql-server mysql-client


























































































 

This will run through the install process. You will be prompted with some password creation prompts and you can set the passwords to whatever you wish, but make sure to write them down as they will be needed later.

 

With MySQL installed, its time to install Apache2 run the following command

apt-get install apache2

























































































 

Let the install process finish, and grab something to drink in the meantime. When the install finishes, install the following packages.

apt-get install php5
apt-get install php5-mysql
apt-get install php5-gd
apt-get install libapache2-mod-php5 
apt-get install php5-curl

 

With those packages installed, we can move on to building a Drupal-based website on the BeagleBone Black.

 

Installing and Configuring a Drupal-based Website

 

In the terminal, navigate to the srv/www directory, and create the directory in which we will install the Drupal instance too. We will need to create several sub-directories as well, including backup and error log directories as well as the website doc root.

cd /srv/www
mkdir bbb.news
cd /srv/www/bbb.news























































































 

Now create the sub directories using the commands located below

mkdir backup
mkdir download
mkdir logs























































































 

While only two of these directories are really needed, I prefer to follow what I consider to be best practice when installing drupal on any server. The backup directory is used to backup the website's database, making life much easier when things go bad. We will use the download directory to download the Drupal install tarball to. The logs directory will house two required error log files, and the htdocs directory will be where our website resides.

 

While we are at this level of the file structure, lets go ahead and create two files that apache will need later. Navigate to the logs directory and enter the two touch commands below.

cd logs
touch access.log
touch error.log























































































 

This creates the needed log files that will help troubleshoot issues if they occur.

 

With these files created we can navigate back to the main bbb.news directory and then into the download directory.

cd ../download























































































 

Once inside the download directory, we can use the wget feature to grab a fresh copy of the latest version of Drupal 7. While Drupal 8 is right around the corner, I chose to go with Drupal 7 because it is the most widely adopted version at the moment and has the most module support currently. Follow the commands below to download Drupal and untar it into the htdocs directory.

wget http://ftp.drupal.org/files/projects/drupal-7.32.tar.gz
tar -zxvf drupal-7.32.tar.gz
mv drupal-7.32 ../
mv drupal-7.32 htdocs

 

Now verify that the files were successfully extracted to the htdocs directory by entering the following commands and matching up your directory with the screenshot below.

cd ../htdocs
ls























































































 

drupal-root1.jpg
If the extraction was successful, you should see something similar to this.

 

Now we need to navigate to the sites/default directory and copy the default.settings.php file and rename it to settings.php, then set the permissions on the settings.php file to 777.

cd sites/default
cp default.settings.php settings.php
chmod 777 settings.php















































































 

To finish getting our Drupal site ready we need to build a database for it to store things in. You will need the MySQL username and password that you set up earlier in the tutorial for this. Use the following commands (with your unique credentials) to build the database we will use for the Drupal installation.

mysql -u (username) -p (password)














































































 

That should drop you into the MySQL terminal and from here you can create the database with the following commands.

create database (database_name);
grant all on (database_name).* to '(database_username)' identified by '(database_password)';














































































 

If everything was entered correctly, then you should see a line returned with the following command Query OK, 0 rows affected (0.00 sec) .  Be sure to write down the database name, database user name, and database password as you will need them shortly. If things are correct, you can now exit the MySQL terminal using the following command.

exit









































































 

Now lets set up the vhost file for our newly installed website. Navigate to the apache2/sites-available directory using the following command.

cd /etc/apache2/sites-available









































































 

Now we need to create a new virtual host file (vhost) for our newly installed drupal website. To do this, we are simply going to use the touch command to create the file. If you want to be able to access the website you are building from an external network, you will need to configure a new A-record on a domain name that you own, and point it to your home's IP address, then forward the ports in your router to the BBB for that domain name. I wont cover those aspects in this tutorial, but the internet is full of valuable resources that will help you along the way. For the purpose of this tutorial, I have already completed these steps, and that is why you see the charlesjgantt.com domain attached to the vhost. When doing this on your own, simply replace my domain name with yours. I will post some screen shots of what my setup looks like, to help you along though.

touch bbb.news.charlesjgantt.com




































































 

Now run an ls command to verify that the file was created.

ls



































































 

vhost1.jpg

If the file was created correctly, you should see it here.

 

Now we need to edit the file and paste in a few lines of text. Nano into the bbb.news.(your_domain.com) and paste the following text. Remember to remove the (your_domain) and replace it with your domain url.

nano bbb.news.(your_domain.com)


























































 

<VirtualHost *:80>

    ServerAdmin webmaster@(your_domain).com

    ServerName bbb.news.(your_domain).com

    DocumentRoot /srv/www/bbb.news/htdocs

    ErrorLog /srv/www/bbb.news/logs/error.log

    CustomLog /srv/www/bbb.news/logs/access.log combined

</VirtualHost>

 

Now save and exit out of nano by pressing ctrl+x and y when prompted then saving with the same name.

 

You have successfully created a proper Virtual Host file for the new Drupal site. Now we need to enable that site using the following command. Once enabled, apache will need to be restarted.

a2ensite bbb.news.(your_domain).com
service apache2 reload























































 

Below are the screenshots I promised of my A-Record entry in my DNS settings for charlesjgantt.com as well as the port forwarding settings in my router.

a-record settings.jpg

Your A-Record can be found in the DNS settings of your domain. Simply give it the sub-domain name you wish to use, and then point it towards your home IP address. (Mine has been blurred out for security purposes.)

portforwarding3.jpg

 

Now enter your router's settings and find the port forwarding settings. You want to point the port you set in the V-Host file (80) to port 80 on your router. After saving this, you may need to reboot your router depending on its model.

 

In preparation for the BB View_70 LCD we will cover later in this tutorial, we need to disable a few services on the BBB to ensure that port 80 is free to serve our new website.

systemctl disable bonescript.service  
systemctl disable bonescript.socket
systemctl disable bonescript-autorun.service
systemctl disable avahi-daemon.service 
systemctl disable cloud9.service  
systemctl disable gateone.service 




















 

With these items finished, we are ready to test the website and see if it works. From within your local network, open up a browser such as Chrome and visit http://bbb.news.(your_domain).com:80 and if everything is correct, you should be prompted with the drupal install page as seen below. You will have to select your preferred language, and then if the steps above were followed, you will be taken to the Database configuration tab. Select MySQL, and then enter the correct Database Name, Database Username, and Database Password that you set up a few minutes ago.

dbconfig.png

Now click save and continue, and proceed to fill out the requested information on the final few pages. When finished you should be prompted to visit your new website which should look something like this!

bbb.button.jpg

 

Now that we have the news website up and running on the BeagleBone Black, we need to begin importing "News" content to it. Before we can do that, we need to install a few modules and to make life easier, we are going to install a utility called Drush (Drupal Shell) that will make life much easier moving forward. While seasoned Drupal Developers will certainly cringe at the method I am about to use to install Drush, it will work fine for this tutorial. To install Drush using the preferred method, visit the install instructions found at https://github.com/drush-ops/drush

apt-get install drush




































 

Before we can install any modules we need to modify the permissions on the modules directory, and then create two new directories.

chmod -R 777 sites/all/modules
cd sites/all/modules
mkdir contrib
mkdir features




































 

When the Drush install finishes, navigate to the websites root, and then use the drush pm-install and pm-enable commands to install the needed modules. The final command we will run in this set will clear all of Drupal's various caches.

cd /srv/www/bbb.news/htdocs
drush dl admin_menu adminimal_admin_menu module_filter features feeds feeds_tamper feeds_imagegrabber job_scheduler strongarm context
drush en admin_menu adminimal_admin_menu module_filter features feeds feeds_tamper feeds_imagegrabber job_scheduler strongarm context -ydrush cc all




































 

With those modules installed and enabled, hard-refresh the webpage and then visit the modules page to verify that everything we needed was properly enabled. Go through the list and make sure that Context, Context UI Features, Feeds Admin UI, Feeds Import, Feeds News Feeds Tamper Admin UI, Feeds Tamper, Feeds, Feeds Image Grabber, Job Scheduler, Job Scheduler Trigger, and Strongarm are enabled.

modules_page.jpg

 

With all the necessary modules enabled, we can move forward and build a feed importer that will allow us to pull in news from any website with an RSS feed. For the purpose of this tutorial, I am going to pull new content from Element14's All Content RSS Feed which can be found at http://www.element14.com/community/groups/feeds/allcontent. I am also going to pull in a feed of the newest 3D models uploaded to httphttp://www.thingiverse.com. To keep all of this organized, I need to create a new content type for the Thingiverse feed. Before I decided what I would be importing, I named this new content type "Tech News" but its sole purpose is to import from Thingiverse, so ignore the name and make yours whatever you want.

 

To get started creating the new content type and feed, follow along with the videos below.

 

 

 

 

 

I apologize for not being able to show the Thingiverse Feed creation. At the time of recording, the Thingiverse RSS feed had a bug that prevented me from mapping the elements correctly, but basically it is the same as creating the previous feed, but involves using Feeds Tamper, and Feeds Image Importer to get the images displaying correctly. A quick good search will show you the best way to utilize these extension modules.

 

Now that we have the feeds importing, and the site is up and running as it should, lets take a moment and look at how we can add the BB View_70 7-inch Touchscreen LCD CapeBB View_70 7-inch Touchscreen LCD Cape to create a desktop rss news reader.  For those of you who are new to the BeagleBone Black, the BB View_70 is a 7-inch 18-bit, 800x480 resolution LCD screen with a 4-wire resistive touchscreen digitizer on top. The Cape features two user-defined LEDs, and five tactile switches of which 4 can be user defined.

c8bd85ad871e36df5647ba9f3588e75c.png

 

Originally, the BB View_70 ran on the Angstrom distro, but with Debian being added as the default OS in revision C of the BeagleBone Black, the BB View_70 cape is easier than ever to get up and running. I could cover that entire process here, but it is actually very thoroughly covered in the BB View User Manual beginning at page 23. Note that if you run the BB View_70, you will need to supply an external 5V 1amp power source to the BeagleBone Black before the screen will turn on.

 

bbb-stand-2.jpg

 

For those of you with access to a 3D Printer, you can head over to Thingiverse and download the BBVIEW 7 LCD + Beaglebone Black Stand by Chumsize. This stand provides a clean way to securely and safely mate the BeagleBone Black and the BB View_70 together. The stand holds the screen at the perfect angle for both viewing and touch screen navigation. The stand takes about 35 minutes to print on a decent printer running at about 80mm/sec and uses hardly any ABS filament. I did find that the holes designed to capture the standoffs need a bit of cleaning, and you will need to heat the standoffs with a cigarette lighter or soldering iron and then press them into the holes for a more secure fit. Overall I think this stand is one of the coolest things I have ever printed, and it has made my development time with the BB View_70 much easier.

 

bbb-stand-1.jpg

 

I'm a screen junky and at my home office, I have tripple 24" monitors, with about 30 tabs open in each at any given time. I know this is excessive, but having this little news reader on my desk really does make life easier. As an additional bonus, since it only imports the latest 10-items every hour, I spend less time down a news rabbit hole, and more time working. I hope this tutorial was as informative as I hoped it would be, and I would love to hear from anyone who makes their own news reader, or takes these instructions and builds something completely different. I have a few more tutorials coming out in the next couple of weeks that will build upon this basic starter, such as learning how to control the BeagleBone Black's GPIO pins via the internet using Drupal. Until then #HackTheWorld & #MakeAwesome!

 

final.jpg