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.
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.
Ok, so we need to install PHP
and then lighttpd with the fastcgi module
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:
Using whichever text editor you prefer, edit lighttpd.conf and uncomment mod_fastcgi:
Further down the file, find and uncomment (remove the # from) the following lines:
You need to make sure the path to php-cgi is correct. You can find the correct path with:
Now restart lighttpd using:
You can double check is it working using:
and you should get something similar to:
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:
Ok so we can now get on with checking that PHP is working. Create a file as follows:
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.
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:
and similarly to turn an LED off, we can use:
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:
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.