1. Go to this page and download the library: Download machinateur/phpduino library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
machinateur / phpduino example snippets
// First register the protocol and stream wrapper, ...
\Machinateur\Arduino\streamWrapper::register();
// ... then use the protocol in a stream function...
$fd = \fopen('arduino://'.$deviceName, 'r+b');
// ... and finally do things with the $fd (fread()/fwrite() ops).
// See `./example/echo` for some simple examples.
/**
* The device's name in `/dev/`.
* Prefer `cu` if on Mac, use `ls /dev/tty.*` to find the available devices.
*
* On windows, use the device manager to identify the correct serial (com) port, under the `COM` device group.
*/
if ('Darwin' === \PHP_OS_FAMILY) {
$deviceName = 'tty.usbmodem2101';
$deviceName = 'cu.usbmodem2101';
} elseif ('Windows' === \PHP_OS_FAMILY) {
$deviceName = 'COM7';
} else {
$deviceName = '';
\trigger_error(
\sprintf('No device name specified in "%s" on line "%d"!', __FILE__, __LINE__ - 3), \E_USER_ERROR);
}
/**
* The device's baud rate.
*
* See Arduino docs at https://docs.arduino.cc/learn/built-in-libraries/software-serial#begin for conventional rates.
*
* Supported baud rates are:
* - ` 300`
* - ` 600`
* - ` 1200`
* - ` 2400`
* - ` 4800`
* - ` 9600`
* - ` 14400`
* - ` 19200`
* - ` 28800`
* - ` 31250`
* - ` 38400`
* - ` 57600`
* - `115200`
*/
$deviceBaudRate = 9600;
/**
* The device's parity bit configuration.
*
* See Arduino docs at https://www.arduino.cc/reference/en/language/functions/communication/serial/begin/.
* - `none` = `-1`
* - ` odd` = ` 1`
* - `even` = ` 0`
*
* Default is `SERIAL_8N1`, so `N` is the data size.
*/
$deviceParity = -1;
/**
* The device's data size configuration.
*
* See Arduino docs at https://www.arduino.cc/reference/en/language/functions/communication/serial/begin/.
*
* Default is `SERIAL_8N1`, so `8` is the data size.
*/
$deviceDataSize = 8;
/**
* The device's stop bit size configuration.
*
* See Arduino docs at https://www.arduino.cc/reference/en/language/functions/communication/serial/begin/.
*
* Default is `SERIAL_8N1`, so `1` is the stop bit size.
*/
$deviceStopSize = 1;
/**
* The device custom command. If set, will be yielded in favor of default commands.
*
* Try to stop the reset on `fclose()`, see https://stackoverflow.com/a/59549518.
* - `[-]hup` = send a hangup signal when the last process closes the tty
*
* Ideally only run with the `-hup` option if not yet set, so there will be no more restarts due to RTS HANGUP.
* Only use if desired.
*/
$deviceCustomCommand = null;
// The stream context configuration.
$context = \stream_context_create([
\Machinateur\Arduino\streamWrapper::PROTOCOL_NAME => [
'baud_rate' => $deviceBaudRate,
'parity' => $deviceParity,
'data_size' => $deviceDataSize,
'stop_size' => $deviceStopSize,
'custom_command' => $deviceCustomCommand,
// A safe threshold for the arduino to boot on `fopen()`.
'usleep_s' => 2,
],
]);
$fd = \fopen('arduino://'.$deviceName, 'r+b', $context);
// Example program for "echo.ino" communication to/from Arduino via USB serial.
use Machinateur\Arduino\streamWrapper;
Name = 'tty.usbmodem2101';
$deviceName = 'cu.usbmodem2101';
} elseif ('Windows' === \PHP_OS_FAMILY) {
$deviceName = 'COM7';
} else {
$deviceName = '';
\trigger_error(
\sprintf('No device name specified in "%s" on line "%d"!', __FILE__, __LINE__ - 3), \E_USER_ERROR);
}
$deviceBaudRate = 9600;
$deviceParity = -1;
$deviceDataSize = 8;
$deviceStopSize = 1;
// Open the connection. Make sure the device is connected under the configured device name
// and the `echo.ino` program is running.
$fd = \fopen("arduino://{$deviceName}", 'r+b', false);
$input = 'hello world';
$output = '';
\stream_set_chunk_size($fd, 1);
\fwrite($fd, $input);
do {
$output .= \fread($fd, 1);
// Comment in the following line to see the progress byte by byte.
//echo $output.\PHP_EOL;
} while ($output !== $input);
echo $output,
\PHP_EOL,
\PHP_EOL;
\stream_set_chunk_size($fd, 1);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.