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.
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.
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.
- 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.
- 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.
- Misc cables.
- Setting up your LAMP stack.
- Setting up your Arduino.
- 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 –
Choosing Rasbian –
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.
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.