Serial Communication With PHP and Arduino

1

I already have a code in the arduino IDE, in which when I pass the letter 'l', a led will be access, however, I can not access this serial port with PHP, I'm using Fedora 22, Arduino Uno, the codes are below:

Arduino code, port used / dev / ttyACM0

void setup() {
   pinMode(13, OUTPUT);
}

void loop() {
   char caracter = Serial.read();
   if(caracter == 'l'){
      digitalWrite(13, HIGH);
      delay(1000);
   }
}

PHP code, with the 'l' character

<?php
   $porta = fopen("/dev/ttyACM0", "w");
   fwrite($porta, 'l');
   fclose($porta);
?>

I gave a var_dump in the variable $ port and it returned me false, thank you for all the help.

    
asked by anonymous 20.10.2015 / 21:13

4 answers

2

So I solved the problem, I was not able to read and write the serial port data through PHP, but after a lot of searches, I was able to read and write data from the command prompt (I'm using Windows ) and with php I was able to read the data from the prompt. The logic is as follows, PHP commands to execute a powershell script with the function 'exec', this script sends a character to the serial port and the arduino with this data does something. For more information, follow the tutorial that helped me a lot in the powershell scripts part: PowerShell Scripts Serial Port

    
15.11.2015 / 03:09
1

My friend, I'm very much helped by tutorials and forum, I'm an hobbyist so everything I do is with a lot of effort and research, and I've been looking for this solution for 3 months, basically the code in Windows changes a bit:

$fj = fopen("COM2", "w"); 
//o "w" muda de acordo com o que vc quer fazer, pode ser "w" "a" "r" no site do manual php tem mais informacoes.
$escreve = fwrite($fj, '1');
fclose($fj);

It works on COM2 port and you will need to change the settings on the serial port under "Device Manager", "Ports", "USB Serial Port (COM2)", "Port Settings" and "Advanced", "I changed the timer and I checked all the options on the left side, but there is a risk of burning the serial input of the Arduino, which probably happened to me after a few times I lit a led.

Obs. you need to make the configuration change after sending the Arduino code, otherwise it will not work. Attempts and errors. Do not install such a Firmata, just zimbra the Arduino and can not take more.

    
19.05.2017 / 07:17
0

Have you tried via socket? Here's a basic example:

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock,"187.15.6.131", 8081); // Ip e porta que você configurou no seu arduino
socket_write($sock,'l', 1);
socket_close($sock);

For more functions and better understanding, see the php manual .

Hope it helps, hugs.

    
21.10.2015 / 01:31
0

Use PHP-Serial link here

example:

error_reporting(E_ALL);
ini_set('display_errors', '1');
include "php_serial.class.php";

$serial = new phpSerial;
$serial->deviceSet("/dev/ttyAMA0");
$serial->confBaudRate(115200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->deviceOpen();
$serial->sendMessage("L");

$serial->deviceClose();

echo "comando enviado! \n\r";
    
05.11.2015 / 21:18