Figured I'd post what worked for me (so far)..
on getting PHP to communicate to the Serial COM ports on you local machine using WAMP Server.. (using a WINDOWS machine)
(I believe it would only be 1 little step further to open this up to the 'internet'.. but putting WAMP server 'online', ad forwarding your router HTTP port to the machine that has WAMP server installed on it)
First.. make sure you have WAMP Server installed (this lets you locally, install and run, Apache web server, PHP scripting language and MySQL database framework)
WAMP Server:
Once you have that installed you can make a folder in the www directory: C:\wamp\www
(or left click on WAMP icon in sys tray and choose 'www directory' to go to folder)
(I made a folder called arduino_projects)
the Arduino code/sketch I am using as a quick test to determine if the incomingByte is a 1 or a 0.
ARDUINO CODE:
//first php to serial attemp
int ledPin13 = 13; // the pin that the green LED is attached to
int incomingByte; // a variable to read incoming serial data intovoid setup() {
Serial.begin(9600); // initialize serial communication
pinMode(ledPin13, OUTPUT); // initialize the green LED pin as an output
}void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
Serial.println("Serial data available......");
incomingByte = Serial.read(); // read the oldest byte in the serial buffer
//Preform the code to switch on or off the leds
if (incomingByte == '0') {
Serial.println("nothing yet......");
digitalWrite(ledPin13, HIGH); //If the serial data is 0 turn red LED on
delay(200);
digitalWrite(ledPin13, LOW); //If the serial data is 0 turn red LED on
delay(250);
digitalWrite(ledPin13, HIGH); //If the serial data is 0 turn red LED on
delay(250);
digitalWrite(ledPin13, LOW); //If the serial data is 0 turn red LED on
delay(250);
digitalWrite(ledPin13, HIGH); //If the serial data is 0 turn red LED on}
if (incomingByte == '1') {
Serial.println(incomingByte, HEX);
digitalWrite(ledPin13, LOW); //If the serial data is 1 turn red LED off
}
}
}
Now.. I have actually gotten '2' different PHP examples working..
PHP SAMPLE 1:
1.) this doesnt seem to use any other extra PHP classes or anything.
<?PHP
exec("mode COM3 BAUD=9600 PARITY=N data=8 stop=1 xon=off");
$fp = fopen("com3", "w");
if (!$fp) {
echo "Not open";
} else {
sleep(10);
echo "Open";
fwrite($fp, "0");
fclose($fp);
}
?>
**Note: you'll notice a sleep(); function in there.. this is ONLY there because I have NOT disabled the AUTO-RESET on SERIAL OPEN/CLOSE that the Arduino does (either by edit bootloader/firmware or by adding resistor)
The second PHP approach uses an extra PHP class you need to import..
its called the php_serial.class
and can be downloaded here:
php_serial.class.php:
or
http://code.google.com/p/php-serial/source/browse/trunk/php_serial.class.php?r=11
put the above class in the same folder/directory as this .php script.
<?php
/*
Utilizes the PHP Serial class by Rémy Sanchez <thenux@gmail.com>
(Thanks you rule!!) to communicate with the Arduino Serial!
*/
//Load the serial port class
require("php_serial.class.php");
//Initialize the class
$serial = new phpSerial();
//Specify the serial port to use... in this case COM1
//SET THIS TO WHATEVER YOUR SERIAL DEVICE HAPPENS TO BE,
//YOU CAN FIND THIS UNDER THE ARDUINO SOFTWARE'S MENU
//$serial->deviceSet("/dev/tty.usbserial-A7006QCP");
$serial->deviceSet("com3");
//Set the serial port parameters. The documentation says 9600 8-N-1, so
$serial->confBaudRate(9600); //Baud rate: 9600
//$serial->confParity("none"); //Parity (this is the "N" in "8-N-1") ******THIS PART OF THE CODE WAS NOT NEEDED
//$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") ******THIS PART OF THE CODE WAS NOT NEEDED
//$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") ******THIS PART OF THE CODE WAS NOT NEEDED
//$serial->confFlowControl("none"); //******THIS PART OF THE CODE WAS NOT NEEDED
//Now we "open" the serial port so we can write to it
$serial->deviceOpen();
sleep(5);
//to turn the GREEN LED ON, we issue the command
$serial->sendMessage("0");
//We're done, so close the serial port again
$serial->deviceClose();
?>
BOTH of these have worked.. at least enough to get the LED to blink on pin13. ![]()
I have NOT tried reading anything (yet).. or even researching into what will work... (or wont)
now that we have a BASIC PHP >> ARDUINO communication going..
I will attempt the following as the next step/demo.
1.) altering our Arduino code to accept/read/parse more than 1 byte.. (perhaps a value of 0-255 for PWM)
2.) making a little FLASH interface/front-end (slider or stepper) to send this data to Arduino and adjust the PWM brightness on a led/pin.
thanks...