Hi All,
This is my first post here and have just started coding for the Arduino its been 2 days and have tried out a lot of sensors but i wanted to try something more challenging.
I have general idea of programming languages and syntax but PHP is new to me.
Basically what i want to do is switch an LED ON/OFF on PIN 13 using PHP Page.
- PHP writes the string "light" to Serial Port Com6 with baud rate 9600
- Arduino Code keeps monitoring for string "light" if light is found then the LED on Pin 13 goes HIGH
- Ardunio now sends "1" to Serial.println which should be read by the PHP Page to display that the LED on Pin 13 has been switched ON.
- Now again PHP writes the string "light" to Serial Port Com6 with baud rate 9600
- Arduino Code keeps monitoring for string "light" if light is found then the LED on Pin 13 goes LOW if its previously HIGH [Like Toggle]
- Ardunio now sends "0" to Serial.println which should be read by the PHP Page to display that the LED on Pin 13 has been switched OFF.
Basically i want to code the Serial Communication PHP Page in Windows , is there any links or suggestions on how can i get this to work.
Arduino Code
//Arduino PHP Serial Code
String readString;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
while (Serial.available())
{
delay(10);
char c = Serial.read();
if (c == ',')
{
break;
}
readString += c;
}
if (readString == "light")
{
if(digitalRead(13) == LOW)
{
digitalWrite(13, HIGH);
delay(100);
Serial.println(1);
}
else if(digitalRead(13) == HIGH)
{
digitalWrite(13, LOW);
delay(100);
Serial.println(0);
}
readString="";
}
}
Now If i use Serial Monitor on Arduino
Type "light" and click Send --> the LED on Pin 13 is ON/HIGH
Serial Monitor Displays "1"
Type "light" again and click Send --> the LED on Pin 13 is OFF/LOW
Serial Monitor Displays "0"
I want to send this "light" command via a Webpage or a PHP Page to Switch the LED on and off.
For the PHP Part i tried something.
http://blogs.vinuthomas.com/2007/04/09/php-and-serial-ports/
http://www.easyvitools.com/phpserial/php_ser_reference.html#function_ref
Performed the Installation
Installation:
- download from website
- unzip php_ser.zip and place php_ser++.dll file in PHP extension folder.
For PHP5 this is usualy: drive:\PHP_install_folder\ext\
Example: C:\PHP\ext\
- find php.ini file (usualy placed in C:\Windows folder) and open it
- fill extension_dir directive with extension folder, for example:
extension_dir= ".\ext"
- fill an extension directive to load php_ser automatically:
extension=php_ser++.dll
- save and close php.ini file
- test with php_ser_test.php file (included in distribution), which lists functions
Test.php File
<html>
<head>
<title>PHP serial extension test page</title>
</head>
<body>
<h3>PHP serial extension test page</h3>
<?php
$module = 'win_serial';
if (extension_loaded($module)) {
$str = "Module loaded";
} else {
$str = "Module $module is not compiled into PHP";
die("Module $module is not compiled into PHP");
}
echo "$str
";
$functions = get_extension_funcs($module);
echo "Functions available in the $module extension:
\n";
foreach($functions as $func) {
echo $func."
";
}
echo "
";
echo "Version ".ser_version();
echo "
";
ser_open( "COM6", 9600, 8, "None", "1", "None" );
echo "
";
if (ser_isopen())
echo "Port is open!.";
echo "
";
echo "Write light to serial port";
ser_write("light");
echo "
";
$str = ser_read();
echo "Received: $str";
echo "
";
echo "flush serial port";
ser_flush(true,true);
echo "
";
ser_close();
echo "Serial Port Closed";
echo "
";
?>
OUTPUT
PHP serial extension test page
Module loaded
Functions available in the win_serial extension:
ser_version
ser_open
ser_close
ser_write
ser_read
ser_isopen
ser_writebyte
ser_readbyte
ser_inputcount
ser_flush
ser_setRTS
ser_setDTR
Version 20091007.1 TRIAL 0 in 20 out
Port is open!.
Write light to serial port
Received:
flush serial port
Serial Port Closed
I followed the Steps here but the led on Pin 13 just blinks it should stay on when "light" is written to Serial Port Com6 using test.php
Any help would be highly appreciated.
Thanks and Regards,
Sanket