PHP Classes

Arduino PHP Serial Linux: Communicate with a Arduino board via serial port

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 359 All time: 6,922 This week: 88Up
Version License PHP version Categories
arduinolinuxserial 1.0GNU Lesser Genera...5PHP 5, Unix, Hardware
Description 

Author

This class can communicate with a Arduino board via serial port.

It can open a connection with a USB serial port of a given name on Linux.

The class can send and receive messages from the board.

Innovation Award
PHP Programming Innovation award winner
January 2018
Winner


Prize: One big elePHPant Plush Mascott
This class can communicate with a Arduino board via serial port.

It can open a connection with a USB serial port of a given name on Linux.

The class can send and receive messages from the board.

Manuel Lemos
Picture of Marco Sillano
  Performance   Level  
Name: Marco Sillano <contact>
Classes: 4 packages by
Country: Italy Italy
Age: ???
All time rank: 201279 in Italy Italy
Week rank: 20 Up1 in Italy Italy Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Example

<html>
<head>

<?php
$d
= dirname(__FILE__);
include(
"$d/ArduinoLinuxSerial.php");

// new object
$ArduinoSerial = new ArduinoLinuxSerial('/dev/ttyACM0'); // Linux device

$led = 'LED ??';

if (isset(
$_GET['LED'])){
   if (
$_GET['LED'] == 'ON'){
  
$led = $ArduinoSerial->sendMessage('1 test'); // command 1: led 13 ON, ' test' is dummy
  
if (isset($_GET['AUTO'])) // blinking 3 sec.
       
header('refresh: 3; url=index.php?AUTO=BLINK&LED=OFF');
   }
   if (
$_GET['LED'] == 'OFF'){
  
$led = $ArduinoSerial->sendMessage('2 test'); // command 2: led 13 OFF, ' test' is dummy
  
if (isset($_GET['AUTO'])) // blinking 3 sec.
       
header('refresh: 3; url=index.php?AUTO=BLINK&LED=ON');
   }
}
?>

</head>
<body
 style="font-family: -moz-fixed; white-space: -moz-pre-wrap; width: 72ch;">
<pre></pre>
<h1> TEST ARDUINO Linux Serial</h1>
&nbsp;&nbsp;&nbsp;&nbsp;<b><i><?php echo $led; ?></i></b><br><br>
<form action='index.php' method='GET'>
<select id='led' name='LED'>
<option id='0'>OFF</option>
<option id='1'>ON</option>
</select>&nbsp;&nbsp;&nbsp;&nbsp;
<input type='submit' value='SET'>&nbsp;&nbsp;&nbsp;&nbsp;
<input type='submit' name='AUTO' value='BLINK'>
</form>

<br>
</body>
</html>



Details

ArduinoLinuxSerial

This is an helper class for php-Arduino communications on Linux

After a week of googling and tests, i build this Class to communicate from php running on Linux (master) to an Arduino board via serial USB.

The master message max length is 60 char (serial Arduino rx buffer limit), the response (from Arduino) as no limits. The '\n' char is used internally as terminator. The protocol uses CRC8 to insure correctness. In case of error the message is resended 3 times before exit in error state.

A Bash script (serialArduino.sh) is used to setup the USB device on Linux, after startup or USB connection. The serial communication is open for every message: in the Bash script the DTR pin is disabled to avoid the Arduino auto reset. (see: https://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection)

TROUBLESHOOTING

- ERROR LCRC: bad CRC Linux -> Arduino - ERROR ACRC: bad CRC Arduino -> Linux - ERROR CODE: sended a command code not implemented in Arduino - ERROR SERIAL: USB not plugged, Arduino not running or php fail in open the serial device.

   In this case the file 'status.txt' contains: 'stty: /dev/ttyACM0: Inappropriate ioctl for device' ?? 
   Solution: disconect and reconnect Arduino USB

SETUP

see ArduinoLinuxSerial.php file.

CONCLUSIONS

Now you can develop MySQL and web enabled Arduino applications only working on Arduino and PHP. 
To keep ligth the Arduino Sketch, you can port all not realtime logic to PHP side.
At the end your application will works on MXQ+Arduino UNO even 24/7 with only 20 Watt AC power, and can
be controlled by smartphone via WiFi.
What more?
Enjoy.

## see also

I am very interested in using TVboxes (like MXQ) as dedicated WEB servers with Arduino extensions: so about (Linux + Android)-Arduino USB serial communication I tested many solutions:

 Using ArduinoLinuxSerial (https://github.com/msillano/ArduinoLinuxSerial) the Master is php, and you need the 
 serial driver (uses devices like /dev/ACMx or /dev/USBx). It is blocking the master and the php->Arduino message
 is limited to 60 char. Fastest.

 Using USBphpTunnel (https://github.com/msillano/USBphpTunnel) the master is Arduino, and the Android app uses 
 ports like /dev/bus/dev/00X/00Y, so you don't need the serial driver. It is blocking the master (arduino) and 
 it allows big messages. It works in background of the main php program. Useful for data logging.
 
 Using USBphpTunnel_Fifo (https://github.com/msillano/USBphpTunnel_fifo) the master is php. It is NOT blocking the 
 master, allows payload of 4k, allows concurrence. Works in background, and it is no fast (depending from polling 
 Arduno frequence, 5-15 sec. plus processing time). Well placed for long Arduino processes (like waiting user 
 actions).
 

  Files folder image Files (5)  
File Role Description
Files folder imagearduino (1 file)
Files folder imagetest (1 file)
Plain text file ArduinoLinuxSerial.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file serialArduino.sh Data Auxiliary data

  Files folder image Files (5)  /  arduino  
File Role Description
  Accessible without login Plain text file testSerial.ino Data Auxiliary data

  Files folder image Files (5)  /  test  
File Role Description
  Accessible without login Plain text file testSerial.php Example Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:359
This week:0
All time:6,922
This week:88Up
User Comments (1)
Thats a amazing class ;-)
6 years ago (José Filipe Lopes Santos)
80%StarStarStarStarStar