Help : Arduino UNO + Serial Port + PHP + Windows

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.

  1. PHP writes the string "light" to Serial Port Com6 with baud rate 9600
  2. Arduino Code keeps monitoring for string "light" if light is found then the LED on Pin 13 goes HIGH
  3. 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.
  4. Now again PHP writes the string "light" to Serial Port Com6 with baud rate 9600
  5. 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]
  6. 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

In my readings about PHP, two important things to know, 1) PHP opens and closes the serial port when it sends something to the serial port, so the arduino auto reset will need to be defeated, and 2) PHP does not have any way to receive data from the arduino via the serial port.

PHP does not have any way to receive data from the arduino via the serial port.

On Windows, that is true. On Linux, PHP can read data from the serial port.

But, since OP is using Windows, it's true enough.

zoomkat:
In my readings about PHP, two important things to know, 1) PHP opens and closes the serial port when it sends something to the serial port, so the arduino auto reset will need to be defeated, and 2) PHP does not have any way to receive data from the arduino via the serial port.

  1. How to defeat the autoreset
  2. so there is absolutely no way to read serial data n windows , can another programming language read serial data like a C# code using asp.net
  1. How to defeat the autoreset

You need to check the UNO specs as it may have a jumper that can be removed to prevent resetting. Otherwise a 100 ohm resistor between the +5v and reset pin or a ~5uf capacitor between the reset pin and ground will probably do the job.

  1. so there is absolutely no way to read serial data n windows

Not with PHP as I undrstand it. You can make what is commonly called a CGI application to use with apache that can read/write the serial port using any programming language that supports such activity. I made one using a programming language called freeBasic.

so there is absolutely no way to read serial data n windows , can another programming language read serial data like a C# code using asp.net

Have you looked at the playground? There is a section on interfacing with software. Lots of languages support reading from and writing to the serial port. Some make it easy; some not so easy. With C#, it's extremely easy.

zoomkat:
Not with PHP as I undrstand it. You can make what is commonly called a CGI application to use with apache that can read/write the serial port using any programming language that supports such activity. I made one using a programming language called freeBasic.

Can you provide a simple freebasic code to read and write serial port.

I would like to write text to serial port for the arduino to interpret and then arduino will write some response which should be read by the freebasic program.

PaulS:

so there is absolutely no way to read serial data n windows , can another programming language read serial data like a C# code using asp.net

Have you looked at the playground? There is a section on interfacing with software. Lots of languages support reading from and writing to the serial port. Some make it easy; some not so easy. With C#, it's extremely easy.

Playground is an amazing code repo for arduino , thanks didnt knew there was something like this.

If you are interested in the C# route, PM me. I have a sample C# application that talks to an Arduino.

Can you provide a simple freebasic code to read and write serial port.

Below is some simple freebasic test code. The code is compiled into an .exe file like cgiapp.exe and put in the apache cgi-bin folder. Then a "get" request would be sent like www.mysite.com/cgi-bin/cgiapp.exe?mydata . Apache will then place mydata into the query_string environmental varable, then start cgiapp.exe, which opens the com port and sends mydata to an attached gizmo. cgiapp.exe then waits for 200 ms for a reply, at which time it sends the returned data in a simple page to apache, or tells apache that there is no data to send. Then apache sends the results to the browser client and closes the connection.

Dim As String qs, dat
Dim idx As Integer

qs = Environ("QUERY_STRING")
if qs = "" goto nodata

again:
  idx = Instr(qs, "-")
  Mid(qs, idx, 1) = "#"
  if idx > 0 goto again

qs = qs 

Open Com "COM5: 9600,N,8,1,BIN,CD,CS,DS,RS" For Binary As #1
print #1,, qs
Sleep 200
dat = Input$(loc(1), #1)
Close #1

if dat = "" goto nodata
if dat <> "" goto gotdata

nodata:
Print "status: 204"
Print
Print
goto fini

gotdata:
Print "Content-type: text/html"
Print
print "<html><body>"
Print dat
Print "</body></html>"
goto fini

fini:
end

Thanks zoomkat.

Paul i have sent you a PM.

Thanks guys , will keep posting my progress.

With the serial extension php_ser++.dll YOU CAN WRITE AND READ ANY DATA TO A SERIAL PORT WITH PHP.

regards
Franco

franco190453:
With the serial extension php_ser++.dll YOU CAN WRITE AND READ ANY DATA TO A SERIAL PORT WITH PHP.

regards
Franco

Its been a really long time but thanks for your reply. :slight_smile:

A much easier solution would be to use swDuino: