Controlling an Arduino with PHP or LAMP

As part of another project, I needed to confirm that I could control an Arduino with a PHP script.

After a bit of learning, and some programming, I was able to create a nifty little setup that allowed me to control up to 4 LEDs in a number of different ways.

I used a Rasberry Pi to host a web page programmed using Javascript/PHP which passes instructions to the Arduino by way of the USB port.

The Arduino was programmed to read and parse specifically formatted strings, and then, based upon a number of pre-programmed options, execute a series of functions to produce different effects in the LEDs.

I included functions to individually turn LEDs on and off, as well and make them blink.  I also included the ability to increase/decrease LED brightness, and change the rate of blink for each LED separately.

Video of an Arduino controlled by php web page.

Now that this side project is complete, I want to make as much of the information available in a coherent manner as possible.  Below you will find detailed steps, and links to code which will allow you to reproduce this setup, and improve upon it.

In this tutorial, I will attempt to provide either links, or a walk-through for each of the components involved.  However, I will assume some basic knowledge of Linux, Arduino, Electronics, etc.

  1. To host the web page, and PHP scripts, I used a Rasberry Pi running Rasbian, however, you can use any computer for this project.  I did all of my programming on a Dell D630 running Ubuntu before moving the scripts over.
  2. For the micro controller, I used an Arduino Leonardo.  Hypothetically, this should work on any of the boards, however as of now, it is untested, and even if it doesn’t, it should only require minor modifications to the code.

Things you will need:

  • A computer with a LAMP (Linux, Apache, MySQL, PHP) stack installed.
  • An Arduino.
  • 4 LEDs.
  • Breadboard.
  • Misc cables.

Outline:

  1. Setting up your LAMP stack.
  2. Setting up your Arduino.
  3. See it in action.

1. Setting up your LAMP stack.

In order for this to work, you will need a computer capable of running Linux.  It should not matter which distribution you choose; however, I will be providing steps for Ubuntu, and Rasbian.

Choosing Ubuntu –

To install Ubuntu follow this guide to installing Ubuntu Linux. Once that is done, you can install the AMP part of LAMP with a few quick terminal commands.

Choosing Rasbian –

To install Rasbian, follow the instructions outlined in this guide to getting Rasbian on your SD card and this guide to getting your Rasberry Pi configured for the first time.

Once Rasbian is installed, and running, open a terminal window and type the list of commands that follow one at a time.  Answer the prompts as they appear, and you will be done quite quickly.

sudo apt-get install apache2
sudo apt-get install mysql-server
sudo apt-get install php5
sudo apt-get install php5-mysql

Installing the Web Page –

At this point, you should be ready to install the web page, which is as simple as downloading this zip file containing the necessary scripts, and then extracting them to the “/var/www” directory on your LAMP device.

Configuring Serial Ports –

While programming on my Dell D630, the PHP scripts were able to pass the commands to the Arduino without issue.  Howeve, when I moved the scripts to the Rasberry Pi, I encountered an issue where the commands were getting jumbled inexplicably.  After quite a bit of searching, I was able to learn that you have to tell the OS which standards to use when writing to a socket.  I have included the necessary command in the PHP script.  However, if you find that you are having problems talking to your Arduino, you may want to try this terminal command:

stty -F /dev/ttyACM0 -brkint -icrnl ixoff -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

2.  Setting up your Arduino.

The first step to setting up your Arduino is downloading the sketch, and programming your Arduino.  You can find the sketch for this project here.

Next, you will need to wire your circuit.

web_arduino_circuit

Once you’ve done this, your only remaining step should be to plug the Arduino into the LAMP machine.

3. See it in action

To see the web controlled Arduino in action, open up the web page in the computer browser and start pushing buttons.