https://github.com/marabesi/arduino-php-wrapper
Simple wrapper to connect with Arduino
https://github.com/marabesi/arduino-php-wrapper
arduino-php-wrapper arduino-serial iot oop php wrapper
Last synced: about 1 month ago
JSON representation
Simple wrapper to connect with Arduino
- Host: GitHub
- URL: https://github.com/marabesi/arduino-php-wrapper
- Owner: marabesi
- License: mit
- Created: 2016-06-15T23:27:44.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-10-04T10:34:40.000Z (about 5 years ago)
- Last Synced: 2025-03-17T22:08:01.167Z (8 months ago)
- Topics: arduino-php-wrapper, arduino-serial, iot, oop, php, wrapper
- Language: PHP
- Homepage:
- Size: 610 KB
- Stars: 22
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# arduino-php-wrapper (inspired by Johnny-Five JS)
[](https://www.codacy.com/app/matheus-marabesi/arduino-php-wrapper?utm_source=github.com&utm_medium=referral&utm_content=marabesi/arduino-php-wrapper&utm_campaign=badger)
[](https://travis-ci.org/marabesi/arduino-php-wrapper)
[](https://packagist.org/packages/marabesi/arduino-php-wrapper)
[](https://packagist.org/packages/marabesi/arduino-php-wrapper)
[](https://packagist.org/packages/marabesi/arduino-php-wrapper)
If you are wondering how to control the Arduino serial port via PHP, here is the solution.
The **arduino://** wrapper is a easy and straightforward way to write and read data from Arduino.
## Install
```
composer require marabesi/arduino-php-wrapper
```
## Usage
To read data from Arduino serial just use the regular I/O functions in PHP such as **fread** or **file_get_contents**
``` php
\Arduino\Wrapper::register();
//reads data from Arduino
$resource = fopen('arduino://ttyUSB0', 'r+');
print fread($resource, 1024);
```
Or if you prefer, you can use **file_get_contents** and get the same result
``` php
print file_get_contents('arduino://ttyUSB0');
```
To write data in the Arduino serial is as easy as it could be
``` php
\Arduino\Wrapper::register();
//writes data to Arduino
$resource = fopen('arduino://ttyUSB0', 'r+');
print fwrite('hello Arduino');
```
``` php
\Arduino\Wrapper::register();
print file_put_contents('arduino://hello Arduino');
```
## OOP
You can use in your project in a OOP style
### Sending data
``` php
$writer = new Arduino\Writer(new Arduino\Wrapper());
$bytes = $writer->out('ttyUSB0', 'from oop');
```
### Reading data
``` php
$arduino = new \Arduino\Wrapper();
$writer = new \Arduino\Reader($arduino);
while (true) {
print $writer->from('/dev/cu.usbmodem1411');
}
```
## Improvements
As you can see is really simple and we can improve it much more as the sensors are identified.
- Prevent arduino from reload everytime a request is made by PHP
## Slides (talks based on this lib)
[](https://www.slideshare.net/marabesi/introduction-to-iot-and-php-nerdzao-day-1) [Introduction to IoT and PHP - Nerdzão day #1](https://www.slideshare.net/marabesi/introduction-to-iot-and-php-nerdzao-day-1)
[](
https://www.slideshare.net/marabesi/iot-powered-by-php-and-streams-phpexperience2017) [IoT powered by PHP and streams - PHPExperience2017](https://www.slideshare.net/marabesi/iot-powered-by-php-and-streams-phpexperience2017)
[](
https://www.slideshare.net/marabesi/controll-your-house-with-the-elephpant-phpconf2016) [Control your house with the elePHPant - PHPConf2016](https://www.slideshare.net/marabesi/controll-your-house-with-the-elephpant-phpconf2016)