Go Down

Topic: Arduino Windows PHP (Read 43871 times) previous topic - next topic


Hello everybody,

I am trying to get an arduino uno to work with PHP through the serial port using the USB cable of Arduino.

I have used the codes that are available in internet that allow you to make this communication between PHP and Arduino without having to install any additional software, library or whatever.

I have programmed the website as follows:

1st file "Serial.php":

// declare port

 `mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;


2nd file "SerialTest.php":

 //open port com1

 $fp = fopen ("COM1:", "w+");

 if (!$fp) {

    echo "Error while opening COM1.";
} else {

    $data = escapeshellcmd($_REQUEST["data"]);
    fwrite ($fp, $data );

fclose ($fp);


 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Serial port connection</title>
 <form method="get" action="SerialTest.php">
   <br />
   <button type="submit" name="data" value="1">ONE</button>
   <br />
   <button type="submit" name="data" value="2">TWO</button>
   <br />

The first file seems to work although I am not 100% sure but the leds of the card blink.

the 2nd file gives this error

Warning: fopen(COM1:) [function.fopen]: failed to open stream: Permission denied in <filepath>\SerialTest.php on line 5

Might anyone help me with this issue?

Thank you very much in advance.


Well, I don't understand this witchery very well, but it looks like the matter is that the first file doesn't do it's job well.

To make it to work I had to start the port COM1 from the Arduino's IDE. And looks like not all ports work, I didn't try it but I found somewhere that just port COM1 to COM4 can be used.

So if you have to change the number of the port because you got something like port COM6 you may change it to COM1 or COM2, COM3, COM4 with the "device manager".

there has to be any other better way using just PHP and Windows for sure.

if you know it please let me know if you got the same problem I may try to help you as soon as I can.

Greetings everybody!


Well i think i've seen this done before by someone using IOT core arduino c++in visual studio IDE... this code was communicating to a IP based device... i'm not extreemly familiar w/ network IP hardware systems communicating to interfaces but i'm sure this was communicating directly to a dynamic remote server for two way communication which had the .exe... i think i have the link w/ the IOT core just keep in mind windows group policy local/remote or user hosts restricts this type of behavior... so this is strictly for appliances running on a server somewhere w/ full administrative access....



I did some tests on this a WHILE back.. using Flash, PHP and an Arduino..


I'm not clear what it is you are even trying to do, to be honest.

Are you trying to READ FROM Arduino serial data using PHP?  or SEND serial data TO an Arduino using PHP?

From what I recall (and where I left off at).. that you can NOT use PHP to read from an Arduio on Windows.. (Linux/Mac do not have this issue)..

sending data TO an Arudino using PHP is possible though...

Also.. you can loading a completely new php file.. you want to do some COM port stuff.. but you never initiate the connection...  file 1 does nothing by itself..


Hello, I have the same problem, I try send commands (Serial port) to arduino using PHP, but first I need open the SerialMonitor of IDE Arduino. what the solution for this?


I get this example https://condtec.com.br/arduino/arduino-e-php-conexao-via-html-controle-remoto-do-arduino-via-servidor-web/85, and modified the fopen for execute in Windows  - "$port = fopen("COM6", "w+");". But run just when I open the SerialMonitor (IDE Arduino) and close the program.

I think that has a service that need run for acept commands in the serial port.


If you using PHP, on a Windows platform.. you will only be able to WRITE to the serial port natively.
If you want to READ...  you will need to use another OS... or use some sort of man-in-the-middle PROXY


Yes, I do this using:

Code: [Select]
$port = fopen("COM6", "w+");
if ($_POST['estado']=="Amarelo")
echo "Ligou o Amarelo";
fwrite($port, "a");

this work in my windows, but just when open and close the SerialMonitor, I think when execute the serialmonitor, it's execute a process that permite the arduino receive commands in the serial port.


I'm still not following..

what is your Arduino code to take this incoming serial data and parse it for use?


I solved my problem, I needed execute this command in the prompt command: "mode COM6: baud=9600 parity=N data 8 dtr=on xon=off odsr=off octs=off rts=on idsr=off".

When I execute this, I can send commands via PHP.


Not sure whether this will work with website or not but you can surely control your Arduino using PHP.

Step 1: Install a -AMP Webserver
Step 2: (Debian Only) Add Permissions for Serial Port
Step 3: Get PhpSerial
Step 4: Finally! Write Your Arduino Code
#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

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

$msg = '';


$serial = new phpSerial;







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

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


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





<title>Arduino control</title>



<form method="POST">

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





I hope this solves your problem!



its (was) about the permissions when using WINDOWS..   not a linux/unix based OS..

Go Up