Food Living Outside Play Technology Workshop
Control Arduino using PHP
by rhbvkleef on June 10, 2014
Table of Contents
Control Arduino using PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Intro: Control Arduino using PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Step 1: Install a -AMP webserver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Step 2: (Debian only) Add permissions for serial port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Step 3: Get PhpSerial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Step 4: Finally! Write your arduino code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Step 5: Creating your webpage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
http://www.instructables.com/id/Control-Arduino-using-PHP/
Intro: 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).
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() {
http://www.instructables.com/id/Control-Arduino-using-PHP/
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>
http://www.instructables.com/id/Control-Arduino-using-PHP/
Related Instructables
Control an
How To Get Transform Paperduino Tiny
Arduino with
Started With Ubuntu 8.04 To (Photos) by
PHP by
Linux by Chad Look Like Mac
Arduino to How to connect aloishis89 ok1cdj
Baxter OSX by
Processing: Arduino to a PC
KenGriffin
Serial through the
Communication serial port by
without Firmata dvdllms87
by danm_daniel
Advertisements
Comments
2 comments Add Comment
DCengineer says: Jun 10, 2014. 9:25 PM REPLY
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.
Danger is my middle name says: Jun 10, 2014. 4:33 PM REPLY
Good info, thanks for posting!
http://www.instructables.com/id/Control-Arduino-using-PHP/