Serial Communication With PHP and Arduino

Asked

Viewed 2,455 times

1

I already have a code in the Arduino IDE, in which when passed the letter 'l', an led will be access, however, I can not in any way access this serial port with PHP, I am 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, setting the character 'l'

<?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, I appreciate the help of all.

  • Probably the PHP or apache executable does not have write permissions on /dev/ttyACM0, seems to me more likely. I also don’t know if your kernel has the drivers nescessários. Some arduinos "pirates" have specific drivers

  • Sure it’s permission I had this same problem on linux, from a chmod 777 /dev/ttyACM0 that will work. !!!

4 answers

2


then I solved the problem, I could not in any way read and write the data of the serial port through PHP, but after many searches, I was able to read and write the data through the command prompt (I am using Windows) and with php I was able to read the data from the prompt. The logic is this, PHP sends a powershell script with the 'exec' function, this script sends a character to the serial port and Arduino with this data does something. For more information, follow the tutorial that helped me a lot in the part of powershell scripts: Powershell Scripts Serial Port

1

Friend, I am very helped by tutorials and forum, I am hobbyist so everything I do is with a lot of effort and research, and I spent about 3 months searching for this solution, basically the code in Windows changes a little:

$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 the COM2 port and you will need to change the settings on the serial port within "Device Manager", "ports", "USB serial port(COM2)", "Port settings" and "advanced", "I changed the timer and marked all the options on the left side, but there is the risk of burning the Arduino serial entry, which probably happened to me after a few times I lit a led.

Obs. you need to make the configuration change after you send the Arduino code, otherwise it doesn’t work. Trials and errors. No install such a Firmata, only Zimbra the Arduino and can not take more.

0

Use the 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";
  • So, I have tried to implement with this script, but also without success, the same error occurs, access denied the port. Note: I performed tests on three different machines, with Fedora 22, with Ubuntu 14.04 on with Windows 10, all on different machines and without any success.

  • this site has the example of php Serial in use in linux, at the end has the command to serial port work. http://www.embarcados.com.br/sistema-web-com-raspberry-pi-e-arduino/

  • I’ll give a check as soon as possible and I’ll be returning the result.

0

Ever tried via socket? Follow 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 php manual.

I hope it helps, hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.