Introduction: Control Arduino Using PHP

Picture of Control Arduino Using PHP

I have recently seen a lot of problems regarding PHP and Arduino. Many people don't know about facts like that an arduino needs a 2 second (!!!) delay before it can receive any messages through serial. There are a few other facts you need to pay attention to, I will be going over them in this instructable.

Step 1: Install a -AMP Webserver

It is important you have a webserver with PHP 5.3 or higher. My recommendation is to use LAMP for linux, WAMP for windows and XAMP for osx (though I don't know if it works on osx).

To install LAMP on debian (Ubuntu, turnkey, RPI, etc.) you want to execute the following command:

sudo apt-get install lamp-server^ Don't forget the ^ here!

On windows you just have to go to http://www.wampserver.com/

Check if the webserver is working by going to "localhost". If not:
You might have skype installed, check its documentation on how to disable it on port 80

Step 2: (Debian Only) Add Permissions for Serial Port

By executing the following command, you grant PHP to use the serial port (USB)

If you are not running Linux you don't have to run this command, in fact, you can't.

For other linux distributions you are going to have to check its documentation (same for osx).

usermod -a -G dialout www-data

(might be executed as sudo)

Step 3: Get PhpSerial

Now, you are going to have to download PhpSerial, which is a cross-platform solution for controlling serial ports. We will be using this to send our messages to our arduino.

Go to https://github.com/Xowap/PHP-Serial and download zip (on the right side)

Extract the zip and get copy the file src/PhpSerial.php to your %SERVERROOT%/www/ directory

Step 4: Finally! Write Your Arduino Code

Now you are going to have to write some code for your arduino. I have an example below:

#include "USBSerial_main.h"

int incomingByte = 0; int pin = 2;

void setup() {

Serial.begin(9600); //Starts the serial connection

pinMode(2, OUTPUT); //Sets pin 2 to be output

}

void loop() {

if(Serial.available() > 0){

Serial.read(); /Removes the message from the serial cache

digitalWrite(2, true); //Enables the led on pin 2

delay(100); //Waits 100 ms

digitalWrite(2, false); //Disables the led on pin 2

}

}

Step 5: Creating Your Webpage

I have created an example page that uses the PhpSerial library.

This is easier when you are on unix-based systems, then you can just use fopen (check the php docs on that)

Upload the file with your code to %SERVERROOT%/www and go to localhost.

Here's the code:

<?php

$comPort = "/dev/ttyACM0"; //The com port address. This is a debian address

$msg = '';

if(isset($_POST["hi"])){

$serial = new phpSerial;

$serial->deviceSet($comPort);

$serial->confBaudRate(9600);

$serial->confParity("none");

$serial->confCharacterLength(8);

$serial->confStopBits(1);

$serial->deviceOpen();

sleep(2); //Unfortunately this is nessesary, arduino requires a 2 second delay in order to receive the message

$serial->sendMessage("Well hello!");

$serial->deviceClose();

$msg = "You message has been sent! WOHOO!";

}

?>

<html>

<head>

<title>Arduino control</title>

</head>

<body>

<form method="POST">

<input type="submit" value="Send" name="hi">

</form><br>

<?=$msg?>

</body>

</html>

Comments

Onairam (author)2015-08-10

Thanks for the tutorial, but it returns some errors:

Warning: Specified serial port is not valid in C:\xampp\htdocs\www\PhpSerial.php on line 120

Warning: Unable to set the baud rate : the device is either not set or opened in C:\xampp\htdocs\www\PhpSerial.php on line 221

Warning: Unable to set parity : the device is either not set or opened in C:\xampp\htdocs\www\PhpSerial.php on line 289

Warning: Unable to set length of a character : the device is either not set or opened in C:\xampp\htdocs\www\PhpSerial.php on line 342

Warning: Unable to set the length of a stop bit : the device is either not set or opened in C:\xampp\htdocs\www\PhpSerial.php on line 395

Warning: The device must be set before to be open in C:\xampp\htdocs\www\PhpSerial.php on line 149

Warning: Device must be opened in C:\xampp\htdocs\www\PhpSerial.php on line 661

How could i fix it?

BartE5 (author)Onairam2017-04-07

You have to give persmission to use the COM port:

sudo chmod 666 ttyACM0 (where ttyACM0 should be the right address).

PramodK4 (author)Onairam2015-08-18

Hi ihave the same errors how did you fix it .

I am using windows

Onairam (author)PramodK42015-08-18

Hi, i couldn't fix it yet, i guess ill try it on linux

rhbvkleef (author)Onairam2015-08-10

this means that either the arduino is not connected or the port name is wrong ($comPort variable) try to list all files in /var/ while the arduino is plugged in and out. then see which appears when arduino is plugged in and use that as the com port address

Onairam (author)rhbvkleef2015-08-10

Thanks, maybe it doesn't work properly on windows because (i think) i wrote the port name correctly; but i'll better try it on linux and see what happens

rhbvkleef (author)Onairam2015-08-11

I would love to help now, I do have some code that *should* work on Windows but I currently don't have access to that, or even an arduino...

guille_140 (author)2016-07-02

Where is "USBSerial_main.h" from?

rhbvkleef (author)guille_1402016-07-03

The USBSerial_main.h is the header for this sketch, it can be left out.

aggrav8d (author)2016-01-24

Where is "USBSerial_main.h" from?

When PHP opens the serial connection the Arduino reboots. Every. Single. Page hit.

I solved that problem with two Arduinos: https://wp.me/p6QGAE-2iU

Good info, thanks for posting!

Hey people I am about to embark on a project using this software. Design objective : create a browser screen logged into Pi running a robot. The Pi and PHP will be used to replace the Infrared controller as an easy-control-screen via ERX. Will keep you informed

rhbvkleef (author)IanM702015-11-16

hey! nice to see this. good luck!

may I suggest using another language, like python for the backend. or use php which then calls something like python. That way, you are also able to process inputs from the robot and respond to it without user intervention.

ratheeshbr (author)2015-10-07

How can we read data from arduino?

rhbvkleef (author)ratheeshbr2015-10-07

it is not a good idea to read data using PHP because of large delays, you should then move to some other language like python.

ratheeshbr (author)rhbvkleef2015-10-07

Its a PHP based project. What should I do using python? Read the data to a text file and then read using php?

rhbvkleef (author)ratheeshbr2015-10-08

PHP is not meant to be reading on interrupt. You should use python or some other realtime language to store the responses and read them in using PHP on a later time, on web request or maybe even trigger a CRON/cli and notify the client to do a new web request.

ratheeshbr (author)rhbvkleef2015-10-07

Thanks for the reply

ShawnB14 (author)2015-05-28

DCengineer (author)2014-06-10

One question: what benefit does being able to program in PHP provide?
NOTE: I don't know PHP, but I do know C++ and Python, JavaScript shouldn't be hard to learn.

About This Instructable

42,752views

57favorites

License:

More by rhbvkleef:Control Arduino using PHP
Add instructable to: