Instructables
Picture of Control an Arduino with PHP
Here's how to make your Arduino do things from over the internet without an ethernet or wifi shield. This is a simple way to make your project controllable from anywhere in the world!
 
Remove these adsRemove these ads by Signing Up

Step 1: Platform Compatability

This method WILL NOT work under Windows. I can't tell you how long it took me to figure that out. The command we're using, fopen() is not implemented the same way under Windows and Linux, so you have to use Linux. Never used it before? Don't panic, it's not that bad.

Step 2: Setting up your server

Picture of Setting up your server
You'll need to get a server running PHP for this to work. I'm going to assume you have a fresh copy of Ubuntu. There are literally thousands of tutorials on how to install Ubuntu, so I'll refer you to those if you don't. 

Open a terminal and type:
sudo apt-get install php5 mysql-server apache2

It will prompt you for a root MySQL password, so give it one.

Open a browser and go to http://localhost. You should see a page that says "It works!".  Next run this in a terminal:
sudo gedit /var/www/info.php

And paste in this code:
<?php
phpinfo();
?>

Save and close the file, then run
sudo service apache2 restart

Go to http://localhost/info.php
and you should see a purple table with a bunch of PHP stuff. If you do, congrats! You are now running a PHP server. Next, let's set up a page to control the Arduino.



jelimoore3 months ago

So I'd (like) to assume this also works under OSX, too?

brianfit4 months ago

Nice one. Was looking for help in writing to the serial port from a php sequence to trigger output on the Arduino, this was just the ticket. No Servos, I just needed buttons and LED response. Thanks, aloishis89!

SorinMS6 months ago

I've fixed the problem.

I give Apache permission to use the serial port. I did this in terminal
with the following command:

sudo adduser www-data dialout

Once this is done, I restart Apache with the following terminal command:


sudo /etc/init.d/apache2 restart

SorinMS6 months ago

It doesn't work for me.

hi, this only works for me if i have the arduino serial monitor open. if i close it and try then php just hangs. do you know why this might be?
for me /dev/tty.usbETC.. only worked when the arduino serial monitor was open, but if i used /dev/cu.usbETC... then it worked always (mac 10.8.4)

And even THAT isn't working with me, I'm on Mac as well.

Apparently when there is no serial connection happening, the arduino goes to sleep and takes a while to reboot. Go here http://playground.arduino.cc/Main/DisablingAutoRes... for some solutions. I just used the capacitor going to ground.

lkokoszka8 months ago

Do I have to allow PHP permissions to do anything special? I have been working over many a code and still nothing comes out of php other than ls and simple commands.

wdipa lkokoszka7 months ago

put this code-> $PHP_SELF="your file php name";

after this code

$verz="1.0";

$comPort = "com4"; /*change to correct com port */

example:

$verz="1.0";

$comPort = "com4"; /*change to correct com port */

$PHP_SELF="control.php";

lkokoszka wdipa7 months ago

Thanks for your help. I forgot I inquired about this issue. I actually got my code to work after changing some permissions on the usb device (making the specific USB port world writable). Probably not the most secure thing to do, but will also be ignoring escape characters or what not.

dmanovski8 months ago
Put code file on running. Web server and access it from web browser
sss11 months ago
very good tutorials that help me to create web application controller
thanks so much

very good tutorials that help me to find what I need to build a web based application and control from internet sensors Using Arduino board
Raphango1 year ago
Congratulations! I really appreciate your idea! When my just bought arduino arrives i'll try it!
ajindam1 year ago
For windows users, this code should do.

exec("mode COM3: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
$fp = fopen("COM3", "w");
fwrite($fp, "1"); //write string to serial
fclose($fp);
Thank you so much for posting this! I've been searching for a way to control a parallax propeller from a wamp server. This code got me up and running.
abubakr0221 year ago
where we have to put this code?