Beaglebone Black (BBB) Simple Web IO

Some time ago I wrote a short guide to getting simple webpage based control of the GPIO pins working on a Raspberry Pi.  I thought it time to do something similar for the BBB.

First off, I removed unwanted services but you may not be bothered about that so consider it optional.

After that, install Lighttpd and Php5 – see below.

Remove unwanted services

The BBB uses systemd to manage services so in order to prevent a specific service from being started, we just need to remove the symbolic link.  This can be done manually if desired (edit contents of .wants files under /etc/systemd/system) but systemd provides a simple way to achieve what we want so we will use that.

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

With that done, reboot.

Install Lighttpd and Php

If this is the first time you have installed anything on your BBB, you may need to update the installation package repository.

opkg update

Ok, so we need to install PHP

opkg install php php-cgi php-cli

and then lighttpd with the fastcgi module

opkg install lighttpd lighttpd-module-fastcgi

Once that is all installed, we need to enable the fastcgi module so edit the lighttpd config file located at /etc/lighttpd.conf and find the server.modules line.  Personally I prefer using nano as a text editor rather than vi.  Nano can be installed with:

opkg install nano

Using whichever text editor you prefer, edit lighttpd.conf and uncomment mod_fastcgi:

server.modules = (
#                               "mod_rewrite",
#                               "mod_redirect",
#                               "mod_alias",
                                "mod_access",
#                               "mod_cml",
#                               "mod_trigger_b4_dl",
#                               "mod_auth",
#                               "mod_status",
#                               "mod_setenv",
                                "mod_fastcgi",
#                               "mod_proxy",
#                               "mod_simple_vhost",
#                               "mod_evhost",
#                               "mod_userdir",
#                               "mod_cgi",
#                               "mod_compress",
#                               "mod_ssi",
#                               "mod_usertrack",
#                               "mod_expire",
#                               "mod_secdownload",
#                               "mod_rrdtool",
#                               "mod_webdav",
                                "mod_accesslog" )

Further down the file, find and uncomment (remove the # from) the following lines:

fastcgi.server = ( ".php" =>
                   ( "localhost" =>
                     (
                       "socket" => "/tmp/php.socket",
                       "bin-path" => "/usr/bin/php-cgi"
                     )
                   )
                 )

You need to make sure the path to php-cgi is correct.  You can find the correct path with:

which php-cgi

Now restart lighttpd using:

systemctl restart lighttpd &

You can double check is it working using:

systemctl status lighttpd.service

and you should get something similar to:

lighttpd.service - Lightning Fast Webserver With Light System Requirements
Loaded: loaded (/lib/systemd/system/lighttpd.service; enabled)
Active: activating (start) since Mon 2013-06-17 08:27:49 UTC; 13s ago
Main PID: 390 (lighttpd)
CGroup: name=systemd:/system/lighttpd.service
|-390 /usr/sbin/lighttpd -D -f /etc/lighttpd.conf
|-391 /usr/bin/php-cgi
|-392 /usr/bin/php-cgi
|-393 /usr/bin/php-cgi
|-394 /usr/bin/php-cgi
|-395 /usr/bin/php-cgi
|-396 /usr/bin/php-cgi
|-397 /usr/bin/php-cgi
`-398 /usr/bin/php-cgi

Once that is done, it is useful to check that the PHP module is working correctly. There is a simple way to do that by creating a basic php file. Before that though, a simple bit of house keeping can make it easier for a user to create web content.

The pages that are served by lighttpd are by default stored in “/www/pages“. The simplest way to enable a user to write content to that location is to change the owner and group for the “/www” directory and subdirectories to “www-data“. To achieve this:

chown -R www-data:www-data /www

Ok so we can now get on with checking that PHP is working. Create a file as follows:

<?php
phpinfo();
?>

Save this in the “/www/pages” directory with a name of you choosing (e.g. phpinfo.php) and then back in the browser, access the page by appending it to the IP address (e.g. 192.168.7.2/phpinfo.php). If all has gone to plan, you should be presented with a page showing you details about the PHP configuration.

Ok, with that done, you can now successfully serve pages that you place in the “/www/pages” directory.  The simplest way to demonstrate control from the webpage is to create a PHP file which will display a couple of buttons and respond to button press events by turning on or off one of the user LEDs.

Controlling LEDs

Fortunately, the LEDs already have an interface in the root file system which we can access therefore it is not necessary for this task to get involved with Device Tree Overlays.  We can simply write to the “brightness” file for the LED we wish to modify.  If we write 1 to it then we turn it on and conversely if we write 0 to it, we turn it off.

The 4 controllable LEDs appear in the file system under “/sys/class/leds/beaglebone:green:usrX” where X is 0 to 3 and they are already set for read, write and execute for all users so we do not need to worry about file permissions.

In order to turn an LED on, we can use the simple echo command as follows:

echo 1 > /sys/class/leds/beaglebone:green:usr3/brightness

and similarly to turn an LED off, we can use:

echo 0 > /sys/class/leds/beaglebone:green:usr3/brightness

That works well as a simple proof that we can switch it on and off but we want to do that from a webpage so we need to create a PHP file as follows:

<?php
/*
 *
 * Copyright 2013 Martin Berriman
 *
 * Redistribution and use with or without modification, are permitted
 * provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 */ 

$result = 'nothing';

if (isset($_POST['set_off']))
{
 $result = 'turned off';
 exec("/bin/echo 0 > /sys/class/leds/beaglebone:green:usr3/brightness");
}

if (isset($_POST['set_on']))
{
 $result = 'turned on';
 exec("/bin/echo 1 > /sys/class/leds/beaglebone:green:usr3/brightness");
}

?>

<html>
<head>
 <title>Martin's Simple BBB WebIO Demo</title>
 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<body>
 <h1>Martin's Simple BBB WebIO Demo</h1>
 <p>Click on the button below to turn user LED:3 (D5) on or off</p>

 <?php if (isset($result)) { ?>
 <h1> Action: <?php echo $result ?></h1>
 <?php } ?>

 <form action="" method="post">
 <p><input type="hidden" name="set_on" value="yes"/><input type="submit" value="LED On"/></p></form>

 <form action="" method="post">
 <p><input type="hidden" name="set_off" value="yes"/><input type="submit" value="LED Off"/></p></form>
</body>

</html>

and save it as index.php to the “/www/pages” directory.

Assuming that is the only .php file and there are no index.html files in the pages directory, simply visiting 192.168.7.2 should show us a page with two buttons.  Pressing the buttons will turn the LED on or off.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>