PHP REST API Server for Arduino
- Arduino use Ethernet Shield to communicate with the Server
- Provided functionality to manipulate Arduino I/O pins through HTTP protocol
- Supported Arduino functions are digitalRead, digitalWrite, analogRead and analogWrite
- Use JSON as the data-interchange-format
- Apache Web server with mod_rewrite enabled
- PHP 5.4
- Supported Arduino functions are digitalRead, digitalWrite, analogRead and analogWrite
The constructor required one parameter which is an array contain the Arduino IP Address and Port number. For example :
<?php
require('lib/ArduinoServer.php');
$config = array(
'arduinoIP' => '192.168.1.2',
'arduinoPort' => '3000'
);
$server = new ArduinoServer($config);
$server->startServer();
?>
Another required step is rewrite the url using .htaccess file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Read Input pin state You need to create HTTP Request(GET) using this URL format:
http://[server ip address]/read/[digital-analog]/[pin number]
Example
http://192.168.1.1/read/digital/6
**Write value to output pin** You need to create HTTP Request(POST) to this URL:
http://[server ip address]/write
The POST Data :
Key | Value (Digital) | Value (Analog) |
---|---|---|
mode | digital | analog |
pin | 0,1,2... | (PWM) 3,5,6,... |
value | 0 or 1 | 0 - 255 |
{
status : {
result : "Success"
},
data : {
mode : "analog",
pin : 5,
value : 512
}
}
The arduino code based on this awesome project