Sunday, 29 July 2012 11:04

Control Arduino via PHP (EasyPHP)

Control Arduino via PHP Control Arduino via PHP

The Arduino Duemilanove (not Arduino Uno) is control by a computer over the network or internet via PHP. This article assumed that you already install WAMP web server & Arduino driver in your computer.

My Arduino Duemilanove is connecting to the USB port of my computer, the USB serial com port is detected as COM4.

  • Microsoft Windows, I'm using Windows XP SP3
  • Web Server, I'm using EasyPHP as my web server.
Determine the COM port
This step is based on Windows XP
  1. Click Start>Settings>Control Panel
  2. Double click System
  3. Click on Hardware tab & click Device Manager button
Your will see your USB serial list under Ports (COM & LPT) as shown in figure below 
COM Port for USB Serial

PHP Source Code: from my website download page
Arduino Sketch: Ocean Control KTA-223


Arduino Source Code
The Arduino source code use in this article is downloaded from Ocean Control. Upload this code to your Arduino if you don't want to buy the device from Ocean Control.


It is very easy to control the Arduino through COM port based on Ocean Control. See example below:

"@00 on 1\r" to turn on LED1
"@00 of 1\r" to turn off LED1
"@00 on 2\r" to turn on LED2
"@00 on 2\r" to turn off LED2

The command string must start will @ & follow by two zero address space which is intended to use for RS485.  But you still can use it for RS232.

Remember to put a \r at the end of the command string, it is use to terminate the string with Carriage Return. 

There is also a very detail PDF file on how to send the command string which can be download from Ocean Control.

PHP Source Code
The PHP code is very simple, but I encounter a problem that the Arduino always cannot be control when the first command send to the Arduino.  I must add a line at line 19 to open the COM port without any valid command in order to make the Arduino to working correctly.


Try & catch is used for error detection. It is also not working in my computer. If the Arduino is not connect to the computer, it just display some nonsense message as below. 

I had make a tutorial to explain how to use the serial command from my Tutorials page:

Running PHP Code
  1. Download the PHP source code & place to the WWW folder of the web server. 
  2. Open Internet Explorer or any browser
  3. Enter http://localhost/phpArduino.php (for EasyPHP 5.39) or http://localhost:8888/phpArduino.php (for EasyPHP 12.0). Note: The port 8888 might be changing.  

You will see 4 buttons display on top left of the browser screen, first two buttons use to turn on & off LED1, the next two buttons use to turn on & off LED2. For example, click on button 1 (label 1 on) will send the string command (@00 on 1\n) to the Arduino to turn on LED1, while click on button 2 (label 1 off) will send the string command (@00 of 1\n) to the Arduino to turn off LED1. 



Read 67758 times Last modified on Friday, 12 September 2014 22:50
Back to Top